1 """Config flow for Fronius integration."""
3 from __future__
import annotations
7 from typing
import Any, Final
9 from pyfronius
import Fronius, FroniusError
10 import voluptuous
as vol
19 from .const
import DOMAIN, FroniusConfigEntryData
21 _LOGGER: Final = logging.getLogger(__name__)
23 DHCP_REQUEST_DELAY: Final = 60
27 """Return the title of the config flow."""
29 f
"SolarNet {'Datalogger' if info['is_logger'] else 'Inverter'}"
35 hass: HomeAssistant, host: str
36 ) -> tuple[str, FroniusConfigEntryData]:
37 """Validate the user input allows us to connect."""
41 datalogger_info: dict[str, Any]
42 datalogger_info = await fronius.current_logger_info()
43 except FroniusError
as err:
46 logger_uid: str = datalogger_info[
"unique_identifier"][
"value"]
53 inverter_info = await fronius.inverter_info()
54 first_inverter = next(inverter
for inverter
in inverter_info[
"inverters"])
55 except FroniusError
as err:
57 raise CannotConnect
from err
58 except StopIteration
as err:
59 raise CannotConnect(
"No supported Fronius SolarNet device found.")
from err
60 first_inverter_uid: str = first_inverter[
"unique_id"][
"value"]
68 """Handle a config flow for Fronius."""
73 """Initialize flow."""
74 self.
infoinfo: FroniusConfigEntryData
77 self, user_input: dict[str, Any] |
None =
None
78 ) -> ConfigFlowResult:
79 """Handle the initial step."""
82 if user_input
is not None:
84 unique_id, info = await
validate_host(self.hass, user_input[CONF_HOST])
86 errors[
"base"] =
"cannot_connect"
88 _LOGGER.exception(
"Unexpected exception")
89 errors[
"base"] =
"unknown"
98 data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
103 self, discovery_info: DhcpServiceInfo
104 ) -> ConfigFlowResult:
105 """Handle a flow initiated by the DHCP client."""
107 if entry.data[CONF_HOST].removeprefix(
"http://").rstrip(
"/").lower()
in (
109 discovery_info.hostname,
114 await asyncio.sleep(DHCP_REQUEST_DELAY)
117 except CannotConnect:
126 self, user_input: dict[str, Any] |
None =
None
127 ) -> ConfigFlowResult:
128 """Attempt to confirm."""
130 if user_input
is not None:
134 self.context.
update({
"title_placeholders": {
"device": title}})
136 step_id=
"confirm_discovery",
137 description_placeholders={
143 self, user_input: dict[str, Any] |
None =
None
144 ) -> ConfigFlowResult:
145 """Add reconfigure step to allow to reconfigure a config entry."""
149 if user_input
is not None:
151 unique_id, info = await
validate_host(self.hass, user_input[CONF_HOST])
152 except CannotConnect:
153 errors[
"base"] =
"cannot_connect"
155 _LOGGER.exception(
"Unexpected exception")
156 errors[
"base"] =
"unknown"
163 host = reconfigure_entry.data[CONF_HOST]
165 step_id=
"reconfigure",
166 data_schema=vol.Schema({vol.Required(CONF_HOST, default=host): str}),
167 description_placeholders={
"device": reconfigure_entry.title},
173 """Error to indicate we cannot connect."""
ConfigFlowResult async_step_confirm_discovery(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_dhcp(self, DhcpServiceInfo 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")
None _set_confirm_only(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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=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)
ConfigEntry _get_reconfigure_entry(self)
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)
str create_title(FroniusConfigEntryData info)
tuple[str, FroniusConfigEntryData] validate_host(HomeAssistant hass, str host)
IssData update(pyiss.ISS iss)
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)