Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for mopeka integration."""
2 
3 from __future__ import annotations
4 
5 from enum import Enum
6 from typing import Any
7 
8 from mopeka_iot_ble import MopekaIOTBluetoothDeviceData as DeviceData
9 import voluptuous as vol
10 
11 from homeassistant import config_entries
13  BluetoothServiceInfoBleak,
14  async_discovered_service_info,
15 )
16 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
17 from homeassistant.const import CONF_ADDRESS
18 from homeassistant.core import callback
19 
20 from .const import CONF_MEDIUM_TYPE, DEFAULT_MEDIUM_TYPE, DOMAIN, MediumType
21 
22 
23 def format_medium_type(medium_type: Enum) -> str:
24  """Format the medium type for human reading."""
25  return medium_type.name.replace("_", " ").title()
26 
27 
28 MEDIUM_TYPES_BY_NAME = {
29  medium.value: format_medium_type(medium) for medium in MediumType
30 }
31 
32 
33 def async_generate_schema(medium_type: str | None = None) -> vol.Schema:
34  """Return the base schema with formatted medium types."""
35  return vol.Schema(
36  {
37  vol.Required(
38  CONF_MEDIUM_TYPE, default=medium_type or DEFAULT_MEDIUM_TYPE
39  ): vol.In(MEDIUM_TYPES_BY_NAME)
40  }
41  )
42 
43 
44 class MopekaConfigFlow(ConfigFlow, domain=DOMAIN):
45  """Handle a config flow for mopeka."""
46 
47  VERSION = 1
48 
49  def __init__(self) -> None:
50  """Initialize the config flow."""
51  self._discovery_info_discovery_info: BluetoothServiceInfoBleak | None = None
52  self._discovered_device_discovered_device: DeviceData | None = None
53  self._discovered_devices: dict[str, str] = {}
54 
55  @callback
56  @staticmethod
58  config_entry: config_entries.ConfigEntry,
59  ) -> MopekaOptionsFlow:
60  """Return the options flow for this handler."""
61  return MopekaOptionsFlow()
62 
64  self, discovery_info: BluetoothServiceInfoBleak
65  ) -> ConfigFlowResult:
66  """Handle the bluetooth discovery step."""
67  await self.async_set_unique_idasync_set_unique_id(discovery_info.address)
68  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
69  device = DeviceData()
70  if not device.supported(discovery_info):
71  return self.async_abortasync_abortasync_abort(reason="not_supported")
72  self._discovery_info_discovery_info = discovery_info
73  self._discovered_device_discovered_device = device
74  return await self.async_step_bluetooth_confirmasync_step_bluetooth_confirm()
75 
77  self, user_input: dict[str, Any] | None = None
78  ) -> ConfigFlowResult:
79  """Confirm discovery and select medium type."""
80  assert self._discovered_device_discovered_device is not None
81  device = self._discovered_device_discovered_device
82  assert self._discovery_info_discovery_info is not None
83  discovery_info = self._discovery_info_discovery_info
84  title = device.title or device.get_device_name() or discovery_info.name
85  if user_input is not None:
86  self._discovered_devices[discovery_info.address] = title
87  return self.async_create_entryasync_create_entryasync_create_entry(
88  title=self._discovered_devices[discovery_info.address],
89  data={CONF_MEDIUM_TYPE: user_input[CONF_MEDIUM_TYPE]},
90  )
91 
92  self._set_confirm_only_set_confirm_only()
93  placeholders = {"name": title}
94  self.context["title_placeholders"] = placeholders
95  return self.async_show_formasync_show_formasync_show_form(
96  step_id="bluetooth_confirm",
97  description_placeholders=placeholders,
98  data_schema=async_generate_schema(),
99  )
100 
101  async def async_step_user(
102  self, user_input: dict[str, Any] | None = None
103  ) -> ConfigFlowResult:
104  """Handle the user step to pick discovered device and select medium type."""
105  if user_input is not None:
106  address = user_input[CONF_ADDRESS]
107  await self.async_set_unique_idasync_set_unique_id(address, raise_on_progress=False)
108  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
109  return self.async_create_entryasync_create_entryasync_create_entry(
110  title=self._discovered_devices[address],
111  data={CONF_MEDIUM_TYPE: user_input[CONF_MEDIUM_TYPE]},
112  )
113 
114  current_addresses = self._async_current_ids_async_current_ids()
115  for discovery_info in async_discovered_service_info(self.hass, False):
116  address = discovery_info.address
117  if address in current_addresses or address in self._discovered_devices:
118  continue
119  device = DeviceData()
120  if device.supported(discovery_info):
121  self._discovered_devices[address] = (
122  device.title or device.get_device_name() or discovery_info.name
123  )
124 
125  if not self._discovered_devices:
126  return self.async_abortasync_abortasync_abort(reason="no_devices_found")
127 
128  return self.async_show_formasync_show_formasync_show_form(
129  step_id="user",
130  data_schema=vol.Schema(
131  {
132  vol.Required(CONF_ADDRESS): vol.In(self._discovered_devices),
133  **async_generate_schema().schema,
134  }
135  ),
136  )
137 
138 
140  """Handle options for the Mopeka component."""
141 
142  async def async_step_init(
143  self, user_input: dict[str, Any] | None = None
144  ) -> ConfigFlowResult:
145  """Handle options flow."""
146  if user_input is not None:
147  new_data = {
148  **self.config_entryconfig_entryconfig_entry.data,
149  CONF_MEDIUM_TYPE: user_input[CONF_MEDIUM_TYPE],
150  }
151  self.hass.config_entries.async_update_entry(
152  self.config_entryconfig_entryconfig_entry, data=new_data
153  )
154  await self.hass.config_entries.async_reload(self.config_entryconfig_entryconfig_entry.entry_id)
155  return self.async_create_entryasync_create_entry(title="", data={})
156 
157  return self.async_show_formasync_show_form(
158  step_id="init",
159  data_schema=async_generate_schema(
160  self.config_entryconfig_entryconfig_entry.data.get(CONF_MEDIUM_TYPE)
161  ),
162  )
ConfigFlowResult async_step_bluetooth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:78
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:103
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
Definition: config_flow.py:65
MopekaOptionsFlow async_get_options_flow(config_entries.ConfigEntry config_entry)
Definition: config_flow.py:59
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:144
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)
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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
Iterable[BluetoothServiceInfoBleak] async_discovered_service_info(HomeAssistant hass, bool connectable=True)
Definition: api.py:72
vol.Schema async_generate_schema(str|None medium_type=None)
Definition: config_flow.py:33