Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Big Ass Fans binary sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import cast
8 
9 from aiobafi6 import Device
10 
12  BinarySensorDeviceClass,
13  BinarySensorEntity,
14  BinarySensorEntityDescription,
15 )
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from . import BAFConfigEntry
20 from .entity import BAFDescriptionEntity
21 
22 
23 @dataclass(frozen=True, kw_only=True)
25  BinarySensorEntityDescription,
26 ):
27  """Class describing BAF binary sensor entities."""
28 
29  value_fn: Callable[[Device], bool | None]
30 
31 
32 OCCUPANCY_SENSORS = (
34  key="occupancy",
35  device_class=BinarySensorDeviceClass.OCCUPANCY,
36  value_fn=lambda device: cast(bool | None, device.fan_occupancy_detected),
37  ),
38 )
39 
40 
42  hass: HomeAssistant,
43  entry: BAFConfigEntry,
44  async_add_entities: AddEntitiesCallback,
45 ) -> None:
46  """Set up BAF binary sensors."""
47  device = entry.runtime_data
48  if device.has_occupancy:
50  BAFBinarySensor(device, description) for description in OCCUPANCY_SENSORS
51  )
52 
53 
55  """BAF binary sensor."""
56 
57  entity_description: BAFBinarySensorDescription
58 
59  @callback
60  def _async_update_attrs(self) -> None:
61  """Update attrs from device."""
62  self._attr_is_on_attr_is_on = self.entity_descriptionentity_description.value_fn(self._device_device)
None async_setup_entry(HomeAssistant hass, BAFConfigEntry entry, AddEntitiesCallback async_add_entities)