Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """ConfigFlow for Energenie-Power-Sockets devices."""
2 
3 from typing import Any
4 
5 from pyegps import get_device, search_for_devices
6 from pyegps.exceptions import MissingLibrary, UsbError
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
10 
11 from .const import CONF_DEVICE_API_ID, DOMAIN, LOGGER
12 
13 
14 class EGPSConfigFlow(ConfigFlow, domain=DOMAIN):
15  """Handle the config flow for EGPS devices."""
16 
17  async def async_step_user(
18  self, user_input: dict[str, Any] | None = None
19  ) -> ConfigFlowResult:
20  """Initiate user flow."""
21 
22  if user_input is not None:
23  dev_id = user_input[CONF_DEVICE_API_ID]
24  dev = await self.hass.async_add_executor_job(get_device, dev_id)
25  if dev is not None:
26  await self.async_set_unique_idasync_set_unique_id(dev.device_id)
27  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
28 
29  return self.async_create_entryasync_create_entryasync_create_entry(
30  title=dev_id,
31  data={CONF_DEVICE_API_ID: dev_id},
32  )
33  return self.async_abortasync_abortasync_abort(reason="device_not_found")
34 
35  currently_configured = self._async_current_ids_async_current_ids(include_ignore=True)
36  try:
37  found_devices = await self.hass.async_add_executor_job(search_for_devices)
38  except (MissingLibrary, UsbError):
39  LOGGER.exception("Unable to access USB devices")
40  return self.async_abortasync_abortasync_abort(reason="usb_error")
41 
42  devices = [
43  d
44  for d in found_devices
45  if d.get_device_type() == "PowerStrip"
46  and d.device_id not in currently_configured
47  ]
48  LOGGER.debug("Found %d devices", len(devices))
49  if len(devices) > 0:
50  options = {d.device_id: f"{d.name} ({d.device_id})" for d in devices}
51  data_schema = {CONF_DEVICE_API_ID: vol.In(options)}
52  else:
53  return self.async_abortasync_abortasync_abort(reason="no_device")
54 
55  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=vol.Schema(data_schema))
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:19
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
set[str|None] _async_current_ids(self, bool include_ignore=True)
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)
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)