Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Bose SoundTouch integration."""
2 
3 from typing import Any
4 
5 from libsoundtouch import soundtouch_device
6 from requests import RequestException
7 import voluptuous as vol
8 
9 from homeassistant.components.zeroconf import ZeroconfServiceInfo
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_HOST
12 from homeassistant.helpers import config_validation as cv
13 
14 from .const import DOMAIN
15 
16 
17 class SoundtouchConfigFlow(ConfigFlow, domain=DOMAIN):
18  """Handle a config flow for Bose SoundTouch."""
19 
20  VERSION = 1
21 
22  def __init__(self) -> None:
23  """Initialize a new SoundTouch config flow."""
24  self.hosthost: str | None = None
25  self.namename: str | None = None
26 
27  async def async_step_user(
28  self, user_input: dict[str, Any] | None = None
29  ) -> ConfigFlowResult:
30  """Handle a flow initiated by the user."""
31  errors = {}
32 
33  if user_input is not None:
34  self.hosthost = user_input[CONF_HOST]
35 
36  try:
37  await self._async_get_device_id_async_get_device_id(raise_on_progress=False)
38  except RequestException:
39  errors["base"] = "cannot_connect"
40  else:
41  return await self._async_create_soundtouch_entry_async_create_soundtouch_entry()
42 
43  return self.async_show_formasync_show_formasync_show_form(
44  step_id="user",
45  last_step=True,
46  data_schema=vol.Schema(
47  {
48  vol.Required(CONF_HOST): cv.string,
49  }
50  ),
51  errors=errors,
52  )
53 
55  self, discovery_info: ZeroconfServiceInfo
56  ) -> ConfigFlowResult:
57  """Handle a flow initiated by a zeroconf discovery."""
58  self.hosthost = discovery_info.host
59 
60  try:
61  await self._async_get_device_id_async_get_device_id()
62  except RequestException:
63  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
64 
65  if self.namename:
66  # If we have a name, use it as flow title
67  self.context["title_placeholders"] = {"name": self.namename}
68  return await self.async_step_zeroconf_confirmasync_step_zeroconf_confirm()
69 
71  self, user_input: dict[str, Any] | None = None
72  ) -> ConfigFlowResult:
73  """Handle user-confirmation of discovered node."""
74  if user_input is not None:
75  return await self._async_create_soundtouch_entry_async_create_soundtouch_entry()
76  return self.async_show_formasync_show_formasync_show_form(
77  step_id="zeroconf_confirm",
78  last_step=True,
79  description_placeholders={"name": self.namename or "?"},
80  )
81 
82  async def _async_get_device_id(self, raise_on_progress: bool = True) -> None:
83  """Get device ID from SoundTouch device."""
84  device = await self.hass.async_add_executor_job(soundtouch_device, self.hosthost)
85 
86  # Check if already configured
87  await self.async_set_unique_idasync_set_unique_id(
88  device.config.device_id, raise_on_progress=raise_on_progress
89  )
90  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates={CONF_HOST: self.hosthost})
91 
92  self.namename = device.config.name
93 
94  async def _async_create_soundtouch_entry(self) -> ConfigFlowResult:
95  """Finish config flow and create a SoundTouch config entry."""
96  return self.async_create_entryasync_create_entryasync_create_entry(
97  title=self.namename or "SoundTouch",
98  data={
99  CONF_HOST: self.hosthost,
100  },
101  )
ConfigFlowResult async_step_zeroconf_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:72
None _async_get_device_id(self, bool raise_on_progress=True)
Definition: config_flow.py:82
ConfigFlowResult async_step_zeroconf(self, ZeroconfServiceInfo discovery_info)
Definition: config_flow.py:56
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:29
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)