Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flows for the ENOcean integration."""
2 
3 from typing import Any
4 
5 import voluptuous as vol
6 
7 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
8 from homeassistant.const import CONF_DEVICE
9 
10 from . import dongle
11 from .const import DOMAIN, ERROR_INVALID_DONGLE_PATH, LOGGER
12 
13 
14 class EnOceanFlowHandler(ConfigFlow, domain=DOMAIN):
15  """Handle the enOcean config flows."""
16 
17  VERSION = 1
18  MANUAL_PATH_VALUE = "Custom path"
19 
20  def __init__(self) -> None:
21  """Initialize the EnOcean config flow."""
22  self.dongle_pathdongle_path = None
23  self.discovery_infodiscovery_info = None
24 
25  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
26  """Import a yaml configuration."""
27 
28  if not await self.validate_enocean_confvalidate_enocean_conf(import_data):
29  LOGGER.warning(
30  "Cannot import yaml configuration: %s is not a valid dongle path",
31  import_data[CONF_DEVICE],
32  )
33  return self.async_abortasync_abortasync_abort(reason="invalid_dongle_path")
34 
35  return self.create_enocean_entrycreate_enocean_entry(import_data)
36 
37  async def async_step_user(
38  self, user_input: dict[str, Any] | None = None
39  ) -> ConfigFlowResult:
40  """Handle an EnOcean config flow start."""
41  return await self.async_step_detectasync_step_detect()
42 
43  async def async_step_detect(
44  self, user_input: dict[str, Any] | None = None
45  ) -> ConfigFlowResult:
46  """Propose a list of detected dongles."""
47  errors = {}
48  if user_input is not None:
49  if user_input[CONF_DEVICE] == self.MANUAL_PATH_VALUEMANUAL_PATH_VALUE:
50  return await self.async_step_manualasync_step_manual()
51  if await self.validate_enocean_confvalidate_enocean_conf(user_input):
52  return self.create_enocean_entrycreate_enocean_entry(user_input)
53  errors = {CONF_DEVICE: ERROR_INVALID_DONGLE_PATH}
54 
55  bridges = await self.hass.async_add_executor_job(dongle.detect)
56  if len(bridges) == 0:
57  return await self.async_step_manualasync_step_manual(user_input)
58 
59  bridges.append(self.MANUAL_PATH_VALUEMANUAL_PATH_VALUE)
60  return self.async_show_formasync_show_formasync_show_form(
61  step_id="detect",
62  data_schema=vol.Schema({vol.Required(CONF_DEVICE): vol.In(bridges)}),
63  errors=errors,
64  )
65 
66  async def async_step_manual(
67  self, user_input: dict[str, Any] | None = None
68  ) -> ConfigFlowResult:
69  """Request manual USB dongle path."""
70  default_value = None
71  errors = {}
72  if user_input is not None:
73  if await self.validate_enocean_confvalidate_enocean_conf(user_input):
74  return self.create_enocean_entrycreate_enocean_entry(user_input)
75  default_value = user_input[CONF_DEVICE]
76  errors = {CONF_DEVICE: ERROR_INVALID_DONGLE_PATH}
77 
78  return self.async_show_formasync_show_formasync_show_form(
79  step_id="manual",
80  data_schema=vol.Schema(
81  {vol.Required(CONF_DEVICE, default=default_value): str}
82  ),
83  errors=errors,
84  )
85 
86  async def validate_enocean_conf(self, user_input) -> bool:
87  """Return True if the user_input contains a valid dongle path."""
88  dongle_path = user_input[CONF_DEVICE]
89  return await self.hass.async_add_executor_job(dongle.validate_path, dongle_path)
90 
91  def create_enocean_entry(self, user_input):
92  """Create an entry for the provided configuration."""
93  return self.async_create_entryasync_create_entryasync_create_entry(title="EnOcean", data=user_input)
ConfigFlowResult async_step_manual(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:68
ConfigFlowResult async_step_detect(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:45
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:39
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
Definition: config_flow.py:25
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)