Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure russound_rio component."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 import logging
7 from typing import Any
8 
9 from aiorussound import RussoundClient, RussoundTcpConnectionHandler
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_HOST, CONF_PORT
14 from homeassistant.helpers import config_validation as cv
15 
16 from .const import CONNECT_TIMEOUT, DOMAIN, RUSSOUND_RIO_EXCEPTIONS
17 
18 DATA_SCHEMA = vol.Schema(
19  {
20  vol.Required(CONF_HOST): cv.string,
21  vol.Optional(CONF_PORT, default=9621): cv.port,
22  }
23 )
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
28 class FlowHandler(ConfigFlow, domain=DOMAIN):
29  """Russound RIO configuration flow."""
30 
31  VERSION = 1
32 
33  async def async_step_user(
34  self, user_input: dict[str, Any] | None = None
35  ) -> ConfigFlowResult:
36  """Handle a flow initialized by the user."""
37  errors: dict[str, str] = {}
38  if user_input is not None:
39  host = user_input[CONF_HOST]
40  port = user_input[CONF_PORT]
41 
42  client = RussoundClient(RussoundTcpConnectionHandler(host, port))
43  try:
44  async with asyncio.timeout(CONNECT_TIMEOUT):
45  await client.connect()
46  controller = client.controllers[1]
47  await client.disconnect()
48  except RUSSOUND_RIO_EXCEPTIONS:
49  _LOGGER.exception("Could not connect to Russound RIO")
50  errors["base"] = "cannot_connect"
51  else:
52  await self.async_set_unique_idasync_set_unique_id(controller.mac_address)
53  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
54  data = {CONF_HOST: host, CONF_PORT: port}
55  return self.async_create_entryasync_create_entryasync_create_entry(
56  title=controller.controller_type, data=data
57  )
58 
59  return self.async_show_formasync_show_formasync_show_form(
60  step_id="user", data_schema=DATA_SCHEMA, errors=errors
61  )
62 
63  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
64  """Attempt to import the existing configuration."""
65  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: import_data[CONF_HOST]})
66  host = import_data[CONF_HOST]
67  port = import_data.get(CONF_PORT, 9621)
68 
69  # Connection logic is repeated here since this method will be removed in future releases
70  client = RussoundClient(RussoundTcpConnectionHandler(host, port))
71  try:
72  async with asyncio.timeout(CONNECT_TIMEOUT):
73  await client.connect()
74  controller = client.controllers[1]
75  await client.disconnect()
76  except RUSSOUND_RIO_EXCEPTIONS:
77  _LOGGER.exception("Could not connect to Russound RIO")
78  return self.async_abortasync_abortasync_abort(
79  reason="cannot_connect", description_placeholders={}
80  )
81  else:
82  await self.async_set_unique_idasync_set_unique_id(controller.mac_address)
83  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
84  data = {CONF_HOST: host, CONF_PORT: port}
85  return self.async_create_entryasync_create_entryasync_create_entry(title=controller.controller_type, data=data)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:35
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
Definition: config_flow.py:63
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)