Skip to content

Commit

Permalink
Unsubscribe event listeners on remove of Ecovacs legacy bot entities (h…
Browse files Browse the repository at this point in the history
…ome-assistant#122731)

* unsubscribe on entity remove, create base EcovacsLegacyEntity

* fix name and model in device info

* apply suggestion

* add manufacturer to device info

* fix device info
  • Loading branch information
mib1185 authored Jul 28, 2024
1 parent ba266ab commit d765b92
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
34 changes: 34 additions & 0 deletions homeassistant/components/ecovacs/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from deebot_client.device import Device
from deebot_client.events import AvailabilityEvent
from deebot_client.events.base import Event
from sucks import EventListener, VacBot

from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
Expand Down Expand Up @@ -120,3 +121,36 @@ class EcovacsCapabilityEntityDescription(
"""Ecovacs entity description."""

capability_fn: Callable[[Capabilities], CapabilityEntity | None]


class EcovacsLegacyEntity(Entity):
"""Ecovacs legacy bot entity."""

_attr_has_entity_name = True
_attr_should_poll = False

def __init__(self, device: VacBot) -> None:
"""Initialize the legacy Ecovacs entity."""
self.device = device
vacuum = device.vacuum

self.error: str | None = None
self._attr_unique_id = vacuum["did"]

if (name := vacuum.get("nick")) is None:
name = vacuum["did"]

self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, vacuum["did"])},
manufacturer="Ecovacs",
model=vacuum.get("deviceName"),
name=name,
serial_number=vacuum["did"],
)

self._event_listeners: list[EventListener] = []

async def async_will_remove_from_hass(self) -> None:
"""Remove event listeners on entity remove."""
for listener in self._event_listeners:
listener.unsubscribe()
45 changes: 18 additions & 27 deletions homeassistant/components/ecovacs/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
from homeassistant.core import HomeAssistant, SupportsResponse
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import entity_platform
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.icon import icon_for_battery_level
from homeassistant.util import slugify

from . import EcovacsConfigEntry
from .const import DOMAIN
from .entity import EcovacsEntity
from .entity import EcovacsEntity, EcovacsLegacyEntity
from .util import get_name_key

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -72,12 +71,10 @@ async def async_setup_entry(
)


class EcovacsLegacyVacuum(StateVacuumEntity):
class EcovacsLegacyVacuum(EcovacsLegacyEntity, StateVacuumEntity):
"""Legacy Ecovacs vacuums."""

_attr_fan_speed_list = [sucks.FAN_SPEED_NORMAL, sucks.FAN_SPEED_HIGH]
_attr_has_entity_name = True
_attr_should_poll = False
_attr_supported_features = (
VacuumEntityFeature.BATTERY
| VacuumEntityFeature.RETURN_HOME
Expand All @@ -90,30 +87,24 @@ class EcovacsLegacyVacuum(StateVacuumEntity):
| VacuumEntityFeature.FAN_SPEED
)

def __init__(self, device: sucks.VacBot) -> None:
"""Initialize the Ecovacs Vacuum."""
self.device = device
vacuum = self.device.vacuum

self.error: str | None = None
self._attr_unique_id = vacuum["did"]

if (name := vacuum.get("nick")) is None:
name = vacuum.get("name", vacuum["did"])

self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, vacuum["did"])},
model=vacuum.get("deviceName"),
name=name,
serial_number=vacuum["did"],
)

async def async_added_to_hass(self) -> None:
"""Set up the event listeners now that hass is ready."""
self.device.statusEvents.subscribe(lambda _: self.schedule_update_ha_state())
self.device.batteryEvents.subscribe(lambda _: self.schedule_update_ha_state())
self.device.lifespanEvents.subscribe(lambda _: self.schedule_update_ha_state())
self.device.errorEvents.subscribe(self.on_error)
self._event_listeners.append(
self.device.statusEvents.subscribe(
lambda _: self.schedule_update_ha_state()
)
)
self._event_listeners.append(
self.device.batteryEvents.subscribe(
lambda _: self.schedule_update_ha_state()
)
)
self._event_listeners.append(
self.device.lifespanEvents.subscribe(
lambda _: self.schedule_update_ha_state()
)
)
self._event_listeners.append(self.device.errorEvents.subscribe(self.on_error))

def on_error(self, error: str) -> None:
"""Handle an error event from the robot.
Expand Down

0 comments on commit d765b92

Please sign in to comment.