Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Adds config flow for Dune HD integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from pdunehd import DuneHDPlayer
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_HOST
12 from homeassistant.exceptions import HomeAssistantError
13 from homeassistant.util.network import is_host_valid
14 
15 from .const import DOMAIN
16 
17 
18 class DuneHDConfigFlow(ConfigFlow, domain=DOMAIN):
19  """Handle a config flow for Dune HD integration."""
20 
21  VERSION = 1
22 
23  async def init_device(self, host: str) -> None:
24  """Initialize Dune HD player."""
25  player = DuneHDPlayer(host)
26  state = await self.hass.async_add_executor_job(player.update_state)
27  if not state:
28  raise CannotConnect
29 
30  async def async_step_user(
31  self, user_input: dict[str, Any] | None = None
32  ) -> ConfigFlowResult:
33  """Handle the initial step."""
34  errors = {}
35 
36  if user_input is not None:
37  if is_host_valid(user_input[CONF_HOST]):
38  host: str = user_input[CONF_HOST]
39 
40  try:
41  if self.host_already_configuredhost_already_configured(host):
42  raise AlreadyConfigured # noqa: TRY301
43  await self.init_deviceinit_device(host)
44  except CannotConnect:
45  errors[CONF_HOST] = "cannot_connect"
46  except AlreadyConfigured:
47  errors[CONF_HOST] = "already_configured"
48  else:
49  return self.async_create_entryasync_create_entryasync_create_entry(title=host, data=user_input)
50  else:
51  errors[CONF_HOST] = "invalid_host"
52 
53  return self.async_show_formasync_show_formasync_show_form(
54  step_id="user",
55  data_schema=vol.Schema({vol.Required(CONF_HOST, default=""): str}),
56  errors=errors,
57  )
58 
59  def host_already_configured(self, host: str) -> bool:
60  """See if we already have a dunehd entry matching user input configured."""
61  existing_hosts = {
62  entry.data[CONF_HOST] for entry in self._async_current_entries_async_current_entries()
63  }
64  return host in existing_hosts
65 
66 
68  """Error to indicate we cannot connect."""
69 
70 
72  """Error to indicate device is already configured."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:32
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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=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)
bool is_host_valid(str host)
Definition: network.py:93