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
|
|
|
|
|
2021-03-22 18:47:44 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-04-10 22:24:03 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import DATA_BYTES, DATA_RATE_KIBIBYTES_PER_SECOND
|
2021-04-22 14:53:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-04-10 22:24:03 +00:00
|
|
|
|
2021-08-17 18:23:41 +00:00
|
|
|
from . import UpnpDataUpdateCoordinator, UpnpEntity
|
2020-04-10 22:24:03 +00:00
|
|
|
from .const import (
|
|
|
|
BYTES_RECEIVED,
|
|
|
|
BYTES_SENT,
|
|
|
|
DATA_PACKETS,
|
|
|
|
DATA_RATE_PACKETS_PER_SECOND,
|
|
|
|
DOMAIN,
|
|
|
|
KIBIBYTE,
|
2021-08-17 18:23:41 +00:00
|
|
|
LOGGER,
|
2020-04-10 22:24:03 +00:00
|
|
|
PACKETS_RECEIVED,
|
|
|
|
PACKETS_SENT,
|
|
|
|
TIMESTAMP,
|
|
|
|
)
|
2018-04-12 22:22:52 +00:00
|
|
|
|
2017-06-19 04:32:39 +00:00
|
|
|
SENSOR_TYPES = {
|
2020-04-10 22:24:03 +00:00
|
|
|
BYTES_RECEIVED: {
|
|
|
|
"device_value_key": BYTES_RECEIVED,
|
|
|
|
"name": f"{DATA_BYTES} received",
|
|
|
|
"unit": DATA_BYTES,
|
|
|
|
"unique_id": BYTES_RECEIVED,
|
|
|
|
"derived_name": f"{DATA_RATE_KIBIBYTES_PER_SECOND} received",
|
|
|
|
"derived_unit": DATA_RATE_KIBIBYTES_PER_SECOND,
|
|
|
|
"derived_unique_id": "KiB/sec_received",
|
|
|
|
},
|
|
|
|
BYTES_SENT: {
|
|
|
|
"device_value_key": BYTES_SENT,
|
|
|
|
"name": f"{DATA_BYTES} sent",
|
|
|
|
"unit": DATA_BYTES,
|
|
|
|
"unique_id": BYTES_SENT,
|
|
|
|
"derived_name": f"{DATA_RATE_KIBIBYTES_PER_SECOND} sent",
|
|
|
|
"derived_unit": DATA_RATE_KIBIBYTES_PER_SECOND,
|
|
|
|
"derived_unique_id": "KiB/sec_sent",
|
|
|
|
},
|
|
|
|
PACKETS_RECEIVED: {
|
|
|
|
"device_value_key": PACKETS_RECEIVED,
|
|
|
|
"name": f"{DATA_PACKETS} received",
|
|
|
|
"unit": DATA_PACKETS,
|
|
|
|
"unique_id": PACKETS_RECEIVED,
|
|
|
|
"derived_name": f"{DATA_RATE_PACKETS_PER_SECOND} received",
|
|
|
|
"derived_unit": DATA_RATE_PACKETS_PER_SECOND,
|
|
|
|
"derived_unique_id": "packets/sec_received",
|
|
|
|
},
|
|
|
|
PACKETS_SENT: {
|
|
|
|
"device_value_key": PACKETS_SENT,
|
|
|
|
"name": f"{DATA_PACKETS} sent",
|
|
|
|
"unit": DATA_PACKETS,
|
|
|
|
"unique_id": PACKETS_SENT,
|
|
|
|
"derived_name": f"{DATA_RATE_PACKETS_PER_SECOND} sent",
|
|
|
|
"derived_unit": DATA_RATE_PACKETS_PER_SECOND,
|
|
|
|
"derived_unique_id": "packets/sec_sent",
|
|
|
|
},
|
2017-06-19 04:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(
|
2021-04-22 14:53:57 +00:00
|
|
|
hass: HomeAssistant, config, async_add_entities, discovery_info=None
|
2020-04-10 22:24:03 +00:00
|
|
|
) -> None:
|
2018-10-01 16:25:54 +00:00
|
|
|
"""Old way of setting up UPnP/IGD sensors."""
|
2021-08-17 18:23:41 +00:00
|
|
|
LOGGER.debug(
|
2019-07-31 19:25:30 +00:00
|
|
|
"async_setup_platform: config: %s, discovery: %s", config, discovery_info
|
|
|
|
)
|
2018-10-01 16:25:54 +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."""
|
2021-08-17 18:23:41 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
2021-04-14 21:39:44 +00:00
|
|
|
|
2021-08-17 18:23:41 +00:00
|
|
|
LOGGER.debug("Adding sensors")
|
2020-04-10 22:24:03 +00:00
|
|
|
|
|
|
|
sensors = [
|
2021-08-17 18:23:41 +00:00
|
|
|
RawUpnpSensor(coordinator, SENSOR_TYPES[BYTES_RECEIVED]),
|
|
|
|
RawUpnpSensor(coordinator, SENSOR_TYPES[BYTES_SENT]),
|
|
|
|
RawUpnpSensor(coordinator, SENSOR_TYPES[PACKETS_RECEIVED]),
|
|
|
|
RawUpnpSensor(coordinator, SENSOR_TYPES[PACKETS_SENT]),
|
|
|
|
DerivedUpnpSensor(coordinator, SENSOR_TYPES[BYTES_RECEIVED]),
|
|
|
|
DerivedUpnpSensor(coordinator, SENSOR_TYPES[BYTES_SENT]),
|
|
|
|
DerivedUpnpSensor(coordinator, SENSOR_TYPES[PACKETS_RECEIVED]),
|
|
|
|
DerivedUpnpSensor(coordinator, SENSOR_TYPES[PACKETS_SENT]),
|
2020-04-10 22:24:03 +00:00
|
|
|
]
|
2021-08-17 18:23:41 +00:00
|
|
|
async_add_entities(sensors)
|
2018-10-01 16:25:54 +00:00
|
|
|
|
|
|
|
|
2021-08-17 18:23:41 +00:00
|
|
|
class UpnpSensor(UpnpEntity, SensorEntity):
|
2018-10-01 16:25:54 +00:00
|
|
|
"""Base class for UPnP/IGD sensors."""
|
|
|
|
|
2020-04-10 22:24:03 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-08-17 18:23:41 +00:00
|
|
|
coordinator: UpnpDataUpdateCoordinator,
|
|
|
|
sensor_type: dict[str, str],
|
2020-04-10 22:24:03 +00:00
|
|
|
) -> None:
|
2018-10-01 16:25:54 +00:00
|
|
|
"""Initialize the base sensor."""
|
2020-08-30 16:17:41 +00:00
|
|
|
super().__init__(coordinator)
|
2020-04-10 22:24:03 +00:00
|
|
|
self._sensor_type = sensor_type
|
2021-08-17 18:23:41 +00:00
|
|
|
self._attr_name = f"{coordinator.device.name} {sensor_type['name']}"
|
|
|
|
self._attr_unique_id = f"{coordinator.device.udn}_{sensor_type['unique_id']}"
|
2018-10-01 16:25:54 +00:00
|
|
|
|
2020-04-10 22:24:03 +00:00
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return "mdi:server-network"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if entity is available."""
|
2021-08-17 18:23:41 +00:00
|
|
|
return super().available and self.coordinator.data.get(
|
|
|
|
self._sensor_type["device_value_key"]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-01 16:25:54 +00:00
|
|
|
|
2020-04-10 22:24:03 +00:00
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_unit_of_measurement(self) -> str:
|
2020-04-10 22:24:03 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return self._sensor_type["unit"]
|
2018-10-01 16:25:54 +00:00
|
|
|
|
2017-06-19 04:32:39 +00:00
|
|
|
|
2020-04-10 22:24:03 +00:00
|
|
|
class RawUpnpSensor(UpnpSensor):
|
|
|
|
"""Representation of a UPnP/IGD sensor."""
|
2018-09-01 19:20:15 +00:00
|
|
|
|
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."""
|
2020-04-10 22:24:03 +00:00
|
|
|
device_value_key = self._sensor_type["device_value_key"]
|
2020-08-30 16:17:41 +00:00
|
|
|
value = self.coordinator.data[device_value_key]
|
2020-06-22 23:39:57 +00:00
|
|
|
if value is None:
|
|
|
|
return None
|
2020-04-10 22:24:03 +00:00
|
|
|
return format(value, "d")
|
2018-09-01 16:13:45 +00:00
|
|
|
|
|
|
|
|
2020-04-10 22:24:03 +00:00
|
|
|
class DerivedUpnpSensor(UpnpSensor):
|
|
|
|
"""Representation of a UNIT Sent/Received per second sensor."""
|
2018-09-01 16:13:45 +00:00
|
|
|
|
2021-08-17 18:23:41 +00:00
|
|
|
def __init__(self, coordinator: UpnpDataUpdateCoordinator, sensor_type) -> None:
|
2019-12-10 22:25:06 +00:00
|
|
|
"""Initialize sensor."""
|
2021-08-17 18:23:41 +00:00
|
|
|
super().__init__(coordinator, sensor_type)
|
2018-09-01 16:13:45 +00:00
|
|
|
self._last_value = None
|
2020-04-10 22:24:03 +00:00
|
|
|
self._last_timestamp = None
|
2021-08-17 18:23:41 +00:00
|
|
|
self._attr_name = f"{coordinator.device.name} {sensor_type['derived_name']}"
|
|
|
|
self._attr_unique_id = (
|
|
|
|
f"{coordinator.device.udn}_{sensor_type['derived_unique_id']}"
|
|
|
|
)
|
2017-06-19 04:32:39 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_unit_of_measurement(self) -> str:
|
2017-06-19 04:32:39 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2020-04-10 22:24:03 +00:00
|
|
|
return self._sensor_type["derived_unit"]
|
2018-09-01 16:13:45 +00:00
|
|
|
|
2020-04-10 22:24:03 +00:00
|
|
|
def _has_overflowed(self, current_value) -> bool:
|
2018-09-01 16:13:45 +00:00
|
|
|
"""Check if value has overflowed."""
|
2020-04-10 22:24:03 +00:00
|
|
|
return current_value < self._last_value
|
2018-08-29 19:19:04 +00:00
|
|
|
|
2018-09-01 16:13:45 +00:00
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_value(self) -> str | None:
|
2018-09-01 16:13:45 +00:00
|
|
|
"""Return the state of the device."""
|
2020-04-10 22:24:03 +00:00
|
|
|
# Can't calculate any derivative if we have only one value.
|
|
|
|
device_value_key = self._sensor_type["device_value_key"]
|
2020-08-30 16:17:41 +00:00
|
|
|
current_value = self.coordinator.data[device_value_key]
|
2020-06-22 23:39:57 +00:00
|
|
|
if current_value is None:
|
|
|
|
return None
|
2020-08-30 16:17:41 +00:00
|
|
|
current_timestamp = self.coordinator.data[TIMESTAMP]
|
2020-04-10 22:24:03 +00: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 19:20:15 +00:00
|
|
|
return None
|
2018-08-29 19:19:04 +00:00
|
|
|
|
2020-04-10 22:24:03 +00:00
|
|
|
# Calculate derivative.
|
|
|
|
delta_value = current_value - self._last_value
|
|
|
|
if self._sensor_type["unit"] == DATA_BYTES:
|
|
|
|
delta_value /= KIBIBYTE
|
|
|
|
delta_time = current_timestamp - self._last_timestamp
|
2021-04-21 00:41:36 +00:00
|
|
|
if delta_time.total_seconds() == 0:
|
2020-04-10 22:24:03 +00:00
|
|
|
# Prevent division by 0.
|
2018-09-01 19:20:15 +00:00
|
|
|
return None
|
2021-04-21 00:41:36 +00:00
|
|
|
derived = delta_value / delta_time.total_seconds()
|
2020-04-10 22:24:03 +00:00
|
|
|
|
|
|
|
# Store current values for future use.
|
|
|
|
self._last_value = current_value
|
|
|
|
self._last_timestamp = current_timestamp
|
2018-09-01 16:13:45 +00:00
|
|
|
|
2020-04-10 22:24:03 +00:00
|
|
|
return format(derived, ".1f")
|