Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Support for Devialet Phantom speakers."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from devialet.devialet_api import DevialetApi
9 import voluptuous as vol
10 
11 from homeassistant.components import zeroconf
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_HOST, CONF_NAME
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import DOMAIN
17 
18 LOGGER = logging.getLogger(__package__)
19 
20 
21 class DevialetFlowHandler(ConfigFlow, domain=DOMAIN):
22  """Config flow for Devialet."""
23 
24  VERSION = 1
25 
26  _host: str
27  _model: str
28  _name: str
29  _serial: str
30 
31  def __init__(self) -> None:
32  """Initialize flow."""
33  self._errors: dict[str, str] = {}
34 
35  async def async_validate_input(self) -> ConfigFlowResult | None:
36  """Validate the input using the Devialet API."""
37 
38  self._errors.clear()
39  session = async_get_clientsession(self.hass)
40  client = DevialetApi(self._host_host, session)
41 
42  if not await client.async_update() or client.serial is None:
43  self._errors["base"] = "cannot_connect"
44  LOGGER.error("Cannot connect")
45  return None
46 
47  await self.async_set_unique_idasync_set_unique_id(client.serial)
48  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
49 
50  return self.async_create_entryasync_create_entryasync_create_entry(
51  title=client.device_name,
52  data={CONF_HOST: self._host_host, CONF_NAME: client.device_name},
53  )
54 
55  async def async_step_user(
56  self, user_input: dict[str, Any] | None = None
57  ) -> ConfigFlowResult:
58  """Handle a flow initialized by the user or zeroconf."""
59 
60  if user_input is not None:
61  self._host_host = user_input[CONF_HOST]
62  result = await self.async_validate_inputasync_validate_input()
63  if result is not None:
64  return result
65 
66  return self.async_show_formasync_show_formasync_show_form(
67  step_id="user",
68  data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
69  errors=self._errors,
70  )
71 
73  self, discovery_info: zeroconf.ZeroconfServiceInfo
74  ) -> ConfigFlowResult:
75  """Handle a flow initialized by zeroconf discovery."""
76  LOGGER.debug("Devialet device found via ZEROCONF: %s", discovery_info)
77 
78  self._host_host = discovery_info.host
79  self._name_name = discovery_info.name.split(".", 1)[0]
80  self._model_model = discovery_info.properties["model"]
81  self._serial_serial = discovery_info.properties["serialNumber"]
82 
83  await self.async_set_unique_idasync_set_unique_id(self._serial_serial)
84  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
85 
86  self.context["title_placeholders"] = {"title": self._name_name}
87  return await self.async_step_confirmasync_step_confirm()
88 
89  async def async_step_confirm(
90  self, user_input: dict[str, Any] | None = None
91  ) -> ConfigFlowResult:
92  """Handle user-confirmation of discovered node."""
93  title = f"{self._name} ({self._model})"
94 
95  if user_input is not None:
96  result = await self.async_validate_inputasync_validate_input()
97  if result is not None:
98  return result
99 
100  return self.async_show_formasync_show_formasync_show_form(
101  step_id="confirm",
102  description_placeholders={"device": self._model_model, "title": title},
103  errors=self._errors,
104  last_step=True,
105  )
ConfigFlowResult async_step_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:91
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:57
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
Definition: config_flow.py:74
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)
_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)