1 """Config flow for the sma integration."""
3 from __future__
import annotations
9 import voluptuous
as vol
17 from .const
import CONF_GROUP, DOMAIN, GROUPS
19 _LOGGER = logging.getLogger(__name__)
22 async
def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
23 """Validate the user input allows us to connect."""
26 protocol =
"https" if data[CONF_SSL]
else "http"
27 url = f
"{protocol}://{data[CONF_HOST]}"
29 sma = pysma.SMA(session, url, data[CONF_PASSWORD], group=data[CONF_GROUP])
32 await sma.new_session()
33 device_info = await sma.device_info()
34 await sma.close_session()
40 """Handle a config flow for SMA."""
47 self._data: dict[str, Any] = {
48 CONF_HOST: vol.UNDEFINED,
50 CONF_VERIFY_SSL:
True,
51 CONF_GROUP: GROUPS[0],
52 CONF_PASSWORD: vol.UNDEFINED,
56 self, user_input: dict[str, Any] |
None =
None
57 ) -> ConfigFlowResult:
58 """First step in config flow."""
60 if user_input
is not None:
61 self._data[CONF_HOST] = user_input[CONF_HOST]
62 self._data[CONF_SSL] = user_input[CONF_SSL]
63 self._data[CONF_VERIFY_SSL] = user_input[CONF_VERIFY_SSL]
64 self._data[CONF_GROUP] = user_input[CONF_GROUP]
65 self._data[CONF_PASSWORD] = user_input[CONF_PASSWORD]
69 except pysma.exceptions.SmaConnectionException:
70 errors[
"base"] =
"cannot_connect"
71 except pysma.exceptions.SmaAuthenticationException:
72 errors[
"base"] =
"invalid_auth"
73 except pysma.exceptions.SmaReadException:
74 errors[
"base"] =
"cannot_retrieve_device_info"
76 _LOGGER.exception(
"Unexpected exception")
77 errors[
"base"] =
"unknown"
83 title=self._data[CONF_HOST], data=self._data
88 data_schema=vol.Schema(
90 vol.Required(CONF_HOST, default=self._data[CONF_HOST]): cv.string,
91 vol.Optional(CONF_SSL, default=self._data[CONF_SSL]): cv.boolean,
93 CONF_VERIFY_SSL, default=self._data[CONF_VERIFY_SSL]
95 vol.Optional(CONF_GROUP, default=self._data[CONF_GROUP]): vol.In(
98 vol.Required(CONF_PASSWORD): cv.string,
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|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_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)
dict[str, Any] 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)