Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the honeywell integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 import aiosomecomfort
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import (
12  ConfigEntry,
13  ConfigFlow,
14  ConfigFlowResult,
15  OptionsFlow,
16 )
17 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
18 from homeassistant.core import callback
19 from homeassistant.helpers.aiohttp_client import async_get_clientsession
20 
21 from .const import (
22  CONF_COOL_AWAY_TEMPERATURE,
23  CONF_HEAT_AWAY_TEMPERATURE,
24  DEFAULT_COOL_AWAY_TEMPERATURE,
25  DEFAULT_HEAT_AWAY_TEMPERATURE,
26  DOMAIN,
27 )
28 
29 REAUTH_SCHEMA = vol.Schema(
30  {
31  vol.Required(CONF_USERNAME): str,
32  vol.Required(CONF_PASSWORD): str,
33  }
34 )
35 
36 
37 class HoneywellConfigFlow(ConfigFlow, domain=DOMAIN):
38  """Handle a honeywell config flow."""
39 
40  VERSION = 1
41 
42  async def async_step_reauth(
43  self, entry_data: Mapping[str, Any]
44  ) -> ConfigFlowResult:
45  """Handle re-authentication with Honeywell."""
46  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
47 
49  self, user_input: dict[str, Any] | None = None
50  ) -> ConfigFlowResult:
51  """Confirm re-authentication with Honeywell."""
52  errors: dict[str, str] = {}
53 
54  reauth_entry = self._get_reauth_entry_get_reauth_entry()
55  if user_input:
56  try:
57  await self.is_validis_valid(
58  username=user_input[CONF_USERNAME],
59  password=user_input[CONF_PASSWORD],
60  )
61 
62  except aiosomecomfort.AuthError:
63  errors["base"] = "invalid_auth"
64  except (
65  aiosomecomfort.ConnectionError,
66  aiosomecomfort.ConnectionTimeout,
67  TimeoutError,
68  ):
69  errors["base"] = "cannot_connect"
70  else:
71  return self.async_update_reload_and_abortasync_update_reload_and_abort(
72  reauth_entry,
73  data_updates=user_input,
74  )
75 
76  return self.async_show_formasync_show_formasync_show_form(
77  step_id="reauth_confirm",
78  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
79  REAUTH_SCHEMA, reauth_entry.data
80  ),
81  errors=errors,
82  description_placeholders={"name": "Honeywell"},
83  )
84 
85  async def async_step_user(self, user_input=None) -> ConfigFlowResult:
86  """Create config entry. Show the setup form to the user."""
87  errors: dict[str, str] = {}
88  if user_input is not None:
89  try:
90  await self.is_validis_valid(**user_input)
91  except aiosomecomfort.AuthError:
92  errors["base"] = "invalid_auth"
93  except (
94  aiosomecomfort.ConnectionError,
95  aiosomecomfort.ConnectionTimeout,
96  TimeoutError,
97  ):
98  errors["base"] = "cannot_connect"
99  if not errors:
100  return self.async_create_entryasync_create_entryasync_create_entry(
101  title=DOMAIN,
102  data=user_input,
103  )
104 
105  data_schema = {
106  vol.Required(CONF_USERNAME): str,
107  vol.Required(CONF_PASSWORD): str,
108  }
109  return self.async_show_formasync_show_formasync_show_form(
110  step_id="user",
111  data_schema=vol.Schema(data_schema),
112  errors=errors,
113  )
114 
115  async def is_valid(self, **kwargs) -> bool:
116  """Check if login credentials are valid."""
117  client = aiosomecomfort.AIOSomeComfort(
118  kwargs[CONF_USERNAME],
119  kwargs[CONF_PASSWORD],
120  session=async_get_clientsession(self.hass),
121  )
122 
123  await client.login()
124  return True
125 
126  @staticmethod
127  @callback
129  config_entry: ConfigEntry,
130  ) -> HoneywellOptionsFlowHandler:
131  """Options callback for Honeywell."""
133 
134 
136  """Config flow options for Honeywell."""
137 
138  async def async_step_init(self, user_input=None) -> ConfigFlowResult:
139  """Manage the options."""
140  if user_input is not None:
141  return self.async_create_entryasync_create_entry(title=DOMAIN, data=user_input)
142 
143  return self.async_show_formasync_show_form(
144  step_id="init",
145  data_schema=vol.Schema(
146  {
147  vol.Required(
148  CONF_COOL_AWAY_TEMPERATURE,
149  default=self.config_entryconfig_entryconfig_entry.options.get(
150  CONF_COOL_AWAY_TEMPERATURE, DEFAULT_COOL_AWAY_TEMPERATURE
151  ),
152  ): int,
153  vol.Required(
154  CONF_HEAT_AWAY_TEMPERATURE,
155  default=self.config_entryconfig_entryconfig_entry.options.get(
156  CONF_HEAT_AWAY_TEMPERATURE, DEFAULT_HEAT_AWAY_TEMPERATURE
157  ),
158  ): int,
159  }
160  ),
161  )
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:44
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:50
ConfigFlowResult async_step_user(self, user_input=None)
Definition: config_flow.py:85
HoneywellOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:130
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_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)
None config_entry(self, ConfigEntry value)
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)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)