Use default rounding/presentation mechanism for upnp (#89954)

This commit is contained in:
Steven Looman 2023-03-20 15:44:05 +01:00 committed by GitHub
parent 9949ca13aa
commit 9a5ceb9ef8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 20 deletions

View File

@ -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()

View File

@ -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()

View File

@ -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

View File

@ -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]