core/homeassistant/components/litterrobot/vacuum.py

125 lines
3.9 KiB
Python
Raw Normal View History

2021-02-22 18:53:57 +00:00
"""Support for Litter-Robot "Vacuum"."""
from __future__ import annotations
2021-11-15 19:09:22 +00:00
import logging
from typing import Any
from pylitterbot.enums import LitterBoxStatus
import voluptuous as vol
2021-02-22 18:53:57 +00:00
from homeassistant.components.vacuum import (
STATE_CLEANING,
STATE_DOCKED,
STATE_ERROR,
STATE_PAUSED,
StateVacuumEntity,
VacuumEntityFeature,
2021-02-22 18:53:57 +00:00
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_OFF
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2021-02-22 18:53:57 +00:00
from .const import DOMAIN
from .entity import LitterRobotControlEntity
from .hub import LitterRobotHub
2021-02-22 18:53:57 +00:00
2021-11-15 19:09:22 +00:00
_LOGGER = logging.getLogger(__name__)
2021-02-22 18:53:57 +00:00
TYPE_LITTER_BOX = "Litter Box"
SERVICE_SET_SLEEP_MODE = "set_sleep_mode"
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,
}
2021-02-22 18:53:57 +00:00
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
2021-02-22 18:53:57 +00:00
"""Set up Litter-Robot cleaner using config entry."""
hub: LitterRobotHub = hass.data[DOMAIN][entry.entry_id]
2021-02-22 18:53:57 +00:00
async_add_entities(
LitterRobotCleaner(robot=robot, entity_type=TYPE_LITTER_BOX, hub=hub)
for robot in hub.litter_robots()
)
platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(
SERVICE_SET_SLEEP_MODE,
{
vol.Required("enabled"): cv.boolean,
vol.Optional("start_time"): cv.time,
},
"async_set_sleep_mode",
)
class LitterRobotCleaner(LitterRobotControlEntity, StateVacuumEntity):
2021-02-22 18:53:57 +00:00
"""Litter-Robot "Vacuum" Cleaner."""
_attr_supported_features = (
VacuumEntityFeature.START
| VacuumEntityFeature.STATE
| VacuumEntityFeature.STATUS
| VacuumEntityFeature.TURN_OFF
| VacuumEntityFeature.TURN_ON
)
2021-02-22 18:53:57 +00:00
@property
def state(self) -> str:
2021-02-22 18:53:57 +00:00
"""Return the state of the cleaner."""
return LITTER_BOX_STATUS_STATE_MAP.get(self.robot.status, STATE_ERROR)
2021-02-22 18:53:57 +00:00
@property
def status(self) -> str:
2021-02-22 18:53:57 +00:00
"""Return the status of the cleaner."""
return (
f"{self.robot.status.text}{' (Sleeping)' if self.robot.is_sleeping else ''}"
)
2021-02-22 18:53:57 +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)
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)
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)
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
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,
"sleep_mode_enabled": self.robot.sleep_mode_enabled,
2021-02-22 18:53:57 +00:00
"power_status": self.robot.power_status,
"status": self.status,
2021-02-22 18:53:57 +00:00
}