Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary sensor entities for the madVR integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
9  BinarySensorEntity,
10  BinarySensorEntityDescription,
11 )
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from . import MadVRConfigEntry
16 from .coordinator import MadVRCoordinator
17 from .entity import MadVREntity
18 
19 _HDR_FLAG = "hdr_flag"
20 _OUTGOING_HDR_FLAG = "outgoing_hdr_flag"
21 _POWER_STATE = "power_state"
22 _SIGNAL_STATE = "signal_state"
23 
24 
25 @dataclass(frozen=True, kw_only=True)
27  """Describe madVR binary sensor entity."""
28 
29  value_fn: Callable[[MadVRCoordinator], bool]
30 
31 
32 BINARY_SENSORS: tuple[MadvrBinarySensorEntityDescription, ...] = (
34  key=_POWER_STATE,
35  translation_key=_POWER_STATE,
36  value_fn=lambda coordinator: coordinator.data.get("is_on", False),
37  ),
39  key=_SIGNAL_STATE,
40  translation_key=_SIGNAL_STATE,
41  value_fn=lambda coordinator: coordinator.data.get("is_signal", False),
42  ),
44  key=_HDR_FLAG,
45  translation_key=_HDR_FLAG,
46  value_fn=lambda coordinator: coordinator.data.get("hdr_flag", False),
47  ),
49  key=_OUTGOING_HDR_FLAG,
50  translation_key=_OUTGOING_HDR_FLAG,
51  value_fn=lambda coordinator: coordinator.data.get("outgoing_hdr_flag", False),
52  ),
53 )
54 
55 
57  hass: HomeAssistant,
58  entry: MadVRConfigEntry,
59  async_add_entities: AddEntitiesCallback,
60 ) -> None:
61  """Set up the binary sensor entities."""
62  coordinator = entry.runtime_data
64  MadvrBinarySensor(coordinator, description) for description in BINARY_SENSORS
65  )
66 
67 
69  """Base class for madVR binary sensors."""
70 
71  entity_description: MadvrBinarySensorEntityDescription
72 
73  def __init__(
74  self,
75  coordinator: MadVRCoordinator,
76  description: MadvrBinarySensorEntityDescription,
77  ) -> None:
78  """Initialize the binary sensor."""
79  super().__init__(coordinator)
80  self.entity_descriptionentity_description = description
81  self._attr_unique_id_attr_unique_id = f"{coordinator.mac}_{description.key}"
82 
83  @property
84  def is_on(self) -> bool:
85  """Return true if the binary sensor is on."""
86  return self.entity_descriptionentity_description.value_fn(self.coordinator)
None __init__(self, MadVRCoordinator coordinator, MadvrBinarySensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, MadVRConfigEntry entry, AddEntitiesCallback async_add_entities)