1 """Support for Netgear routers."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import date, datetime
8 from decimal
import Decimal
15 SensorEntityDescription,
35 KEY_COORDINATOR_SPEED,
36 KEY_COORDINATOR_TRAFFIC,
40 from .entity
import NetgearDeviceEntity, NetgearRouterCoordinatorEntity
41 from .router
import NetgearRouter
43 _LOGGER = logging.getLogger(__name__)
48 translation_key=
"link_type",
49 entity_category=EntityCategory.DIAGNOSTIC,
53 translation_key=
"link_rate",
54 native_unit_of_measurement=
"Mbps",
55 entity_category=EntityCategory.DIAGNOSTIC,
59 translation_key=
"signal_strength",
60 native_unit_of_measurement=PERCENTAGE,
61 entity_category=EntityCategory.DIAGNOSTIC,
65 translation_key=
"ssid",
66 entity_category=EntityCategory.DIAGNOSTIC,
70 translation_key=
"access_point_mac",
71 entity_category=EntityCategory.DIAGNOSTIC,
76 @dataclass(frozen=True)
78 """Class describing Netgear sensor entities."""
80 value: Callable =
lambda data: data
84 SENSOR_TRAFFIC_TYPES = [
87 translation_key=
"upload_today",
88 entity_category=EntityCategory.DIAGNOSTIC,
89 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
90 device_class=SensorDeviceClass.DATA_SIZE,
93 key=
"NewTodayDownload",
94 translation_key=
"download_today",
95 entity_category=EntityCategory.DIAGNOSTIC,
96 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
97 device_class=SensorDeviceClass.DATA_SIZE,
100 key=
"NewYesterdayUpload",
101 translation_key=
"upload_yesterday",
102 entity_category=EntityCategory.DIAGNOSTIC,
103 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
104 device_class=SensorDeviceClass.DATA_SIZE,
107 key=
"NewYesterdayDownload",
108 translation_key=
"download_yesterday",
109 entity_category=EntityCategory.DIAGNOSTIC,
110 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
111 device_class=SensorDeviceClass.DATA_SIZE,
115 translation_key=
"upload_week",
116 entity_category=EntityCategory.DIAGNOSTIC,
117 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
118 device_class=SensorDeviceClass.DATA_SIZE,
120 value=
lambda data: data[0],
124 translation_key=
"upload_week_average",
125 entity_category=EntityCategory.DIAGNOSTIC,
126 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
127 device_class=SensorDeviceClass.DATA_SIZE,
129 value=
lambda data: data[1],
132 key=
"NewWeekDownload",
133 translation_key=
"download_week",
134 entity_category=EntityCategory.DIAGNOSTIC,
135 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
136 device_class=SensorDeviceClass.DATA_SIZE,
138 value=
lambda data: data[0],
141 key=
"NewWeekDownload",
142 translation_key=
"download_week_average",
143 entity_category=EntityCategory.DIAGNOSTIC,
144 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
145 device_class=SensorDeviceClass.DATA_SIZE,
147 value=
lambda data: data[1],
150 key=
"NewMonthUpload",
151 translation_key=
"upload_month",
152 entity_category=EntityCategory.DIAGNOSTIC,
153 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
154 device_class=SensorDeviceClass.DATA_SIZE,
156 value=
lambda data: data[0],
159 key=
"NewMonthUpload",
160 translation_key=
"upload_month_average",
161 entity_category=EntityCategory.DIAGNOSTIC,
162 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
163 device_class=SensorDeviceClass.DATA_SIZE,
165 value=
lambda data: data[1],
168 key=
"NewMonthDownload",
169 translation_key=
"download_month",
170 entity_category=EntityCategory.DIAGNOSTIC,
171 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
172 device_class=SensorDeviceClass.DATA_SIZE,
174 value=
lambda data: data[0],
177 key=
"NewMonthDownload",
178 translation_key=
"download_month_average",
179 entity_category=EntityCategory.DIAGNOSTIC,
180 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
181 device_class=SensorDeviceClass.DATA_SIZE,
183 value=
lambda data: data[1],
186 key=
"NewLastMonthUpload",
187 translation_key=
"upload_last_month",
188 entity_category=EntityCategory.DIAGNOSTIC,
189 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
190 device_class=SensorDeviceClass.DATA_SIZE,
192 value=
lambda data: data[0],
195 key=
"NewLastMonthUpload",
196 translation_key=
"upload_last_month_average",
197 entity_category=EntityCategory.DIAGNOSTIC,
198 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
199 device_class=SensorDeviceClass.DATA_SIZE,
201 value=
lambda data: data[1],
204 key=
"NewLastMonthDownload",
205 translation_key=
"download_last_month",
206 entity_category=EntityCategory.DIAGNOSTIC,
207 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
208 device_class=SensorDeviceClass.DATA_SIZE,
210 value=
lambda data: data[0],
213 key=
"NewLastMonthDownload",
214 translation_key=
"download_last_month_average",
215 entity_category=EntityCategory.DIAGNOSTIC,
216 native_unit_of_measurement=UnitOfInformation.MEGABYTES,
217 device_class=SensorDeviceClass.DATA_SIZE,
219 value=
lambda data: data[1],
223 SENSOR_SPEED_TYPES = [
225 key=
"NewOOKLAUplinkBandwidth",
226 translation_key=
"uplink_bandwidth",
227 entity_category=EntityCategory.DIAGNOSTIC,
228 native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
229 device_class=SensorDeviceClass.DATA_RATE,
232 key=
"NewOOKLADownlinkBandwidth",
233 translation_key=
"downlink_bandwidth",
234 entity_category=EntityCategory.DIAGNOSTIC,
235 native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
236 device_class=SensorDeviceClass.DATA_RATE,
240 translation_key=
"average_ping",
241 entity_category=EntityCategory.DIAGNOSTIC,
242 native_unit_of_measurement=UnitOfTime.MILLISECONDS,
246 SENSOR_UTILIZATION = [
248 key=
"NewCPUUtilization",
249 translation_key=
"cpu_utilization",
250 entity_category=EntityCategory.DIAGNOSTIC,
251 native_unit_of_measurement=PERCENTAGE,
252 state_class=SensorStateClass.MEASUREMENT,
255 key=
"NewMemoryUtilization",
256 translation_key=
"memory_utilization",
257 entity_category=EntityCategory.DIAGNOSTIC,
258 native_unit_of_measurement=PERCENTAGE,
259 state_class=SensorStateClass.MEASUREMENT,
263 SENSOR_LINK_TYPES = [
265 key=
"NewEthernetLinkStatus",
266 translation_key=
"ethernet_link_status",
267 entity_category=EntityCategory.DIAGNOSTIC,
273 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
275 """Set up device tracker for Netgear component."""
276 router = hass.data[DOMAIN][entry.entry_id][KEY_ROUTER]
277 coordinator = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR]
278 coordinator_traffic = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR_TRAFFIC]
279 coordinator_speed = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR_SPEED]
280 coordinator_utilization = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR_UTIL]
281 coordinator_link = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR_LINK]
285 for (coordinator, descriptions)
in (
286 (coordinator_traffic, SENSOR_TRAFFIC_TYPES),
287 (coordinator_speed, SENSOR_SPEED_TYPES),
288 (coordinator_utilization, SENSOR_UTILIZATION),
289 (coordinator_link, SENSOR_LINK_TYPES),
291 for description
in descriptions
296 sensors = [
"type",
"link_rate",
"signal"]
297 if router.method_version == 2:
298 sensors.extend([
"ssid",
"conn_ap_mac"])
301 def new_device_callback() -> None:
302 """Add new devices if needed."""
303 if not coordinator.data:
306 new_entities: list[NetgearSensorEntity] = []
308 for mac, device
in router.devices.items():
314 for attribute
in sensors
320 entry.async_on_unload(coordinator.async_add_listener(new_device_callback))
322 coordinator.data =
True
323 new_device_callback()
327 """Representation of a device connected to a Netgear router."""
329 _attr_entity_registry_enabled_default =
False
333 coordinator: DataUpdateCoordinator,
334 router: NetgearRouter,
338 """Initialize a Netgear device."""
339 super().
__init__(coordinator, router, device)
343 self.
_state_state = device.get(attribute)
347 """Return the state of the sensor."""
352 """Update the Netgear device."""
360 """Representation of a device connected to a Netgear router."""
362 _attr_entity_registry_enabled_default =
False
363 entity_description: NetgearSensorEntityDescription
367 coordinator: DataUpdateCoordinator,
368 router: NetgearRouter,
369 entity_description: NetgearSensorEntityDescription,
371 """Initialize a Netgear device."""
372 super().
__init__(coordinator, router)
376 self.
_value_value: StateType | date | datetime | Decimal =
None
381 """Return the state of the sensor."""
385 """Handle entity which will be added."""
387 if self.coordinator.data
is None:
389 if sensor_data
is not None:
390 self.
_value_value = sensor_data.native_value
392 await self.coordinator.async_request_refresh()
396 """Update the Netgear device."""
397 if self.coordinator.data
is None:
404 "key '%s' not in Netgear router response '%s'",
None async_update_device(self)
None async_added_to_hass(self)
None __init__(self, DataUpdateCoordinator coordinator, NetgearRouter router, NetgearSensorEntityDescription entity_description)
None async_update_device(self)
None __init__(self, DataUpdateCoordinator coordinator, NetgearRouter router, dict device, str attribute)
None async_update_device(self)
SensorExtraStoredData|None async_get_last_sensor_data(self)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)