From 9a5ceb9ef832f1ca4e95fc00708ae24db3331707 Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Mon, 20 Mar 2023 15:44:05 +0100 Subject: [PATCH] Use default rounding/presentation mechanism for upnp (#89954) --- homeassistant/components/upnp/coordinator.py | 11 +++++---- homeassistant/components/upnp/device.py | 7 ++++-- homeassistant/components/upnp/entity.py | 1 - homeassistant/components/upnp/sensor.py | 25 ++++++++++---------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/upnp/coordinator.py b/homeassistant/components/upnp/coordinator.py index 2820a584632..72e14ecc4ff 100644 --- a/homeassistant/components/upnp/coordinator.py +++ b/homeassistant/components/upnp/coordinator.py @@ -1,7 +1,6 @@ """UPnP/IGD coordinator.""" -from datetime import timedelta -from typing import Any +from datetime import datetime, timedelta from async_upnp_client.exceptions import UpnpCommunicationError @@ -13,7 +12,9 @@ from .const import LOGGER from .device import Device -class UpnpDataUpdateCoordinator(DataUpdateCoordinator): +class UpnpDataUpdateCoordinator( + DataUpdateCoordinator[dict[str, str | datetime | int | float | None]] +): """Define an object to update data from UPNP device.""" def __init__( @@ -34,7 +35,9 @@ class UpnpDataUpdateCoordinator(DataUpdateCoordinator): update_interval=update_interval, ) - async def _async_update_data(self) -> dict[str, Any]: + async def _async_update_data( + self, + ) -> dict[str, str | datetime | int | float | None]: """Update data.""" try: return await self.device.async_get_data() diff --git a/homeassistant/components/upnp/device.py b/homeassistant/components/upnp/device.py index ed06a9eb363..b62edbf9bc2 100644 --- a/homeassistant/components/upnp/device.py +++ b/homeassistant/components/upnp/device.py @@ -1,6 +1,7 @@ """Home Assistant representation of an UPnP/IGD.""" from __future__ import annotations +from datetime import datetime from functools import partial from ipaddress import ip_address from typing import Any @@ -68,7 +69,9 @@ class Device: """Initialize UPnP/IGD device.""" self.hass = hass self._igd_device = igd_device - self.coordinator: DataUpdateCoordinator | None = None + self.coordinator: DataUpdateCoordinator[ + dict[str, str | datetime | int | float | None] + ] | None = None self.original_udn: str | None = None async def async_get_mac_address(self) -> str | None: @@ -134,7 +137,7 @@ class Device: """Get string representation.""" return f"IGD Device: {self.name}/{self.udn}::{self.device_type}" - async def async_get_data(self) -> dict[str, Any]: + async def async_get_data(self) -> dict[str, str | datetime | int | float | None]: """Get all data from device.""" _LOGGER.debug("Getting data for device: %s", self) igd_state = await self._igd_device.async_get_traffic_and_status_data() diff --git a/homeassistant/components/upnp/entity.py b/homeassistant/components/upnp/entity.py index b787018adcc..cd39609d9d5 100644 --- a/homeassistant/components/upnp/entity.py +++ b/homeassistant/components/upnp/entity.py @@ -13,7 +13,6 @@ from .coordinator import UpnpDataUpdateCoordinator class UpnpEntityDescription(EntityDescription): """UPnP entity description.""" - format: str = "s" unique_id: str | None = None value_key: str | None = None diff --git a/homeassistant/components/upnp/sensor.py b/homeassistant/components/upnp/sensor.py index 1a374714be8..6f0fe340f30 100644 --- a/homeassistant/components/upnp/sensor.py +++ b/homeassistant/components/upnp/sensor.py @@ -2,6 +2,7 @@ from __future__ import annotations from dataclasses import dataclass +from datetime import datetime from homeassistant.components.sensor import ( SensorDeviceClass, @@ -52,9 +53,9 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = ( icon="mdi:server-network", device_class=SensorDeviceClass.DATA_SIZE, native_unit_of_measurement=UnitOfInformation.BYTES, - format="d", entity_registry_enabled_default=False, state_class=SensorStateClass.TOTAL_INCREASING, + suggested_display_precision=0, ), UpnpSensorEntityDescription( key=BYTES_SENT, @@ -62,27 +63,27 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = ( icon="mdi:server-network", device_class=SensorDeviceClass.DATA_SIZE, native_unit_of_measurement=UnitOfInformation.BYTES, - format="d", entity_registry_enabled_default=False, state_class=SensorStateClass.TOTAL_INCREASING, + suggested_display_precision=0, ), UpnpSensorEntityDescription( key=PACKETS_RECEIVED, name=f"{DATA_PACKETS} received", icon="mdi:server-network", native_unit_of_measurement=DATA_PACKETS, - format="d", entity_registry_enabled_default=False, state_class=SensorStateClass.TOTAL_INCREASING, + suggested_display_precision=0, ), UpnpSensorEntityDescription( key=PACKETS_SENT, name=f"{DATA_PACKETS} sent", icon="mdi:server-network", native_unit_of_measurement=DATA_PACKETS, - format="d", entity_registry_enabled_default=False, state_class=SensorStateClass.TOTAL_INCREASING, + suggested_display_precision=0, ), UpnpSensorEntityDescription( key=ROUTER_IP, @@ -96,8 +97,8 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = ( icon="mdi:server-network", native_unit_of_measurement=UnitOfTime.SECONDS, entity_registry_enabled_default=False, - format="d", entity_category=EntityCategory.DIAGNOSTIC, + suggested_display_precision=0, ), UpnpSensorEntityDescription( key=WAN_STATUS, @@ -114,8 +115,8 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = ( icon="mdi:server-network", device_class=SensorDeviceClass.DATA_RATE, native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND, - format=".1f", state_class=SensorStateClass.MEASUREMENT, + suggested_display_precision=1, ), UpnpSensorEntityDescription( key=BYTES_SENT, @@ -125,8 +126,8 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = ( icon="mdi:server-network", device_class=SensorDeviceClass.DATA_RATE, native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND, - format=".1f", state_class=SensorStateClass.MEASUREMENT, + suggested_display_precision=1, ), UpnpSensorEntityDescription( key=PACKETS_RECEIVED, @@ -135,9 +136,9 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = ( name=f"{DATA_RATE_PACKETS_PER_SECOND} received", icon="mdi:server-network", native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND, - format=".1f", entity_registry_enabled_default=False, state_class=SensorStateClass.MEASUREMENT, + suggested_display_precision=1, ), UpnpSensorEntityDescription( key=PACKETS_SENT, @@ -146,9 +147,9 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = ( name=f"{DATA_RATE_PACKETS_PER_SECOND} sent", icon="mdi:server-network", native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND, - format=".1f", entity_registry_enabled_default=False, state_class=SensorStateClass.MEASUREMENT, + suggested_display_precision=1, ), ) @@ -180,10 +181,8 @@ class UpnpSensor(UpnpEntity, SensorEntity): entity_description: UpnpSensorEntityDescription @property - def native_value(self) -> str | None: + def native_value(self) -> str | datetime | int | float | None: """Return the state of the device.""" if (key := self.entity_description.value_key) is None: return None - if (value := self.coordinator.data[key]) is None: - return None - return format(value, self.entity_description.format) + return self.coordinator.data[key]