1 """Config flow for Bosch Smart Home Controller integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
7 from os
import makedirs
8 from typing
import Any, cast
10 from boschshcpy
import SHCRegisterClient, SHCSession
11 from boschshcpy.exceptions
import (
12 SHCAuthenticationError,
17 import voluptuous
as vol
33 _LOGGER = logging.getLogger(__name__)
35 HOST_SCHEMA = vol.Schema(
37 vol.Required(CONF_HOST): str,
43 hass: HomeAssistant, folder: str, filename: str, asset: bytes
45 """Write the tls assets to disk."""
46 makedirs(hass.config.path(DOMAIN, folder), exist_ok=
True)
48 hass.config.path(DOMAIN, folder, filename),
"w", encoding=
"utf8"
50 file_handle.write(asset.decode(
"utf-8"))
57 user_input: dict[str, Any],
59 ) -> dict[str, Any] |
None:
60 """Create and store credentials and validate session."""
61 helper = SHCRegisterClient(host, user_input[CONF_PASSWORD])
62 result = helper.register(host,
"HomeAssistant")
64 if result
is not None:
72 hass.config.path(DOMAIN, unique_id, CONF_SHC_CERT),
73 hass.config.path(DOMAIN, unique_id, CONF_SHC_KEY),
77 session.authenticate()
84 ) -> dict[str, str |
None]:
85 """Get information from host."""
93 information = session.mdns_info()
94 return {
"title": information.name,
"unique_id": information.unique_id}
98 """Handle a config flow for Bosch SHC."""
101 info: dict[str, str |
None]
105 self, entry_data: Mapping[str, Any]
106 ) -> ConfigFlowResult:
107 """Perform reauth upon an API authentication error."""
111 self, user_input: dict[str, Any] |
None =
None
112 ) -> ConfigFlowResult:
113 """Dialog that informs the user that reauth is required."""
114 if user_input
is None:
116 step_id=
"reauth_confirm",
117 data_schema=HOST_SCHEMA,
119 self.
hosthost = user_input[CONF_HOST]
124 self, user_input: dict[str, Any] |
None =
None
125 ) -> ConfigFlowResult:
126 """Handle the initial step."""
127 errors: dict[str, str] = {}
128 if user_input
is not None:
129 self.
hosthost = user_input[CONF_HOST]
132 except SHCConnectionError:
133 errors[
"base"] =
"cannot_connect"
135 _LOGGER.exception(
"Unexpected exception")
136 errors[
"base"] =
"unknown"
143 step_id=
"user", data_schema=HOST_SCHEMA, errors=errors
147 self, user_input: dict[str, Any] |
None =
None
148 ) -> ConfigFlowResult:
149 """Handle the credentials step."""
150 errors: dict[str, str] = {}
151 if user_input
is not None:
152 zeroconf_instance = await zeroconf.async_get_instance(self.hass)
155 unique_id = self.
infoinfo[
"unique_id"]
158 result = await self.hass.async_add_executor_job(
159 create_credentials_and_validate,
166 except SHCAuthenticationError:
167 errors[
"base"] =
"invalid_auth"
168 except SHCConnectionError:
169 errors[
"base"] =
"cannot_connect"
170 except SHCSessionError
as err:
171 _LOGGER.warning(
"Session error: %s", err.message)
172 errors[
"base"] =
"session_error"
173 except SHCRegistrationError
as err:
174 _LOGGER.warning(
"Registration error: %s", err.message)
175 errors[
"base"] =
"pairing_failed"
177 _LOGGER.exception(
"Unexpected exception")
178 errors[
"base"] =
"unknown"
183 CONF_SSL_CERTIFICATE: self.hass.config.path(
184 DOMAIN, unique_id, CONF_SHC_CERT
186 CONF_SSL_KEY: self.hass.config.path(
187 DOMAIN, unique_id, CONF_SHC_KEY
189 CONF_HOST: self.
hosthost,
190 CONF_TOKEN: result[
"token"],
191 CONF_HOSTNAME: result[
"token"].split(
":", 1)[1],
201 title=cast(str, self.
infoinfo[
"title"]),
210 CONF_PASSWORD, default=user_input.get(CONF_PASSWORD,
"")
216 step_id=
"credentials", data_schema=schema, errors=errors
220 self, discovery_info: zeroconf.ZeroconfServiceInfo
221 ) -> ConfigFlowResult:
222 """Handle zeroconf discovery."""
223 if not discovery_info.name.startswith(
"Bosch SHC"):
227 self.
infoinfo = await self.
_get_info_get_info(discovery_info.host)
228 except SHCConnectionError:
230 self.
hosthost = discovery_info.host
232 local_name = discovery_info.hostname[:-1]
233 node_name = local_name.removesuffix(
".local")
237 self.context[
"title_placeholders"] = {
"name": node_name}
241 self, user_input: dict[str, Any] |
None =
None
242 ) -> ConfigFlowResult:
243 """Handle discovery confirm."""
244 errors: dict[str, str] = {}
245 if user_input
is not None:
249 step_id=
"confirm_discovery",
250 description_placeholders={
251 "model":
"Bosch SHC",
252 "host": self.
hosthost,
257 async
def _get_info(self, host: str) -> dict[str, str |
None]:
258 """Get additional information."""
259 zeroconf_instance = await zeroconf.async_get_instance(self.hass)
261 return await self.hass.async_add_executor_job(
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_credentials(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_confirm_discovery(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
dict[str, str|None] _get_info(self, str host)
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)
ConfigFlowResult async_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
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, Any]|None create_credentials_and_validate(HomeAssistant hass, str host, str unique_id, dict[str, Any] user_input, zeroconf.HaZeroconf zeroconf_instance)
dict[str, str|None] get_info_from_host(HomeAssistant hass, str host, zeroconf.HaZeroconf zeroconf_instance)
None write_tls_asset(HomeAssistant hass, str folder, str filename, bytes asset)
None open(self, **Any kwargs)