1 """Config flow for solarlog integration."""
3 from collections.abc
import Mapping
6 from urllib.parse
import ParseResult, urlparse
8 from solarlog_cli.solarlog_connector
import SolarLogConnector
9 from solarlog_cli.solarlog_exceptions
import (
10 SolarLogAuthenticationError,
11 SolarLogConnectionError,
14 import voluptuous
as vol
20 from .const
import CONF_HAS_PWD, DEFAULT_HOST, DEFAULT_NAME, DOMAIN
22 _LOGGER = logging.getLogger(__name__)
26 """Handle a config flow for solarlog."""
32 """Initialize the config flow."""
37 """Return parsed host url."""
38 url = urlparse(host,
"http")
39 netloc = url.netloc
or url.path
40 path = url.path
if url.netloc
else ""
41 url = ParseResult(
"http", netloc, path, *url[3:])
45 """Check if we can connect to the Solar-Log device."""
46 solarlog = SolarLogConnector(host)
48 await solarlog.test_connection()
49 except SolarLogConnectionError:
50 self.
_errors_errors = {CONF_HOST:
"cannot_connect"}
53 self.
_errors_errors = {CONF_HOST:
"unknown"}
56 await solarlog.client.close()
61 """Check if we get extended data from Solar-Log device."""
62 response: bool =
False
63 solarlog = SolarLogConnector(host, password=pwd)
65 response = await solarlog.test_extended_data_available()
66 except SolarLogAuthenticationError:
67 self.
_errors_errors = {CONF_HOST:
"password_error"}
70 self.
_errors_errors = {CONF_HOST:
"unknown"}
73 await solarlog.client.close()
78 self, user_input: dict[str, Any] |
None =
None
79 ) -> ConfigFlowResult:
80 """Step when user initializes a integration."""
82 if user_input
is not None:
83 user_input[CONF_HOST] = self.
_parse_url_parse_url(user_input[CONF_HOST])
87 user_input[CONF_NAME] =
slugify(user_input[CONF_NAME])
90 if user_input[CONF_HAS_PWD]:
95 title=user_input[CONF_NAME], data=user_input
98 user_input = {CONF_NAME: DEFAULT_NAME, CONF_HOST: DEFAULT_HOST}
102 data_schema=vol.Schema(
104 vol.Required(CONF_NAME, default=user_input[CONF_NAME]): str,
105 vol.Required(CONF_HOST, default=user_input[CONF_HOST]): str,
106 vol.Required(CONF_HAS_PWD, default=
False): bool,
113 self, user_input: dict[str, Any] |
None =
None
114 ) -> ConfigFlowResult:
115 """Step when user sets password ."""
117 if user_input
is not None:
119 self.
_user_input_user_input[CONF_HOST], user_input[CONF_PASSWORD]
126 user_input = {CONF_PASSWORD:
""}
130 data_schema=vol.Schema(
132 vol.Required(CONF_PASSWORD): str,
139 self, user_input: dict[str, Any] |
None =
None
140 ) -> ConfigFlowResult:
141 """Handle a reconfiguration flow initialized by the user."""
143 if user_input
is not None:
144 if not user_input[CONF_HAS_PWD]
or user_input.get(CONF_PASSWORD,
"") ==
"":
145 user_input[CONF_PASSWORD] =
""
146 user_input[CONF_HAS_PWD] =
False
148 reconfigure_entry, data_updates=user_input
152 reconfigure_entry.data[CONF_HOST], user_input.get(CONF_PASSWORD,
"")
157 data_updates=user_input,
161 step_id=
"reconfigure",
162 data_schema=vol.Schema(
165 CONF_HAS_PWD, default=reconfigure_entry.data[CONF_HAS_PWD]
167 vol.Optional(CONF_PASSWORD): str,
173 self, entry_data: Mapping[str, Any]
174 ) -> ConfigFlowResult:
175 """Handle flow upon an API authentication error."""
179 self, user_input: dict[str, Any] |
None =
None
180 ) -> ConfigFlowResult:
181 """Handle reauthorization flow."""
184 reauth_entry.data[CONF_HOST], user_input.get(CONF_PASSWORD,
"")
187 reauth_entry, data_updates=user_input
190 data_schema = vol.Schema(
193 CONF_HAS_PWD, default=reauth_entry.data[CONF_HAS_PWD]
195 vol.Optional(CONF_PASSWORD): str,
199 step_id=
"reauth_confirm",
200 data_schema=data_schema,
bool _test_connection(self, str host)
ConfigFlowResult async_step_password(self, dict[str, Any]|None user_input=None)
str _parse_url(self, str host)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
bool _test_extended_data(self, str host, str pwd="")
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigEntry _get_reauth_entry(self)
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=None)
ConfigEntry _get_reconfigure_entry(self)
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)