Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """BinarySensor integration microBees."""
2 
3 from microBeesPy import Sensor
4 
6  BinarySensorDeviceClass,
7  BinarySensorEntity,
8  BinarySensorEntityDescription,
9 )
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .const import DOMAIN
15 from .coordinator import MicroBeesUpdateCoordinator
16 from .entity import MicroBeesEntity
17 
18 BINARYSENSOR_TYPES = {
20  device_class=BinarySensorDeviceClass.MOTION,
21  key="motion_sensor",
22  ),
24  device_class=BinarySensorDeviceClass.DOOR,
25  key="door_sensor",
26  ),
28  device_class=BinarySensorDeviceClass.MOISTURE,
29  key="moisture_sensor",
30  ),
32  device_class=BinarySensorDeviceClass.SMOKE,
33  key="smoke_sensor",
34  ),
35 }
36 
37 
39  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
40 ) -> None:
41  """Set up the microBees binary sensor platform."""
42  coordinator: MicroBeesUpdateCoordinator = hass.data[DOMAIN][
43  entry.entry_id
44  ].coordinator
46  MBBinarySensor(coordinator, entity_description, bee_id, binary_sensor.id)
47  for bee_id, bee in coordinator.data.bees.items()
48  for binary_sensor in bee.sensors
49  if (entity_description := BINARYSENSOR_TYPES.get(binary_sensor.device_type))
50  is not None
51  )
52 
53 
55  """Representation of a microBees BinarySensor."""
56 
57  def __init__(
58  self,
59  coordinator: MicroBeesUpdateCoordinator,
60  entity_description: BinarySensorEntityDescription,
61  bee_id: int,
62  sensor_id: int,
63  ) -> None:
64  """Initialize the microBees BinarySensor."""
65  super().__init__(coordinator, bee_id)
66  self._attr_unique_id_attr_unique_id = f"{bee_id}_{sensor_id}"
67  self.sensor_idsensor_id = sensor_id
68  self.entity_descriptionentity_description = entity_description
69 
70  @property
71  def name(self) -> str:
72  """Name of the BinarySensor."""
73  return self.sensorsensor.name
74 
75  @property
76  def is_on(self) -> bool:
77  """Return the state of the BinarySensor."""
78  return self.sensorsensor.value
79 
80  @property
81  def sensor(self) -> Sensor:
82  """Return the BinarySensor."""
83  return self.coordinator.data.sensors[self.sensor_idsensor_id]
None __init__(self, MicroBeesUpdateCoordinator coordinator, BinarySensorEntityDescription entity_description, int bee_id, int sensor_id)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)