Skip to content

Commit

Permalink
Move iaqualink base entity to separate module (home-assistant#126100)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Sep 17, 2024
1 parent ecea251 commit 6dfa6b0
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 53 deletions.
50 changes: 2 additions & 48 deletions homeassistant/components/iaqualink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from iaqualink.client import AqualinkClient
from iaqualink.device import (
AqualinkBinarySensor,
AqualinkDevice,
AqualinkLight,
AqualinkSensor,
AqualinkSwitch,
Expand All @@ -29,16 +28,12 @@
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__)

Expand Down Expand Up @@ -194,44 +189,3 @@ async def wrapper(
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
2 changes: 1 addition & 1 deletion homeassistant/components/iaqualink/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
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

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/iaqualink/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
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__)
Expand Down
52 changes: 52 additions & 0 deletions homeassistant/components/iaqualink/entity.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion homeassistant/components/iaqualink/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
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
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/iaqualink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
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

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/iaqualink/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
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
Expand Down

0 comments on commit 6dfa6b0

Please sign in to comment.