Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Rituals Perfume Genie binary sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from pyrituals import Diffuser
9 
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13  BinarySensorEntityDescription,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import EntityCategory
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .const import DOMAIN
21 from .coordinator import RitualsDataUpdateCoordinator
22 from .entity import DiffuserEntity
23 
24 
25 @dataclass(frozen=True, kw_only=True)
27  """Class describing Rituals binary sensor entities."""
28 
29  is_on_fn: Callable[[Diffuser], bool]
30  has_fn: Callable[[Diffuser], bool]
31 
32 
33 ENTITY_DESCRIPTIONS = (
35  key="charging",
36  device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
37  entity_category=EntityCategory.DIAGNOSTIC,
38  is_on_fn=lambda diffuser: diffuser.charging,
39  has_fn=lambda diffuser: diffuser.has_battery,
40  ),
41 )
42 
43 
45  hass: HomeAssistant,
46  config_entry: ConfigEntry,
47  async_add_entities: AddEntitiesCallback,
48 ) -> None:
49  """Set up the diffuser binary sensors."""
50  coordinators: dict[str, RitualsDataUpdateCoordinator] = hass.data[DOMAIN][
51  config_entry.entry_id
52  ]
53 
55  RitualsBinarySensorEntity(coordinator, description)
56  for coordinator in coordinators.values()
57  for description in ENTITY_DESCRIPTIONS
58  if description.has_fn(coordinator.diffuser)
59  )
60 
61 
63  """Defines a Rituals binary sensor entity."""
64 
65  entity_description: RitualsBinarySensorEntityDescription
66 
67  @property
68  def is_on(self) -> bool:
69  """Return the state of the binary sensor."""
70  return self.entity_descriptionentity_description.is_on_fn(self.coordinator.diffuser)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)