mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Use default rounding/presentation mechanism for upnp (#89954)
This commit is contained in:
parent
9949ca13aa
commit
9a5ceb9ef8
@ -1,7 +1,6 @@
|
|||||||
"""UPnP/IGD coordinator."""
|
"""UPnP/IGD coordinator."""
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from async_upnp_client.exceptions import UpnpCommunicationError
|
from async_upnp_client.exceptions import UpnpCommunicationError
|
||||||
|
|
||||||
@ -13,7 +12,9 @@ from .const import LOGGER
|
|||||||
from .device import Device
|
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."""
|
"""Define an object to update data from UPNP device."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -34,7 +35,9 @@ class UpnpDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
update_interval=update_interval,
|
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."""
|
"""Update data."""
|
||||||
try:
|
try:
|
||||||
return await self.device.async_get_data()
|
return await self.device.async_get_data()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Home Assistant representation of an UPnP/IGD."""
|
"""Home Assistant representation of an UPnP/IGD."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from ipaddress import ip_address
|
from ipaddress import ip_address
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@ -68,7 +69,9 @@ class Device:
|
|||||||
"""Initialize UPnP/IGD device."""
|
"""Initialize UPnP/IGD device."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._igd_device = igd_device
|
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
|
self.original_udn: str | None = None
|
||||||
|
|
||||||
async def async_get_mac_address(self) -> str | None:
|
async def async_get_mac_address(self) -> str | None:
|
||||||
@ -134,7 +137,7 @@ class Device:
|
|||||||
"""Get string representation."""
|
"""Get string representation."""
|
||||||
return f"IGD Device: {self.name}/{self.udn}::{self.device_type}"
|
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."""
|
"""Get all data from device."""
|
||||||
_LOGGER.debug("Getting data for device: %s", self)
|
_LOGGER.debug("Getting data for device: %s", self)
|
||||||
igd_state = await self._igd_device.async_get_traffic_and_status_data()
|
igd_state = await self._igd_device.async_get_traffic_and_status_data()
|
||||||
|
@ -13,7 +13,6 @@ from .coordinator import UpnpDataUpdateCoordinator
|
|||||||
class UpnpEntityDescription(EntityDescription):
|
class UpnpEntityDescription(EntityDescription):
|
||||||
"""UPnP entity description."""
|
"""UPnP entity description."""
|
||||||
|
|
||||||
format: str = "s"
|
|
||||||
unique_id: str | None = None
|
unique_id: str | None = None
|
||||||
value_key: str | None = None
|
value_key: str | None = None
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
@ -52,9 +53,9 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
|
|||||||
icon="mdi:server-network",
|
icon="mdi:server-network",
|
||||||
device_class=SensorDeviceClass.DATA_SIZE,
|
device_class=SensorDeviceClass.DATA_SIZE,
|
||||||
native_unit_of_measurement=UnitOfInformation.BYTES,
|
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||||
format="d",
|
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
|
suggested_display_precision=0,
|
||||||
),
|
),
|
||||||
UpnpSensorEntityDescription(
|
UpnpSensorEntityDescription(
|
||||||
key=BYTES_SENT,
|
key=BYTES_SENT,
|
||||||
@ -62,27 +63,27 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
|
|||||||
icon="mdi:server-network",
|
icon="mdi:server-network",
|
||||||
device_class=SensorDeviceClass.DATA_SIZE,
|
device_class=SensorDeviceClass.DATA_SIZE,
|
||||||
native_unit_of_measurement=UnitOfInformation.BYTES,
|
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||||
format="d",
|
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
|
suggested_display_precision=0,
|
||||||
),
|
),
|
||||||
UpnpSensorEntityDescription(
|
UpnpSensorEntityDescription(
|
||||||
key=PACKETS_RECEIVED,
|
key=PACKETS_RECEIVED,
|
||||||
name=f"{DATA_PACKETS} received",
|
name=f"{DATA_PACKETS} received",
|
||||||
icon="mdi:server-network",
|
icon="mdi:server-network",
|
||||||
native_unit_of_measurement=DATA_PACKETS,
|
native_unit_of_measurement=DATA_PACKETS,
|
||||||
format="d",
|
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
|
suggested_display_precision=0,
|
||||||
),
|
),
|
||||||
UpnpSensorEntityDescription(
|
UpnpSensorEntityDescription(
|
||||||
key=PACKETS_SENT,
|
key=PACKETS_SENT,
|
||||||
name=f"{DATA_PACKETS} sent",
|
name=f"{DATA_PACKETS} sent",
|
||||||
icon="mdi:server-network",
|
icon="mdi:server-network",
|
||||||
native_unit_of_measurement=DATA_PACKETS,
|
native_unit_of_measurement=DATA_PACKETS,
|
||||||
format="d",
|
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
|
suggested_display_precision=0,
|
||||||
),
|
),
|
||||||
UpnpSensorEntityDescription(
|
UpnpSensorEntityDescription(
|
||||||
key=ROUTER_IP,
|
key=ROUTER_IP,
|
||||||
@ -96,8 +97,8 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
|
|||||||
icon="mdi:server-network",
|
icon="mdi:server-network",
|
||||||
native_unit_of_measurement=UnitOfTime.SECONDS,
|
native_unit_of_measurement=UnitOfTime.SECONDS,
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
format="d",
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
suggested_display_precision=0,
|
||||||
),
|
),
|
||||||
UpnpSensorEntityDescription(
|
UpnpSensorEntityDescription(
|
||||||
key=WAN_STATUS,
|
key=WAN_STATUS,
|
||||||
@ -114,8 +115,8 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
|
|||||||
icon="mdi:server-network",
|
icon="mdi:server-network",
|
||||||
device_class=SensorDeviceClass.DATA_RATE,
|
device_class=SensorDeviceClass.DATA_RATE,
|
||||||
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
|
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
|
||||||
format=".1f",
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=1,
|
||||||
),
|
),
|
||||||
UpnpSensorEntityDescription(
|
UpnpSensorEntityDescription(
|
||||||
key=BYTES_SENT,
|
key=BYTES_SENT,
|
||||||
@ -125,8 +126,8 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
|
|||||||
icon="mdi:server-network",
|
icon="mdi:server-network",
|
||||||
device_class=SensorDeviceClass.DATA_RATE,
|
device_class=SensorDeviceClass.DATA_RATE,
|
||||||
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
|
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
|
||||||
format=".1f",
|
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=1,
|
||||||
),
|
),
|
||||||
UpnpSensorEntityDescription(
|
UpnpSensorEntityDescription(
|
||||||
key=PACKETS_RECEIVED,
|
key=PACKETS_RECEIVED,
|
||||||
@ -135,9 +136,9 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
|
|||||||
name=f"{DATA_RATE_PACKETS_PER_SECOND} received",
|
name=f"{DATA_RATE_PACKETS_PER_SECOND} received",
|
||||||
icon="mdi:server-network",
|
icon="mdi:server-network",
|
||||||
native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
|
native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
|
||||||
format=".1f",
|
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=1,
|
||||||
),
|
),
|
||||||
UpnpSensorEntityDescription(
|
UpnpSensorEntityDescription(
|
||||||
key=PACKETS_SENT,
|
key=PACKETS_SENT,
|
||||||
@ -146,9 +147,9 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
|
|||||||
name=f"{DATA_RATE_PACKETS_PER_SECOND} sent",
|
name=f"{DATA_RATE_PACKETS_PER_SECOND} sent",
|
||||||
icon="mdi:server-network",
|
icon="mdi:server-network",
|
||||||
native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
|
native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
|
||||||
format=".1f",
|
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
suggested_display_precision=1,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -180,10 +181,8 @@ class UpnpSensor(UpnpEntity, SensorEntity):
|
|||||||
entity_description: UpnpSensorEntityDescription
|
entity_description: UpnpSensorEntityDescription
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> str | None:
|
def native_value(self) -> str | datetime | int | float | None:
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
if (key := self.entity_description.value_key) is None:
|
if (key := self.entity_description.value_key) is None:
|
||||||
return None
|
return None
|
||||||
if (value := self.coordinator.data[key]) is None:
|
return self.coordinator.data[key]
|
||||||
return None
|
|
||||||
return format(value, self.entity_description.format)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user