Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for UPnP/IGD Sensors."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from datetime import datetime
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12  SensorStateClass,
13 )
14 from homeassistant.const import (
15  EntityCategory,
16  UnitOfDataRate,
17  UnitOfInformation,
18  UnitOfTime,
19 )
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 
23 from . import UpnpConfigEntry
24 from .const import (
25  BYTES_RECEIVED,
26  BYTES_SENT,
27  DATA_PACKETS,
28  DATA_RATE_PACKETS_PER_SECOND,
29  KIBIBYTES_PER_SEC_RECEIVED,
30  KIBIBYTES_PER_SEC_SENT,
31  LOGGER,
32  PACKETS_PER_SEC_RECEIVED,
33  PACKETS_PER_SEC_SENT,
34  PACKETS_RECEIVED,
35  PACKETS_SENT,
36  PORT_MAPPING_NUMBER_OF_ENTRIES_IPV4,
37  ROUTER_IP,
38  ROUTER_UPTIME,
39  WAN_STATUS,
40 )
41 from .entity import UpnpEntity, UpnpEntityDescription
42 
43 
44 @dataclass(frozen=True)
46  """A class that describes a sensor UPnP entities."""
47 
48 
49 SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
51  key=BYTES_RECEIVED,
52  translation_key="data_received",
53  device_class=SensorDeviceClass.DATA_SIZE,
54  native_unit_of_measurement=UnitOfInformation.BYTES,
55  entity_registry_enabled_default=False,
56  state_class=SensorStateClass.TOTAL_INCREASING,
57  suggested_display_precision=0,
58  ),
60  key=BYTES_SENT,
61  translation_key="data_sent",
62  device_class=SensorDeviceClass.DATA_SIZE,
63  native_unit_of_measurement=UnitOfInformation.BYTES,
64  entity_registry_enabled_default=False,
65  state_class=SensorStateClass.TOTAL_INCREASING,
66  suggested_display_precision=0,
67  ),
69  key=PACKETS_RECEIVED,
70  translation_key="packets_received",
71  native_unit_of_measurement=DATA_PACKETS,
72  entity_registry_enabled_default=False,
73  state_class=SensorStateClass.TOTAL_INCREASING,
74  suggested_display_precision=0,
75  ),
77  key=PACKETS_SENT,
78  translation_key="packets_sent",
79  native_unit_of_measurement=DATA_PACKETS,
80  entity_registry_enabled_default=False,
81  state_class=SensorStateClass.TOTAL_INCREASING,
82  suggested_display_precision=0,
83  ),
85  key=ROUTER_IP,
86  translation_key="external_ip",
87  entity_category=EntityCategory.DIAGNOSTIC,
88  ),
90  key=ROUTER_UPTIME,
91  translation_key="uptime",
92  device_class=SensorDeviceClass.DURATION,
93  native_unit_of_measurement=UnitOfTime.SECONDS,
94  entity_registry_enabled_default=False,
95  entity_category=EntityCategory.DIAGNOSTIC,
96  suggested_display_precision=0,
97  ),
99  key=WAN_STATUS,
100  translation_key="wan_status",
101  entity_category=EntityCategory.DIAGNOSTIC,
102  entity_registry_enabled_default=False,
103  ),
105  key=PORT_MAPPING_NUMBER_OF_ENTRIES_IPV4,
106  translation_key="port_mapping_number_of_entries_ipv4",
107  entity_category=EntityCategory.DIAGNOSTIC,
108  entity_registry_enabled_default=False,
109  ),
111  key=BYTES_RECEIVED,
112  translation_key="download_speed",
113  value_key=KIBIBYTES_PER_SEC_RECEIVED,
114  unique_id="KiB/sec_received",
115  device_class=SensorDeviceClass.DATA_RATE,
116  native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
117  state_class=SensorStateClass.MEASUREMENT,
118  suggested_display_precision=1,
119  ),
121  key=BYTES_SENT,
122  translation_key="upload_speed",
123  value_key=KIBIBYTES_PER_SEC_SENT,
124  unique_id="KiB/sec_sent",
125  device_class=SensorDeviceClass.DATA_RATE,
126  native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
127  state_class=SensorStateClass.MEASUREMENT,
128  suggested_display_precision=1,
129  ),
131  key=PACKETS_RECEIVED,
132  translation_key="packet_download_speed",
133  value_key=PACKETS_PER_SEC_RECEIVED,
134  unique_id="packets/sec_received",
135  native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
136  entity_registry_enabled_default=False,
137  state_class=SensorStateClass.MEASUREMENT,
138  suggested_display_precision=1,
139  ),
141  key=PACKETS_SENT,
142  translation_key="packet_upload_speed",
143  value_key=PACKETS_PER_SEC_SENT,
144  unique_id="packets/sec_sent",
145  native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
146  entity_registry_enabled_default=False,
147  state_class=SensorStateClass.MEASUREMENT,
148  suggested_display_precision=1,
149  ),
150 )
151 
152 
154  hass: HomeAssistant,
155  config_entry: UpnpConfigEntry,
156  async_add_entities: AddEntitiesCallback,
157 ) -> None:
158  """Set up the UPnP/IGD sensors."""
159  coordinator = config_entry.runtime_data
160 
161  entities: list[UpnpSensor] = [
162  UpnpSensor(
163  coordinator=coordinator,
164  entity_description=entity_description,
165  )
166  for entity_description in SENSOR_DESCRIPTIONS
167  if coordinator.data.get(entity_description.key) is not None
168  ]
169 
170  async_add_entities(entities)
171  LOGGER.debug("Added sensor entities: %s", entities)
172 
173 
175  """Base class for UPnP/IGD sensors."""
176 
177  entity_description: UpnpSensorEntityDescription
178 
179  @property
180  def native_value(self) -> str | datetime | int | float | None:
181  """Return the state of the device."""
182  if (key := self.entity_descriptionentity_description.value_key) is None:
183  return None
184  return self.coordinator.data[key]
185 
186  async def async_added_to_hass(self) -> None:
187  """Subscribe to updates."""
188  await super().async_added_to_hass()
189 
190  # Register self at coordinator.
191  key = self.entity_descriptionentity_description.key
192  entity_id = self.entity_identity_id
193  unregister = self.coordinator.register_entity(key, entity_id)
194  self.async_on_removeasync_on_remove(unregister)
Callable[[], None] register_entity(self, str key, str entity_id)
Definition: coordinator.py:41
str|datetime|int|float|None native_value(self)
Definition: sensor.py:180
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, UpnpConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:157