Add support for STATES of vacuums (#15573)
* Vacuum: Added support for STATES * Added debug logging and corrected state order * typo * Fix travis error, STATE = STATE for readability * status -> state * Changed to Entity instead of ToogleEntity * Updated some vacuums * Revert changes * Revert Changes * added SUPPORT_STATE * Woof? * Implement on/off if STATE not supported * Moved new state vaccum to Class StateVacuumDevice * Error: I should go to bed * Moved around methods for easier reading * Added StateVacuumDevice demo vacuum * Added tests for StateVacuumDevice demo vacuum * Fix styling errors * Refactored to BaseVaccum * Vacuum will now go back to dock * Class BaseVacuum is for internal use only * return -> await * return -> await
This commit is contained in:
1 parent
f8a478946e
commit
2ff5b4ce95
3 files changed
+344
-99
No files matched your search
@@ -14,12 +14,12 @@ import voluptuous as vol
|
||||
from homeassistant.components import group
|
||||
from homeassistant.const import (
|
||||
ATTR_BATTERY_LEVEL, ATTR_COMMAND, ATTR_ENTITY_ID, SERVICE_TOGGLE,
|
||||
SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_ON)
|
||||
SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_ON, STATE_PAUSED, STATE_IDLE)
|
||||
from homeassistant.loader import bind_hass
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.entity import ToggleEntity
|
||||
from homeassistant.helpers.entity import (ToggleEntity, Entity)
|
||||
from homeassistant.helpers.icon import icon_for_battery_level
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -75,6 +75,13 @@ SERVICE_TO_METHOD = {
|
||||
'schema': VACUUM_SEND_COMMAND_SERVICE_SCHEMA},
|
||||
}
|
||||
|
||||
STATE_CLEANING = 'cleaning'
|
||||
STATE_DOCKED = 'docked'
|
||||
STATE_IDLE = STATE_IDLE
|
||||
STATE_PAUSED = STATE_PAUSED
|
||||
STATE_RETURNING = 'returning'
|
||||
STATE_ERROR = 'error'
|
||||
|
||||
DEFAULT_NAME = 'Vacuum cleaner robot'
|
||||
|
||||
SUPPORT_TURN_ON = 1
|
||||
@@ -89,6 +96,7 @@ SUPPORT_SEND_COMMAND = 256
|
||||
SUPPORT_LOCATE = 512
|
||||
SUPPORT_CLEAN_SPOT = 1024
|
||||
SUPPORT_MAP = 2048
|
||||
SUPPORT_STATE = 4096
|
||||
|
||||
|
||||
@bind_hass
|
||||
@@ -208,33 +216,22 @@ def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
class VacuumDevice(ToggleEntity):
|
||||
"""Representation of a vacuum cleaner robot."""
|
||||
class _BaseVacuum(Entity):
|
||||
"""Representation of a base vacuum.
|
||||
|
||||
Contains common properties and functions for all vacuum devices.
|
||||
"""
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Flag vacuum cleaner features that are supported."""
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def status(self):
|
||||
"""Return the status of the vacuum cleaner."""
|
||||
return None
|
||||
|
||||
@property
|
||||
def battery_level(self):
|
||||
"""Return the battery level of the vacuum cleaner."""
|
||||
return None
|
||||
|
||||
@property
|
||||
def battery_icon(self):
|
||||
"""Return the battery icon for the vacuum cleaner."""
|
||||
charging = False
|
||||
if self.status is not None:
|
||||
charging = 'charg' in self.status.lower()
|
||||
return icon_for_battery_level(
|
||||
battery_level=self.battery_level, charging=charging)
|
||||
|
||||
@property
|
||||
def fan_speed(self):
|
||||
"""Return the fan speed of the vacuum cleaner."""
|
||||
@@ -245,6 +242,106 @@ class VacuumDevice(ToggleEntity):
|
||||
"""Get the list of available fan speed steps of the vacuum cleaner."""
|
||||
raise NotImplementedError()
|
||||
|
||||
def start_pause(self, **kwargs):
|
||||
"""Start, pause or resume the cleaning task."""
|
||||
raise NotImplementedError()
|
||||
|
||||
async def async_start_pause(self, **kwargs):
|
||||
"""Start, pause or resume the cleaning task.
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
await self.hass.async_add_executor_job(
|
||||
partial(self.start_pause, **kwargs))
|
||||
|
||||
def stop(self, **kwargs):
|
||||
"""Stop the vacuum cleaner."""
|
||||
raise NotImplementedError()
|
||||
|
||||
async def async_stop(self, **kwargs):
|
||||
"""Stop the vacuum cleaner.
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
await self.hass.async_add_executor_job(partial(self.stop, **kwargs))
|
||||
|
||||
def return_to_base(self, **kwargs):
|
||||
"""Set the vacuum cleaner to return to the dock."""
|
||||
raise NotImplementedError()
|
||||
|
||||
async def async_return_to_base(self, **kwargs):
|
||||
"""Set the vacuum cleaner to return to the dock.
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
await self.hass.async_add_executor_job(
|
||||
partial(self.return_to_base, **kwargs))
|
||||
|
||||
def clean_spot(self, **kwargs):
|
||||
"""Perform a spot clean-up."""
|
||||
raise NotImplementedError()
|
||||
|
||||
async def async_clean_spot(self, **kwargs):
|
||||
"""Perform a spot clean-up.
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
await self.hass.async_add_executor_job(
|
||||
partial(self.clean_spot, **kwargs))
|
||||
|
||||
def locate(self, **kwargs):
|
||||
"""Locate the vacuum cleaner."""
|
||||
raise NotImplementedError()
|
||||
|
||||
async def async_locate(self, **kwargs):
|
||||
"""Locate the vacuum cleaner.
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
await self.hass.async_add_executor_job(partial(self.locate, **kwargs))
|
||||
|
||||
def set_fan_speed(self, fan_speed, **kwargs):
|
||||
"""Set fan speed."""
|
||||
raise NotImplementedError()
|
||||
|
||||
async def async_set_fan_speed(self, fan_speed, **kwargs):
|
||||
"""Set fan speed.
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
await self.hass.async_add_executor_job(
|
||||
partial(self.set_fan_speed, fan_speed, **kwargs))
|
||||
|
||||
def send_command(self, command, params=None, **kwargs):
|
||||
"""Send a command to a vacuum cleaner."""
|
||||
raise NotImplementedError()
|
||||
|
||||
async def async_send_command(self, command, params=None, **kwargs):
|
||||
"""Send a command to a vacuum cleaner.
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
await self.hass.async_add_executor_job(
|
||||
partial(self.send_command, command, params=params, **kwargs))
|
||||
|
||||
|
||||
class VacuumDevice(_BaseVacuum, ToggleEntity):
|
||||
"""Representation of a vacuum cleaner robot."""
|
||||
|
||||
@property
|
||||
def status(self):
|
||||
"""Return the status of the vacuum cleaner."""
|
||||
return None
|
||||
|
||||
@property
|
||||
def battery_icon(self):
|
||||
"""Return the battery icon for the vacuum cleaner."""
|
||||
charging = False
|
||||
if self.status is not None:
|
||||
charging = 'charg' in self.status.lower()
|
||||
return icon_for_battery_level(
|
||||
battery_level=self.battery_level, charging=charging)
|
||||
|
||||
@property
|
||||
def state_attributes(self):
|
||||
"""Return the state attributes of the vacuum cleaner."""
|
||||
@@ -267,100 +364,54 @@ class VacuumDevice(ToggleEntity):
|
||||
"""Turn the vacuum on and start cleaning."""
|
||||
raise NotImplementedError()
|
||||
|
||||
def async_turn_on(self, **kwargs):
|
||||
async def async_turn_on(self, **kwargs):
|
||||
"""Turn the vacuum on and start cleaning.
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
return self.hass.async_add_job(partial(self.turn_on, **kwargs))
|
||||
await self.hass.async_add_executor_job(
|
||||
partial(self.turn_on, **kwargs))
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the vacuum off stopping the cleaning and returning home."""
|
||||
raise NotImplementedError()
|
||||
|
||||
def async_turn_off(self, **kwargs):
|
||||
async def async_turn_off(self, **kwargs):
|
||||
"""Turn the vacuum off stopping the cleaning and returning home.
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
return self.hass.async_add_job(partial(self.turn_off, **kwargs))
|
||||
await self.hass.async_add_executor_job(
|
||||
partial(self.turn_off, **kwargs))
|
||||
|
||||
def return_to_base(self, **kwargs):
|
||||
"""Set the vacuum cleaner to return to the dock."""
|
||||
raise NotImplementedError()
|
||||
|
||||
def async_return_to_base(self, **kwargs):
|
||||
"""Set the vacuum cleaner to return to the dock.
|
||||
class StateVacuumDevice(_BaseVacuum):
|
||||
"""Representation of a vacuum cleaner robot that supports states."""
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
return self.hass.async_add_job(partial(self.return_to_base, **kwargs))
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the vacuum cleaner."""
|
||||
return None
|
||||
|
||||
def stop(self, **kwargs):
|
||||
"""Stop the vacuum cleaner."""
|
||||
raise NotImplementedError()
|
||||
@property
|
||||
def battery_icon(self):
|
||||
"""Return the battery icon for the vacuum cleaner."""
|
||||
charging = bool(self.state == STATE_DOCKED)
|
||||
|
||||
def async_stop(self, **kwargs):
|
||||
"""Stop the vacuum cleaner.
|
||||
return icon_for_battery_level(
|
||||
battery_level=self.battery_level, charging=charging)
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
return self.hass.async_add_job(partial(self.stop, **kwargs))
|
||||
@property
|
||||
def state_attributes(self):
|
||||
"""Return the state attributes of the vacuum cleaner."""
|
||||
data = {}
|
||||
|
||||
def clean_spot(self, **kwargs):
|
||||
"""Perform a spot clean-up."""
|
||||
raise NotImplementedError()
|
||||
if self.battery_level is not None:
|
||||
data[ATTR_BATTERY_LEVEL] = self.battery_level
|
||||
data[ATTR_BATTERY_ICON] = self.battery_icon
|
||||
|
||||
def async_clean_spot(self, **kwargs):
|
||||
"""Perform a spot clean-up.
|
||||
if self.fan_speed is not None:
|
||||
data[ATTR_FAN_SPEED] = self.fan_speed
|
||||
data[ATTR_FAN_SPEED_LIST] = self.fan_speed_list
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
return self.hass.async_add_job(partial(self.clean_spot, **kwargs))
|
||||
|
||||
def locate(self, **kwargs):
|
||||
"""Locate the vacuum cleaner."""
|
||||
raise NotImplementedError()
|
||||
|
||||
def async_locate(self, **kwargs):
|
||||
"""Locate the vacuum cleaner.
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
return self.hass.async_add_job(partial(self.locate, **kwargs))
|
||||
|
||||
def set_fan_speed(self, fan_speed, **kwargs):
|
||||
"""Set fan speed."""
|
||||
raise NotImplementedError()
|
||||
|
||||
def async_set_fan_speed(self, fan_speed, **kwargs):
|
||||
"""Set fan speed.
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
return self.hass.async_add_job(
|
||||
partial(self.set_fan_speed, fan_speed, **kwargs))
|
||||
|
||||
def start_pause(self, **kwargs):
|
||||
"""Start, pause or resume the cleaning task."""
|
||||
raise NotImplementedError()
|
||||
|
||||
def async_start_pause(self, **kwargs):
|
||||
"""Start, pause or resume the cleaning task.
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
return self.hass.async_add_job(
|
||||
partial(self.start_pause, **kwargs))
|
||||
|
||||
def send_command(self, command, params=None, **kwargs):
|
||||
"""Send a command to a vacuum cleaner."""
|
||||
raise NotImplementedError()
|
||||
|
||||
def async_send_command(self, command, params=None, **kwargs):
|
||||
"""Send a command to a vacuum cleaner.
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
return self.hass.async_add_job(
|
||||
partial(self.send_command, command, params=params, **kwargs))
|
||||
return data
|
||||
Reference in new issue
Block a user