2019-04-03 15:40:03 +00:00
|
|
|
"""Support for UPnP/IGD Sensors."""
|
2021-03-18 13:43:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-10-26 19:34:44 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
SensorStateClass,
|
|
|
|
)
|
2020-04-10 22:24:03 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-09-03 15:15:28 +00:00
|
|
|
from homeassistant.const import DATA_BYTES, DATA_RATE_KIBIBYTES_PER_SECOND, TIME_SECONDS
|
2021-04-22 14:53:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-08-25 21:45:27 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-04-10 22:24:03 +00:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
BYTES_RECEIVED,
|
|
|
|
BYTES_SENT,
|
|
|
|
DATA_PACKETS,
|
|
|
|
DATA_RATE_PACKETS_PER_SECOND,
|
|
|
|
DOMAIN,
|
2022-10-26 19:34:44 +00:00
|
|
|
KIBIBYTES_PER_SEC_RECEIVED,
|
|
|
|
KIBIBYTES_PER_SEC_SENT,
|
2021-10-11 07:35:26 +00:00
|
|
|
LOGGER,
|
2022-10-26 19:34:44 +00:00
|
|
|
PACKETS_PER_SEC_RECEIVED,
|
|
|
|
PACKETS_PER_SEC_SENT,
|
2020-04-10 22:24:03 +00:00
|
|
|
PACKETS_RECEIVED,
|
|
|
|
PACKETS_SENT,
|
2021-09-03 15:15:28 +00:00
|
|
|
ROUTER_IP,
|
|
|
|
ROUTER_UPTIME,
|
|
|
|
WAN_STATUS,
|
2020-04-10 22:24:03 +00:00
|
|
|
)
|
2022-10-26 19:34:44 +00:00
|
|
|
from .coordinator import UpnpDataUpdateCoordinator
|
|
|
|
from .entity import UpnpEntity, UpnpEntityDescription
|
|
|
|
|
2018-04-12 22:22:52 +00:00
|
|
|
|
2022-10-26 19:34:44 +00:00
|
|
|
@dataclass
|
|
|
|
class UpnpSensorEntityDescription(UpnpEntityDescription, SensorEntityDescription):
|
|
|
|
"""A class that describes a sensor UPnP entities."""
|
|
|
|
|
|
|
|
|
|
|
|
SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
|
2021-09-07 20:27:03 +00:00
|
|
|
UpnpSensorEntityDescription(
|
|
|
|
key=BYTES_RECEIVED,
|
|
|
|
name=f"{DATA_BYTES} received",
|
|
|
|
icon="mdi:server-network",
|
|
|
|
native_unit_of_measurement=DATA_BYTES,
|
|
|
|
format="d",
|
2022-08-25 21:45:27 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2022-10-26 19:34:44 +00:00
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-09-03 15:15:28 +00:00
|
|
|
),
|
2021-09-07 20:27:03 +00:00
|
|
|
UpnpSensorEntityDescription(
|
|
|
|
key=BYTES_SENT,
|
|
|
|
name=f"{DATA_BYTES} sent",
|
|
|
|
icon="mdi:server-network",
|
|
|
|
native_unit_of_measurement=DATA_BYTES,
|
|
|
|
format="d",
|
2022-08-25 21:45:27 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2022-10-26 19:34:44 +00:00
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-09-03 15:15:28 +00:00
|
|
|
),
|
2021-09-07 20:27:03 +00:00
|
|
|
UpnpSensorEntityDescription(
|
|
|
|
key=PACKETS_RECEIVED,
|
|
|
|
name=f"{DATA_PACKETS} received",
|
|
|
|
icon="mdi:server-network",
|
|
|
|
native_unit_of_measurement=DATA_PACKETS,
|
|
|
|
format="d",
|
2022-08-25 21:45:27 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2022-10-26 19:34:44 +00:00
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-09-07 20:27:03 +00:00
|
|
|
),
|
|
|
|
UpnpSensorEntityDescription(
|
|
|
|
key=PACKETS_SENT,
|
|
|
|
name=f"{DATA_PACKETS} sent",
|
|
|
|
icon="mdi:server-network",
|
|
|
|
native_unit_of_measurement=DATA_PACKETS,
|
|
|
|
format="d",
|
2022-08-25 21:45:27 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2022-10-26 19:34:44 +00:00
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-09-07 20:27:03 +00:00
|
|
|
),
|
|
|
|
UpnpSensorEntityDescription(
|
|
|
|
key=ROUTER_IP,
|
|
|
|
name="External IP",
|
|
|
|
icon="mdi:server-network",
|
2022-10-26 19:34:44 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-09-07 20:27:03 +00:00
|
|
|
),
|
|
|
|
UpnpSensorEntityDescription(
|
|
|
|
key=ROUTER_UPTIME,
|
|
|
|
name="Uptime",
|
|
|
|
icon="mdi:server-network",
|
|
|
|
native_unit_of_measurement=TIME_SECONDS,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
format="d",
|
2022-08-25 21:45:27 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-09-07 20:27:03 +00:00
|
|
|
),
|
|
|
|
UpnpSensorEntityDescription(
|
|
|
|
key=WAN_STATUS,
|
|
|
|
name="wan status",
|
|
|
|
icon="mdi:server-network",
|
2022-08-25 21:45:27 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
entity_registry_enabled_default=False,
|
2021-09-07 20:27:03 +00:00
|
|
|
),
|
|
|
|
UpnpSensorEntityDescription(
|
2021-10-11 07:35:26 +00:00
|
|
|
key=BYTES_RECEIVED,
|
2022-10-26 19:34:44 +00:00
|
|
|
value_key=KIBIBYTES_PER_SEC_RECEIVED,
|
2021-10-11 07:35:26 +00:00
|
|
|
unique_id="KiB/sec_received",
|
2021-09-07 20:27:03 +00:00
|
|
|
name=f"{DATA_RATE_KIBIBYTES_PER_SECOND} received",
|
|
|
|
icon="mdi:server-network",
|
|
|
|
native_unit_of_measurement=DATA_RATE_KIBIBYTES_PER_SECOND,
|
|
|
|
format=".1f",
|
2022-10-26 19:34:44 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-09-07 20:27:03 +00:00
|
|
|
),
|
|
|
|
UpnpSensorEntityDescription(
|
2021-10-11 07:35:26 +00:00
|
|
|
key=BYTES_SENT,
|
2022-10-26 19:34:44 +00:00
|
|
|
value_key=KIBIBYTES_PER_SEC_SENT,
|
2021-11-03 09:21:54 +00:00
|
|
|
unique_id="KiB/sec_sent",
|
2021-09-07 20:27:03 +00:00
|
|
|
name=f"{DATA_RATE_KIBIBYTES_PER_SECOND} sent",
|
|
|
|
icon="mdi:server-network",
|
|
|
|
native_unit_of_measurement=DATA_RATE_KIBIBYTES_PER_SECOND,
|
|
|
|
format=".1f",
|
2022-10-26 19:34:44 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-09-07 20:27:03 +00:00
|
|
|
),
|
|
|
|
UpnpSensorEntityDescription(
|
2021-10-11 07:35:26 +00:00
|
|
|
key=PACKETS_RECEIVED,
|
2022-10-26 19:34:44 +00:00
|
|
|
value_key=PACKETS_PER_SEC_RECEIVED,
|
2021-10-11 07:35:26 +00:00
|
|
|
unique_id="packets/sec_received",
|
2021-09-07 20:27:03 +00:00
|
|
|
name=f"{DATA_RATE_PACKETS_PER_SECOND} received",
|
|
|
|
icon="mdi:server-network",
|
|
|
|
native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
|
|
|
|
format=".1f",
|
2022-08-25 21:45:27 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2022-10-26 19:34:44 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-09-07 20:27:03 +00:00
|
|
|
),
|
|
|
|
UpnpSensorEntityDescription(
|
2021-10-11 07:35:26 +00:00
|
|
|
key=PACKETS_SENT,
|
2022-10-26 19:34:44 +00:00
|
|
|
value_key=PACKETS_PER_SEC_SENT,
|
2021-11-03 09:21:54 +00:00
|
|
|
unique_id="packets/sec_sent",
|
2021-09-07 20:27:03 +00:00
|
|
|
name=f"{DATA_RATE_PACKETS_PER_SECOND} sent",
|
|
|
|
icon="mdi:server-network",
|
|
|
|
native_unit_of_measurement=DATA_RATE_PACKETS_PER_SECOND,
|
|
|
|
format=".1f",
|
2022-08-25 21:45:27 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2022-10-26 19:34:44 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-09-07 20:27:03 +00:00
|
|
|
),
|
|
|
|
)
|
2017-06-19 04:32:39 +00:00
|
|
|
|
|
|
|
|
2020-04-10 22:24:03 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-30 18:38:59 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-04-10 22:24:03 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the UPnP/IGD sensors."""
|
2022-10-26 19:34:44 +00:00
|
|
|
coordinator: UpnpDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
2021-04-14 21:39:44 +00:00
|
|
|
|
2021-09-07 20:27:03 +00:00
|
|
|
entities: list[UpnpSensor] = [
|
2022-10-26 19:34:44 +00:00
|
|
|
UpnpSensor(
|
2021-09-03 15:15:28 +00:00
|
|
|
coordinator=coordinator,
|
|
|
|
entity_description=entity_description,
|
|
|
|
)
|
2022-10-26 19:34:44 +00:00
|
|
|
for entity_description in SENSOR_DESCRIPTIONS
|
2021-09-03 15:15:28 +00:00
|
|
|
if coordinator.data.get(entity_description.key) is not None
|
2021-09-07 20:27:03 +00:00
|
|
|
]
|
2018-10-01 16:25:54 +00:00
|
|
|
|
2022-02-27 18:29:29 +00:00
|
|
|
LOGGER.debug("Adding sensor entities: %s", entities)
|
2021-09-03 15:15:28 +00:00
|
|
|
async_add_entities(entities)
|
2020-04-10 22:24:03 +00:00
|
|
|
|
2018-10-01 16:25:54 +00:00
|
|
|
|
2021-09-03 15:15:28 +00:00
|
|
|
class UpnpSensor(UpnpEntity, SensorEntity):
|
|
|
|
"""Base class for UPnP/IGD sensors."""
|
2018-10-01 16:25:54 +00:00
|
|
|
|
2022-03-08 06:51:23 +00:00
|
|
|
entity_description: UpnpSensorEntityDescription
|
|
|
|
|
2017-06-19 04:32:39 +00:00
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_value(self) -> str | None:
|
2017-06-19 04:32:39 +00:00
|
|
|
"""Return the state of the device."""
|
2022-10-26 19:34:44 +00:00
|
|
|
value = self.coordinator.data[self.entity_description.value_key]
|
2020-06-22 23:39:57 +00:00
|
|
|
if value is None:
|
|
|
|
return None
|
2021-09-03 15:15:28 +00:00
|
|
|
return format(value, self.entity_description.format)
|