Adjust type hints in androidtv_remote config_flow (#127162)

This commit is contained in:
epenet 2024-10-03 10:21:53 +02:00 committed by GitHub
parent 7d3d693fe8
commit a2e4de2d0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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