1 """Config Flow for Hive."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
8 from apyhiveapi
import Auth
9 from apyhiveapi.helper.hive_exceptions
import (
15 import voluptuous
as vol
27 from .const
import CONF_CODE, CONF_DEVICE_NAME, CONFIG_ENTRY_VERSION, DOMAIN
31 """Handle a Hive config flow."""
33 VERSION = CONFIG_ENTRY_VERSION
37 """Initialize the config flow."""
38 self.data: dict[str, Any] = {}
39 self.
tokenstokens: dict[str, str] = {}
40 self.
entryentry: ConfigEntry |
None =
None
45 self, user_input: dict[str, Any] |
None =
None
46 ) -> ConfigFlowResult:
47 """Prompt user input. Create or edit entry."""
48 errors: dict[str, str] = {}
50 if user_input
is not None:
51 self.data.
update(user_input)
53 username=self.data[CONF_USERNAME], password=self.data[CONF_PASSWORD]
58 if self.context[
"source"] != SOURCE_REAUTH:
64 except HiveInvalidUsername:
65 errors[
"base"] =
"invalid_username"
66 except HiveInvalidPassword:
67 errors[
"base"] =
"invalid_password"
69 errors[
"base"] =
"no_internet_available"
71 if self.
tokenstokens.
get(
"ChallengeName") ==
"SMS_MFA":
79 except UnknownHiveError:
80 errors[
"base"] =
"unknown"
84 {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
89 self, user_input: dict[str, Any] |
None =
None
90 ) -> ConfigFlowResult:
91 """Handle 2fa step."""
94 if user_input
and user_input[
"2fa"] ==
"0000":
99 user_input[
"2fa"], self.
tokenstokens
101 except HiveInvalid2FACode:
102 errors[
"base"] =
"invalid_code"
104 errors[
"base"] =
"no_internet_available"
107 if self.context[
"source"] == SOURCE_REAUTH:
112 schema = vol.Schema({vol.Required(CONF_CODE): str})
116 self, user_input: dict[str, Any] |
None =
None
117 ) -> ConfigFlowResult:
118 """Handle hive configuration step."""
123 self.
device_namedevice_name = user_input[
"device_name"]
125 self.data[
"device_data"] = await self.
hive_authhive_auth.get_device_data()
129 except UnknownHiveError:
130 errors[
"base"] =
"unknown"
133 {vol.Optional(CONF_DEVICE_NAME, default=self.
device_namedevice_name): str}
136 step_id=
"configuration", data_schema=schema, errors=errors
140 """Finish setup and create the config entry."""
142 if "AuthenticationResult" not in self.
tokenstokens:
143 raise UnknownHiveError
146 self.data[
"tokens"] = self.
tokenstokens
147 if self.context[
"source"] == SOURCE_REAUTH:
148 assert self.
entryentry
149 self.hass.config_entries.async_update_entry(
150 self.
entryentry, title=self.data[
"username"], data=self.data
152 await self.hass.config_entries.async_reload(self.
entryentry.entry_id)
157 self, entry_data: Mapping[str, Any]
158 ) -> ConfigFlowResult:
159 """Re Authenticate a user."""
161 CONF_USERNAME: entry_data[CONF_USERNAME],
162 CONF_PASSWORD: entry_data[CONF_PASSWORD],
173 config_entry: ConfigEntry,
174 ) -> HiveOptionsFlowHandler:
175 """Hive options callback."""
180 """Config flow options for Hive."""
182 def __init__(self, config_entry: ConfigEntry) ->
None:
183 """Initialize Hive options flow."""
185 self.
intervalinterval = config_entry.options.get(CONF_SCAN_INTERVAL, 120)
188 self, user_input: dict[str, Any] |
None =
None
189 ) -> ConfigFlowResult:
190 """Manage the options."""
194 self, user_input: dict[str, Any] |
None =
None
195 ) -> ConfigFlowResult:
196 """Handle a flow initialized by the user."""
198 errors: dict[str, str] = {}
199 if user_input
is not None:
200 new_interval = user_input.get(CONF_SCAN_INTERVAL)
202 await self.
hivehive.updateInterval(new_interval)
207 vol.Optional(CONF_SCAN_INTERVAL, default=self.
intervalinterval): vol.All(
208 vol.Coerce(int), vol.Range(min=30)
212 return self.
async_show_formasync_show_form(step_id=
"user", data_schema=schema, errors=errors)
216 """Catch unknown hive error."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_configuration(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
ConfigFlowResult async_setup_hive_entry(self)
ConfigFlowResult async_step_2fa(self, dict[str, Any]|None user_input=None)
HiveOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
None __init__(self, ConfigEntry config_entry)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_init(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_step_user(self, dict[str, Any]|None user_input=None)
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)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
_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)
web.Response get(self, web.Request request, str config_key)
IssData update(pyiss.ISS iss)