Update awair to use CoordinatorEntity (#39469)
parent
56057a638f
commit
705b253a99
|
@ -13,6 +13,7 @@ from homeassistant.helpers import device_registry as dr
|
|||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import (
|
||||
API_DUST,
|
||||
|
@ -82,7 +83,7 @@ async def async_setup_entry(
|
|||
async_add_entities(sensors)
|
||||
|
||||
|
||||
class AwairSensor(Entity):
|
||||
class AwairSensor(CoordinatorEntity):
|
||||
"""Defines an Awair sensor entity."""
|
||||
|
||||
def __init__(
|
||||
|
@ -92,14 +93,9 @@ class AwairSensor(Entity):
|
|||
coordinator: AwairDataUpdateCoordinator,
|
||||
) -> None:
|
||||
"""Set up an individual AwairSensor."""
|
||||
super().__init__(coordinator)
|
||||
self._kind = kind
|
||||
self._device = device
|
||||
self._coordinator = coordinator
|
||||
|
||||
@property
|
||||
def should_poll(self) -> bool:
|
||||
"""Return the polling requirement of the entity."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
|
@ -128,7 +124,7 @@ class AwairSensor(Entity):
|
|||
def available(self) -> bool:
|
||||
"""Determine if the sensor is available based on API results."""
|
||||
# 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...
|
||||
if self._kind in self._air_data.sensors:
|
||||
# then we are available.
|
||||
|
@ -231,20 +227,10 @@ class AwairSensor(Entity):
|
|||
|
||||
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
|
||||
def _air_data(self) -> Optional[AwairResult]:
|
||||
"""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:
|
||||
return result.air_data
|
||||
|
||||
|
|
Loading…
Reference in New Issue