Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for the QNAP QSW binary sensors."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass, replace
6 from typing import Final
7 
8 from aioqsw.const import (
9  QSD_ANOMALY,
10  QSD_FIRMWARE_CONDITION,
11  QSD_LACP_PORTS,
12  QSD_LINK,
13  QSD_MESSAGE,
14  QSD_PORTS,
15  QSD_PORTS_STATUS,
16 )
17 
19  BinarySensorDeviceClass,
20  BinarySensorEntity,
21  BinarySensorEntityDescription,
22 )
23 from homeassistant.config_entries import ConfigEntry
24 from homeassistant.const import EntityCategory
25 from homeassistant.core import HomeAssistant, callback
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 from homeassistant.helpers.typing import UNDEFINED
28 
29 from .const import ATTR_MESSAGE, DOMAIN, QSW_COORD_DATA
30 from .coordinator import QswDataCoordinator
31 from .entity import QswEntityDescription, QswEntityType, QswSensorEntity
32 
33 
34 @dataclass(frozen=True)
36  BinarySensorEntityDescription, QswEntityDescription
37 ):
38  """A class that describes QNAP QSW binary sensor entities."""
39 
40  attributes: dict[str, list[str]] | None = None
41  qsw_type: QswEntityType | None = None
42  sep_key: str = "_"
43 
44 
45 BINARY_SENSOR_TYPES: Final[tuple[QswBinarySensorEntityDescription, ...]] = (
47  attributes={
48  ATTR_MESSAGE: [QSD_FIRMWARE_CONDITION, QSD_MESSAGE],
49  },
50  device_class=BinarySensorDeviceClass.PROBLEM,
51  entity_category=EntityCategory.DIAGNOSTIC,
52  key=QSD_FIRMWARE_CONDITION,
53  subkey=QSD_ANOMALY,
54  ),
55 )
56 
57 LACP_PORT_BINARY_SENSOR_TYPES: Final[tuple[QswBinarySensorEntityDescription, ...]] = (
59  device_class=BinarySensorDeviceClass.CONNECTIVITY,
60  entity_registry_enabled_default=False,
61  key=QSD_PORTS_STATUS,
62  qsw_type=QswEntityType.LACP_PORT,
63  name="Link",
64  subkey=QSD_LINK,
65  ),
66 )
67 
68 PORT_BINARY_SENSOR_TYPES: Final[tuple[QswBinarySensorEntityDescription, ...]] = (
70  device_class=BinarySensorDeviceClass.CONNECTIVITY,
71  entity_registry_enabled_default=False,
72  key=QSD_PORTS_STATUS,
73  qsw_type=QswEntityType.PORT,
74  name="Link",
75  subkey=QSD_LINK,
76  ),
77 )
78 
79 
81  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
82 ) -> None:
83  """Add QNAP QSW binary sensors from a config_entry."""
84  coordinator: QswDataCoordinator = hass.data[DOMAIN][entry.entry_id][QSW_COORD_DATA]
85 
86  entities: list[QswBinarySensor] = [
87  QswBinarySensor(coordinator, description, entry)
88  for description in BINARY_SENSOR_TYPES
89  if (
90  description.key in coordinator.data
91  and description.subkey in coordinator.data[description.key]
92  )
93  ]
94 
95  for description in LACP_PORT_BINARY_SENSOR_TYPES:
96  if (
97  description.key in coordinator.data
98  and QSD_LACP_PORTS in coordinator.data[description.key]
99  ):
100  for port_id, port_values in coordinator.data[description.key][
101  QSD_LACP_PORTS
102  ].items():
103  if description.subkey in port_values:
104  _desc = replace(
105  description,
106  sep_key=f"_lacp_port_{port_id}_",
107  name=f"LACP Port {port_id} {description.name}",
108  )
109  entities.append(QswBinarySensor(coordinator, _desc, entry, port_id))
110 
111  for description in PORT_BINARY_SENSOR_TYPES:
112  if (
113  description.key in coordinator.data
114  and QSD_PORTS in coordinator.data[description.key]
115  ):
116  for port_id, port_values in coordinator.data[description.key][
117  QSD_PORTS
118  ].items():
119  if description.subkey in port_values:
120  _desc = replace(
121  description,
122  sep_key=f"_port_{port_id}_",
123  name=f"Port {port_id} {description.name}",
124  )
125  entities.append(QswBinarySensor(coordinator, _desc, entry, port_id))
126 
127  async_add_entities(entities)
128 
129 
131  """Define a QNAP QSW binary sensor."""
132 
133  entity_description: QswBinarySensorEntityDescription
134 
135  def __init__(
136  self,
137  coordinator: QswDataCoordinator,
138  description: QswBinarySensorEntityDescription,
139  entry: ConfigEntry,
140  type_id: int | None = None,
141  ) -> None:
142  """Initialize."""
143  super().__init__(coordinator, entry, type_id)
144  if description.name == UNDEFINED:
145  self._attr_has_entity_name_attr_has_entity_name = True
146  else:
147  self._attr_name_attr_name = f"{self.product} {description.name}"
148  self._attr_unique_id_attr_unique_id = (
149  f"{entry.unique_id}_{description.key}"
150  f"{description.sep_key}{description.subkey}"
151  )
152  self.entity_descriptionentity_description = description
153  self._async_update_attrs_async_update_attrs_async_update_attrs()
154 
155  @callback
156  def _async_update_attrs(self) -> None:
157  """Update binary sensor attributes."""
158  self._attr_is_on_attr_is_on = self.get_device_valueget_device_value(
159  self.entity_descriptionentity_description.key,
160  self.entity_descriptionentity_description.subkey,
161  self.entity_descriptionentity_description.qsw_type,
162  )
163  super()._async_update_attrs()
None __init__(self, QswDataCoordinator coordinator, QswBinarySensorEntityDescription description, ConfigEntry entry, int|None type_id=None)
Any get_device_value(self, str key, str subkey, QswEntityType|None qsw_type=None)
Definition: entity.py:70
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)