Skip to content

Commit

Permalink
modified: StateVacuumEntity is now used as base class #4
Browse files Browse the repository at this point in the history
  • Loading branch information
plakna committed Aug 5, 2023
1 parent d6b72f8 commit 755571b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 29 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ condition:
- sun

action:
- service: vacuum.turn_on
- service: vacuum.start
target:
entity_id: vacuum.hombot

Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions custom_components/lg_hombot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform

from .const import DOMAIN

PLATFORMS = [Platform.VACUUM]


Expand Down
18 changes: 14 additions & 4 deletions custom_components/lg_hombot/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
83 changes: 61 additions & 22 deletions custom_components/lg_hombot/vacuum.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 755571b

Please sign in to comment.