Migrate from deprecated VacuumEntity to StateVacuumEntity in Ecovacs (#95920)

* migrate to StateVacuumEntity

* harmoize supported features start and stop

* apply suggestions
This commit is contained in:
Michael 2023-07-06 08:49:59 +02:00 committed by GitHub
parent cb7fa494a4
commit af1cb7be58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,15 @@ from typing import Any
import sucks import sucks
from homeassistant.components.vacuum import VacuumEntity, VacuumEntityFeature from homeassistant.components.vacuum import (
STATE_CLEANING,
STATE_DOCKED,
STATE_ERROR,
STATE_IDLE,
STATE_RETURNING,
StateVacuumEntity,
VacuumEntityFeature,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.icon import icon_for_battery_level from homeassistant.helpers.icon import icon_for_battery_level
@ -34,7 +42,7 @@ def setup_platform(
add_entities(vacuums, True) add_entities(vacuums, True)
class EcovacsVacuum(VacuumEntity): class EcovacsVacuum(StateVacuumEntity):
"""Ecovacs Vacuums such as Deebot.""" """Ecovacs Vacuums such as Deebot."""
_attr_fan_speed_list = [sucks.FAN_SPEED_NORMAL, sucks.FAN_SPEED_HIGH] _attr_fan_speed_list = [sucks.FAN_SPEED_NORMAL, sucks.FAN_SPEED_HIGH]
@ -44,10 +52,9 @@ class EcovacsVacuum(VacuumEntity):
| VacuumEntityFeature.RETURN_HOME | VacuumEntityFeature.RETURN_HOME
| VacuumEntityFeature.CLEAN_SPOT | VacuumEntityFeature.CLEAN_SPOT
| VacuumEntityFeature.STOP | VacuumEntityFeature.STOP
| VacuumEntityFeature.TURN_OFF | VacuumEntityFeature.START
| VacuumEntityFeature.TURN_ON
| VacuumEntityFeature.LOCATE | VacuumEntityFeature.LOCATE
| VacuumEntityFeature.STATUS | VacuumEntityFeature.STATE
| VacuumEntityFeature.SEND_COMMAND | VacuumEntityFeature.SEND_COMMAND
| VacuumEntityFeature.FAN_SPEED | VacuumEntityFeature.FAN_SPEED
) )
@ -56,14 +63,13 @@ class EcovacsVacuum(VacuumEntity):
"""Initialize the Ecovacs Vacuum.""" """Initialize the Ecovacs Vacuum."""
self.device = device self.device = device
self.device.connect_and_wait_until_ready() self.device.connect_and_wait_until_ready()
if self.device.vacuum.get("nick") is not None: vacuum = self.device.vacuum
self._attr_name = str(self.device.vacuum["nick"])
else:
# In case there is no nickname defined, use the device id
self._attr_name = str(format(self.device.vacuum["did"]))
self._error = None self.error = None
_LOGGER.debug("Vacuum initialized: %s", self.name) self._attr_unique_id = vacuum["did"]
self._attr_name = vacuum.get("nick", vacuum["did"])
_LOGGER.debug("StateVacuum initialized: %s", self.name)
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Set up the event listeners now that hass is ready.""" """Set up the event listeners now that hass is ready."""
@ -79,9 +85,9 @@ class EcovacsVacuum(VacuumEntity):
to change, that will come through as a separate on_status event to change, that will come through as a separate on_status event
""" """
if error == "no_error": if error == "no_error":
self._error = None self.error = None
else: else:
self._error = error self.error = error
self.hass.bus.fire( self.hass.bus.fire(
"ecovacs_error", {"entity_id": self.entity_id, "error": error} "ecovacs_error", {"entity_id": self.entity_id, "error": error}
@ -89,36 +95,24 @@ class EcovacsVacuum(VacuumEntity):
self.schedule_update_ha_state() self.schedule_update_ha_state()
@property @property
def unique_id(self) -> str: def state(self) -> str | None:
"""Return an unique ID.""" """Return the state of the vacuum cleaner."""
return self.device.vacuum.get("did") if self.error is not None:
return STATE_ERROR
@property if self.device.is_cleaning:
def is_on(self) -> bool: return STATE_CLEANING
"""Return true if vacuum is currently cleaning."""
return self.device.is_cleaning
@property if self.device.is_charging:
def is_charging(self) -> bool: return STATE_DOCKED
"""Return true if vacuum is currently charging."""
return self.device.is_charging
@property if self.device.vacuum_status == sucks.CLEAN_MODE_STOP:
def status(self) -> str | None: return STATE_IDLE
"""Return the status of the vacuum cleaner."""
return self.device.vacuum_status
def return_to_base(self, **kwargs: Any) -> None: if self.device.vacuum_status == sucks.CHARGE_MODE_RETURNING:
"""Set the vacuum cleaner to return to the dock.""" return STATE_RETURNING
self.device.run(sucks.Charge()) return None
@property
def battery_icon(self) -> str:
"""Return the battery icon for the vacuum cleaner."""
return icon_for_battery_level(
battery_level=self.battery_level, charging=self.is_charging
)
@property @property
def battery_level(self) -> int | None: def battery_level(self) -> int | None:
@ -126,22 +120,42 @@ class EcovacsVacuum(VacuumEntity):
if self.device.battery_status is not None: if self.device.battery_status is not None:
return self.device.battery_status * 100 return self.device.battery_status * 100
return super().battery_level return None
@property
def battery_icon(self) -> str:
"""Return the battery icon for the vacuum cleaner."""
return icon_for_battery_level(
battery_level=self.battery_level, charging=self.device.is_charging
)
@property @property
def fan_speed(self) -> str | None: def fan_speed(self) -> str | None:
"""Return the fan speed of the vacuum cleaner.""" """Return the fan speed of the vacuum cleaner."""
return self.device.fan_speed return self.device.fan_speed
def turn_on(self, **kwargs: Any) -> None: @property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device-specific state attributes of this vacuum."""
data: dict[str, Any] = {}
data[ATTR_ERROR] = self.error
for key, val in self.device.components.items():
attr_name = ATTR_COMPONENT_PREFIX + key
data[attr_name] = int(val * 100)
return data
def return_to_base(self, **kwargs: Any) -> None:
"""Set the vacuum cleaner to return to the dock."""
self.device.run(sucks.Charge())
def start(self, **kwargs: Any) -> None:
"""Turn the vacuum on and start cleaning.""" """Turn the vacuum on and start cleaning."""
self.device.run(sucks.Clean()) self.device.run(sucks.Clean())
def turn_off(self, **kwargs: Any) -> None:
"""Turn the vacuum off stopping the cleaning and returning home."""
self.return_to_base()
def stop(self, **kwargs: Any) -> None: def stop(self, **kwargs: Any) -> None:
"""Stop the vacuum cleaner.""" """Stop the vacuum cleaner."""
@ -159,7 +173,7 @@ class EcovacsVacuum(VacuumEntity):
def set_fan_speed(self, fan_speed: str, **kwargs: Any) -> None: def set_fan_speed(self, fan_speed: str, **kwargs: Any) -> None:
"""Set fan speed.""" """Set fan speed."""
if self.is_on: if self.state == STATE_CLEANING:
self.device.run(sucks.Clean(mode=self.device.clean_status, speed=fan_speed)) self.device.run(sucks.Clean(mode=self.device.clean_status, speed=fan_speed))
def send_command( def send_command(
@ -170,15 +184,3 @@ class EcovacsVacuum(VacuumEntity):
) -> None: ) -> None:
"""Send a command to a vacuum cleaner.""" """Send a command to a vacuum cleaner."""
self.device.run(sucks.VacBotCommand(command, params)) self.device.run(sucks.VacBotCommand(command, params))
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device-specific state attributes of this vacuum."""
data: dict[str, Any] = {}
data[ATTR_ERROR] = self._error
for key, val in self.device.components.items():
attr_name = ATTR_COMPONENT_PREFIX + key
data[attr_name] = int(val * 100)
return data