diff --git a/homeassistant/components/androidtv_remote/config_flow.py b/homeassistant/components/androidtv_remote/config_flow.py index 813c0eda14b49c694a68a387e5d214c8a03681c8..89bf321d80b5c90e91d4a3e3a7ca6ba5fbc13f62 100644 --- a/homeassistant/components/androidtv_remote/config_flow.py +++ b/homeassistant/components/androidtv_remote/config_flow.py @@ -16,6 +16,7 @@ import voluptuous as vol from homeassistant.components import zeroconf from homeassistant.config_entries import ( + SOURCE_REAUTH, ConfigEntry, ConfigFlow, ConfigFlowResult, @@ -58,13 +59,11 @@ class AndroidTVRemoteConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self) -> None: - """Initialize a new AndroidTVRemoteConfigFlow.""" - self.api: AndroidTVRemote | None = None - self.reauth_entry: ConfigEntry | None = None - self.host: str | None = None - self.name: str | None = None - self.mac: str | None = None + api: AndroidTVRemote + host: str + name: str + mac: str + reauth_entry: ConfigEntry async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -72,13 +71,11 @@ class AndroidTVRemoteConfigFlow(ConfigFlow, domain=DOMAIN): """Handle the initial step.""" errors: dict[str, str] = {} if user_input is not None: - self.host = user_input["host"] - assert self.host + self.host = user_input[CONF_HOST] api = create_api(self.hass, self.host, enable_ime=False) try: await api.async_generate_cert_if_missing() self.name, self.mac = await api.async_get_name_and_mac() - assert self.mac await self.async_set_unique_id(format_mac(self.mac)) self._abort_if_unique_id_configured(updates={CONF_HOST: self.host}) return await self._async_start_pair() @@ -94,7 +91,6 @@ class AndroidTVRemoteConfigFlow(ConfigFlow, domain=DOMAIN): async def _async_start_pair(self) -> ConfigFlowResult: """Start pairing with the Android TV. Navigate to the pair flow to enter the PIN shown on screen.""" - assert self.host self.api = create_api(self.hass, self.host, enable_ime=False) await self.api.async_generate_cert_if_missing() await self.api.async_start_pairing() @@ -108,14 +104,12 @@ class AndroidTVRemoteConfigFlow(ConfigFlow, domain=DOMAIN): if user_input is not None: try: pin = user_input["pin"] - assert self.api await self.api.async_finish_pairing(pin) - if self.reauth_entry: + if self.source == SOURCE_REAUTH: await self.hass.config_entries.async_reload( self.reauth_entry.entry_id ) return self.async_abort(reason="reauth_successful") - assert self.name return self.async_create_entry( title=self.name, data={ @@ -155,9 +149,9 @@ class AndroidTVRemoteConfigFlow(ConfigFlow, domain=DOMAIN): _LOGGER.debug("Android TV device found via zeroconf: %s", discovery_info) self.host = discovery_info.host self.name = discovery_info.name.removesuffix("._androidtvremote2._tcp.local.") - self.mac = discovery_info.properties.get("bt") - if not self.mac: + if not (mac := discovery_info.properties.get("bt")): return self.async_abort(reason="cannot_connect") + self.mac = mac await self.async_set_unique_id(format_mac(self.mac)) self._abort_if_unique_id_configured( updates={CONF_HOST: self.host, CONF_NAME: self.name} @@ -189,9 +183,7 @@ class AndroidTVRemoteConfigFlow(ConfigFlow, domain=DOMAIN): self.host = entry_data[CONF_HOST] self.name = entry_data[CONF_NAME] self.mac = entry_data[CONF_MAC] - self.reauth_entry = self.hass.config_entries.async_get_entry( - self.context["entry_id"] - ) + self.reauth_entry = self._get_reauth_entry() return await self.async_step_reauth_confirm() async def async_step_reauth_confirm(