Fix upnp add_entities (#55904)
* Fix upnp add_entities * Remove nesting level
This commit is contained in:
parent
d705b35ea1
commit
42f586c585
2 changed files with 89 additions and 98 deletions
|
@ -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"
|
|
||||||
|
|
|
@ -13,99 +13,95 @@ 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",
|
icon="mdi:server-network",
|
||||||
icon="mdi:server-network",
|
native_unit_of_measurement=DATA_BYTES,
|
||||||
native_unit_of_measurement=DATA_BYTES,
|
format="d",
|
||||||
format="d",
|
|
||||||
),
|
|
||||||
UpnpSensorEntityDescription(
|
|
||||||
key=BYTES_SENT,
|
|
||||||
name=f"{DATA_BYTES} sent",
|
|
||||||
icon="mdi:server-network",
|
|
||||||
native_unit_of_measurement=DATA_BYTES,
|
|
||||||
format="d",
|
|
||||||
),
|
|
||||||
UpnpSensorEntityDescription(
|
|
||||||
key=PACKETS_RECEIVED,
|
|
||||||
name=f"{DATA_PACKETS} received",
|
|
||||||
icon="mdi:server-network",
|
|
||||||
native_unit_of_measurement=DATA_PACKETS,
|
|
||||||
format="d",
|
|
||||||
),
|
|
||||||
UpnpSensorEntityDescription(
|
|
||||||
key=PACKETS_SENT,
|
|
||||||
name=f"{DATA_PACKETS} sent",
|
|
||||||
icon="mdi:server-network",
|
|
||||||
native_unit_of_measurement=DATA_PACKETS,
|
|
||||||
format="d",
|
|
||||||
),
|
|
||||||
UpnpSensorEntityDescription(
|
|
||||||
key=ROUTER_IP,
|
|
||||||
name="External IP",
|
|
||||||
icon="mdi:server-network",
|
|
||||||
),
|
|
||||||
UpnpSensorEntityDescription(
|
|
||||||
key=ROUTER_UPTIME,
|
|
||||||
name="Uptime",
|
|
||||||
icon="mdi:server-network",
|
|
||||||
native_unit_of_measurement=TIME_SECONDS,
|
|
||||||
entity_registry_enabled_default=False,
|
|
||||||
format="d",
|
|
||||||
),
|
|
||||||
UpnpSensorEntityDescription(
|
|
||||||
key=WAN_STATUS,
|
|
||||||
name="wan status",
|
|
||||||
icon="mdi:server-network",
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
DERIVED_SENSOR: (
|
UpnpSensorEntityDescription(
|
||||||
UpnpSensorEntityDescription(
|
key=BYTES_SENT,
|
||||||
key="KiB/sec_received",
|
name=f"{DATA_BYTES} sent",
|
||||||
name=f"{DATA_RATE_KIBIBYTES_PER_SECOND} received",
|
icon="mdi:server-network",
|
||||||
icon="mdi:server-network",
|
native_unit_of_measurement=DATA_BYTES,
|
||||||
native_unit_of_measurement=DATA_RATE_KIBIBYTES_PER_SECOND,
|
format="d",
|
||||||
format=".1f",
|
|
||||||
),
|
|
||||||
UpnpSensorEntityDescription(
|
|
||||||
key="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",
|
|
||||||
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",
|
|
||||||
name=f"{DATA_RATE_PACKETS_PER_SECOND} sent",
|
|
||||||
icon="mdi:server-network",
|
|
||||||
native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
|
|
||||||
format=".1f",
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
}
|
UpnpSensorEntityDescription(
|
||||||
|
key=PACKETS_RECEIVED,
|
||||||
|
name=f"{DATA_PACKETS} received",
|
||||||
|
icon="mdi:server-network",
|
||||||
|
native_unit_of_measurement=DATA_PACKETS,
|
||||||
|
format="d",
|
||||||
|
),
|
||||||
|
UpnpSensorEntityDescription(
|
||||||
|
key=PACKETS_SENT,
|
||||||
|
name=f"{DATA_PACKETS} sent",
|
||||||
|
icon="mdi:server-network",
|
||||||
|
native_unit_of_measurement=DATA_PACKETS,
|
||||||
|
format="d",
|
||||||
|
),
|
||||||
|
UpnpSensorEntityDescription(
|
||||||
|
key=ROUTER_IP,
|
||||||
|
name="External IP",
|
||||||
|
icon="mdi:server-network",
|
||||||
|
),
|
||||||
|
UpnpSensorEntityDescription(
|
||||||
|
key=ROUTER_UPTIME,
|
||||||
|
name="Uptime",
|
||||||
|
icon="mdi:server-network",
|
||||||
|
native_unit_of_measurement=TIME_SECONDS,
|
||||||
|
entity_registry_enabled_default=False,
|
||||||
|
format="d",
|
||||||
|
),
|
||||||
|
UpnpSensorEntityDescription(
|
||||||
|
key=WAN_STATUS,
|
||||||
|
name="wan status",
|
||||||
|
icon="mdi:server-network",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
DERIVED_SENSORS: tuple[UpnpSensorEntityDescription, ...] = (
|
||||||
|
UpnpSensorEntityDescription(
|
||||||
|
key="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",
|
||||||
|
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",
|
||||||
|
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",
|
||||||
|
name=f"{DATA_RATE_PACKETS_PER_SECOND} sent",
|
||||||
|
icon="mdi:server-network",
|
||||||
|
native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
|
||||||
|
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
|
|
||||||
)
|
|
||||||
|
|
||||||
entities.append(
|
|
||||||
DerivedUpnpSensor(
|
|
||||||
coordinator=coordinator,
|
|
||||||
entity_description=entity_description,
|
|
||||||
)
|
|
||||||
for entity_description in SENSOR_ENTITY_DESCRIPTIONS[DERIVED_SENSOR]
|
|
||||||
if coordinator.data.get(entity_description.key) is not None
|
if coordinator.data.get(entity_description.key) is not None
|
||||||
|
]
|
||||||
|
entities.extend(
|
||||||
|
[
|
||||||
|
DerivedUpnpSensor(
|
||||||
|
coordinator=coordinator,
|
||||||
|
entity_description=entity_description,
|
||||||
|
)
|
||||||
|
for entity_description in DERIVED_SENSORS
|
||||||
|
if coordinator.data.get(entity_description.key) is not None
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
Loading…
Add table
Reference in a new issue