Update awair to use CoordinatorEntity (#39469)

This commit is contained in:
J. Nick Koston 2020-08-30 14:05:22 -05:00 committed by GitHub
parent 56057a638f
commit 705b253a99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,7 @@ from homeassistant.helpers import device_registry as dr
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import ConfigType, HomeAssistantType from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import ( from .const import (
API_DUST, API_DUST,
@ -82,7 +83,7 @@ async def async_setup_entry(
async_add_entities(sensors) async_add_entities(sensors)
class AwairSensor(Entity): class AwairSensor(CoordinatorEntity):
"""Defines an Awair sensor entity.""" """Defines an Awair sensor entity."""
def __init__( def __init__(
@ -92,14 +93,9 @@ class AwairSensor(Entity):
coordinator: AwairDataUpdateCoordinator, coordinator: AwairDataUpdateCoordinator,
) -> None: ) -> None:
"""Set up an individual AwairSensor.""" """Set up an individual AwairSensor."""
super().__init__(coordinator)
self._kind = kind self._kind = kind
self._device = device self._device = device
self._coordinator = coordinator
@property
def should_poll(self) -> bool:
"""Return the polling requirement of the entity."""
return False
@property @property
def name(self) -> str: def name(self) -> str:
@ -128,7 +124,7 @@ class AwairSensor(Entity):
def available(self) -> bool: def available(self) -> bool:
"""Determine if the sensor is available based on API results.""" """Determine if the sensor is available based on API results."""
# If the last update was successful... # If the last update was successful...
if self._coordinator.last_update_success and self._air_data: if self.coordinator.last_update_success and self._air_data:
# and the results included our sensor type... # and the results included our sensor type...
if self._kind in self._air_data.sensors: if self._kind in self._air_data.sensors:
# then we are available. # then we are available.
@ -231,20 +227,10 @@ class AwairSensor(Entity):
return info return info
async def async_added_to_hass(self) -> None:
"""Connect to dispatcher listening for entity data notifications."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)
async def async_update(self) -> None:
"""Update Awair entity."""
await self._coordinator.async_request_refresh()
@property @property
def _air_data(self) -> Optional[AwairResult]: def _air_data(self) -> Optional[AwairResult]:
"""Return the latest data for our device, or None.""" """Return the latest data for our device, or None."""
result: Optional[AwairResult] = self._coordinator.data.get(self._device.uuid) result: Optional[AwairResult] = self.coordinator.data.get(self._device.uuid)
if result: if result:
return result.air_data return result.air_data