Add EntityFeature enum to Vacuum (#69121)
This commit is contained in:
parent
cce19dc480
commit
2d37066ce5
2 changed files with 73 additions and 59 deletions
|
@ -8,21 +8,9 @@ from homeassistant.components.vacuum import (
|
|||
STATE_IDLE,
|
||||
STATE_PAUSED,
|
||||
STATE_RETURNING,
|
||||
SUPPORT_BATTERY,
|
||||
SUPPORT_CLEAN_SPOT,
|
||||
SUPPORT_FAN_SPEED,
|
||||
SUPPORT_LOCATE,
|
||||
SUPPORT_PAUSE,
|
||||
SUPPORT_RETURN_HOME,
|
||||
SUPPORT_SEND_COMMAND,
|
||||
SUPPORT_START,
|
||||
SUPPORT_STATE,
|
||||
SUPPORT_STATUS,
|
||||
SUPPORT_STOP,
|
||||
SUPPORT_TURN_OFF,
|
||||
SUPPORT_TURN_ON,
|
||||
StateVacuumEntity,
|
||||
VacuumEntity,
|
||||
VacuumEntityFeature,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
@ -30,44 +18,47 @@ from homeassistant.helpers import event
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
SUPPORT_MINIMAL_SERVICES = SUPPORT_TURN_ON | SUPPORT_TURN_OFF
|
||||
SUPPORT_MINIMAL_SERVICES = VacuumEntityFeature.TURN_ON | VacuumEntityFeature.TURN_OFF
|
||||
|
||||
SUPPORT_BASIC_SERVICES = (
|
||||
SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_STATUS | SUPPORT_BATTERY
|
||||
VacuumEntityFeature.TURN_ON
|
||||
| VacuumEntityFeature.TURN_OFF
|
||||
| VacuumEntityFeature.STATUS
|
||||
| VacuumEntityFeature.BATTERY
|
||||
)
|
||||
|
||||
SUPPORT_MOST_SERVICES = (
|
||||
SUPPORT_TURN_ON
|
||||
| SUPPORT_TURN_OFF
|
||||
| SUPPORT_STOP
|
||||
| SUPPORT_RETURN_HOME
|
||||
| SUPPORT_STATUS
|
||||
| SUPPORT_BATTERY
|
||||
VacuumEntityFeature.TURN_ON
|
||||
| VacuumEntityFeature.TURN_OFF
|
||||
| VacuumEntityFeature.STOP
|
||||
| VacuumEntityFeature.RETURN_HOME
|
||||
| VacuumEntityFeature.STATUS
|
||||
| VacuumEntityFeature.BATTERY
|
||||
)
|
||||
|
||||
SUPPORT_ALL_SERVICES = (
|
||||
SUPPORT_TURN_ON
|
||||
| SUPPORT_TURN_OFF
|
||||
| SUPPORT_PAUSE
|
||||
| SUPPORT_STOP
|
||||
| SUPPORT_RETURN_HOME
|
||||
| SUPPORT_FAN_SPEED
|
||||
| SUPPORT_SEND_COMMAND
|
||||
| SUPPORT_LOCATE
|
||||
| SUPPORT_STATUS
|
||||
| SUPPORT_BATTERY
|
||||
| SUPPORT_CLEAN_SPOT
|
||||
VacuumEntityFeature.TURN_ON
|
||||
| VacuumEntityFeature.TURN_OFF
|
||||
| VacuumEntityFeature.PAUSE
|
||||
| VacuumEntityFeature.STOP
|
||||
| VacuumEntityFeature.RETURN_HOME
|
||||
| VacuumEntityFeature.FAN_SPEED
|
||||
| VacuumEntityFeature.SEND_COMMAND
|
||||
| VacuumEntityFeature.LOCATE
|
||||
| VacuumEntityFeature.STATUS
|
||||
| VacuumEntityFeature.BATTERY
|
||||
| VacuumEntityFeature.CLEAN_SPOT
|
||||
)
|
||||
|
||||
SUPPORT_STATE_SERVICES = (
|
||||
SUPPORT_STATE
|
||||
| SUPPORT_PAUSE
|
||||
| SUPPORT_STOP
|
||||
| SUPPORT_RETURN_HOME
|
||||
| SUPPORT_FAN_SPEED
|
||||
| SUPPORT_BATTERY
|
||||
| SUPPORT_CLEAN_SPOT
|
||||
| SUPPORT_START
|
||||
VacuumEntityFeature.STATE
|
||||
| VacuumEntityFeature.PAUSE
|
||||
| VacuumEntityFeature.STOP
|
||||
| VacuumEntityFeature.RETURN_HOME
|
||||
| VacuumEntityFeature.FAN_SPEED
|
||||
| VacuumEntityFeature.BATTERY
|
||||
| VacuumEntityFeature.CLEAN_SPOT
|
||||
| VacuumEntityFeature.START
|
||||
)
|
||||
|
||||
FAN_SPEEDS = ["min", "medium", "high", "max"]
|
||||
|
@ -167,7 +158,7 @@ class DemoVacuum(VacuumEntity):
|
|||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the vacuum on."""
|
||||
if self.supported_features & SUPPORT_TURN_ON == 0:
|
||||
if self.supported_features & VacuumEntityFeature.TURN_ON == 0:
|
||||
return
|
||||
|
||||
self._state = True
|
||||
|
@ -178,7 +169,7 @@ class DemoVacuum(VacuumEntity):
|
|||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the vacuum off."""
|
||||
if self.supported_features & SUPPORT_TURN_OFF == 0:
|
||||
if self.supported_features & VacuumEntityFeature.TURN_OFF == 0:
|
||||
return
|
||||
|
||||
self._state = False
|
||||
|
@ -187,7 +178,7 @@ class DemoVacuum(VacuumEntity):
|
|||
|
||||
def stop(self, **kwargs):
|
||||
"""Stop the vacuum."""
|
||||
if self.supported_features & SUPPORT_STOP == 0:
|
||||
if self.supported_features & VacuumEntityFeature.STOP == 0:
|
||||
return
|
||||
|
||||
self._state = False
|
||||
|
@ -196,7 +187,7 @@ class DemoVacuum(VacuumEntity):
|
|||
|
||||
def clean_spot(self, **kwargs):
|
||||
"""Perform a spot clean-up."""
|
||||
if self.supported_features & SUPPORT_CLEAN_SPOT == 0:
|
||||
if self.supported_features & VacuumEntityFeature.CLEAN_SPOT == 0:
|
||||
return
|
||||
|
||||
self._state = True
|
||||
|
@ -207,7 +198,7 @@ class DemoVacuum(VacuumEntity):
|
|||
|
||||
def locate(self, **kwargs):
|
||||
"""Locate the vacuum (usually by playing a song)."""
|
||||
if self.supported_features & SUPPORT_LOCATE == 0:
|
||||
if self.supported_features & VacuumEntityFeature.LOCATE == 0:
|
||||
return
|
||||
|
||||
self._status = "Hi, I'm over here!"
|
||||
|
@ -215,7 +206,7 @@ class DemoVacuum(VacuumEntity):
|
|||
|
||||
def start_pause(self, **kwargs):
|
||||
"""Start, pause or resume the cleaning task."""
|
||||
if self.supported_features & SUPPORT_PAUSE == 0:
|
||||
if self.supported_features & VacuumEntityFeature.PAUSE == 0:
|
||||
return
|
||||
|
||||
self._state = not self._state
|
||||
|
@ -229,7 +220,7 @@ class DemoVacuum(VacuumEntity):
|
|||
|
||||
def set_fan_speed(self, fan_speed, **kwargs):
|
||||
"""Set the vacuum's fan speed."""
|
||||
if self.supported_features & SUPPORT_FAN_SPEED == 0:
|
||||
if self.supported_features & VacuumEntityFeature.FAN_SPEED == 0:
|
||||
return
|
||||
|
||||
if fan_speed in self.fan_speed_list:
|
||||
|
@ -238,7 +229,7 @@ class DemoVacuum(VacuumEntity):
|
|||
|
||||
def return_to_base(self, **kwargs):
|
||||
"""Tell the vacuum to return to its dock."""
|
||||
if self.supported_features & SUPPORT_RETURN_HOME == 0:
|
||||
if self.supported_features & VacuumEntityFeature.RETURN_HOME == 0:
|
||||
return
|
||||
|
||||
self._state = False
|
||||
|
@ -248,7 +239,7 @@ class DemoVacuum(VacuumEntity):
|
|||
|
||||
def send_command(self, command, params=None, **kwargs):
|
||||
"""Send a command to the vacuum."""
|
||||
if self.supported_features & SUPPORT_SEND_COMMAND == 0:
|
||||
if self.supported_features & VacuumEntityFeature.SEND_COMMAND == 0:
|
||||
return
|
||||
|
||||
self._status = f"Executing {command}({params})"
|
||||
|
@ -310,7 +301,7 @@ class StateDemoVacuum(StateVacuumEntity):
|
|||
|
||||
def start(self):
|
||||
"""Start or resume the cleaning task."""
|
||||
if self.supported_features & SUPPORT_START == 0:
|
||||
if self.supported_features & VacuumEntityFeature.START == 0:
|
||||
return
|
||||
|
||||
if self._state != STATE_CLEANING:
|
||||
|
@ -321,7 +312,7 @@ class StateDemoVacuum(StateVacuumEntity):
|
|||
|
||||
def pause(self):
|
||||
"""Pause the cleaning task."""
|
||||
if self.supported_features & SUPPORT_PAUSE == 0:
|
||||
if self.supported_features & VacuumEntityFeature.PAUSE == 0:
|
||||
return
|
||||
|
||||
if self._state == STATE_CLEANING:
|
||||
|
@ -330,7 +321,7 @@ class StateDemoVacuum(StateVacuumEntity):
|
|||
|
||||
def stop(self, **kwargs):
|
||||
"""Stop the cleaning task, do not return to dock."""
|
||||
if self.supported_features & SUPPORT_STOP == 0:
|
||||
if self.supported_features & VacuumEntityFeature.STOP == 0:
|
||||
return
|
||||
|
||||
self._state = STATE_IDLE
|
||||
|
@ -338,7 +329,7 @@ class StateDemoVacuum(StateVacuumEntity):
|
|||
|
||||
def return_to_base(self, **kwargs):
|
||||
"""Return dock to charging base."""
|
||||
if self.supported_features & SUPPORT_RETURN_HOME == 0:
|
||||
if self.supported_features & VacuumEntityFeature.RETURN_HOME == 0:
|
||||
return
|
||||
|
||||
self._state = STATE_RETURNING
|
||||
|
@ -348,7 +339,7 @@ class StateDemoVacuum(StateVacuumEntity):
|
|||
|
||||
def clean_spot(self, **kwargs):
|
||||
"""Perform a spot clean-up."""
|
||||
if self.supported_features & SUPPORT_CLEAN_SPOT == 0:
|
||||
if self.supported_features & VacuumEntityFeature.CLEAN_SPOT == 0:
|
||||
return
|
||||
|
||||
self._state = STATE_CLEANING
|
||||
|
@ -358,7 +349,7 @@ class StateDemoVacuum(StateVacuumEntity):
|
|||
|
||||
def set_fan_speed(self, fan_speed, **kwargs):
|
||||
"""Set the vacuum's fan speed."""
|
||||
if self.supported_features & SUPPORT_FAN_SPEED == 0:
|
||||
if self.supported_features & VacuumEntityFeature.FAN_SPEED == 0:
|
||||
return
|
||||
|
||||
if fan_speed in self.fan_speed_list:
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
"""Support for vacuum cleaner robots (botvacs)."""
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
from enum import IntEnum
|
||||
from functools import partial
|
||||
import logging
|
||||
from typing import final
|
||||
|
@ -71,6 +72,28 @@ STATES = [STATE_CLEANING, STATE_DOCKED, STATE_RETURNING, STATE_ERROR]
|
|||
|
||||
DEFAULT_NAME = "Vacuum cleaner robot"
|
||||
|
||||
|
||||
class VacuumEntityFeature(IntEnum):
|
||||
"""Supported features of the vacuum entity."""
|
||||
|
||||
TURN_ON = 1
|
||||
TURN_OFF = 2
|
||||
PAUSE = 4
|
||||
STOP = 8
|
||||
RETURN_HOME = 16
|
||||
FAN_SPEED = 32
|
||||
BATTERY = 64
|
||||
STATUS = 128
|
||||
SEND_COMMAND = 256
|
||||
LOCATE = 512
|
||||
CLEAN_SPOT = 1024
|
||||
MAP = 2048
|
||||
STATE = 4096
|
||||
START = 8192
|
||||
|
||||
|
||||
# These SUPPORT_* constants are deprecated as of Home Assistant 2022.5.
|
||||
# Please use the VacuumEntityFeature enum instead.
|
||||
SUPPORT_TURN_ON = 1
|
||||
SUPPORT_TURN_OFF = 2
|
||||
SUPPORT_PAUSE = 4
|
||||
|
@ -178,7 +201,7 @@ class _BaseVacuum(Entity):
|
|||
@property
|
||||
def capability_attributes(self):
|
||||
"""Return capability attributes."""
|
||||
if self.supported_features & SUPPORT_FAN_SPEED:
|
||||
if self.supported_features & VacuumEntityFeature.FAN_SPEED:
|
||||
return {ATTR_FAN_SPEED_LIST: self.fan_speed_list}
|
||||
|
||||
@property
|
||||
|
@ -186,11 +209,11 @@ class _BaseVacuum(Entity):
|
|||
"""Return the state attributes of the vacuum cleaner."""
|
||||
data = {}
|
||||
|
||||
if self.supported_features & SUPPORT_BATTERY:
|
||||
if self.supported_features & VacuumEntityFeature.BATTERY:
|
||||
data[ATTR_BATTERY_LEVEL] = self.battery_level
|
||||
data[ATTR_BATTERY_ICON] = self.battery_icon
|
||||
|
||||
if self.supported_features & SUPPORT_FAN_SPEED:
|
||||
if self.supported_features & VacuumEntityFeature.FAN_SPEED:
|
||||
data[ATTR_FAN_SPEED] = self.fan_speed
|
||||
|
||||
return data
|
||||
|
@ -297,7 +320,7 @@ class VacuumEntity(_BaseVacuum, ToggleEntity):
|
|||
"""Return the state attributes of the vacuum cleaner."""
|
||||
data = super().state_attributes
|
||||
|
||||
if self.supported_features & SUPPORT_STATUS:
|
||||
if self.supported_features & VacuumEntityFeature.STATUS:
|
||||
data[ATTR_STATUS] = self.status
|
||||
|
||||
return data
|
||||
|
|
Loading…
Add table
Reference in a new issue