-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
/
Copy path__init__.py
140 lines (104 loc) · 4.44 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""Motionblinds Bluetooth integration."""
from __future__ import annotations
from functools import partial
import logging
from motionblindsble.const import MotionBlindType
from motionblindsble.crypt import MotionCrypt
from motionblindsble.device import MotionDevice
from homeassistant.components.bluetooth import (
BluetoothCallbackMatcher,
BluetoothChange,
BluetoothScanningMode,
BluetoothServiceInfoBleak,
async_ble_device_from_address,
async_register_callback,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ADDRESS, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.typing import ConfigType
from .const import (
CONF_BLIND_TYPE,
CONF_MAC_CODE,
DOMAIN,
OPTION_DISCONNECT_TIME,
OPTION_PERMANENT_CONNECTION,
)
_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [
Platform.BUTTON,
Platform.COVER,
Platform.SELECT,
Platform.SENSOR,
]
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up Motionblinds Bluetooth integration."""
_LOGGER.debug("Setting up Motionblinds Bluetooth integration")
# The correct time is needed for encryption
_LOGGER.debug("Setting timezone for encryption: %s", hass.config.time_zone)
MotionCrypt.set_timezone(hass.config.time_zone)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Motionblinds Bluetooth device from a config entry."""
_LOGGER.debug("(%s) Setting up device", entry.data[CONF_MAC_CODE])
ble_device = async_ble_device_from_address(hass, entry.data[CONF_ADDRESS])
device = MotionDevice(
ble_device if ble_device is not None else entry.data[CONF_ADDRESS],
blind_type=MotionBlindType[entry.data[CONF_BLIND_TYPE].upper()],
)
# Register Home Assistant functions to use in the library
device.set_create_task_factory(
partial(
entry.async_create_background_task,
hass=hass,
name=device.ble_device.address,
)
)
device.set_call_later_factory(partial(async_call_later, hass=hass))
# Register a callback that updates the BLEDevice in the library
@callback
def async_update_ble_device(
service_info: BluetoothServiceInfoBleak, change: BluetoothChange
) -> None:
"""Update the BLEDevice."""
_LOGGER.debug("(%s) New BLE device found", service_info.address)
device.set_ble_device(service_info.device, rssi=service_info.advertisement.rssi)
entry.async_on_unload(
async_register_callback(
hass,
async_update_ble_device,
BluetoothCallbackMatcher(address=entry.data[CONF_ADDRESS]),
BluetoothScanningMode.ACTIVE,
)
)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = device
# Register OptionsFlow update listener
entry.async_on_unload(entry.add_update_listener(options_update_listener))
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
# Apply options
entry.async_create_background_task(
hass, apply_options(hass, entry), device.ble_device.address
)
_LOGGER.debug("(%s) Finished setting up device", entry.data[CONF_MAC_CODE])
return True
async def options_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle options update."""
_LOGGER.debug(
"(%s) Updated device options: %s", entry.data[CONF_MAC_CODE], entry.options
)
await apply_options(hass, entry)
async def apply_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Apply the options from the OptionsFlow."""
device: MotionDevice = hass.data[DOMAIN][entry.entry_id]
disconnect_time: float | None = entry.options.get(OPTION_DISCONNECT_TIME, None)
permanent_connection: bool = entry.options.get(OPTION_PERMANENT_CONNECTION, False)
device.set_custom_disconnect_time(disconnect_time)
await device.set_permanent_connection(permanent_connection)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload Motionblinds Bluetooth device from a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok