Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for sky_remote."""
2 
3 import logging
4 from typing import Any
5 
6 from skyboxremote import RemoteControl, SkyBoxConnectionError
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
10 from homeassistant.const import CONF_HOST, CONF_PORT
12 
13 from .const import DEFAULT_PORT, DOMAIN, LEGACY_PORT
14 
15 DATA_SCHEMA = vol.Schema(
16  {
17  vol.Required(CONF_HOST): cv.string,
18  }
19 )
20 
21 
22 async def async_find_box_port(host: str) -> int:
23  """Find port box uses for communication."""
24  logging.debug("Attempting to find port to connect to %s on", host)
25  remote = RemoteControl(host, DEFAULT_PORT)
26  try:
27  await remote.check_connectable()
28  except SkyBoxConnectionError:
29  # Try legacy port if the default one failed
30  remote = RemoteControl(host, LEGACY_PORT)
31  await remote.check_connectable()
32  return LEGACY_PORT
33  return DEFAULT_PORT
34 
35 
36 class SkyRemoteConfigFlow(ConfigFlow, domain=DOMAIN):
37  """Handle a config flow for Sky Remote."""
38 
39  VERSION = 1
40  MINOR_VERSION = 1
41 
42  async def async_step_user(
43  self, user_input: dict[str, Any] | None = None
44  ) -> ConfigFlowResult:
45  """Handle the user step."""
46 
47  errors: dict[str, str] = {}
48  if user_input is not None:
49  logging.debug("user_input: %s", user_input)
50  self._async_abort_entries_match_async_abort_entries_match(user_input)
51  try:
52  port = await async_find_box_port(user_input[CONF_HOST])
53  except SkyBoxConnectionError:
54  logging.exception("while finding port of skybox")
55  errors["base"] = "cannot_connect"
56  else:
57  return self.async_create_entryasync_create_entryasync_create_entry(
58  title=user_input[CONF_HOST],
59  data={**user_input, CONF_PORT: port},
60  )
61 
62  return self.async_show_formasync_show_formasync_show_form(
63  step_id="user", data_schema=DATA_SCHEMA, errors=errors
64  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:44
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)
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)
_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)