1 """Config flow for mopeka integration."""
3 from __future__
import annotations
8 from mopeka_iot_ble
import MopekaIOTBluetoothDeviceData
as DeviceData
9 import voluptuous
as vol
11 from homeassistant
import config_entries
13 BluetoothServiceInfoBleak,
14 async_discovered_service_info,
20 from .const
import CONF_MEDIUM_TYPE, DEFAULT_MEDIUM_TYPE, DOMAIN, MediumType
24 """Format the medium type for human reading."""
25 return medium_type.name.replace(
"_",
" ").title()
28 MEDIUM_TYPES_BY_NAME = {
34 """Return the base schema with formatted medium types."""
38 CONF_MEDIUM_TYPE, default=medium_type
or DEFAULT_MEDIUM_TYPE
39 ): vol.In(MEDIUM_TYPES_BY_NAME)
45 """Handle a config flow for mopeka."""
50 """Initialize the config flow."""
51 self.
_discovery_info_discovery_info: BluetoothServiceInfoBleak |
None =
None
53 self._discovered_devices: dict[str, str] = {}
58 config_entry: config_entries.ConfigEntry,
59 ) -> MopekaOptionsFlow:
60 """Return the options flow for this handler."""
64 self, discovery_info: BluetoothServiceInfoBleak
65 ) -> ConfigFlowResult:
66 """Handle the bluetooth discovery step."""
70 if not device.supported(discovery_info):
77 self, user_input: dict[str, Any] |
None =
None
78 ) -> ConfigFlowResult:
79 """Confirm discovery and select medium type."""
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
88 title=self._discovered_devices[discovery_info.address],
89 data={CONF_MEDIUM_TYPE: user_input[CONF_MEDIUM_TYPE]},
93 placeholders = {
"name": title}
94 self.context[
"title_placeholders"] = placeholders
96 step_id=
"bluetooth_confirm",
97 description_placeholders=placeholders,
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]
110 title=self._discovered_devices[address],
111 data={CONF_MEDIUM_TYPE: user_input[CONF_MEDIUM_TYPE]},
116 address = discovery_info.address
117 if address
in current_addresses
or address
in self._discovered_devices:
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
125 if not self._discovered_devices:
130 data_schema=vol.Schema(
132 vol.Required(CONF_ADDRESS): vol.In(self._discovered_devices),
140 """Handle options for the Mopeka component."""
143 self, user_input: dict[str, Any] |
None =
None
144 ) -> ConfigFlowResult:
145 """Handle options flow."""
146 if user_input
is not None:
149 CONF_MEDIUM_TYPE: user_input[CONF_MEDIUM_TYPE],
151 self.hass.config_entries.async_update_entry(
ConfigFlowResult async_step_bluetooth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
MopekaOptionsFlow async_get_options_flow(config_entries.ConfigEntry config_entry)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
None _set_confirm_only(self)
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)
ConfigEntry config_entry(self)
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)
str format_medium_type(Enum medium_type)
vol.Schema async_generate_schema(str|None medium_type=None)