2019-04-03 17:40:03 +02:00
|
|
|
"""Support for UPnP/IGD Sensors."""
|
2021-03-18 14:43:52 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-03-22 19:47:44 +01:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-04-11 00:24:03 +02:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-09-03 09:15:28 -06:00
|
|
|
from homeassistant.const import DATA_BYTES, DATA_RATE_KIBIBYTES_PER_SECOND, TIME_SECONDS
|
2021-04-22 16:53:57 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-30 19:38:59 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-04-11 00:24:03 +02:00
|
|
|
|
2021-09-03 09:15:28 -06:00
|
|
|
from . import UpnpDataUpdateCoordinator, UpnpEntity, UpnpSensorEntityDescription
|
2020-04-11 00:24:03 +02:00
|
|
|
from .const import (
|
|
|
|
BYTES_RECEIVED,
|
|
|
|
BYTES_SENT,
|
|
|
|
DATA_PACKETS,
|
|
|
|
DATA_RATE_PACKETS_PER_SECOND,
|
2021-09-03 09:15:28 -06:00
|
|
|
DERIVED_SENSOR,
|
2020-04-11 00:24:03 +02:00
|
|
|
DOMAIN,
|
|
|
|
KIBIBYTE,
|
2021-08-17 12:23:41 -06:00
|
|
|
LOGGER,
|
2020-04-11 00:24:03 +02:00
|
|
|
PACKETS_RECEIVED,
|
|
|
|
PACKETS_SENT,
|
2021-09-03 09:15:28 -06:00
|
|
|
RAW_SENSOR,
|
|
|
|
ROUTER_IP,
|
|
|
|
ROUTER_UPTIME,
|
2020-04-11 00:24:03 +02:00
|
|
|
TIMESTAMP,
|
2021-09-03 09:15:28 -06:00
|
|
|
WAN_STATUS,
|
2020-04-11 00:24:03 +02:00
|
|
|
)
|
2018-04-12 23:22:52 +01:00
|
|
|
|
2021-09-03 09:15:28 -06:00
|
|
|
SENSOR_ENTITY_DESCRIPTIONS: dict[str, tuple[UpnpSensorEntityDescription, ...]] = {
|
|
|
|
RAW_SENSOR: (
|
|
|
|
UpnpSensorEntityDescription(
|
|
|
|
key=BYTES_RECEIVED,
|
|
|
|
name=f"{DATA_BYTES} received",
|
|
|
|
icon="mdi:server-network",
|
|
|
|
native_unit_of_measurement=DATA_BYTES,
|
|
|
|
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(
|
|
|
|
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",
|
|
|
|
),
|
|
|
|
),
|
2017-06-19 05:32:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-11 00:24:03 +02:00
|
|
|
async def async_setup_entry(
|
2021-04-30 19:38:59 +01:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-04-11 00:24:03 +02:00
|
|
|
) -> None:
|
|
|
|
"""Set up the UPnP/IGD sensors."""
|
2021-08-17 12:23:41 -06:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
2021-04-14 23:39:44 +02:00
|
|
|
|
2021-08-17 12:23:41 -06:00
|
|
|
LOGGER.debug("Adding sensors")
|
2020-04-11 00:24:03 +02:00
|
|
|
|
2021-09-03 09:15:28 -06:00
|
|
|
entities = []
|
|
|
|
entities.append(
|
|
|
|
RawUpnpSensor(
|
|
|
|
coordinator=coordinator,
|
|
|
|
entity_description=entity_description,
|
|
|
|
)
|
|
|
|
for entity_description in SENSOR_ENTITY_DESCRIPTIONS[RAW_SENSOR]
|
|
|
|
if coordinator.data.get(entity_description.key) is not None
|
|
|
|
)
|
2018-10-01 18:25:54 +02:00
|
|
|
|
2021-09-03 09:15:28 -06:00
|
|
|
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
|
|
|
|
)
|
2018-10-01 18:25:54 +02:00
|
|
|
|
2021-09-03 09:15:28 -06:00
|
|
|
async_add_entities(entities)
|
2020-04-11 00:24:03 +02:00
|
|
|
|
2018-10-01 18:25:54 +02:00
|
|
|
|
2021-09-03 09:15:28 -06:00
|
|
|
class UpnpSensor(UpnpEntity, SensorEntity):
|
|
|
|
"""Base class for UPnP/IGD sensors."""
|
2018-10-01 18:25:54 +02:00
|
|
|
|
2017-06-19 05:32:39 +01:00
|
|
|
|
2020-04-11 00:24:03 +02:00
|
|
|
class RawUpnpSensor(UpnpSensor):
|
|
|
|
"""Representation of a UPnP/IGD sensor."""
|
2018-09-01 21:20:15 +02:00
|
|
|
|
2017-06-19 05:32:39 +01:00
|
|
|
@property
|
2021-08-11 18:57:50 +02:00
|
|
|
def native_value(self) -> str | None:
|
2017-06-19 05:32:39 +01:00
|
|
|
"""Return the state of the device."""
|
2021-09-03 09:15:28 -06:00
|
|
|
value = self.coordinator.data[self.entity_description.key]
|
2020-06-23 01:39:57 +02:00
|
|
|
if value is None:
|
|
|
|
return None
|
2021-09-03 09:15:28 -06:00
|
|
|
return format(value, self.entity_description.format)
|
2018-09-01 18:13:45 +02:00
|
|
|
|
|
|
|
|
2020-04-11 00:24:03 +02:00
|
|
|
class DerivedUpnpSensor(UpnpSensor):
|
|
|
|
"""Representation of a UNIT Sent/Received per second sensor."""
|
2018-09-01 18:13:45 +02:00
|
|
|
|
2021-09-03 09:15:28 -06:00
|
|
|
entity_description: UpnpSensorEntityDescription
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: UpnpDataUpdateCoordinator,
|
|
|
|
entity_description: UpnpSensorEntityDescription,
|
|
|
|
) -> None:
|
2019-12-11 00:25:06 +02:00
|
|
|
"""Initialize sensor."""
|
2021-09-03 09:15:28 -06:00
|
|
|
super().__init__(coordinator=coordinator, entity_description=entity_description)
|
2018-09-01 18:13:45 +02:00
|
|
|
self._last_value = None
|
2020-04-11 00:24:03 +02:00
|
|
|
self._last_timestamp = None
|
2018-09-01 18:13:45 +02:00
|
|
|
|
2020-04-11 00:24:03 +02:00
|
|
|
def _has_overflowed(self, current_value) -> bool:
|
2018-09-01 18:13:45 +02:00
|
|
|
"""Check if value has overflowed."""
|
2020-04-11 00:24:03 +02:00
|
|
|
return current_value < self._last_value
|
2018-08-29 21:19:04 +02:00
|
|
|
|
2018-09-01 18:13:45 +02:00
|
|
|
@property
|
2021-08-11 18:57:50 +02:00
|
|
|
def native_value(self) -> str | None:
|
2018-09-01 18:13:45 +02:00
|
|
|
"""Return the state of the device."""
|
2020-04-11 00:24:03 +02:00
|
|
|
# Can't calculate any derivative if we have only one value.
|
2021-09-03 09:15:28 -06:00
|
|
|
current_value = self.coordinator.data[self.entity_description.key]
|
2020-06-23 01:39:57 +02:00
|
|
|
if current_value is None:
|
|
|
|
return None
|
2020-08-30 11:17:41 -05:00
|
|
|
current_timestamp = self.coordinator.data[TIMESTAMP]
|
2020-04-11 00:24:03 +02:00
|
|
|
if self._last_value is None or self._has_overflowed(current_value):
|
|
|
|
self._last_value = current_value
|
|
|
|
self._last_timestamp = current_timestamp
|
2018-09-01 21:20:15 +02:00
|
|
|
return None
|
2018-08-29 21:19:04 +02:00
|
|
|
|
2020-04-11 00:24:03 +02:00
|
|
|
# Calculate derivative.
|
|
|
|
delta_value = current_value - self._last_value
|
2021-09-03 09:15:28 -06:00
|
|
|
if self.entity_description.native_unit_of_measurement == DATA_BYTES:
|
2020-04-11 00:24:03 +02:00
|
|
|
delta_value /= KIBIBYTE
|
|
|
|
delta_time = current_timestamp - self._last_timestamp
|
2021-04-20 17:41:36 -07:00
|
|
|
if delta_time.total_seconds() == 0:
|
2020-04-11 00:24:03 +02:00
|
|
|
# Prevent division by 0.
|
2018-09-01 21:20:15 +02:00
|
|
|
return None
|
2021-04-20 17:41:36 -07:00
|
|
|
derived = delta_value / delta_time.total_seconds()
|
2020-04-11 00:24:03 +02:00
|
|
|
|
|
|
|
# Store current values for future use.
|
|
|
|
self._last_value = current_value
|
|
|
|
self._last_timestamp = current_timestamp
|
2018-09-01 18:13:45 +02:00
|
|
|
|
2021-09-03 09:15:28 -06:00
|
|
|
return format(derived, self.entity_description.format)
|