Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for govee-ble binary sensors."""
2 
3 from __future__ import annotations
4 
5 from govee_ble import (
6  BinarySensorDeviceClass as GoveeBLEBinarySensorDeviceClass,
7  SensorUpdate,
8 )
9 from govee_ble.parser import ERROR
10 
11 from homeassistant import config_entries
13  BinarySensorDeviceClass,
14  BinarySensorEntity,
15  BinarySensorEntityDescription,
16 )
18  PassiveBluetoothDataProcessor,
19  PassiveBluetoothDataUpdate,
20  PassiveBluetoothProcessorEntity,
21 )
22 from homeassistant.core import HomeAssistant
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
25 
26 from .coordinator import GoveeBLEPassiveBluetoothDataProcessor
27 from .device import device_key_to_bluetooth_entity_key
28 
29 BINARY_SENSOR_DESCRIPTIONS = {
30  GoveeBLEBinarySensorDeviceClass.WINDOW: BinarySensorEntityDescription(
31  key=GoveeBLEBinarySensorDeviceClass.WINDOW,
32  device_class=BinarySensorDeviceClass.WINDOW,
33  ),
34  GoveeBLEBinarySensorDeviceClass.MOTION: BinarySensorEntityDescription(
35  key=GoveeBLEBinarySensorDeviceClass.MOTION,
36  device_class=BinarySensorDeviceClass.MOTION,
37  ),
38  GoveeBLEBinarySensorDeviceClass.OCCUPANCY: BinarySensorEntityDescription(
39  key=GoveeBLEBinarySensorDeviceClass.OCCUPANCY,
40  device_class=BinarySensorDeviceClass.OCCUPANCY,
41  ),
42 }
43 
44 
46  sensor_update: SensorUpdate,
47 ) -> PassiveBluetoothDataUpdate[bool | None]:
48  """Convert a sensor update to a bluetooth data update."""
50  devices={
51  device_id: sensor_device_info_to_hass_device_info(device_info)
52  for device_id, device_info in sensor_update.devices.items()
53  },
54  entity_descriptions={
55  device_key_to_bluetooth_entity_key(device_key): BINARY_SENSOR_DESCRIPTIONS[
56  description.device_class
57  ]
58  for device_key, description in sensor_update.binary_entity_descriptions.items()
59  if description.device_class
60  },
61  entity_data={
62  device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
63  for device_key, sensor_values in sensor_update.binary_entity_values.items()
64  },
65  entity_names={
66  device_key_to_bluetooth_entity_key(device_key): sensor_values.name
67  for device_key, sensor_values in sensor_update.binary_entity_values.items()
68  },
69  )
70 
71 
73  hass: HomeAssistant,
75  async_add_entities: AddEntitiesCallback,
76 ) -> None:
77  """Set up the govee-ble BLE sensors."""
78  coordinator = entry.runtime_data
79  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
80  entry.async_on_unload(
81  processor.async_add_entities_listener(
82  GoveeBluetoothBinarySensorEntity, async_add_entities
83  )
84  )
85  entry.async_on_unload(
86  coordinator.async_register_processor(processor, BinarySensorEntityDescription)
87  )
88 
89 
91  PassiveBluetoothProcessorEntity[
92  PassiveBluetoothDataProcessor[bool | None, SensorUpdate]
93  ],
94  BinarySensorEntity,
95 ):
96  """Representation of a govee-ble binary sensor."""
97 
98  processor: GoveeBLEPassiveBluetoothDataProcessor[bool | None]
99 
100  @property
101  def available(self) -> bool:
102  """Return False if sensor is in error."""
103  coordinator = self.processor.coordinator
104  return self.processor.entity_data.get(self.entity_key) != ERROR and ( # type: ignore[comparison-overlap]
105  ((model_info := coordinator.model_info) and model_info.sleepy)
106  or super().available
107  )
108 
109  @property
110  def is_on(self) -> bool | None:
111  """Return the native value."""
112  return self.processor.entity_data.get(self.entity_key)
PassiveBluetoothEntityKey device_key_to_bluetooth_entity_key(DeviceKey device_key)
Definition: device.py:14
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry 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)
Definition: sensor.py:20