1 """Config flow for System Bridge integration."""
3 from __future__
import annotations
6 from collections.abc
import Mapping
10 from systembridgeconnector.exceptions
import (
11 AuthenticationException,
12 ConnectionClosedException,
13 ConnectionErrorException,
15 from systembridgeconnector.websocket_client
import WebSocketClient
16 from systembridgemodels.modules
import GetData, Module
17 import voluptuous
as vol
27 from .const
import DATA_WAIT_TIMEOUT, DOMAIN
29 _LOGGER = logging.getLogger(__name__)
31 STEP_AUTHENTICATE_DATA_SCHEMA = vol.Schema({vol.Required(CONF_TOKEN): cv.string})
32 STEP_USER_DATA_SCHEMA = vol.Schema(
34 vol.Required(CONF_HOST): cv.string,
35 vol.Required(CONF_PORT, default=9170): cv.string,
36 vol.Required(CONF_TOKEN): cv.string,
45 """Validate the user input allows us to connect.
47 Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
50 websocket_client = WebSocketClient(
58 async
with asyncio.timeout(DATA_WAIT_TIMEOUT):
59 await websocket_client.connect()
60 modules_data = await websocket_client.get_data(
61 GetData(modules=[Module.SYSTEM])
63 except AuthenticationException
as exception:
65 "Authentication error when connecting to %s: %s",
69 raise InvalidAuth
from exception
71 ConnectionClosedException,
72 ConnectionErrorException,
75 "Connection error when connecting to %s: %s", data[CONF_HOST], exception
77 raise CannotConnect
from exception
78 except TimeoutError
as exception:
79 _LOGGER.warning(
"Timed out connecting to %s: %s", data[CONF_HOST], exception)
80 raise CannotConnect
from exception
81 except ValueError
as exception:
82 raise CannotConnect
from exception
84 _LOGGER.debug(
"Got modules data: %s", modules_data)
85 if modules_data
is None or modules_data.system
is None:
88 _LOGGER.debug(
"Got System data: %s", modules_data.system)
90 return {
"hostname": data[CONF_HOST],
"uuid": modules_data.system.uuid}
95 user_input: dict[str, Any],
96 ) -> tuple[dict[str, str], dict[str, str] |
None]:
101 except CannotConnect:
102 errors[
"base"] =
"cannot_connect"
104 errors[
"base"] =
"invalid_auth"
106 _LOGGER.exception(
"Unexpected exception")
107 errors[
"base"] =
"unknown"
118 """Handle a config flow for System Bridge."""
126 """Initialize flow."""
127 self.
_input_input: dict[str, Any] = {}
130 self, user_input: dict[str, Any] |
None =
None
131 ) -> ConfigFlowResult:
132 """Handle the initial step."""
133 if user_input
is None:
135 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA
139 if not errors
and info
is not None:
147 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
151 self, user_input: dict[str, Any] |
None =
None
152 ) -> ConfigFlowResult:
153 """Handle getting the api-key for authentication."""
154 errors: dict[str, str] = {}
156 if user_input
is not None:
157 user_input = {**self.
_input_input, **user_input}
159 if not errors
and info
is not None:
169 updates={CONF_HOST: info[
"hostname"]}
175 step_id=
"authenticate",
176 data_schema=STEP_AUTHENTICATE_DATA_SCHEMA,
177 description_placeholders={
"name": self.
_name_name},
182 self, discovery_info: zeroconf.ZeroconfServiceInfo
183 ) -> ConfigFlowResult:
184 """Handle zeroconf discovery."""
185 properties = discovery_info.properties
186 host = properties.get(
"ip")
187 uuid = properties.get(
"uuid")
189 if host
is None or uuid
is None:
199 CONF_PORT: properties.get(
"port"),
205 self, entry_data: Mapping[str, Any]
206 ) -> ConfigFlowResult:
207 """Perform reauth upon an API authentication error."""
208 self.
_name_name = entry_data[CONF_HOST]
210 CONF_HOST: entry_data[CONF_HOST],
211 CONF_PORT: entry_data[CONF_PORT],
217 """Error to indicate we cannot connect."""
221 """Error to indicate there is invalid auth."""
ConfigFlowResult async_step_authenticate(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry _get_reauth_entry(self)
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)
None _abort_if_unique_id_mismatch(self, *str reason="unique_id_mismatch", 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)
tuple[dict[str, str], dict[str, str]|None] _async_get_info(HomeAssistant hass, dict[str, Any] user_input)
dict[str, str] _validate_input(HomeAssistant hass, dict[str, Any] data)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)