2021-02-22 18:53:57 +00:00
|
|
|
"""Support for Litter-Robot "Vacuum"."""
|
2021-04-11 20:35:25 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-05-04 21:36:48 +00:00
|
|
|
from typing import Any
|
2021-04-11 20:35:25 +00:00
|
|
|
|
2022-08-30 16:14:06 +00:00
|
|
|
from pylitterbot import LitterRobot
|
2021-04-11 20:35:25 +00:00
|
|
|
from pylitterbot.enums import LitterBoxStatus
|
|
|
|
import voluptuous as vol
|
2021-02-22 18:53:57 +00:00
|
|
|
|
|
|
|
from homeassistant.components.vacuum import (
|
2022-09-01 18:02:46 +00:00
|
|
|
DOMAIN as PLATFORM,
|
2021-02-22 18:53:57 +00:00
|
|
|
STATE_CLEANING,
|
|
|
|
STATE_DOCKED,
|
|
|
|
STATE_ERROR,
|
2021-04-11 20:35:25 +00:00
|
|
|
STATE_PAUSED,
|
2021-10-30 22:27:12 +00:00
|
|
|
StateVacuumEntity,
|
2022-09-01 18:02:46 +00:00
|
|
|
StateVacuumEntityDescription,
|
2022-04-23 21:43:13 +00:00
|
|
|
VacuumEntityFeature,
|
2021-02-22 18:53:57 +00:00
|
|
|
)
|
2021-04-11 20:35:25 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-04-30 14:46:27 +00:00
|
|
|
from homeassistant.const import STATE_OFF
|
2021-04-11 20:35:25 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
2021-05-04 21:36:48 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-02-22 18:53:57 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
2022-09-01 18:02:46 +00:00
|
|
|
from .entity import LitterRobotControlEntity, async_update_unique_id
|
2021-04-11 20:35:25 +00:00
|
|
|
from .hub import LitterRobotHub
|
2021-02-22 18:53:57 +00:00
|
|
|
|
2021-04-11 20:35:25 +00:00
|
|
|
SERVICE_SET_SLEEP_MODE = "set_sleep_mode"
|
|
|
|
|
2022-04-30 09:43:13 +00:00
|
|
|
LITTER_BOX_STATUS_STATE_MAP = {
|
|
|
|
LitterBoxStatus.CLEAN_CYCLE: STATE_CLEANING,
|
|
|
|
LitterBoxStatus.EMPTY_CYCLE: STATE_CLEANING,
|
|
|
|
LitterBoxStatus.CLEAN_CYCLE_COMPLETE: STATE_DOCKED,
|
|
|
|
LitterBoxStatus.CAT_SENSOR_TIMING: STATE_DOCKED,
|
|
|
|
LitterBoxStatus.DRAWER_FULL_1: STATE_DOCKED,
|
|
|
|
LitterBoxStatus.DRAWER_FULL_2: STATE_DOCKED,
|
|
|
|
LitterBoxStatus.READY: STATE_DOCKED,
|
|
|
|
LitterBoxStatus.CAT_SENSOR_INTERRUPTED: STATE_PAUSED,
|
|
|
|
LitterBoxStatus.OFF: STATE_OFF,
|
|
|
|
}
|
|
|
|
|
2022-09-01 18:02:46 +00:00
|
|
|
LITTER_BOX_ENTITY = StateVacuumEntityDescription("litter_box", name="Litter Box")
|
|
|
|
|
2021-02-22 18:53:57 +00:00
|
|
|
|
2021-04-11 20:35:25 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
2021-05-04 21:36:48 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-04-11 20:35:25 +00:00
|
|
|
) -> None:
|
2021-02-22 18:53:57 +00:00
|
|
|
"""Set up Litter-Robot cleaner using config entry."""
|
2021-04-11 20:35:25 +00:00
|
|
|
hub: LitterRobotHub = hass.data[DOMAIN][entry.entry_id]
|
2021-02-22 18:53:57 +00:00
|
|
|
|
2022-09-01 18:02:46 +00:00
|
|
|
entities = [
|
|
|
|
LitterRobotCleaner(robot=robot, hub=hub, description=LITTER_BOX_ENTITY)
|
2022-08-25 16:32:27 +00:00
|
|
|
for robot in hub.litter_robots()
|
2022-09-01 18:02:46 +00:00
|
|
|
]
|
|
|
|
async_update_unique_id(hass, PLATFORM, entities)
|
|
|
|
async_add_entities(entities)
|
2021-04-11 20:35:25 +00:00
|
|
|
|
2021-05-03 16:34:28 +00:00
|
|
|
platform = entity_platform.async_get_current_platform()
|
2021-04-11 20:35:25 +00:00
|
|
|
platform.async_register_entity_service(
|
|
|
|
SERVICE_SET_SLEEP_MODE,
|
|
|
|
{
|
|
|
|
vol.Required("enabled"): cv.boolean,
|
|
|
|
vol.Optional("start_time"): cv.time,
|
|
|
|
},
|
|
|
|
"async_set_sleep_mode",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-08-30 16:14:06 +00:00
|
|
|
class LitterRobotCleaner(LitterRobotControlEntity[LitterRobot], StateVacuumEntity):
|
2021-02-22 18:53:57 +00:00
|
|
|
"""Litter-Robot "Vacuum" Cleaner."""
|
|
|
|
|
2022-04-23 21:43:13 +00:00
|
|
|
_attr_supported_features = (
|
|
|
|
VacuumEntityFeature.START
|
|
|
|
| VacuumEntityFeature.STATE
|
|
|
|
| VacuumEntityFeature.STATUS
|
|
|
|
| VacuumEntityFeature.TURN_OFF
|
|
|
|
| VacuumEntityFeature.TURN_ON
|
|
|
|
)
|
|
|
|
|
2021-02-22 18:53:57 +00:00
|
|
|
@property
|
2021-04-11 20:35:25 +00:00
|
|
|
def state(self) -> str:
|
2021-02-22 18:53:57 +00:00
|
|
|
"""Return the state of the cleaner."""
|
2022-04-30 09:43:13 +00:00
|
|
|
return LITTER_BOX_STATUS_STATE_MAP.get(self.robot.status, STATE_ERROR)
|
2021-02-22 18:53:57 +00:00
|
|
|
|
|
|
|
@property
|
2021-04-11 20:35:25 +00:00
|
|
|
def status(self) -> str:
|
2021-02-22 18:53:57 +00:00
|
|
|
"""Return the status of the cleaner."""
|
2021-04-11 20:35:25 +00:00
|
|
|
return (
|
|
|
|
f"{self.robot.status.text}{' (Sleeping)' if self.robot.is_sleeping else ''}"
|
|
|
|
)
|
2021-02-22 18:53:57 +00:00
|
|
|
|
2021-04-11 20:35:25 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2021-02-22 18:53:57 +00:00
|
|
|
"""Turn the cleaner on, starting a clean cycle."""
|
|
|
|
await self.perform_action_and_refresh(self.robot.set_power_status, True)
|
|
|
|
|
2021-04-11 20:35:25 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2021-02-22 18:53:57 +00:00
|
|
|
"""Turn the unit off, stopping any cleaning in progress as is."""
|
|
|
|
await self.perform_action_and_refresh(self.robot.set_power_status, False)
|
|
|
|
|
2021-04-11 20:35:25 +00:00
|
|
|
async def async_start(self) -> None:
|
2021-02-22 18:53:57 +00:00
|
|
|
"""Start a clean cycle."""
|
|
|
|
await self.perform_action_and_refresh(self.robot.start_cleaning)
|
|
|
|
|
2021-04-11 20:35:25 +00:00
|
|
|
async def async_set_sleep_mode(
|
|
|
|
self, enabled: bool, start_time: str | None = None
|
|
|
|
) -> None:
|
|
|
|
"""Set the sleep mode."""
|
|
|
|
await self.perform_action_and_refresh(
|
|
|
|
self.robot.set_sleep_mode,
|
|
|
|
enabled,
|
|
|
|
self.parse_time_at_default_timezone(start_time),
|
|
|
|
)
|
|
|
|
|
2021-02-22 18:53:57 +00:00
|
|
|
@property
|
2021-04-11 20:35:25 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2021-02-22 18:53:57 +00:00
|
|
|
"""Return device specific state attributes."""
|
|
|
|
return {
|
|
|
|
"is_sleeping": self.robot.is_sleeping,
|
2021-04-11 20:35:25 +00:00
|
|
|
"sleep_mode_enabled": self.robot.sleep_mode_enabled,
|
2021-02-22 18:53:57 +00:00
|
|
|
"power_status": self.robot.power_status,
|
2021-10-30 22:27:12 +00:00
|
|
|
"status": self.status,
|
2021-02-22 18:53:57 +00:00
|
|
|
}
|