2019-04-03 15:40:03 +00:00
|
|
|
"""Support for UPnP/IGD Sensors."""
|
2020-03-15 17:18:15 +00:00
|
|
|
from datetime import timedelta
|
2020-07-30 15:04:00 +00:00
|
|
|
from typing import Any, Mapping
|
2017-06-19 04:32:39 +00:00
|
|
|
|
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
|
2019-10-31 19:51:35 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2019-03-25 00:46:15 +00:00
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2020-08-30 16:17:41 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2020-04-10 22:24:03 +00:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
BYTES_RECEIVED,
|
|
|
|
BYTES_SENT,
|
2020-05-10 02:52:08 +00:00
|
|
|
CONFIG_ENTRY_SCAN_INTERVAL,
|
|
|
|
CONFIG_ENTRY_UDN,
|
2020-04-10 22:24:03 +00:00
|
|
|
DATA_PACKETS,
|
|
|
|
DATA_RATE_PACKETS_PER_SECOND,
|
2020-05-10 02:52:08 +00:00
|
|
|
DEFAULT_SCAN_INTERVAL,
|
2020-04-10 22:24:03 +00:00
|
|
|
DOMAIN,
|
2020-08-05 00:24:42 +00:00
|
|
|
DOMAIN_COORDINATORS,
|
|
|
|
DOMAIN_DEVICES,
|
2020-04-10 22:24:03 +00:00
|
|
|
KIBIBYTE,
|
|
|
|
LOGGER as _LOGGER,
|
|
|
|
PACKETS_RECEIVED,
|
|
|
|
PACKETS_SENT,
|
|
|
|
TIMESTAMP,
|
|
|
|
)
|
|
|
|
from .device import Device
|
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(
|
|
|
|
hass: HomeAssistantType, 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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"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(
|
|
|
|
hass, config_entry: ConfigEntry, async_add_entities
|
|
|
|
) -> None:
|
|
|
|
"""Set up the UPnP/IGD sensors."""
|
2018-10-01 16:25:54 +00:00
|
|
|
data = config_entry.data
|
2020-05-10 02:52:08 +00:00
|
|
|
if CONFIG_ENTRY_UDN in data:
|
|
|
|
udn = data[CONFIG_ENTRY_UDN]
|
2018-12-21 17:25:23 +00:00
|
|
|
else:
|
|
|
|
# any device will do
|
2020-10-30 14:19:13 +00:00
|
|
|
udn = list(hass.data[DOMAIN][DOMAIN_DEVICES])[0]
|
2018-12-21 17:25:23 +00:00
|
|
|
|
2020-08-05 00:24:42 +00:00
|
|
|
device: Device = hass.data[DOMAIN][DOMAIN_DEVICES][udn]
|
2020-04-10 22:24:03 +00:00
|
|
|
|
2020-05-11 18:03:12 +00:00
|
|
|
update_interval_sec = config_entry.options.get(
|
|
|
|
CONFIG_ENTRY_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
|
|
|
|
)
|
2020-05-10 02:52:08 +00:00
|
|
|
update_interval = timedelta(seconds=update_interval_sec)
|
|
|
|
_LOGGER.debug("update_interval: %s", update_interval)
|
2020-04-10 22:24:03 +00:00
|
|
|
_LOGGER.debug("Adding sensors")
|
2020-07-30 15:04:00 +00:00
|
|
|
coordinator = DataUpdateCoordinator[Mapping[str, Any]](
|
2020-04-10 22:24:03 +00:00
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name=device.name,
|
|
|
|
update_method=device.async_get_traffic_data,
|
2020-05-10 02:52:08 +00:00
|
|
|
update_interval=update_interval,
|
2020-04-10 22:24:03 +00:00
|
|
|
)
|
|
|
|
await coordinator.async_refresh()
|
2020-08-05 00:24:42 +00:00
|
|
|
hass.data[DOMAIN][DOMAIN_COORDINATORS][udn] = coordinator
|
2020-04-10 22:24:03 +00:00
|
|
|
|
|
|
|
sensors = [
|
|
|
|
RawUpnpSensor(coordinator, device, SENSOR_TYPES[BYTES_RECEIVED]),
|
|
|
|
RawUpnpSensor(coordinator, device, SENSOR_TYPES[BYTES_SENT]),
|
|
|
|
RawUpnpSensor(coordinator, device, SENSOR_TYPES[PACKETS_RECEIVED]),
|
|
|
|
RawUpnpSensor(coordinator, device, SENSOR_TYPES[PACKETS_SENT]),
|
|
|
|
DerivedUpnpSensor(coordinator, device, SENSOR_TYPES[BYTES_RECEIVED]),
|
|
|
|
DerivedUpnpSensor(coordinator, device, SENSOR_TYPES[BYTES_SENT]),
|
|
|
|
DerivedUpnpSensor(coordinator, device, SENSOR_TYPES[PACKETS_RECEIVED]),
|
|
|
|
DerivedUpnpSensor(coordinator, device, SENSOR_TYPES[PACKETS_SENT]),
|
|
|
|
]
|
|
|
|
async_add_entities(sensors, True)
|
2018-10-01 16:25:54 +00:00
|
|
|
|
|
|
|
|
2020-08-30 16:17:41 +00:00
|
|
|
class UpnpSensor(CoordinatorEntity):
|
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,
|
2020-07-30 15:04:00 +00:00
|
|
|
coordinator: DataUpdateCoordinator[Mapping[str, Any]],
|
2020-04-10 22:24:03 +00:00
|
|
|
device: Device,
|
|
|
|
sensor_type: Mapping[str, str],
|
2020-05-10 02:52:08 +00:00
|
|
|
update_multiplier: int = 2,
|
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)
|
2018-10-01 16:25:54 +00:00
|
|
|
self._device = device
|
2020-04-10 22:24:03 +00:00
|
|
|
self._sensor_type = sensor_type
|
2020-05-10 02:52:08 +00:00
|
|
|
self._update_counter_max = update_multiplier
|
|
|
|
self._update_counter = 0
|
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."""
|
|
|
|
device_value_key = self._sensor_type["device_value_key"]
|
|
|
|
return (
|
2020-08-30 16:17:41 +00:00
|
|
|
self.coordinator.last_update_success
|
|
|
|
and device_value_key in self.coordinator.data
|
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
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return f"{self._device.name} {self._sensor_type['name']}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return an unique ID."""
|
|
|
|
return f"{self._device.udn}_{self._sensor_type['unique_id']}"
|
2018-10-01 16:25:54 +00:00
|
|
|
|
2020-04-10 22:24:03 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self) -> str:
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return self._sensor_type["unit"]
|
2018-10-01 16:25:54 +00:00
|
|
|
|
2018-12-21 17:25:23 +00:00
|
|
|
@property
|
2020-07-30 15:04:00 +00:00
|
|
|
def device_info(self) -> Mapping[str, Any]:
|
2018-12-21 17:25:23 +00:00
|
|
|
"""Get device info."""
|
|
|
|
return {
|
2019-10-31 19:51:35 +00:00
|
|
|
"connections": {(dr.CONNECTION_UPNP, self._device.udn)},
|
|
|
|
"name": self._device.name,
|
2019-07-31 19:25:30 +00:00
|
|
|
"manufacturer": self._device.manufacturer,
|
2019-10-31 19:51:35 +00:00
|
|
|
"model": self._device.model_name,
|
2018-12-21 17:25:23 +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
|
2018-09-01 16:13:45 +00:00
|
|
|
def state(self) -> str:
|
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
|
|
|
|
2020-04-10 22:24:03 +00:00
|
|
|
def __init__(self, coordinator, device, sensor_type) -> None:
|
2019-12-10 22:25:06 +00:00
|
|
|
"""Initialize sensor."""
|
2020-04-10 22:24:03 +00:00
|
|
|
super().__init__(coordinator, device, 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
|
2018-08-29 19:19:04 +00:00
|
|
|
|
2018-09-01 16:13:45 +00:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the sensor."""
|
2020-04-10 22:24:03 +00:00
|
|
|
return f"{self._device.name} {self._sensor_type['derived_name']}"
|
2018-09-01 16:13:45 +00:00
|
|
|
|
|
|
|
@property
|
2020-04-10 22:24:03 +00:00
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return an unique ID."""
|
|
|
|
return f"{self._device.udn}_{self._sensor_type['derived_unique_id']}"
|
2017-06-19 04:32:39 +00:00
|
|
|
|
|
|
|
@property
|
2018-09-01 16:13:45 +00:00
|
|
|
def 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
|
2018-10-01 16:25:54 +00:00
|
|
|
def state(self) -> str:
|
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
|
|
|
|
if delta_time.seconds == 0:
|
|
|
|
# Prevent division by 0.
|
2018-09-01 19:20:15 +00:00
|
|
|
return None
|
2020-04-10 22:24:03 +00:00
|
|
|
derived = delta_value / delta_time.seconds
|
|
|
|
|
|
|
|
# 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")
|