1 """Config flow for Tado integration."""
3 from __future__
import annotations
9 from PyTado.interface
import Tado
10 import requests.exceptions
11 import voluptuous
as vol
26 CONST_OVERLAY_TADO_DEFAULT,
27 CONST_OVERLAY_TADO_OPTIONS,
32 _LOGGER = logging.getLogger(__name__)
34 DATA_SCHEMA = vol.Schema(
36 vol.Required(CONF_USERNAME): str,
37 vol.Required(CONF_PASSWORD): str,
42 async
def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
43 """Validate the user input allows us to connect.
45 Data has the keys from DATA_SCHEMA with values provided by the user.
49 tado = await hass.async_add_executor_job(
50 Tado, data[CONF_USERNAME], data[CONF_PASSWORD]
52 tado_me = await hass.async_add_executor_job(tado.getMe)
53 except KeyError
as ex:
54 raise InvalidAuth
from ex
55 except RuntimeError
as ex:
56 raise CannotConnect
from ex
57 except requests.exceptions.HTTPError
as ex:
58 if ex.response.status_code > 400
and ex.response.status_code < 500:
59 raise InvalidAuth
from ex
60 raise CannotConnect
from ex
62 if "homes" not in tado_me
or len(tado_me[
"homes"]) == 0:
65 home = tado_me[
"homes"][0]
66 unique_id =
str(home[
"id"])
69 return {
"title": name, UNIQUE_ID: unique_id}
73 """Handle a config flow for Tado."""
78 self, user_input: dict[str, Any] |
None =
None
79 ) -> ConfigFlowResult:
80 """Handle the initial step."""
82 if user_input
is not None:
86 errors[
"base"] =
"cannot_connect"
88 errors[
"base"] =
"invalid_auth"
90 errors[
"base"] =
"no_homes"
92 _LOGGER.exception(
"Unexpected exception")
93 errors[
"base"] =
"unknown"
95 if "base" not in errors:
99 title=validated[
"title"], data=user_input
103 step_id=
"user", data_schema=DATA_SCHEMA, errors=errors
107 self, discovery_info: zeroconf.ZeroconfServiceInfo
108 ) -> ConfigFlowResult:
109 """Handle HomeKit discovery."""
112 key.lower(): value
for (key, value)
in discovery_info.properties.items()
119 self, user_input: dict[str, Any] |
None =
None
120 ) -> ConfigFlowResult:
121 """Handle a reconfiguration flow initialized by the user."""
122 errors: dict[str, str] = {}
125 if user_input
is not None:
126 user_input[CONF_USERNAME] = reconfigure_entry.data[CONF_USERNAME]
129 except CannotConnect:
130 errors[
"base"] =
"cannot_connect"
131 except PyTado.exceptions.TadoWrongCredentialsException:
132 errors[
"base"] =
"invalid_auth"
134 errors[
"base"] =
"no_homes"
136 _LOGGER.exception(
"Unexpected exception")
137 errors[
"base"] =
"unknown"
141 reconfigure_entry, data_updates=user_input
145 step_id=
"reconfigure",
146 data_schema=vol.Schema(
148 vol.Required(CONF_PASSWORD): str,
152 description_placeholders={
153 CONF_USERNAME: reconfigure_entry.data[CONF_USERNAME]
160 config_entry: ConfigEntry,
161 ) -> OptionsFlowHandler:
162 """Get the options flow for this handler."""
167 """Handle an option flow for Tado."""
170 self, user_input: dict[str, Any] |
None =
None
171 ) -> ConfigFlowResult:
172 """Handle options flow."""
173 if user_input
is not None:
176 data_schema = vol.Schema(
181 CONF_FALLBACK, CONST_OVERLAY_TADO_DEFAULT
183 ): vol.In(CONST_OVERLAY_TADO_OPTIONS),
186 return self.
async_show_formasync_show_form(step_id=
"init", data_schema=data_schema)
190 """Error to indicate we cannot connect."""
193 class InvalidAuth(HomeAssistantError):
194 """Error to indicate there is invalid auth."""
198 """Error to indicate the account has no homes."""
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
OptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_homekit(self, zeroconf.ZeroconfServiceInfo discovery_info)
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
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_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_step_user(self, dict[str, Any]|None user_input=None)
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)
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)
dict[str, Any] validate_input(HomeAssistant hass, dict[str, Any] data)