1 """Config flow for Opower integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
13 get_supported_utility_names,
16 import voluptuous
as vol
24 from .const
import CONF_TOTP_SECRET, CONF_UTILITY, DOMAIN
26 _LOGGER = logging.getLogger(__name__)
28 STEP_USER_DATA_SCHEMA = vol.Schema(
30 vol.Required(CONF_UTILITY): vol.In(get_supported_utility_names()),
31 vol.Required(CONF_USERNAME): str,
32 vol.Required(CONF_PASSWORD): str,
38 hass: HomeAssistant, login_data: dict[str, str]
40 """Validate login data and return any errors."""
43 login_data[CONF_UTILITY],
44 login_data[CONF_USERNAME],
45 login_data[CONF_PASSWORD],
46 login_data.get(CONF_TOTP_SECRET),
48 errors: dict[str, str] = {}
50 await api.async_login()
53 "Invalid auth when connecting to %s", login_data[CONF_UTILITY]
55 errors[
"base"] =
"invalid_auth"
57 _LOGGER.exception(
"Could not connect to %s", login_data[CONF_UTILITY])
58 errors[
"base"] =
"cannot_connect"
63 """Handle a config flow for Opower."""
68 """Initialize a new OpowerConfigFlow."""
69 self.
utility_infoutility_info: dict[str, Any] |
None =
None
72 self, user_input: dict[str, Any] |
None =
None
73 ) -> ConfigFlowResult:
74 """Handle the initial step."""
75 errors: dict[str, str] = {}
76 if user_input
is not None:
79 CONF_UTILITY: user_input[CONF_UTILITY],
80 CONF_USERNAME: user_input[CONF_USERNAME],
83 if select_utility(user_input[CONF_UTILITY]).accepts_mfa():
92 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
96 self, user_input: dict[str, Any] |
None =
None
97 ) -> ConfigFlowResult:
98 """Handle MFA step."""
100 errors: dict[str, str] = {}
101 if user_input
is not None:
110 CONF_USERNAME, default=self.
utility_infoutility_info[CONF_USERNAME]
112 vol.Required(CONF_PASSWORD): str,
117 schema[vol.Required(CONF_TOTP_SECRET)] = str
121 data_schema=vol.Schema(schema),
127 """Create the config entry."""
129 title=f
"{data[CONF_UTILITY]} ({data[CONF_USERNAME]})",
134 self, entry_data: Mapping[str, Any]
135 ) -> ConfigFlowResult:
136 """Handle configuration by re-auth."""
140 self, user_input: dict[str, Any] |
None =
None
141 ) -> ConfigFlowResult:
142 """Dialog that informs the user that reauth is required."""
143 errors: dict[str, str] = {}
145 if user_input
is not None:
146 data = {**reauth_entry.data, **user_input}
151 schema: VolDictType = {
152 vol.Required(CONF_USERNAME): reauth_entry.data[CONF_USERNAME],
153 vol.Required(CONF_PASSWORD): str,
155 if select_utility(reauth_entry.data[CONF_UTILITY]).accepts_mfa():
156 schema[vol.Optional(CONF_TOTP_SECRET)] = str
158 step_id=
"reauth_confirm",
159 data_schema=vol.Schema(schema),
161 description_placeholders={CONF_NAME: reauth_entry.title},
ConfigFlowResult _async_create_opower_entry(self, dict[str, Any] data)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
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_mfa(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)
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, str] _validate_login(HomeAssistant hass, dict[str, str] login_data)
aiohttp.ClientSession async_create_clientsession()