Skip to content

Commit

Permalink
Adjust async_step_reauth in samsungtv (home-assistant#74165)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jun 29, 2022
1 parent 75efb54 commit 1b85929
Showing 1 changed file with 27 additions and 36 deletions.
63 changes: 27 additions & 36 deletions homeassistant/components/samsungtv/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from samsungtvws.encrypted.authenticator import SamsungTVEncryptedWSAsyncAuthenticator
import voluptuous as vol

from homeassistant import config_entries, data_entry_flow
from homeassistant import config_entries
from homeassistant.components import dhcp, ssdp, zeroconf
from homeassistant.const import (
CONF_HOST,
Expand All @@ -23,6 +23,7 @@
CONF_TOKEN,
)
from homeassistant.core import callback
from homeassistant.data_entry_flow import AbortFlow, FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import format_mac

Expand Down Expand Up @@ -123,7 +124,7 @@ def _base_config_entry(self) -> dict[str, Any]:
CONF_SSDP_MAIN_TV_AGENT_LOCATION: self._ssdp_main_tv_agent_location,
}

def _get_entry_from_bridge(self) -> data_entry_flow.FlowResult:
def _get_entry_from_bridge(self) -> FlowResult:
"""Get device entry."""
assert self._bridge
data = self._base_config_entry()
Expand All @@ -137,7 +138,7 @@ def _get_entry_from_bridge(self) -> data_entry_flow.FlowResult:
async def _async_set_device_unique_id(self, raise_on_progress: bool = True) -> None:
"""Set device unique_id."""
if not await self._async_get_and_check_device_info():
raise data_entry_flow.AbortFlow(RESULT_NOT_SUPPORTED)
raise AbortFlow(RESULT_NOT_SUPPORTED)
await self._async_set_unique_id_from_udn(raise_on_progress)
self._async_update_and_abort_for_matching_unique_id()

Expand All @@ -156,7 +157,7 @@ async def _async_set_unique_id_from_udn(
self._ssdp_rendering_control_location,
self._ssdp_main_tv_agent_location,
):
raise data_entry_flow.AbortFlow("already_configured")
raise AbortFlow("already_configured")
# Now that we have updated the config entry, we can raise
# if another one is progressing
if raise_on_progress:
Expand Down Expand Up @@ -184,7 +185,7 @@ async def _async_create_bridge(self) -> None:
result, method, _info = await self._async_get_device_info_and_method()
if result not in SUCCESSFUL_RESULTS:
LOGGER.debug("No working config found for %s", self._host)
raise data_entry_flow.AbortFlow(result)
raise AbortFlow(result)
assert method is not None
self._bridge = SamsungTVBridge.get_bridge(self.hass, method, self._host)
return
Expand All @@ -207,7 +208,7 @@ async def _async_get_and_check_device_info(self) -> bool:
"""Try to get the device info."""
result, _method, info = await self._async_get_device_info_and_method()
if result not in SUCCESSFUL_RESULTS:
raise data_entry_flow.AbortFlow(result)
raise AbortFlow(result)
if not info:
return False
dev_info = info.get("device", {})
Expand All @@ -216,7 +217,7 @@ async def _async_get_and_check_device_info(self) -> bool:
LOGGER.debug(
"Host:%s has type: %s which is not supported", self._host, device_type
)
raise data_entry_flow.AbortFlow(RESULT_NOT_SUPPORTED)
raise AbortFlow(RESULT_NOT_SUPPORTED)
self._model = dev_info.get("modelName")
name = dev_info.get("name")
self._name = name.replace("[TV] ", "") if name else device_type
Expand All @@ -230,9 +231,7 @@ async def _async_get_and_check_device_info(self) -> bool:
self._mac = mac
return True

async def async_step_import(
self, user_input: dict[str, Any]
) -> data_entry_flow.FlowResult:
async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
"""Handle configuration by yaml file."""
# We need to import even if we cannot validate
# since the TV may be off at startup
Expand All @@ -257,13 +256,13 @@ async def _async_set_name_host_from_input(self, user_input: dict[str, Any]) -> N
socket.gethostbyname, user_input[CONF_HOST]
)
except socket.gaierror as err:
raise data_entry_flow.AbortFlow(RESULT_UNKNOWN_HOST) from err
raise AbortFlow(RESULT_UNKNOWN_HOST) from err
self._name = user_input.get(CONF_NAME, self._host) or ""
self._title = self._name

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
) -> FlowResult:
"""Handle a flow initialized by the user."""
if user_input is not None:
await self._async_set_name_host_from_input(user_input)
Expand All @@ -281,7 +280,7 @@ async def async_step_user(

async def async_step_pairing(
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
) -> FlowResult:
"""Handle a pairing by accepting the message on the TV."""
assert self._bridge is not None
errors: dict[str, str] = {}
Expand All @@ -290,7 +289,7 @@ async def async_step_pairing(
if result == RESULT_SUCCESS:
return self._get_entry_from_bridge()
if result != RESULT_AUTH_MISSING:
raise data_entry_flow.AbortFlow(result)
raise AbortFlow(result)
errors = {"base": RESULT_AUTH_MISSING}

self.context["title_placeholders"] = {"device": self._title}
Expand All @@ -303,7 +302,7 @@ async def async_step_pairing(

async def async_step_encrypted_pairing(
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
) -> FlowResult:
"""Handle a encrypted pairing."""
assert self._host is not None
await self._async_start_encrypted_pairing(self._host)
Expand Down Expand Up @@ -421,26 +420,24 @@ def _async_start_discovery_with_mac_address(self) -> None:
if (entry := self._async_update_existing_matching_entry()) and entry.unique_id:
# If we have the unique id and the mac we abort
# as we do not need anything else
raise data_entry_flow.AbortFlow("already_configured")
raise AbortFlow("already_configured")
self._async_abort_if_host_already_in_progress()

@callback
def _async_abort_if_host_already_in_progress(self) -> None:
self.context[CONF_HOST] = self._host
for progress in self._async_in_progress():
if progress.get("context", {}).get(CONF_HOST) == self._host:
raise data_entry_flow.AbortFlow("already_in_progress")
raise AbortFlow("already_in_progress")

@callback
def _abort_if_manufacturer_is_not_samsung(self) -> None:
if not self._manufacturer or not self._manufacturer.lower().startswith(
"samsung"
):
raise data_entry_flow.AbortFlow(RESULT_NOT_SUPPORTED)
raise AbortFlow(RESULT_NOT_SUPPORTED)

async def async_step_ssdp(
self, discovery_info: ssdp.SsdpServiceInfo
) -> data_entry_flow.FlowResult:
async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
"""Handle a flow initialized by ssdp discovery."""
LOGGER.debug("Samsung device found via SSDP: %s", discovery_info)
model_name: str = discovery_info.upnp.get(ssdp.ATTR_UPNP_MODEL_NAME) or ""
Expand Down Expand Up @@ -485,9 +482,7 @@ async def async_step_ssdp(
self.context["title_placeholders"] = {"device": self._title}
return await self.async_step_confirm()

async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
) -> data_entry_flow.FlowResult:
async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle a flow initialized by dhcp discovery."""
LOGGER.debug("Samsung device found via DHCP: %s", discovery_info)
self._mac = discovery_info.macaddress
Expand All @@ -499,7 +494,7 @@ async def async_step_dhcp(

async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> data_entry_flow.FlowResult:
) -> FlowResult:
"""Handle a flow initialized by zeroconf discovery."""
LOGGER.debug("Samsung device found via ZEROCONF: %s", discovery_info)
self._mac = format_mac(discovery_info.properties["deviceid"])
Expand All @@ -511,7 +506,7 @@ async def async_step_zeroconf(

async def async_step_confirm(
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
) -> FlowResult:
"""Handle user-confirmation of discovered node."""
if user_input is not None:
await self._async_create_bridge()
Expand All @@ -524,24 +519,20 @@ async def async_step_confirm(
step_id="confirm", description_placeholders={"device": self._title}
)

async def async_step_reauth(
self, data: Mapping[str, Any]
) -> data_entry_flow.FlowResult:
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
"""Handle configuration by re-auth."""
self._reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
assert self._reauth_entry
data = self._reauth_entry.data
if data.get(CONF_MODEL) and data.get(CONF_NAME):
self._title = f"{data[CONF_NAME]} ({data[CONF_MODEL]})"
if entry_data.get(CONF_MODEL) and entry_data.get(CONF_NAME):
self._title = f"{entry_data[CONF_NAME]} ({entry_data[CONF_MODEL]})"
else:
self._title = data.get(CONF_NAME) or data[CONF_HOST]
self._title = entry_data.get(CONF_NAME) or entry_data[CONF_HOST]
return await self.async_step_reauth_confirm()

async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
) -> FlowResult:
"""Confirm reauth."""
errors = {}
assert self._reauth_entry
Expand Down Expand Up @@ -586,7 +577,7 @@ async def _async_start_encrypted_pairing(self, host: str) -> None:

async def async_step_reauth_confirm_encrypted(
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
) -> FlowResult:
"""Confirm reauth (encrypted method)."""
errors = {}
assert self._reauth_entry
Expand Down

0 comments on commit 1b85929

Please sign in to comment.