Fix upnp add_entities (#55904)

* Fix upnp add_entities

* Remove nesting level
This commit is contained in:
Marc Mueller 2021-09-07 22:27:03 +02:00 committed by GitHub
parent d705b35ea1
commit 42f586c585
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 98 deletions

View File

@ -31,6 +31,3 @@ DEFAULT_SCAN_INTERVAL = timedelta(seconds=30).total_seconds()
ST_IGD_V1 = "urn:schemas-upnp-org:device:InternetGatewayDevice:1" ST_IGD_V1 = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
ST_IGD_V2 = "urn:schemas-upnp-org:device:InternetGatewayDevice:2" ST_IGD_V2 = "urn:schemas-upnp-org:device:InternetGatewayDevice:2"
SSDP_SEARCH_TIMEOUT = 4 SSDP_SEARCH_TIMEOUT = 4
RAW_SENSOR = "raw_sensor"
DERIVED_SENSOR = "derived_sensor"

View File

@ -13,21 +13,17 @@ from .const import (
BYTES_SENT, BYTES_SENT,
DATA_PACKETS, DATA_PACKETS,
DATA_RATE_PACKETS_PER_SECOND, DATA_RATE_PACKETS_PER_SECOND,
DERIVED_SENSOR,
DOMAIN, DOMAIN,
KIBIBYTE, KIBIBYTE,
LOGGER,
PACKETS_RECEIVED, PACKETS_RECEIVED,
PACKETS_SENT, PACKETS_SENT,
RAW_SENSOR,
ROUTER_IP, ROUTER_IP,
ROUTER_UPTIME, ROUTER_UPTIME,
TIMESTAMP, TIMESTAMP,
WAN_STATUS, WAN_STATUS,
) )
SENSOR_ENTITY_DESCRIPTIONS: dict[str, tuple[UpnpSensorEntityDescription, ...]] = { RAW_SENSORS: tuple[UpnpSensorEntityDescription, ...] = (
RAW_SENSOR: (
UpnpSensorEntityDescription( UpnpSensorEntityDescription(
key=BYTES_RECEIVED, key=BYTES_RECEIVED,
name=f"{DATA_BYTES} received", name=f"{DATA_BYTES} received",
@ -74,8 +70,9 @@ SENSOR_ENTITY_DESCRIPTIONS: dict[str, tuple[UpnpSensorEntityDescription, ...]] =
name="wan status", name="wan status",
icon="mdi:server-network", icon="mdi:server-network",
), ),
), )
DERIVED_SENSOR: (
DERIVED_SENSORS: tuple[UpnpSensorEntityDescription, ...] = (
UpnpSensorEntityDescription( UpnpSensorEntityDescription(
key="KiB/sec_received", key="KiB/sec_received",
name=f"{DATA_RATE_KIBIBYTES_PER_SECOND} received", name=f"{DATA_RATE_KIBIBYTES_PER_SECOND} received",
@ -104,8 +101,7 @@ SENSOR_ENTITY_DESCRIPTIONS: dict[str, tuple[UpnpSensorEntityDescription, ...]] =
native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND, native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
format=".1f", format=".1f",
), ),
), )
}
async def async_setup_entry( async def async_setup_entry(
@ -116,25 +112,23 @@ async def async_setup_entry(
"""Set up the UPnP/IGD sensors.""" """Set up the UPnP/IGD sensors."""
coordinator = hass.data[DOMAIN][config_entry.entry_id] coordinator = hass.data[DOMAIN][config_entry.entry_id]
LOGGER.debug("Adding sensors") entities: list[UpnpSensor] = [
entities = []
entities.append(
RawUpnpSensor( RawUpnpSensor(
coordinator=coordinator, coordinator=coordinator,
entity_description=entity_description, entity_description=entity_description,
) )
for entity_description in SENSOR_ENTITY_DESCRIPTIONS[RAW_SENSOR] for entity_description in RAW_SENSORS
if coordinator.data.get(entity_description.key) is not None if coordinator.data.get(entity_description.key) is not None
) ]
entities.extend(
entities.append( [
DerivedUpnpSensor( DerivedUpnpSensor(
coordinator=coordinator, coordinator=coordinator,
entity_description=entity_description, entity_description=entity_description,
) )
for entity_description in SENSOR_ENTITY_DESCRIPTIONS[DERIVED_SENSOR] for entity_description in DERIVED_SENSORS
if coordinator.data.get(entity_description.key) is not None if coordinator.data.get(entity_description.key) is not None
]
) )
async_add_entities(entities) async_add_entities(entities)