1 """Config flow for Tradfri."""
3 from __future__
import annotations
9 from pytradfri
import Gateway, RequestError
10 from pytradfri.api.aiocoap_api
import APIFactory
11 import voluptuous
as vol
18 from .const
import CONF_GATEWAY_ID, CONF_IDENTITY, CONF_KEY, DOMAIN
20 KEY_SECURITY_CODE =
"security_code"
24 """Exception if authentication occurs."""
27 """Initialize exception."""
33 """Handle a config flow."""
38 """Initialize flow."""
39 self.
_host_host: str |
None =
None
42 self, user_input: dict[str, Any] |
None =
None
43 ) -> ConfigFlowResult:
44 """Handle a flow initialized by the user."""
48 self, user_input: dict[str, Any] |
None =
None
49 ) -> ConfigFlowResult:
50 """Handle the authentication with a gateway."""
51 errors: dict[str, str] = {}
53 if user_input
is not None:
54 host = user_input.get(CONF_HOST, self.
_host_host)
57 self.hass, host, user_input[KEY_SECURITY_CODE]
62 except AuthError
as err:
63 errors[
"base"] = err.code
69 if self.
_host_host
is None:
70 fields[vol.Required(CONF_HOST, default=user_input.get(CONF_HOST))] = str
73 vol.Required(KEY_SECURITY_CODE, default=user_input.get(KEY_SECURITY_CODE))
77 step_id=
"auth", data_schema=vol.Schema(fields), errors=errors
81 self, discovery_info: zeroconf.ZeroconfServiceInfo
82 ) -> ConfigFlowResult:
83 """Handle homekit discovery."""
85 discovery_info.properties[zeroconf.ATTR_PROPERTIES_ID]
89 host = discovery_info.host
92 if entry.data.get(CONF_HOST) != host:
96 if not entry.unique_id:
97 self.hass.config_entries.async_update_entry(
99 unique_id=discovery_info.properties[zeroconf.ATTR_PROPERTIES_ID],
108 """Create an entry from data."""
109 host = data[CONF_HOST]
110 gateway_id = data[CONF_GATEWAY_ID]
115 if entry.data.get(CONF_GATEWAY_ID) == gateway_id
116 or entry.data.get(CONF_HOST) == host
122 asyncio.create_task(self.hass.config_entries.async_remove(entry_id))
123 for entry_id
in same_hub_entries
131 hass: HomeAssistant, host: str, security_code: str
132 ) -> dict[str, str | bool]:
133 """Authenticate with a Tradfri hub."""
135 identity = uuid4().hex
137 api_factory = await APIFactory.init(host, psk_id=identity)
140 async
with asyncio.timeout(5):
141 key = await api_factory.generate_psk(security_code)
142 except RequestError
as err:
143 raise AuthError(
"invalid_security_code")
from err
144 except TimeoutError
as err:
147 await api_factory.shutdown()
154 hass: HomeAssistant, host: str, identity: str, key: str
155 ) -> dict[str, str | bool]:
156 """Return info for the gateway."""
159 factory = await APIFactory.init(host, psk_id=identity, psk=key)
161 api = factory.request
163 gateway_info_result = await
api(gateway.get_gateway_info())
165 await factory.shutdown()
166 except (OSError, RequestError)
as err:
169 raise AuthError(
"cannot_connect")
from err
173 CONF_IDENTITY: identity,
175 CONF_GATEWAY_ID: gateway_info_result.id,
None __init__(self, str code)
ConfigFlowResult async_step_auth(self, dict[str, Any]|None user_input=None)
ConfigFlowResult _entry_from_data(self, dict[str, Any] data)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_homekit(self, zeroconf.ZeroconfServiceInfo discovery_info)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
dict[str, str|bool] authenticate(HomeAssistant hass, str host, str security_code)
dict[str, str|bool] get_gateway_info(HomeAssistant hass, str host, str identity, str key)