Unsubscribe event listeners on remove of Ecovacs legacy bot entities (#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
This commit is contained in:
Michael 2024-07-28 15:01:34 +02:00 committed by GitHub
parent ba266ab13c
commit d765b92cca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 27 deletions

View File

@ -10,6 +10,7 @@ from deebot_client.capabilities import Capabilities
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
@ -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()

View File

@ -26,14 +26,13 @@ from homeassistant.components.vacuum import (
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__)
@ -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
@ -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.