Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure LinkPlay component."""
2 
3 import logging
4 from typing import Any
5 
6 from aiohttp import ClientSession
7 from linkplay.bridge import LinkPlayBridge
8 from linkplay.discovery import linkplay_factory_httpapi_bridge
9 from linkplay.exceptions import LinkPlayRequestException
10 import voluptuous as vol
11 
12 from homeassistant.components import zeroconf
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_HOST, CONF_MODEL
15 
16 from .const import DOMAIN
17 from .utils import async_get_client_session
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
22 class LinkPlayConfigFlow(ConfigFlow, domain=DOMAIN):
23  """LinkPlay config flow."""
24 
25  def __init__(self) -> None:
26  """Initialize the LinkPlay config flow."""
27  self.data: dict[str, Any] = {}
28 
30  self, discovery_info: zeroconf.ZeroconfServiceInfo
31  ) -> ConfigFlowResult:
32  """Handle Zeroconf discovery."""
33 
34  session: ClientSession = await async_get_client_session(self.hass)
35  bridge: LinkPlayBridge | None = None
36 
37  try:
38  bridge = await linkplay_factory_httpapi_bridge(discovery_info.host, session)
39  except LinkPlayRequestException:
40  _LOGGER.exception(
41  "Failed to connect to LinkPlay device at %s", discovery_info.host
42  )
43  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
44 
45  self.data[CONF_HOST] = discovery_info.host
46  self.data[CONF_MODEL] = bridge.device.name
47 
48  await self.async_set_unique_idasync_set_unique_id(bridge.device.uuid)
49  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates={CONF_HOST: discovery_info.host})
50 
51  self.context["title_placeholders"] = {
52  "name": self.data[CONF_MODEL],
53  }
54  return await self.async_step_discovery_confirmasync_step_discovery_confirm()
55 
57  self, user_input: dict[str, Any] | None = None
58  ) -> ConfigFlowResult:
59  """Confirm discovery."""
60  if user_input is not None:
61  return self.async_create_entryasync_create_entryasync_create_entry(
62  title=self.data[CONF_MODEL],
63  data={CONF_HOST: self.data[CONF_HOST]},
64  )
65 
66  self._set_confirm_only_set_confirm_only()
67  return self.async_show_formasync_show_formasync_show_form(
68  step_id="discovery_confirm",
69  description_placeholders={
70  "name": self.data[CONF_MODEL],
71  },
72  )
73 
74  async def async_step_user(
75  self, user_input: dict[str, Any] | None = None
76  ) -> ConfigFlowResult:
77  """Handle a flow initialized by the user."""
78  errors: dict[str, str] = {}
79  if user_input:
80  session: ClientSession = await async_get_client_session(self.hass)
81  bridge: LinkPlayBridge | None = None
82 
83  try:
84  bridge = await linkplay_factory_httpapi_bridge(
85  user_input[CONF_HOST], session
86  )
87  except LinkPlayRequestException:
88  _LOGGER.exception(
89  "Failed to connect to LinkPlay device at %s", user_input[CONF_HOST]
90  )
91  errors["base"] = "cannot_connect"
92 
93  if bridge is not None:
94  self.data[CONF_HOST] = user_input[CONF_HOST]
95  self.data[CONF_MODEL] = bridge.device.name
96 
97  await self.async_set_unique_idasync_set_unique_id(
98  bridge.device.uuid, raise_on_progress=False
99  )
100  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
101  updates={CONF_HOST: self.data[CONF_HOST]}
102  )
103 
104  return self.async_create_entryasync_create_entryasync_create_entry(
105  title=self.data[CONF_MODEL],
106  data={CONF_HOST: self.data[CONF_HOST]},
107  )
108 
109  return self.async_show_formasync_show_formasync_show_form(
110  step_id="user",
111  data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
112  errors=errors,
113  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:76
ConfigFlowResult async_step_discovery_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:58
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
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_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)
ClientSession async_get_client_session(HomeAssistant hass)
Definition: utils.py:80