Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Elgato Light integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from elgato import Elgato, ElgatoError
8 import voluptuous as vol
9 
10 from homeassistant.components import onboarding, zeroconf
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_HOST, CONF_MAC, CONF_PORT
13 from homeassistant.core import callback
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import DOMAIN
17 
18 
19 class ElgatoFlowHandler(ConfigFlow, domain=DOMAIN):
20  """Handle a Elgato Light config flow."""
21 
22  VERSION = 1
23 
24  host: str
25  port: int
26  serial_number: str
27  mac: str | None = None
28 
29  async def async_step_user(
30  self, user_input: dict[str, Any] | None = None
31  ) -> ConfigFlowResult:
32  """Handle a flow initiated by the user."""
33  if user_input is None:
34  return self._async_show_setup_form()
35 
36  self.hosthost = user_input[CONF_HOST]
37  self.portport = user_input[CONF_PORT]
38 
39  try:
40  await self._get_elgato_serial_number(raise_on_progress=False)
41  except ElgatoError:
42  return self._async_show_setup_form({"base": "cannot_connect"})
43 
44  return self._async_create_entry()
45 
46  async def async_step_zeroconf(
47  self, discovery_info: zeroconf.ZeroconfServiceInfo
48  ) -> ConfigFlowResult:
49  """Handle zeroconf discovery."""
50  self.hosthost = discovery_info.host
51  self.macmac = discovery_info.properties.get("id")
52  self.portport = discovery_info.port or 9123
53 
54  try:
55  await self._get_elgato_serial_number()
56  except ElgatoError:
57  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
58 
59  if not onboarding.async_is_onboarded(self.hass):
60  return self._async_create_entry()
61 
62  self._set_confirm_only_set_confirm_only()
63  return self.async_show_formasync_show_formasync_show_form(
64  step_id="zeroconf_confirm",
65  description_placeholders={"serial_number": self.serial_numberserial_number},
66  )
67 
68  async def async_step_zeroconf_confirm(
69  self, _: dict[str, Any] | None = None
70  ) -> ConfigFlowResult:
71  """Handle a flow initiated by zeroconf."""
72  return self._async_create_entry()
73 
74  @callback
75  def _async_show_setup_form(
76  self, errors: dict[str, str] | None = None
77  ) -> ConfigFlowResult:
78  """Show the setup form to the user."""
79  return self.async_show_formasync_show_formasync_show_form(
80  step_id="user",
81  data_schema=vol.Schema(
82  {
83  vol.Required(CONF_HOST): str,
84  vol.Optional(CONF_PORT, default=9123): int,
85  }
86  ),
87  errors=errors or {},
88  )
89 
90  @callback
91  def _async_create_entry(self) -> ConfigFlowResult:
92  return self.async_create_entryasync_create_entryasync_create_entry(
93  title=self.serial_numberserial_number,
94  data={
95  CONF_HOST: self.hosthost,
96  CONF_PORT: self.portport,
97  CONF_MAC: self.macmac,
98  },
99  )
100 
101  async def _get_elgato_serial_number(self, raise_on_progress: bool = True) -> None:
102  """Get device information from an Elgato Light device."""
103  session = async_get_clientsession(self.hass)
104  elgato = Elgato(
105  host=self.hosthost,
106  port=self.portport,
107  session=session,
108  )
109  info = await elgato.info()
110 
111  # Check if already configured
112  await self.async_set_unique_idasync_set_unique_id(
113  info.serial_number, raise_on_progress=raise_on_progress
114  )
115  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
116  updates={CONF_HOST: self.hosthost, CONF_PORT: self.portport, CONF_MAC: self.macmac}
117  )
118 
119  self.serial_numberserial_number = info.serial_number
ConfigFlowResult async_step_user(self, dict[str, Any]|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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
ConfigFlowResult async_step_zeroconf(self, ZeroconfServiceInfo discovery_info)
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)
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)