Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure ZWaveMe integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from url_normalize import url_normalize
8 import voluptuous as vol
9 
10 from homeassistant.components.zeroconf import ZeroconfServiceInfo
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_TOKEN, CONF_URL
13 
14 from . import helpers
15 from .const import DOMAIN
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 
20 class ZWaveMeConfigFlow(ConfigFlow, domain=DOMAIN):
21  """ZWaveMe integration config flow."""
22 
23  def __init__(self) -> None:
24  """Initialize flow."""
25  self.urlurl: str | None = None
26  self.tokentoken: str | None = None
27  self.uuiduuid: str | None = None
28 
29  async def async_step_user(
30  self, user_input: dict[str, str] | None = None
31  ) -> ConfigFlowResult:
32  """Handle a flow initialized by the user or started with zeroconf."""
33  errors = {}
34  placeholders = {
35  "local_token": "/112f7a4a-0051-cc2b-3b61-1898181b9950",
36  "find_token": (
37  "0481effe8a5c6f757b455babb678dc0e764feae279/112f7a4a-0051"
38  "-cc2b-3b61-1898181b9950"
39  ),
40  "local_url": "ws://192.168.1.39:8083",
41  "add_on_url": "ws://127.0.0.1:8083",
42  "find_url": "wss://find.z-wave.me",
43  "remote_url": "wss://87.250.250.242:8083",
44  }
45  if self.urlurl is None:
46  schema = vol.Schema(
47  {
48  vol.Required(CONF_URL): str,
49  vol.Required(CONF_TOKEN): str,
50  }
51  )
52  else:
53  schema = vol.Schema(
54  {
55  vol.Required(CONF_TOKEN): str,
56  }
57  )
58 
59  if user_input is not None:
60  if self.urlurl is None:
61  self.urlurl = user_input[CONF_URL]
62 
63  self.tokentoken = user_input[CONF_TOKEN]
64  if not self.urlurl.startswith(("ws://", "wss://")):
65  self.urlurl = f"ws://{self.url}"
66  self.urlurl = url_normalize(self.urlurl, default_scheme="ws")
67  assert self.urlurl
68  if self.uuiduuid is None:
69  self.uuiduuid = await helpers.get_uuid(self.urlurl, self.tokentoken)
70  if self.uuiduuid is not None:
71  await self.async_set_unique_idasync_set_unique_id(self.uuiduuid, raise_on_progress=False)
72  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
73  else:
74  errors["base"] = "no_valid_uuid_set"
75 
76  if not errors:
77  return self.async_create_entryasync_create_entryasync_create_entry(
78  title=self.urlurl,
79  data={CONF_URL: self.urlurl, CONF_TOKEN: self.tokentoken},
80  )
81 
82  return self.async_show_formasync_show_formasync_show_form(
83  step_id="user",
84  description_placeholders=placeholders,
85  data_schema=schema,
86  errors=errors,
87  )
88 
90  self, discovery_info: ZeroconfServiceInfo
91  ) -> ConfigFlowResult:
92  """Handle a discovered Z-Wave accessory - get url to pass into user step.
93 
94  This flow is triggered by the discovery component.
95  """
96  self.urlurl = discovery_info.host
97  self.uuiduuid = await helpers.get_uuid(self.urlurl)
98  if self.uuiduuid is None:
99  return self.async_abortasync_abortasync_abort(reason="no_valid_uuid_set")
100 
101  await self.async_set_unique_idasync_set_unique_id(self.uuiduuid)
102  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
103  return await self.async_step_userasync_step_userasync_step_user()
ConfigFlowResult async_step_zeroconf(self, ZeroconfServiceInfo discovery_info)
Definition: config_flow.py:91
ConfigFlowResult async_step_user(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:31
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_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)