1 """Support for BTHome binary sensors."""
3 from __future__
import annotations
5 from bthome_ble
import (
6 BinarySensorDeviceClass
as BTHomeBinarySensorDeviceClass,
11 BinarySensorDeviceClass,
13 BinarySensorEntityDescription,
16 PassiveBluetoothDataUpdate,
17 PassiveBluetoothProcessorEntity,
23 from .coordinator
import BTHomePassiveBluetoothDataProcessor
24 from .device
import device_key_to_bluetooth_entity_key
25 from .types
import BTHomeConfigEntry
27 BINARY_SENSOR_DESCRIPTIONS = {
29 key=BTHomeBinarySensorDeviceClass.BATTERY,
30 device_class=BinarySensorDeviceClass.BATTERY,
33 key=BTHomeBinarySensorDeviceClass.BATTERY_CHARGING,
34 device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
37 key=BTHomeBinarySensorDeviceClass.CO,
38 device_class=BinarySensorDeviceClass.CO,
41 key=BTHomeBinarySensorDeviceClass.COLD,
42 device_class=BinarySensorDeviceClass.COLD,
45 key=BTHomeBinarySensorDeviceClass.CONNECTIVITY,
46 device_class=BinarySensorDeviceClass.CONNECTIVITY,
49 key=BTHomeBinarySensorDeviceClass.DOOR,
50 device_class=BinarySensorDeviceClass.DOOR,
53 key=BTHomeBinarySensorDeviceClass.HEAT,
54 device_class=BinarySensorDeviceClass.HEAT,
57 key=BTHomeBinarySensorDeviceClass.GARAGE_DOOR,
58 device_class=BinarySensorDeviceClass.GARAGE_DOOR,
61 key=BTHomeBinarySensorDeviceClass.GAS,
62 device_class=BinarySensorDeviceClass.GAS,
65 key=BTHomeBinarySensorDeviceClass.GENERIC,
68 key=BTHomeBinarySensorDeviceClass.LIGHT,
69 device_class=BinarySensorDeviceClass.LIGHT,
72 key=BTHomeBinarySensorDeviceClass.LOCK,
73 device_class=BinarySensorDeviceClass.LOCK,
76 key=BTHomeBinarySensorDeviceClass.MOISTURE,
77 device_class=BinarySensorDeviceClass.MOISTURE,
80 key=BTHomeBinarySensorDeviceClass.MOTION,
81 device_class=BinarySensorDeviceClass.MOTION,
84 key=BTHomeBinarySensorDeviceClass.MOVING,
85 device_class=BinarySensorDeviceClass.MOVING,
88 key=BTHomeBinarySensorDeviceClass.OCCUPANCY,
89 device_class=BinarySensorDeviceClass.OCCUPANCY,
92 key=BTHomeBinarySensorDeviceClass.OPENING,
93 device_class=BinarySensorDeviceClass.OPENING,
96 key=BTHomeBinarySensorDeviceClass.PLUG,
97 device_class=BinarySensorDeviceClass.PLUG,
100 key=BTHomeBinarySensorDeviceClass.POWER,
101 device_class=BinarySensorDeviceClass.POWER,
104 key=BTHomeBinarySensorDeviceClass.PRESENCE,
105 device_class=BinarySensorDeviceClass.PRESENCE,
108 key=BTHomeBinarySensorDeviceClass.PROBLEM,
109 device_class=BinarySensorDeviceClass.PROBLEM,
112 key=BTHomeBinarySensorDeviceClass.RUNNING,
113 device_class=BinarySensorDeviceClass.RUNNING,
116 key=BTHomeBinarySensorDeviceClass.SAFETY,
117 device_class=BinarySensorDeviceClass.SAFETY,
120 key=BTHomeBinarySensorDeviceClass.SMOKE,
121 device_class=BinarySensorDeviceClass.SMOKE,
124 key=BTHomeBinarySensorDeviceClass.SOUND,
125 device_class=BinarySensorDeviceClass.SOUND,
128 key=BTHomeBinarySensorDeviceClass.TAMPER,
129 device_class=BinarySensorDeviceClass.TAMPER,
132 key=BTHomeBinarySensorDeviceClass.VIBRATION,
133 device_class=BinarySensorDeviceClass.VIBRATION,
136 key=BTHomeBinarySensorDeviceClass.WINDOW,
137 device_class=BinarySensorDeviceClass.WINDOW,
143 sensor_update: SensorUpdate,
144 ) -> PassiveBluetoothDataUpdate[bool |
None]:
145 """Convert a binary sensor update to a bluetooth data update."""
149 for device_id, device_info
in sensor_update.devices.items()
151 entity_descriptions={
153 description.device_class
155 for device_key, description
in sensor_update.binary_entity_descriptions.items()
156 if description.device_class
160 for device_key, sensor_values
in sensor_update.binary_entity_values.items()
164 for device_key, sensor_values
in sensor_update.binary_entity_values.items()
171 entry: BTHomeConfigEntry,
172 async_add_entities: AddEntitiesCallback,
174 """Set up the BTHome BLE binary sensors."""
175 coordinator = entry.runtime_data
177 sensor_update_to_bluetooth_data_update
179 entry.async_on_unload(
180 processor.async_add_entities_listener(
181 BTHomeBluetoothBinarySensorEntity, async_add_entities
184 entry.async_on_unload(
185 coordinator.async_register_processor(processor, BinarySensorEntityDescription)
190 PassiveBluetoothProcessorEntity[BTHomePassiveBluetoothDataProcessor[bool |
None]],
193 """Representation of a BTHome binary sensor."""
197 """Return the native value."""
198 return self.processor.entity_data.get(self.entity_key)
202 """Return True if entity is available."""
203 return self.processor.coordinator.sleepy_device
or super().available
PassiveBluetoothEntityKey device_key_to_bluetooth_entity_key(DeviceKey device_key)
None async_setup_entry(HomeAssistant hass, BTHomeConfigEntry entry, AddEntitiesCallback async_add_entities)
PassiveBluetoothDataUpdate[bool|None] sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)