-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
101 lines (78 loc) · 3.28 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""The LD2405 BLE integration."""
import logging
from bleak_retry_connector import (
BleakError,
close_stale_connections_by_address,
get_device,
)
from .ld2450_ble import LD2450BLE
from homeassistant.components import bluetooth
from homeassistant.components.bluetooth.match import ADDRESS, BluetoothCallbackMatcher
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ADDRESS, EVENT_HOMEASSISTANT_STOP, Platform
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from .const import DOMAIN
from .coordinator import LD2450BLECoordinator
from .models import LD2450BLEData
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.SWITCH, Platform.BINARY_SENSOR, Platform.SELECT, Platform.BUTTON, Platform.NUMBER]
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up LD2450 BLE from a config entry."""
address: str = entry.data[CONF_ADDRESS]
await close_stale_connections_by_address(address)
ble_device = bluetooth.async_ble_device_from_address(
hass, address.upper(), True
) or await get_device(address)
if not ble_device:
raise ConfigEntryNotReady(
f"Could not find LD2450 device with address {address}"
)
ld2450_ble = LD2450BLE(ble_device)
coordinator = LD2450BLECoordinator(hass, ld2450_ble)
try:
await ld2450_ble.initialise()
except BleakError as exc:
raise ConfigEntryNotReady(
f"Could not initialise LD2450 device with address {address}"
) from exc
@callback
def _async_update_ble(
service_info: bluetooth.BluetoothServiceInfoBleak,
change: bluetooth.BluetoothChange,
) -> None:
"""Update from a ble callback."""
ld2450_ble.set_ble_device_and_advertisement_data(
service_info.device, service_info.advertisement
)
entry.async_on_unload(
bluetooth.async_register_callback(
hass,
_async_update_ble,
BluetoothCallbackMatcher({ADDRESS: address}),
bluetooth.BluetoothScanningMode.ACTIVE,
)
)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = LD2450BLEData(
entry.title, ld2450_ble, coordinator
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
async def _async_stop(event: Event) -> None:
"""Close the connection."""
await ld2450_ble.stop()
entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop)
)
return True
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle options update."""
data: LD2450BLEData = hass.data[DOMAIN][entry.entry_id]
if entry.title != data.title:
await hass.config_entries.async_reload(entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
data: LD2450BLEData = hass.data[DOMAIN].pop(entry.entry_id)
await data.device.stop()
return unload_ok