Update WLED to use CoordinatorEntity (#39442)

This commit is contained in:
springstan 2020-08-30 18:05:55 +02:00 committed by GitHub
parent 77fe206084
commit ce1ef73366
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,9 +14,12 @@ from homeassistant.const import ATTR_NAME, CONF_HOST
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
from .const import ( from .const import (
ATTR_IDENTIFIERS, ATTR_IDENTIFIERS,
@ -139,7 +142,7 @@ class WLEDDataUpdateCoordinator(DataUpdateCoordinator[WLEDDevice]):
raise UpdateFailed(f"Invalid response from API: {error}") from error raise UpdateFailed(f"Invalid response from API: {error}") from error
class WLEDEntity(Entity): class WLEDEntity(CoordinatorEntity):
"""Defines a base WLED entity.""" """Defines a base WLED entity."""
def __init__( def __init__(
@ -152,12 +155,12 @@ class WLEDEntity(Entity):
enabled_default: bool = True, enabled_default: bool = True,
) -> None: ) -> None:
"""Initialize the WLED entity.""" """Initialize the WLED entity."""
super().__init__(coordinator)
self._enabled_default = enabled_default self._enabled_default = enabled_default
self._entry_id = entry_id self._entry_id = entry_id
self._icon = icon self._icon = icon
self._name = name self._name = name
self._unsub_dispatcher = None self._unsub_dispatcher = None
self.coordinator = coordinator
@property @property
def name(self) -> str: def name(self) -> str:
@ -169,31 +172,11 @@ class WLEDEntity(Entity):
"""Return the mdi icon of the entity.""" """Return the mdi icon of the entity."""
return self._icon return self._icon
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self.coordinator.last_update_success
@property @property
def entity_registry_enabled_default(self) -> bool: def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry.""" """Return if the entity should be enabled when first added to the entity registry."""
return self._enabled_default return self._enabled_default
@property
def should_poll(self) -> bool:
"""Return the polling requirement of the entity."""
return False
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 WLED entity."""
await self.coordinator.async_request_refresh()
class WLEDDeviceEntity(WLEDEntity): class WLEDDeviceEntity(WLEDEntity):
"""Defines a WLED device entity.""" """Defines a WLED device entity."""