Fix upnp creating derived sensors (#57436)

This commit is contained in:
Steven Looman 2021-10-11 09:35:26 +02:00 committed by GitHub
parent ba0196137e
commit 3825f80a2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 7 deletions

View File

@ -198,6 +198,7 @@ class UpnpBinarySensorEntityDescription(BinarySensorEntityDescription):
"""A class that describes UPnP entities."""
format: str = "s"
unique_id: str | None = None
@dataclass
@ -205,6 +206,7 @@ class UpnpSensorEntityDescription(SensorEntityDescription):
"""A class that describes a sensor UPnP entities."""
format: str = "s"
unique_id: str | None = None
class UpnpDataUpdateCoordinator(DataUpdateCoordinator):
@ -250,7 +252,7 @@ class UpnpEntity(CoordinatorEntity):
self._device = coordinator.device
self.entity_description = entity_description
self._attr_name = f"{coordinator.device.name} {entity_description.name}"
self._attr_unique_id = f"{coordinator.device.udn}_{entity_description.key}"
self._attr_unique_id = f"{coordinator.device.udn}_{entity_description.unique_id or entity_description.key}"
self._attr_device_info = {
"connections": {(dr.CONNECTION_UPNP, coordinator.device.udn)},
"name": coordinator.device.name,

View File

@ -30,14 +30,16 @@ async def async_setup_entry(
LOGGER.debug("Adding binary sensor")
async_add_entities(
entities = [
UpnpStatusBinarySensor(
coordinator=coordinator,
entity_description=entity_description,
)
for entity_description in BINARYSENSOR_ENTITY_DESCRIPTIONS
if coordinator.data.get(entity_description.key) is not None
)
]
LOGGER.debug("Adding entities: %s", entities)
async_add_entities(entities)
class UpnpStatusBinarySensor(UpnpEntity, BinarySensorEntity):

View File

@ -15,6 +15,7 @@ from .const import (
DATA_RATE_PACKETS_PER_SECOND,
DOMAIN,
KIBIBYTE,
LOGGER,
PACKETS_RECEIVED,
PACKETS_SENT,
ROUTER_IP,
@ -74,28 +75,32 @@ RAW_SENSORS: tuple[UpnpSensorEntityDescription, ...] = (
DERIVED_SENSORS: tuple[UpnpSensorEntityDescription, ...] = (
UpnpSensorEntityDescription(
key="KiB/sec_received",
key=BYTES_RECEIVED,
unique_id="KiB/sec_received",
name=f"{DATA_RATE_KIBIBYTES_PER_SECOND} received",
icon="mdi:server-network",
native_unit_of_measurement=DATA_RATE_KIBIBYTES_PER_SECOND,
format=".1f",
),
UpnpSensorEntityDescription(
key="KiB/sent",
key=BYTES_SENT,
unique_id="KiB/sent",
name=f"{DATA_RATE_KIBIBYTES_PER_SECOND} sent",
icon="mdi:server-network",
native_unit_of_measurement=DATA_RATE_KIBIBYTES_PER_SECOND,
format=".1f",
),
UpnpSensorEntityDescription(
key="packets/sec_received",
key=PACKETS_RECEIVED,
unique_id="packets/sec_received",
name=f"{DATA_RATE_PACKETS_PER_SECOND} received",
icon="mdi:server-network",
native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
format=".1f",
),
UpnpSensorEntityDescription(
key="packets/sent",
key=PACKETS_SENT,
unique_id="packets/sent",
name=f"{DATA_RATE_PACKETS_PER_SECOND} sent",
icon="mdi:server-network",
native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
@ -131,6 +136,7 @@ async def async_setup_entry(
]
)
LOGGER.debug("Adding entities: %s", entities)
async_add_entities(entities)