Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Mopeka integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from mopeka_iot_ble import MediumType, MopekaIOTBluetoothDeviceData
8 
9 from homeassistant.components.bluetooth import BluetoothScanningMode
11  PassiveBluetoothProcessorCoordinator,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import Platform
15 from homeassistant.core import HomeAssistant
16 
17 from .const import CONF_MEDIUM_TYPE, DEFAULT_MEDIUM_TYPE
18 
19 PLATFORMS: list[Platform] = [Platform.SENSOR]
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
24 type MopekaConfigEntry = ConfigEntry[PassiveBluetoothProcessorCoordinator]
25 
26 
27 async def async_setup_entry(hass: HomeAssistant, entry: MopekaConfigEntry) -> bool:
28  """Set up Mopeka BLE device from a config entry."""
29  address = entry.unique_id
30  assert address is not None
31 
32  # Default sensors configured prior to the introduction of MediumType
33  medium_type_str = entry.data.get(CONF_MEDIUM_TYPE, DEFAULT_MEDIUM_TYPE)
34  data = MopekaIOTBluetoothDeviceData(MediumType(medium_type_str))
35  coordinator = entry.runtime_data = PassiveBluetoothProcessorCoordinator(
36  hass,
37  _LOGGER,
38  address=address,
39  mode=BluetoothScanningMode.PASSIVE,
40  update_method=data.update,
41  )
42  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
43  # only start after all platforms have had a chance to subscribe
44  entry.async_on_unload(coordinator.async_start())
45  entry.async_on_unload(entry.add_update_listener(update_listener))
46  return True
47 
48 
49 async def update_listener(hass: HomeAssistant, entry: MopekaConfigEntry) -> None:
50  """Handle options update."""
51  await hass.config_entries.async_reload(entry.entry_id)
52 
53 
54 async def async_unload_entry(hass: HomeAssistant, entry: MopekaConfigEntry) -> bool:
55  """Unload a config entry."""
56  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, MopekaConfigEntry entry)
Definition: __init__.py:27
None update_listener(HomeAssistant hass, MopekaConfigEntry entry)
Definition: __init__.py:49
bool async_unload_entry(HomeAssistant hass, MopekaConfigEntry entry)
Definition: __init__.py:54