-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path__init__.py
95 lines (74 loc) · 3.22 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
"""The Sonicare BLE integration."""
import logging
from bleak_retry_connector import BleakError, get_device
from sonicare_bletb import SonicareBLETB
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 SonicareBLETBCoordinator
from .models import SonicareBLETBData
PLATFORMS: list[Platform] = [Platform.SENSOR]
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Sonicare BLE toothbrush from a config entry."""
address: str = entry.data[CONF_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 SonicareBLE toothbrush device with address {address}"
)
sonicare_ble = SonicareBLETB(ble_device)
coordinator = SonicareBLETBCoordinator(hass, sonicare_ble)
try:
await sonicare_ble.initialise()
except BleakError as exc:
raise ConfigEntryNotReady(
f"Could not initialise SonicareBLE toothbrush device with address {address}"
) from exc
@callback
def _async_update_ble(
service_info: bluetooth.BluetoothServiceInfoBleak,
change: bluetooth.BluetoothChange,
) -> None:
"""Update from a ble callback."""
_LOGGER.warning("_async_update_ble")
sonicare_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] = SonicareBLETBData(
entry.title, sonicare_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 sonicare_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: SonicareBLETBData = 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: SonicareBLETBData = hass.data[DOMAIN].pop(entry.entry_id)
await data.device.stop()
return unload_ok