Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Snapcast config flow."""
2 
3 from __future__ import annotations
4 
5 import logging
6 import socket
7 
8 import snapcast.control
9 from snapcast.control.server import CONTROL_PORT
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_HOST, CONF_PORT
14 
15 from .const import DEFAULT_TITLE, DOMAIN
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 SNAPCAST_SCHEMA = vol.Schema(
20  {
21  vol.Required(CONF_HOST): str,
22  vol.Required(CONF_PORT, default=CONTROL_PORT): int,
23  }
24 )
25 
26 
27 class SnapcastConfigFlow(ConfigFlow, domain=DOMAIN):
28  """Snapcast config flow."""
29 
30  async def async_step_user(self, user_input=None) -> ConfigFlowResult:
31  """Handle first step."""
32  errors = {}
33  if user_input:
34  self._async_abort_entries_match_async_abort_entries_match(user_input)
35  host = user_input[CONF_HOST]
36  port = user_input[CONF_PORT]
37 
38  # Attempt to create the server - make sure it's going to work
39  try:
40  client = await snapcast.control.create_server(
41  self.hass.loop, host, port, reconnect=False
42  )
43  except socket.gaierror:
44  errors["base"] = "invalid_host"
45  except OSError:
46  errors["base"] = "cannot_connect"
47  else:
48  client.stop()
49  return self.async_create_entryasync_create_entryasync_create_entry(title=DEFAULT_TITLE, data=user_input)
50  return self.async_show_formasync_show_formasync_show_form(
51  step_id="user", data_schema=SNAPCAST_SCHEMA, errors=errors
52  )
ConfigFlowResult async_step_user(self, user_input=None)
Definition: config_flow.py:30
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)
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)