Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Wemo."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import fields
6 from typing import Any, get_type_hints
7 
8 import pywemo
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigEntry, ConfigFlowResult, OptionsFlow
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers.config_entry_flow import DiscoveryFlowHandler
14 
15 from .const import DOMAIN
16 from .coordinator import Options, OptionsValidationError
17 
18 
19 async def _async_has_devices(hass: HomeAssistant) -> bool:
20  """Return if there are devices that can be discovered."""
21  return bool(await hass.async_add_executor_job(pywemo.discover_devices))
22 
23 
24 class WemoFlow(DiscoveryFlowHandler, domain=DOMAIN):
25  """Discovery flow with options for Wemo."""
26 
27  def __init__(self) -> None:
28  """Init discovery flow."""
29  super().__init__(DOMAIN, "Wemo", _async_has_devices)
30 
31  @staticmethod
32  @callback
33  def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
34  """Get the options flow for this handler."""
35  return WemoOptionsFlow()
36 
37 
39  """Options flow for the WeMo component."""
40 
41  async def async_step_init(
42  self, user_input: dict[str, Any] | None = None
43  ) -> ConfigFlowResult:
44  """Manage options for the WeMo component."""
45  errors: dict[str, str] | None = None
46  if user_input is not None:
47  try:
48  Options(**user_input)
49  except OptionsValidationError as err:
50  errors = {err.field_key: err.error_key}
51  else:
52  return self.async_create_entryasync_create_entry(title="", data=user_input)
53 
54  return self.async_show_formasync_show_form(
55  step_id="init",
56  data_schema=_schema_for_options(Options(**self.config_entryconfig_entryconfig_entry.options)),
57  errors=errors,
58  )
59 
60 
61 def _schema_for_options(options: Options) -> vol.Schema:
62  """Return the Voluptuous schema for the Options instance.
63 
64  All values are optional. The default value is set to the current value and
65  the type hint is set to the value of the field type annotation.
66  """
67  return vol.Schema(
68  {
69  vol.Optional(
70  field.name, default=getattr(options, field.name)
71  ): get_type_hints(options)[field.name]
72  for field in fields(options)
73  }
74  )
OptionsFlow async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:33
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:43
None config_entry(self, ConfigEntry value)
_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 _async_has_devices(HomeAssistant hass)
Definition: config_flow.py:19
vol.Schema _schema_for_options(Options options)
Definition: config_flow.py:61