Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Wolf SmartSet Service integration."""
2 
3 import logging
4 
5 from httpcore import ConnectError
6 import voluptuous as vol
7 from wolf_comm.models import Device
8 from wolf_comm.token_auth import InvalidAuth
9 from wolf_comm.wolf_client import WolfClient
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
13 
14 from .const import DEVICE_GATEWAY, DEVICE_ID, DEVICE_NAME, DOMAIN
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 USER_SCHEMA = vol.Schema(
19  {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
20 )
21 
22 
23 class WolfLinkConfigFlow(ConfigFlow, domain=DOMAIN):
24  """Handle a config flow for Wolf SmartSet Service."""
25 
26  VERSION = 1
27  MINOR_VERSION = 2
28 
29  fetched_systems: list[Device]
30 
31  def __init__(self) -> None:
32  """Initialize with empty username and password."""
33  self.usernameusername: str | None = None
34  self.passwordpassword: str | None = None
35 
36  async def async_step_user(
37  self, user_input: dict[str, str] | None = None
38  ) -> ConfigFlowResult:
39  """Handle the initial step to get connection parameters."""
40  errors = {}
41  if user_input is not None:
42  wolf_client = WolfClient(
43  user_input[CONF_USERNAME], user_input[CONF_PASSWORD]
44  )
45  try:
46  self.fetched_systemsfetched_systems = await wolf_client.fetch_system_list()
47  except ConnectError:
48  errors["base"] = "cannot_connect"
49  except InvalidAuth:
50  errors["base"] = "invalid_auth"
51  except Exception:
52  _LOGGER.exception("Unexpected exception")
53  errors["base"] = "unknown"
54  else:
55  self.usernameusername = user_input[CONF_USERNAME]
56  self.passwordpassword = user_input[CONF_PASSWORD]
57  return await self.async_step_deviceasync_step_device()
58  return self.async_show_formasync_show_formasync_show_form(
59  step_id="user", data_schema=USER_SCHEMA, errors=errors
60  )
61 
62  async def async_step_device(
63  self, user_input: dict[str, str] | None = None
64  ) -> ConfigFlowResult:
65  """Allow user to select device from devices connected to specified account."""
66  errors: dict[str, str] = {}
67  if user_input is not None:
68  device_name = user_input[DEVICE_NAME]
69  system = [
70  device for device in self.fetched_systemsfetched_systems if device.name == device_name
71  ]
72  device_id = system[0].id
73  await self.async_set_unique_idasync_set_unique_id(str(device_id))
74  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
75  return self.async_create_entryasync_create_entryasync_create_entry(
76  title=user_input[DEVICE_NAME],
77  data={
78  CONF_USERNAME: self.usernameusername,
79  CONF_PASSWORD: self.passwordpassword,
80  DEVICE_NAME: device_name,
81  DEVICE_GATEWAY: system[0].gateway,
82  DEVICE_ID: device_id,
83  },
84  )
85 
86  data_schema = vol.Schema(
87  {
88  vol.Required(DEVICE_NAME): vol.In(
89  [info.name for info in self.fetched_systemsfetched_systems]
90  )
91  }
92  )
93  return self.async_show_formasync_show_formasync_show_form(
94  step_id="device", data_schema=data_schema, errors=errors
95  )
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)
str
_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)