Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for the Vallox integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from vallox_websocket_api import Vallox, ValloxApiException
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_HOST, CONF_NAME
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import HomeAssistantError
15 from homeassistant.util.network import is_ip_address
16 
17 from .const import DEFAULT_NAME, DOMAIN
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 CONFIG_SCHEMA = vol.Schema(
22  {
23  vol.Required(CONF_HOST): str,
24  }
25 )
26 
27 
28 async def validate_host(hass: HomeAssistant, host: str) -> None:
29  """Validate that the user input allows us to connect."""
30 
31  if not is_ip_address(host):
32  raise InvalidHost(f"Invalid IP address: {host}")
33 
34  client = Vallox(host)
35  await client.fetch_metric_data()
36 
37 
38 class ValloxConfigFlow(ConfigFlow, domain=DOMAIN):
39  """Handle a config flow for the Vallox integration."""
40 
41  VERSION = 1
42 
43  async def async_step_user(
44  self, user_input: dict[str, Any] | None = None
45  ) -> ConfigFlowResult:
46  """Handle the initial step."""
47  if user_input is None:
48  return self.async_show_formasync_show_formasync_show_form(
49  step_id="user",
50  data_schema=CONFIG_SCHEMA,
51  )
52 
53  errors: dict[str, str] = {}
54 
55  host = user_input[CONF_HOST]
56 
57  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: host})
58 
59  try:
60  await validate_host(self.hass, host)
61  except InvalidHost:
62  errors[CONF_HOST] = "invalid_host"
63  except ValloxApiException:
64  errors[CONF_HOST] = "cannot_connect"
65  except Exception:
66  _LOGGER.exception("Unexpected exception")
67  errors[CONF_HOST] = "unknown"
68  else:
69  return self.async_create_entryasync_create_entryasync_create_entry(
70  title=DEFAULT_NAME,
71  data={
72  **user_input,
73  CONF_NAME: DEFAULT_NAME,
74  },
75  )
76 
77  return self.async_show_formasync_show_formasync_show_form(
78  step_id="user",
79  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
80  CONFIG_SCHEMA, {CONF_HOST: host}
81  ),
82  errors=errors,
83  )
84 
86  self, user_input: dict[str, Any] | None = None
87  ) -> ConfigFlowResult:
88  """Handle reconfiguration of the Vallox device host address."""
89  reconfigure_entry = self._get_reconfigure_entry_get_reconfigure_entry()
90  if not user_input:
91  return self.async_show_formasync_show_formasync_show_form(
92  step_id="reconfigure",
93  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
94  CONFIG_SCHEMA, {CONF_HOST: reconfigure_entry.data.get(CONF_HOST)}
95  ),
96  )
97 
98  updated_host = user_input[CONF_HOST]
99 
100  if reconfigure_entry.data.get(CONF_HOST) != updated_host:
101  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: updated_host})
102 
103  errors: dict[str, str] = {}
104 
105  try:
106  await validate_host(self.hass, updated_host)
107  except InvalidHost:
108  errors[CONF_HOST] = "invalid_host"
109  except ValloxApiException:
110  errors[CONF_HOST] = "cannot_connect"
111  except Exception: # pylint: disable=broad-except
112  _LOGGER.exception("Unexpected exception")
113  errors[CONF_HOST] = "unknown"
114  else:
115  return self.async_update_reload_and_abortasync_update_reload_and_abort(
116  reconfigure_entry, data_updates={CONF_HOST: updated_host}
117  )
118 
119  return self.async_show_formasync_show_formasync_show_form(
120  step_id="reconfigure",
121  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
122  CONFIG_SCHEMA, {CONF_HOST: updated_host}
123  ),
124  errors=errors,
125  )
126 
127 
129  """Error to indicate an invalid host was input."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:45
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:87
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)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)
None validate_host(HomeAssistant hass, str host)
Definition: config_flow.py:28
bool is_ip_address(str address)
Definition: network.py:63