Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 124 additions & 73 deletions homeassistant/components/vacuum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
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__)
Expand Down Expand Up @@ -75,6 +75,13 @@
'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
Expand All @@ -89,6 +96,7 @@
SUPPORT_LOCATE = 512
SUPPORT_CLEAN_SPOT = 1024
SUPPORT_MAP = 2048
SUPPORT_STATE = 4096


@bind_hass
Expand Down Expand Up @@ -208,33 +216,22 @@ def async_handle_vacuum_service(service):
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."""
Expand All @@ -245,45 +242,28 @@ def fan_speed_list(self):
"""Get the list of available fan speed steps of the vacuum cleaner."""
raise NotImplementedError()

@property
def state_attributes(self):
"""Return the state attributes of the vacuum cleaner."""
data = {}

if self.status is not None:
data[ATTR_STATUS] = self.status

if self.battery_level is not None:
data[ATTR_BATTERY_LEVEL] = self.battery_level
data[ATTR_BATTERY_ICON] = self.battery_icon

if self.fan_speed is not None:
data[ATTR_FAN_SPEED] = self.fan_speed
data[ATTR_FAN_SPEED_LIST] = self.fan_speed_list

return data

def turn_on(self, **kwargs):
"""Turn the vacuum on and start cleaning."""
def start_pause(self, **kwargs):
"""Start, pause or resume the cleaning task."""
raise NotImplementedError()

def async_turn_on(self, **kwargs):
"""Turn the vacuum on and start cleaning.
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.turn_on, **kwargs))
return self.hass.async_add_executor_job(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should not return anything. Instead await the task returned by self.hass.async_add_executor_job.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also update the docstring to reflect the change.

partial(self.start_pause, **kwargs))

def turn_off(self, **kwargs):
"""Turn the vacuum off stopping the cleaning and returning home."""
def stop(self, **kwargs):
"""Stop the vacuum cleaner."""
raise NotImplementedError()

def async_turn_off(self, **kwargs):
"""Turn the vacuum off stopping the cleaning and returning home.
def async_stop(self, **kwargs):
"""Stop the vacuum cleaner.

This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(partial(self.turn_off, **kwargs))
return 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."""
Expand All @@ -294,18 +274,8 @@ def async_return_to_base(self, **kwargs):

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))

def stop(self, **kwargs):
"""Stop the vacuum cleaner."""
raise NotImplementedError()

def async_stop(self, **kwargs):
"""Stop the vacuum cleaner.

This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(partial(self.stop, **kwargs))
return self.hass.async_add_executor_job(
partial(self.return_to_base, **kwargs))

def clean_spot(self, **kwargs):
"""Perform a spot clean-up."""
Expand All @@ -316,7 +286,8 @@ def async_clean_spot(self, **kwargs):

This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(partial(self.clean_spot, **kwargs))
return self.hass.async_add_executor_job(
partial(self.clean_spot, **kwargs))

def locate(self, **kwargs):
"""Locate the vacuum cleaner."""
Expand All @@ -327,7 +298,7 @@ def async_locate(self, **kwargs):

This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(partial(self.locate, **kwargs))
return self.hass.async_add_executor_job(partial(self.locate, **kwargs))

def set_fan_speed(self, fan_speed, **kwargs):
"""Set fan speed."""
Expand All @@ -338,29 +309,109 @@ def async_set_fan_speed(self, fan_speed, **kwargs):

This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_job(
return self.hass.async_add_executor_job(
partial(self.set_fan_speed, fan_speed, **kwargs))

def start_pause(self, **kwargs):
"""Start, pause or resume the cleaning task."""
def send_command(self, command, params=None, **kwargs):
"""Send a command to a vacuum cleaner."""
raise NotImplementedError()

def async_start_pause(self, **kwargs):
"""Start, pause or resume the cleaning task.
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.start_pause, **kwargs))
return self.hass.async_add_executor_job(
partial(self.send_command, command, params=params, **kwargs))

def send_command(self, command, params=None, **kwargs):
"""Send a command to a vacuum cleaner."""

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."""
data = {}

if self.status is not None:
data[ATTR_STATUS] = self.status

if self.battery_level is not None:
data[ATTR_BATTERY_LEVEL] = self.battery_level
data[ATTR_BATTERY_ICON] = self.battery_icon

if self.fan_speed is not None:
data[ATTR_FAN_SPEED] = self.fan_speed
data[ATTR_FAN_SPEED_LIST] = self.fan_speed_list

return data

def turn_on(self, **kwargs):
"""Turn the vacuum on and start cleaning."""
raise NotImplementedError()

def async_send_command(self, command, params=None, **kwargs):
"""Send a command to a vacuum cleaner.
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.
"""
return self.hass.async_add_job(
partial(self.send_command, command, params=params, **kwargs))
return 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):
"""Turn the vacuum off stopping the cleaning and returning home.

This method must be run in the event loop and returns a coroutine.
"""
return self.hass.async_add_executor_job(
partial(self.turn_off, **kwargs))


class StateVacuumDevice(_BaseVacuum):
"""Representation of a vacuum cleaner robot that supports states."""

@property
def state(self):
"""Return the state of the vacuum cleaner."""
return None

@property
def battery_icon(self):
"""Return the battery icon for the vacuum cleaner."""
charging = bool(self.state == STATE_DOCKED)

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."""
data = {}

if self.battery_level is not None:
data[ATTR_BATTERY_LEVEL] = self.battery_level
data[ATTR_BATTERY_ICON] = self.battery_icon

if self.fan_speed is not None:
data[ATTR_FAN_SPEED] = self.fan_speed
data[ATTR_FAN_SPEED_LIST] = self.fan_speed_list

return data
Loading