Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for an Intergas heater via an InComfort/InTouch Lan2RF gateway."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from incomfortclient import Heater as InComfortHeater
10 
12  BinarySensorDeviceClass,
13  BinarySensorEntity,
14  BinarySensorEntityDescription,
15 )
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from . import InComfortConfigEntry
20 from .coordinator import InComfortDataCoordinator
21 from .entity import IncomfortBoilerEntity
22 
23 
24 @dataclass(frozen=True, kw_only=True)
26  """Describes Incomfort binary sensor entity."""
27 
28  value_key: str
29  extra_state_attributes_fn: Callable[[dict[str, Any]], dict[str, Any]] | None = None
30 
31 
32 SENSOR_TYPES: tuple[IncomfortBinarySensorEntityDescription, ...] = (
34  key="failed",
35  translation_key="fault",
36  device_class=BinarySensorDeviceClass.PROBLEM,
37  value_key="is_failed",
38  extra_state_attributes_fn=lambda status: {
39  "fault_code": status["fault_code"] or "none",
40  },
41  ),
43  key="is_pumping",
44  translation_key="is_pumping",
45  device_class=BinarySensorDeviceClass.RUNNING,
46  value_key="is_pumping",
47  ),
49  key="is_burning",
50  translation_key="is_burning",
51  device_class=BinarySensorDeviceClass.RUNNING,
52  value_key="is_burning",
53  ),
55  key="is_tapping",
56  translation_key="is_tapping",
57  device_class=BinarySensorDeviceClass.RUNNING,
58  value_key="is_tapping",
59  ),
60 )
61 
62 
64  hass: HomeAssistant,
65  entry: InComfortConfigEntry,
66  async_add_entities: AddEntitiesCallback,
67 ) -> None:
68  """Set up an InComfort/InTouch binary_sensor entity."""
69  incomfort_coordinator = entry.runtime_data
70  heaters = incomfort_coordinator.data.heaters
72  IncomfortBinarySensor(incomfort_coordinator, h, description)
73  for h in heaters
74  for description in SENSOR_TYPES
75  )
76 
77 
79  """Representation of an InComfort binary sensor."""
80 
81  entity_description: IncomfortBinarySensorEntityDescription
82 
83  def __init__(
84  self,
85  coordinator: InComfortDataCoordinator,
86  heater: InComfortHeater,
87  description: IncomfortBinarySensorEntityDescription,
88  ) -> None:
89  """Initialize the binary sensor."""
90  super().__init__(coordinator, heater)
91  self.entity_descriptionentity_description = description
92  self._attr_unique_id_attr_unique_id = f"{heater.serial_no}_{description.key}"
93 
94  @property
95  def is_on(self) -> bool:
96  """Return the status of the sensor."""
97  return self._heater_heater.status[self.entity_descriptionentity_description.value_key]
98 
99  @property
100  def extra_state_attributes(self) -> dict[str, Any] | None:
101  """Return the device state attributes."""
102  if (attributes_fn := self.entity_descriptionentity_description.extra_state_attributes_fn) is None:
103  return None
104  return attributes_fn(self._heater_heater.status)
None __init__(self, InComfortDataCoordinator coordinator, InComfortHeater heater, IncomfortBinarySensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, InComfortConfigEntry entry, AddEntitiesCallback async_add_entities)