Move iaqualink base entity to separate module (#126100)

This commit is contained in:
epenet 2024-09-17 15:40:25 +02:00 committed by GitHub
parent ecea251efa
commit 6dfa6b0001
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 62 additions and 53 deletions

View File

@ -12,7 +12,6 @@ import httpx
from iaqualink.client import AqualinkClient
from iaqualink.device import (
AqualinkBinarySensor,
AqualinkDevice,
AqualinkLight,
AqualinkSensor,
AqualinkSwitch,
@ -29,16 +28,12 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.httpx_client import get_async_client
from .const import DOMAIN, UPDATE_INTERVAL
from .entity import AqualinkEntity
_LOGGER = logging.getLogger(__name__)
@ -194,44 +189,3 @@ def refresh_system[_AqualinkEntityT: AqualinkEntity, **_P](
async_dispatcher_send(self.hass, DOMAIN)
return wrapper
class AqualinkEntity(Entity):
"""Abstract class for all Aqualink platforms.
Entity state is updated via the interval timer within the integration.
Any entity state change via the iaqualink library triggers an internal
state refresh which is then propagated to all the entities in the system
via the refresh_system decorator above to the _update_callback in this
class.
"""
_attr_should_poll = False
def __init__(self, dev: AqualinkDevice) -> None:
"""Initialize the entity."""
self.dev = dev
self._attr_unique_id = f"{dev.system.serial}_{dev.name}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._attr_unique_id)},
manufacturer=dev.manufacturer,
model=dev.model,
name=dev.label,
via_device=(DOMAIN, dev.system.serial),
)
async def async_added_to_hass(self) -> None:
"""Set up a listener when this entity is added to HA."""
self.async_on_remove(
async_dispatcher_connect(self.hass, DOMAIN, self.async_write_ha_state)
)
@property
def assumed_state(self) -> bool:
"""Return whether the state is based on actual reading from the device."""
return self.dev.system.online in [False, None]
@property
def available(self) -> bool:
"""Return whether the device is available or not."""
return self.dev.system.online is True

View File

@ -13,8 +13,8 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import AqualinkEntity
from .const import DOMAIN as AQUALINK_DOMAIN
from .entity import AqualinkEntity
PARALLEL_UPDATES = 0

View File

@ -20,8 +20,9 @@ from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import AqualinkEntity, refresh_system
from . import refresh_system
from .const import DOMAIN as AQUALINK_DOMAIN
from .entity import AqualinkEntity
from .utils import await_or_reraise
_LOGGER = logging.getLogger(__name__)

View File

@ -0,0 +1,52 @@
"""Component to embed Aqualink devices."""
from __future__ import annotations
from iaqualink.device import AqualinkDevice
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from .const import DOMAIN
class AqualinkEntity(Entity):
"""Abstract class for all Aqualink platforms.
Entity state is updated via the interval timer within the integration.
Any entity state change via the iaqualink library triggers an internal
state refresh which is then propagated to all the entities in the system
via the refresh_system decorator above to the _update_callback in this
class.
"""
_attr_should_poll = False
def __init__(self, dev: AqualinkDevice) -> None:
"""Initialize the entity."""
self.dev = dev
self._attr_unique_id = f"{dev.system.serial}_{dev.name}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._attr_unique_id)},
manufacturer=dev.manufacturer,
model=dev.model,
name=dev.label,
via_device=(DOMAIN, dev.system.serial),
)
async def async_added_to_hass(self) -> None:
"""Set up a listener when this entity is added to HA."""
self.async_on_remove(
async_dispatcher_connect(self.hass, DOMAIN, self.async_write_ha_state)
)
@property
def assumed_state(self) -> bool:
"""Return whether the state is based on actual reading from the device."""
return self.dev.system.online in [False, None]
@property
def available(self) -> bool:
"""Return whether the device is available or not."""
return self.dev.system.online is True

View File

@ -18,8 +18,9 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import AqualinkEntity, refresh_system
from . import refresh_system
from .const import DOMAIN as AQUALINK_DOMAIN
from .entity import AqualinkEntity
from .utils import await_or_reraise
PARALLEL_UPDATES = 0

View File

@ -14,8 +14,8 @@ from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import AqualinkEntity
from .const import DOMAIN as AQUALINK_DOMAIN
from .entity import AqualinkEntity
PARALLEL_UPDATES = 0

View File

@ -11,8 +11,9 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import AqualinkEntity, refresh_system
from . import refresh_system
from .const import DOMAIN as AQUALINK_DOMAIN
from .entity import AqualinkEntity
from .utils import await_or_reraise
PARALLEL_UPDATES = 0