Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Music Player Daemon config flow."""
2 
3 from asyncio import timeout
4 from contextlib import suppress
5 from socket import gaierror
6 from typing import Any
7 
8 import mpd
9 from mpd.asyncio import MPDClient
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_PORT
14 
15 from .const import DOMAIN, LOGGER
16 
17 SCHEMA = vol.Schema(
18  {
19  vol.Required(CONF_HOST): str,
20  vol.Optional(CONF_PASSWORD): str,
21  vol.Optional(CONF_PORT, default=6600): int,
22  }
23 )
24 
25 
26 class MPDConfigFlow(ConfigFlow, domain=DOMAIN):
27  """Music Player Daemon config flow."""
28 
29  async def async_step_user(
30  self, user_input: dict[str, Any] | None = None
31  ) -> ConfigFlowResult:
32  """Handle a flow initiated by the user."""
33  errors = {}
34  if user_input:
35  self._async_abort_entries_match_async_abort_entries_match(
36  {CONF_HOST: user_input[CONF_HOST], CONF_PORT: user_input[CONF_PORT]}
37  )
38  client = MPDClient()
39  client.timeout = 30
40  client.idletimeout = 10
41  try:
42  async with timeout(35):
43  await client.connect(user_input[CONF_HOST], user_input[CONF_PORT])
44  if CONF_PASSWORD in user_input:
45  await client.password(user_input[CONF_PASSWORD])
46  with suppress(mpd.ConnectionError):
47  client.disconnect()
48  except (
49  TimeoutError,
50  gaierror,
51  mpd.ConnectionError,
52  OSError,
53  ):
54  errors["base"] = "cannot_connect"
55  except Exception: # noqa: BLE001
56  LOGGER.exception("Unknown exception")
57  errors["base"] = "unknown"
58  else:
59  return self.async_create_entryasync_create_entryasync_create_entry(
60  title="Music Player Daemon",
61  data=user_input,
62  )
63 
64  return self.async_show_formasync_show_formasync_show_form(
65  step_id="user",
66  data_schema=SCHEMA,
67  errors=errors,
68  )
69 
70  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
71  """Attempt to import the existing configuration."""
72  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: import_data[CONF_HOST]})
73  client = MPDClient()
74  client.timeout = 30
75  client.idletimeout = 10
76  try:
77  async with timeout(35):
78  await client.connect(import_data[CONF_HOST], import_data[CONF_PORT])
79  if CONF_PASSWORD in import_data:
80  await client.password(import_data[CONF_PASSWORD])
81  with suppress(mpd.ConnectionError):
82  client.disconnect()
83  except (
84  TimeoutError,
85  gaierror,
86  mpd.ConnectionError,
87  OSError,
88  ):
89  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
90  except Exception: # noqa: BLE001
91  LOGGER.exception("Unknown exception")
92  return self.async_abortasync_abortasync_abort(reason="unknown")
93 
94  return self.async_create_entryasync_create_entryasync_create_entry(
95  title=import_data.get(CONF_NAME, "Music Player Daemon"),
96  data={
97  CONF_HOST: import_data[CONF_HOST],
98  CONF_PORT: import_data[CONF_PORT],
99  CONF_PASSWORD: import_data.get(CONF_PASSWORD),
100  },
101  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:31
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)
config_entries.ConfigFlowResult async_step_import(self, dict[str, Any]|None _)