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