1 """Config flow for NuHeat integration."""
3 from http
import HTTPStatus
8 import requests.exceptions
9 import voluptuous
as vol
16 from .const
import CONF_SERIAL_NUMBER, DOMAIN
18 _LOGGER = logging.getLogger(__name__)
20 DATA_SCHEMA = vol.Schema(
22 vol.Required(CONF_USERNAME): str,
23 vol.Required(CONF_PASSWORD): str,
24 vol.Required(CONF_SERIAL_NUMBER): str,
30 """Validate the user input allows us to connect.
32 Data has the keys from DATA_SCHEMA with values provided by the user.
34 api = nuheat.NuHeat(data[CONF_USERNAME], data[CONF_PASSWORD])
37 await hass.async_add_executor_job(api.authenticate)
38 except requests.exceptions.Timeout
as ex:
39 raise CannotConnect
from ex
40 except requests.exceptions.HTTPError
as ex:
42 ex.response.status_code > HTTPStatus.BAD_REQUEST
43 and ex.response.status_code < HTTPStatus.INTERNAL_SERVER_ERROR
45 raise InvalidAuth
from ex
46 raise CannotConnect
from ex
50 except Exception
as ex:
51 raise InvalidAuth
from ex
54 thermostat = await hass.async_add_executor_job(
55 api.get_thermostat, data[CONF_SERIAL_NUMBER]
57 except requests.exceptions.HTTPError
as ex:
58 raise InvalidThermostat
from ex
60 return {
"title": thermostat.room,
"serial_number": thermostat.serial_number}
64 """Handle a config flow for NuHeat."""
69 self, user_input: dict[str, Any] |
None =
None
70 ) -> ConfigFlowResult:
71 """Handle the initial step."""
73 if user_input
is not None:
77 errors[
"base"] =
"cannot_connect"
79 errors[
"base"] =
"invalid_auth"
80 except InvalidThermostat:
81 errors[
"base"] =
"invalid_thermostat"
83 _LOGGER.exception(
"Unexpected exception")
84 errors[
"base"] =
"unknown"
86 if "base" not in errors:
92 step_id=
"user", data_schema=DATA_SCHEMA, errors=errors
97 """Error to indicate we cannot connect."""
101 """Error to indicate there is invalid auth."""
105 """Error to indicate there is invalid thermostat."""
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_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)
def validate_input(HomeAssistant hass, data)