diff --git a/README.md b/README.md index 79e5c92..ac2b18b 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ condition: - sun action: - - service: vacuum.turn_on + - service: vacuum.start target: entity_id: vacuum.hombot @@ -67,6 +67,9 @@ mode: single - Version 1.0.0: - Removal of yaml file support: This function is no longer supported and should not be used anymore. +- Version 1.0.3: + - The service vacuum.turn_on has been changed to vacuum.start (deprecated). + - The status has been standardized according to [HomeAssistant conventions](https://developers.home-assistant.io/docs/core/entity/vacuum/#states). ## Credits diff --git a/custom_components/lg_hombot/__init__.py b/custom_components/lg_hombot/__init__.py index aae0806..ed1d2ef 100644 --- a/custom_components/lg_hombot/__init__.py +++ b/custom_components/lg_hombot/__init__.py @@ -4,8 +4,6 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform -from .const import DOMAIN - PLATFORMS = [Platform.VACUUM] diff --git a/custom_components/lg_hombot/const.py b/custom_components/lg_hombot/const.py index d896165..330361c 100644 --- a/custom_components/lg_hombot/const.py +++ b/custom_components/lg_hombot/const.py @@ -5,10 +5,20 @@ DOMAIN = "lg_hombot" - SUPPORTED_SERVICES = ( - VacuumEntityFeature.STATUS + VacuumEntityFeature.STATE | VacuumEntityFeature.BATTERY - | VacuumEntityFeature.TURN_ON - | VacuumEntityFeature.TURN_OFF + | VacuumEntityFeature.FAN_SPEED + | VacuumEntityFeature.START + | VacuumEntityFeature.PAUSE + | VacuumEntityFeature.RETURN_HOME + | VacuumEntityFeature.STOP +) + +SPEED_NORMAL = "Normal" +SPEED_TURBO = "Turbo" + +SUPPORTED_SPEEDS = ( + SPEED_NORMAL, + SPEED_TURBO ) diff --git a/custom_components/lg_hombot/vacuum.py b/custom_components/lg_hombot/vacuum.py index cfe1172..6e6871e 100644 --- a/custom_components/lg_hombot/vacuum.py +++ b/custom_components/lg_hombot/vacuum.py @@ -1,16 +1,16 @@ from homeassistant.core import HomeAssistant from homeassistant.config_entries import ConfigEntry -from homeassistant.components.vacuum import VacuumEntity +from homeassistant.components.vacuum import STATE_CLEANING, STATE_DOCKED, STATE_PAUSED, STATE_RETURNING, StateVacuumEntity + from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback - from homeassistant.helpers.aiohttp_client import async_get_clientsession from typing import Any from urllib.parse import quote -from .const import DOMAIN, SUPPORTED_SERVICES +from .const import DOMAIN, SPEED_NORMAL, SPEED_TURBO, SUPPORTED_SERVICES, SUPPORTED_SPEEDS async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entites: AddEntitiesCallback) -> None: @@ -24,7 +24,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn async_add_entites([hombot_vac]) -class HombotVacuum(VacuumEntity): +class HombotVacuum(StateVacuumEntity): """Representation of a Hombot vacuum cleaner robot.""" def __init__(self, name: str, url: str) -> None: @@ -35,9 +35,10 @@ def __init__(self, name: str, url: str) -> None: self._url = url - self._state = False - self._status = None + self._state = None self._battery_level = None + self._fan_speed = SPEED_NORMAL + self._fan_speed_list = SUPPORTED_SPEEDS @property def unique_id(self) -> str | None: @@ -58,20 +59,23 @@ def device_info(self) -> DeviceInfo: return device_info @property - def is_on(self) -> bool: - """Return true if vacuum is on.""" - return self._state - - @property - def status(self) -> str: + def state(self) -> str: """Return the status of the vacuum.""" - return self._status + return self._state @property def battery_level(self) -> int: """Return the status of the vacuum.""" return self._battery_level + @property + def fan_speed(self) -> str | None: + return self._fan_speed + + @property + def fan_speed_list(self) -> list[str]: + return self._fan_speed_list + async def async_update(self): """Update status properties.""" # generate url @@ -90,22 +94,57 @@ async def async_update(self): attributes[name] = var.strip('"') # assign properties - self._status = attributes["JSON_ROBOT_STATE"] - self._state = self._status in ["WORKING", "BACKMOVING_INIT"] + self._state = self.convert_state(attributes["JSON_ROBOT_STATE"]) self._battery_level = int(attributes["JSON_BATTPERC"]) - - async def async_turn_on(self, **kwargs: Any) -> None: + if attributes["JSON_TURBO"] == "true": + self._fan_speed = SPEED_TURBO + else: + self._fan_speed = SPEED_NORMAL + + def convert_state(self, current_state) -> str: + """Converts the status of hombot to that of HomeAssistant -> see https://developers.home-assistant.io/docs/core/entity/vacuum/#states""" + match current_state: + case "CHARGING": + return STATE_DOCKED + case "BACKMOVING_INIT": + return STATE_CLEANING + case "WORKING": + return STATE_CLEANING + case "PAUSE": + return STATE_PAUSED + case "HOMING": + return STATE_RETURNING + case"DOCKING": + return STATE_RETURNING + + async def async_start(self, **kwargs: Any) -> None: """Turn the vacuum on.""" - self._state = True - self._status = "WORKING" + self._state = STATE_CLEANING await self.query('{"COMMAND":"CLEAN_START"}') - async def async_turn_off(self, **kwargs: Any) -> None: + async def async_pause(self, **kwargs: Any) -> None: + """Turn the vacuum on.""" + self._state = STATE_PAUSED + await self.query('{"COMMAND":"PAUSE"}') + + async def async_return_to_base(self, **kwargs: Any) -> None: """Turn the vacuum off.""" - self._state = False - self._status = "HOMING" + self._state = STATE_RETURNING await self.query('{"COMMAND":"HOMING"}') + async def async_stop(self, **kwargs: Any) -> None: + """Turn the vacuum off.""" + self._state = STATE_RETURNING + await self.query('{"COMMAND":"HOMING"}') + + async def async_set_fan_speed(self, fan_speed: str, **kwargs: Any) -> None: + """Sets fan speed.""" + self._fan_speed = fan_speed + if fan_speed == SPEED_TURBO: + await self.query('{"COMMAND":{"TURBO":"true"}}') + else: + await self.query('{"COMMAND":{"TURBO":"false"}}') + async def query(self, command): """Execute command.""" # generate url