1 """Support for UPnP/IGD Sensors."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
6 from datetime
import datetime
11 SensorEntityDescription,
23 from .
import UpnpConfigEntry
28 DATA_RATE_PACKETS_PER_SECOND,
29 KIBIBYTES_PER_SEC_RECEIVED,
30 KIBIBYTES_PER_SEC_SENT,
32 PACKETS_PER_SEC_RECEIVED,
36 PORT_MAPPING_NUMBER_OF_ENTRIES_IPV4,
41 from .entity
import UpnpEntity, UpnpEntityDescription
44 @dataclass(frozen=True)
46 """A class that describes a sensor UPnP entities."""
49 SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
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,
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,
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,
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,
86 translation_key=
"external_ip",
87 entity_category=EntityCategory.DIAGNOSTIC,
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,
100 translation_key=
"wan_status",
101 entity_category=EntityCategory.DIAGNOSTIC,
102 entity_registry_enabled_default=
False,
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,
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,
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,
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,
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,
155 config_entry: UpnpConfigEntry,
156 async_add_entities: AddEntitiesCallback,
158 """Set up the UPnP/IGD sensors."""
159 coordinator = config_entry.runtime_data
161 entities: list[UpnpSensor] = [
163 coordinator=coordinator,
164 entity_description=entity_description,
166 for entity_description
in SENSOR_DESCRIPTIONS
167 if coordinator.data.get(entity_description.key)
is not None
171 LOGGER.debug(
"Added sensor entities: %s", entities)
175 """Base class for UPnP/IGD sensors."""
177 entity_description: UpnpSensorEntityDescription
181 """Return the state of the device."""
184 return self.coordinator.data[key]
187 """Subscribe to updates."""
Callable[[], None] register_entity(self, str key, str entity_id)
None async_added_to_hass(self)
str|datetime|int|float|None native_value(self)
None async_on_remove(self, CALLBACK_TYPE func)
None async_setup_entry(HomeAssistant hass, UpnpConfigEntry config_entry, AddEntitiesCallback async_add_entities)