1 """Motionblinds Bluetooth integration."""
3 from __future__
import annotations
5 from functools
import partial
8 from motionblindsble.const
import MotionBlindType
9 from motionblindsble.crypt
import MotionCrypt
10 from motionblindsble.device
import MotionDevice
13 BluetoothCallbackMatcher,
15 BluetoothScanningMode,
16 BluetoothServiceInfoBleak,
17 async_ble_device_from_address,
18 async_register_callback,
31 OPTION_DISCONNECT_TIME,
32 OPTION_PERMANENT_CONNECTION,
35 _LOGGER = logging.getLogger(__name__)
37 PLATFORMS: list[Platform] = [
44 CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
47 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
48 """Set up Motionblinds Bluetooth integration."""
50 _LOGGER.debug(
"Setting up Motionblinds Bluetooth integration")
53 _LOGGER.debug(
"Setting timezone for encryption: %s", hass.config.time_zone)
54 MotionCrypt.set_timezone(hass.config.time_zone)
60 """Set up Motionblinds Bluetooth device from a config entry."""
62 _LOGGER.debug(
"(%s) Setting up device", entry.data[CONF_MAC_CODE])
66 ble_device
if ble_device
is not None else entry.data[CONF_ADDRESS],
67 blind_type=MotionBlindType[entry.data[CONF_BLIND_TYPE].upper()],
71 device.set_create_task_factory(
73 entry.async_create_background_task,
75 name=device.ble_device.address,
78 device.set_call_later_factory(partial(async_call_later, hass=hass))
82 def async_update_ble_device(
83 service_info: BluetoothServiceInfoBleak, change: BluetoothChange
85 """Update the BLEDevice."""
86 _LOGGER.debug(
"(%s) New BLE device found", service_info.address)
87 device.set_ble_device(service_info.device, rssi=service_info.advertisement.rssi)
89 entry.async_on_unload(
92 async_update_ble_device,
94 BluetoothScanningMode.ACTIVE,
98 hass.data.setdefault(DOMAIN, {})[entry.entry_id] = device
101 entry.async_on_unload(entry.add_update_listener(options_update_listener))
103 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
106 entry.async_create_background_task(
110 _LOGGER.debug(
"(%s) Finished setting up device", entry.data[CONF_MAC_CODE])
116 """Handle options update."""
118 "(%s) Updated device options: %s", entry.data[CONF_MAC_CODE], entry.options
124 """Apply the options from the OptionsFlow."""
126 device: MotionDevice = hass.data[DOMAIN][entry.entry_id]
127 disconnect_time: float |
None = entry.options.get(OPTION_DISCONNECT_TIME,
None)
128 permanent_connection: bool = entry.options.get(OPTION_PERMANENT_CONNECTION,
False)
130 device.set_custom_disconnect_time(disconnect_time)
131 await device.set_permanent_connection(permanent_connection)
135 """Unload Motionblinds Bluetooth device from a config entry."""
137 if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
138 hass.data[DOMAIN].pop(entry.entry_id)
BLEDevice|None async_ble_device_from_address(HomeAssistant hass, str address, bool connectable=True)
None options_update_listener(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
None apply_options(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Callable[[], None] async_register_callback(HomeAssistant hass, Callable[[SsdpServiceInfo, SsdpChange], Coroutine[Any, Any, None]|None] callback, dict[str, str]|None match_dict=None)