8000 Add support for STATES of vacuums by cnrd · Pull Request #15573 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for STATES of vacuums #15573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
221 changes: 136 additions & 85 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,122 +242,176 @@ 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.
async 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.
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.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.
async def async_stop(self, **kwargs):
"""Stop the vacuum cleaner.

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.stop, **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):
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 and returns a coroutine.
This method must be run in the event loop.
"""
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))
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()

def async_clean_spot(self, **kwargs):
async def async_clean_spot(self, **kwargs):
"""Perform a spot clean-up.

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.clean_spot, **kwargs))
await self.hass.async_add_executor_job(
partial(self.clean_spot, **kwargs))

def locate(self, **kwargs):
"""Locate the vacuum cleaner."""
raise NotImplementedError()

def async_locate(self, **kwargs):
async def async_locate(self, **kwargs):
"""Locate the vacuum cleaner.

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.locate, **kwargs))
await self.hass.async_add_executor_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):
async 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.
This method must be run in the event loop.
"""
return self.hass.async_add_job(
await 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.
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 and returns a coroutine.
This method must be run in the event loop.
"""
return self.hass.async_add_job(
partial(self.start_pause, **kwargs))
await 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.
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.send_command, command, params=params, **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()

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.
"""
await 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
0