Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Plugwise Binary Sensor component for Home Assistant."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from plugwise.constants import BinarySensorType
10 
12  BinarySensorDeviceClass,
13  BinarySensorEntity,
14  BinarySensorEntityDescription,
15 )
16 from homeassistant.const import EntityCategory
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import PlugwiseConfigEntry
21 from .coordinator import PlugwiseDataUpdateCoordinator
22 from .entity import PlugwiseEntity
23 
24 SEVERITIES = ["other", "info", "warning", "error"]
25 
26 
27 @dataclass(frozen=True)
29  """Describes a Plugwise binary sensor entity."""
30 
31  key: BinarySensorType
32 
33 
34 BINARY_SENSORS: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
36  key="low_battery",
37  translation_key="low_battery",
38  device_class=BinarySensorDeviceClass.BATTERY,
39  entity_category=EntityCategory.DIAGNOSTIC,
40  ),
42  key="compressor_state",
43  translation_key="compressor_state",
44  entity_category=EntityCategory.DIAGNOSTIC,
45  ),
47  key="cooling_enabled",
48  translation_key="cooling_enabled",
49  entity_category=EntityCategory.DIAGNOSTIC,
50  ),
52  key="dhw_state",
53  translation_key="dhw_state",
54  entity_category=EntityCategory.DIAGNOSTIC,
55  ),
57  key="flame_state",
58  translation_key="flame_state",
59  name="Flame state",
60  entity_category=EntityCategory.DIAGNOSTIC,
61  ),
63  key="heating_state",
64  translation_key="heating_state",
65  entity_category=EntityCategory.DIAGNOSTIC,
66  ),
68  key="cooling_state",
69  translation_key="cooling_state",
70  entity_category=EntityCategory.DIAGNOSTIC,
71  ),
73  key="secondary_boiler_state",
74  translation_key="secondary_boiler_state",
75  entity_category=EntityCategory.DIAGNOSTIC,
76  ),
78  key="plugwise_notification",
79  translation_key="plugwise_notification",
80  entity_category=EntityCategory.DIAGNOSTIC,
81  ),
82 )
83 
84 
86  hass: HomeAssistant,
87  entry: PlugwiseConfigEntry,
88  async_add_entities: AddEntitiesCallback,
89 ) -> None:
90  """Set up the Smile binary_sensors from a config entry."""
91  coordinator = entry.runtime_data
92 
93  @callback
94  def _add_entities() -> None:
95  """Add Entities."""
96  if not coordinator.new_devices:
97  return
98 
100  PlugwiseBinarySensorEntity(coordinator, device_id, description)
101  for device_id in coordinator.new_devices
102  if (
103  binary_sensors := coordinator.data.devices[device_id].get(
104  "binary_sensors"
105  )
106  )
107  for description in BINARY_SENSORS
108  if description.key in binary_sensors
109  )
110 
111  _add_entities()
112  entry.async_on_unload(coordinator.async_add_listener(_add_entities))
113 
114 
116  """Represent Smile Binary Sensors."""
117 
118  entity_description: PlugwiseBinarySensorEntityDescription
119 
120  def __init__(
121  self,
122  coordinator: PlugwiseDataUpdateCoordinator,
123  device_id: str,
124  description: PlugwiseBinarySensorEntityDescription,
125  ) -> None:
126  """Initialise the binary_sensor."""
127  super().__init__(coordinator, device_id)
128  self.entity_descriptionentity_description = description
129  self._attr_unique_id_attr_unique_id = f"{device_id}-{description.key}"
130 
131  @property
132  def is_on(self) -> bool:
133  """Return true if the binary sensor is on."""
134  return self.devicedevice["binary_sensors"][self.entity_descriptionentity_description.key]
135 
136  @property
137  def extra_state_attributes(self) -> Mapping[str, Any] | None:
138  """Return entity specific state attributes."""
139  if self.entity_descriptionentity_description.key != "plugwise_notification":
140  return None
141 
142  attrs: dict[str, list[str]] = {f"{severity}_msg": [] for severity in SEVERITIES}
143  if notify := self.coordinator.data.gateway["notifications"]:
144  for details in notify.values():
145  for msg_type, msg in details.items():
146  msg_type = msg_type.lower()
147  if msg_type not in SEVERITIES:
148  msg_type = "other"
149  attrs[f"{msg_type}_msg"].append(msg)
150 
151  return attrs
None __init__(self, PlugwiseDataUpdateCoordinator coordinator, str device_id, PlugwiseBinarySensorEntityDescription description)
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, PlugwiseConfigEntry entry, AddEntitiesCallback async_add_entities)