Update upnp to use CoordinatorEntity (#39434)

This commit is contained in:
J. Nick Koston 2020-08-30 11:17:41 -05:00 committed by GitHub
parent 42818c150f
commit 225743a620
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,9 +5,11 @@ from typing import Any, Mapping
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DATA_BYTES, DATA_RATE_KIBIBYTES_PER_SECOND
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from .const import (
BYTES_RECEIVED,
@ -119,7 +121,7 @@ async def async_setup_entry(
async_add_entities(sensors, True)
class UpnpSensor(Entity):
class UpnpSensor(CoordinatorEntity):
"""Base class for UPnP/IGD sensors."""
def __init__(
@ -130,17 +132,12 @@ class UpnpSensor(Entity):
update_multiplier: int = 2,
) -> None:
"""Initialize the base sensor."""
self._coordinator = coordinator
super().__init__(coordinator)
self._device = device
self._sensor_type = sensor_type
self._update_counter_max = update_multiplier
self._update_counter = 0
@property
def should_poll(self) -> bool:
"""Inform we should not be polled."""
return False
@property
def icon(self) -> str:
"""Icon to use in the frontend, if any."""
@ -151,8 +148,8 @@ class UpnpSensor(Entity):
"""Return if entity is available."""
device_value_key = self._sensor_type["device_value_key"]
return (
self._coordinator.last_update_success
and device_value_key in self._coordinator.data
self.coordinator.last_update_success
and device_value_key in self.coordinator.data
)
@property
@ -180,17 +177,6 @@ class UpnpSensor(Entity):
"model": self._device.model_name,
}
async def async_update(self):
"""Request an update."""
await self._coordinator.async_request_refresh()
async def async_added_to_hass(self) -> None:
"""Subscribe to sensors events."""
remove_from_coordinator = self._coordinator.async_add_listener(
self.async_write_ha_state
)
self.async_on_remove(remove_from_coordinator)
class RawUpnpSensor(UpnpSensor):
"""Representation of a UPnP/IGD sensor."""
@ -199,7 +185,7 @@ class RawUpnpSensor(UpnpSensor):
def state(self) -> str:
"""Return the state of the device."""
device_value_key = self._sensor_type["device_value_key"]
value = self._coordinator.data[device_value_key]
value = self.coordinator.data[device_value_key]
if value is None:
return None
return format(value, "d")
@ -238,10 +224,10 @@ class DerivedUpnpSensor(UpnpSensor):
"""Return the state of the device."""
# Can't calculate any derivative if we have only one value.
device_value_key = self._sensor_type["device_value_key"]
current_value = self._coordinator.data[device_value_key]
current_value = self.coordinator.data[device_value_key]
if current_value is None:
return None
current_timestamp = self._coordinator.data[TIMESTAMP]
current_timestamp = self.coordinator.data[TIMESTAMP]
if self._last_value is None or self._has_overflowed(current_value):
self._last_value = current_value
self._last_timestamp = current_timestamp