Improve typing of config flow (#69438)
This commit is contained in:
parent
bc5594e263
commit
f8367d3c01
1 changed files with 43 additions and 41 deletions
|
@ -11,6 +11,7 @@ import async_timeout
|
||||||
from pydeconz.errors import RequestError, ResponseError
|
from pydeconz.errors import RequestError, ResponseError
|
||||||
from pydeconz.gateway import DeconzSession
|
from pydeconz.gateway import DeconzSession
|
||||||
from pydeconz.utils import (
|
from pydeconz.utils import (
|
||||||
|
DiscoveredBridge,
|
||||||
discovery as deconz_discovery,
|
discovery as deconz_discovery,
|
||||||
get_bridge_id as deconz_get_bridge_id,
|
get_bridge_id as deconz_get_bridge_id,
|
||||||
normalize_bridge_id,
|
normalize_bridge_id,
|
||||||
|
@ -58,6 +59,11 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
|
|
||||||
_hassio_discovery: dict[str, Any]
|
_hassio_discovery: dict[str, Any]
|
||||||
|
|
||||||
|
bridges: list[DiscoveredBridge]
|
||||||
|
host: str
|
||||||
|
port: int
|
||||||
|
api_key: str
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
|
def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
|
||||||
|
@ -67,8 +73,6 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
"""Initialize the deCONZ config flow."""
|
"""Initialize the deCONZ config flow."""
|
||||||
self.bridge_id = ""
|
self.bridge_id = ""
|
||||||
self.bridges: list[dict[str, int | str]] = []
|
|
||||||
self.deconz_config: dict[str, int | str] = {}
|
|
||||||
|
|
||||||
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
|
||||||
|
@ -85,11 +89,9 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
|
|
||||||
for bridge in self.bridges:
|
for bridge in self.bridges:
|
||||||
if bridge[CONF_HOST] == user_input[CONF_HOST]:
|
if bridge[CONF_HOST] == user_input[CONF_HOST]:
|
||||||
self.bridge_id = cast(str, bridge["id"])
|
self.bridge_id = bridge["id"]
|
||||||
self.deconz_config = {
|
self.host = bridge[CONF_HOST]
|
||||||
CONF_HOST: bridge[CONF_HOST],
|
self.port = bridge[CONF_PORT]
|
||||||
CONF_PORT: bridge[CONF_PORT],
|
|
||||||
}
|
|
||||||
return await self.async_step_link()
|
return await self.async_step_link()
|
||||||
|
|
||||||
session = aiohttp_client.async_get_clientsession(self.hass)
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
||||||
|
@ -123,7 +125,8 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Manual configuration."""
|
"""Manual configuration."""
|
||||||
if user_input:
|
if user_input:
|
||||||
self.deconz_config = user_input
|
self.host = user_input[CONF_HOST]
|
||||||
|
self.port = user_input[CONF_PORT]
|
||||||
return await self.async_step_link()
|
return await self.async_step_link()
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
|
@ -143,16 +146,12 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
|
|
||||||
LOGGER.debug(
|
LOGGER.debug(
|
||||||
"Preparing linking with deCONZ gateway %s", pformat(self.deconz_config)
|
"Preparing linking with deCONZ gateway %s %d", self.host, self.port
|
||||||
)
|
)
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
session = aiohttp_client.async_get_clientsession(self.hass)
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
||||||
deconz_session = DeconzSession(
|
deconz_session = DeconzSession(session, self.host, self.port)
|
||||||
session,
|
|
||||||
host=self.deconz_config[CONF_HOST],
|
|
||||||
port=self.deconz_config[CONF_PORT],
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(10):
|
async with async_timeout.timeout(10):
|
||||||
|
@ -162,7 +161,7 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
errors["base"] = "no_key"
|
errors["base"] = "no_key"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.deconz_config[CONF_API_KEY] = api_key
|
self.api_key = api_key
|
||||||
return await self._create_entry()
|
return await self._create_entry()
|
||||||
|
|
||||||
return self.async_show_form(step_id="link", errors=errors)
|
return self.async_show_form(step_id="link", errors=errors)
|
||||||
|
@ -175,31 +174,36 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(10):
|
async with async_timeout.timeout(10):
|
||||||
self.bridge_id = await deconz_get_bridge_id(
|
self.bridge_id = await deconz_get_bridge_id(
|
||||||
session, **self.deconz_config
|
session, self.host, self.port, self.api_key
|
||||||
)
|
)
|
||||||
await self.async_set_unique_id(self.bridge_id)
|
await self.async_set_unique_id(self.bridge_id)
|
||||||
|
|
||||||
self._abort_if_unique_id_configured(
|
self._abort_if_unique_id_configured(
|
||||||
updates={
|
updates={
|
||||||
CONF_HOST: self.deconz_config[CONF_HOST],
|
CONF_HOST: self.host,
|
||||||
CONF_PORT: self.deconz_config[CONF_PORT],
|
CONF_PORT: self.port,
|
||||||
CONF_API_KEY: self.deconz_config[CONF_API_KEY],
|
CONF_API_KEY: self.api_key,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
return self.async_abort(reason="no_bridges")
|
return self.async_abort(reason="no_bridges")
|
||||||
|
|
||||||
return self.async_create_entry(title=self.bridge_id, data=self.deconz_config)
|
return self.async_create_entry(
|
||||||
|
title=self.bridge_id,
|
||||||
|
data={
|
||||||
|
CONF_HOST: self.host,
|
||||||
|
CONF_PORT: self.port,
|
||||||
|
CONF_API_KEY: self.api_key,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
async def async_step_reauth(self, config: dict[str, Any]) -> FlowResult:
|
async def async_step_reauth(self, config: dict[str, Any]) -> FlowResult:
|
||||||
"""Trigger a reauthentication flow."""
|
"""Trigger a reauthentication flow."""
|
||||||
self.context["title_placeholders"] = {CONF_HOST: config[CONF_HOST]}
|
self.context["title_placeholders"] = {CONF_HOST: config[CONF_HOST]}
|
||||||
|
|
||||||
self.deconz_config = {
|
self.host = config[CONF_HOST]
|
||||||
CONF_HOST: config[CONF_HOST],
|
self.port = config[CONF_PORT]
|
||||||
CONF_PORT: config[CONF_PORT],
|
|
||||||
}
|
|
||||||
|
|
||||||
return await self.async_step_link()
|
return await self.async_step_link()
|
||||||
|
|
||||||
|
@ -220,22 +224,23 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
if entry and entry.source == config_entries.SOURCE_HASSIO:
|
if entry and entry.source == config_entries.SOURCE_HASSIO:
|
||||||
return self.async_abort(reason="already_configured")
|
return self.async_abort(reason="already_configured")
|
||||||
|
|
||||||
hostname = cast(str, parsed_url.hostname)
|
self.host = cast(str, parsed_url.hostname)
|
||||||
port = cast(int, parsed_url.port)
|
self.port = cast(int, parsed_url.port)
|
||||||
|
|
||||||
self._abort_if_unique_id_configured(
|
self._abort_if_unique_id_configured(
|
||||||
updates={CONF_HOST: hostname, CONF_PORT: port}
|
updates={
|
||||||
|
CONF_HOST: self.host,
|
||||||
|
CONF_PORT: self.port,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
self.context.update(
|
self.context.update(
|
||||||
{
|
{
|
||||||
"title_placeholders": {"host": hostname},
|
"title_placeholders": {"host": self.host},
|
||||||
"configuration_url": f"http://{hostname}:{port}",
|
"configuration_url": f"http://{self.host}:{self.port}",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
self.deconz_config = {CONF_HOST: hostname, CONF_PORT: port}
|
|
||||||
|
|
||||||
return await self.async_step_link()
|
return await self.async_step_link()
|
||||||
|
|
||||||
async def async_step_hassio(self, discovery_info: HassioServiceInfo) -> FlowResult:
|
async def async_step_hassio(self, discovery_info: HassioServiceInfo) -> FlowResult:
|
||||||
|
@ -248,16 +253,19 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
self.bridge_id = normalize_bridge_id(discovery_info.config[CONF_SERIAL])
|
self.bridge_id = normalize_bridge_id(discovery_info.config[CONF_SERIAL])
|
||||||
await self.async_set_unique_id(self.bridge_id)
|
await self.async_set_unique_id(self.bridge_id)
|
||||||
|
|
||||||
|
self.host = discovery_info.config[CONF_HOST]
|
||||||
|
self.port = discovery_info.config[CONF_PORT]
|
||||||
|
self.api_key = discovery_info.config[CONF_API_KEY]
|
||||||
|
|
||||||
self._abort_if_unique_id_configured(
|
self._abort_if_unique_id_configured(
|
||||||
updates={
|
updates={
|
||||||
CONF_HOST: discovery_info.config[CONF_HOST],
|
CONF_HOST: self.host,
|
||||||
CONF_PORT: discovery_info.config[CONF_PORT],
|
CONF_PORT: self.port,
|
||||||
CONF_API_KEY: discovery_info.config[CONF_API_KEY],
|
CONF_API_KEY: self.api_key,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
self.context["configuration_url"] = HASSIO_CONFIGURATION_URL
|
self.context["configuration_url"] = HASSIO_CONFIGURATION_URL
|
||||||
|
|
||||||
self._hassio_discovery = discovery_info.config
|
self._hassio_discovery = discovery_info.config
|
||||||
|
|
||||||
return await self.async_step_hassio_confirm()
|
return await self.async_step_hassio_confirm()
|
||||||
|
@ -268,12 +276,6 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
"""Confirm a Hass.io discovery."""
|
"""Confirm a Hass.io discovery."""
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
self.deconz_config = {
|
|
||||||
CONF_HOST: self._hassio_discovery[CONF_HOST],
|
|
||||||
CONF_PORT: self._hassio_discovery[CONF_PORT],
|
|
||||||
CONF_API_KEY: self._hassio_discovery[CONF_API_KEY],
|
|
||||||
}
|
|
||||||
|
|
||||||
return await self._create_entry()
|
return await self._create_entry()
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
|
|
Loading…
Add table
Reference in a new issue