Add EntityFeature enum to Cover (#69088)

This commit is contained in:
Franck Nijhof 2022-04-01 18:38:21 +02:00 committed by GitHub
parent 93571c2d01
commit 330c931067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 28 deletions

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from enum import IntEnum
import functools as ft import functools as ft
import logging import logging
from typing import Any, final from typing import Any, final
@ -79,6 +80,22 @@ DEVICE_CLASS_SHADE = CoverDeviceClass.SHADE.value
DEVICE_CLASS_SHUTTER = CoverDeviceClass.SHUTTER.value DEVICE_CLASS_SHUTTER = CoverDeviceClass.SHUTTER.value
DEVICE_CLASS_WINDOW = CoverDeviceClass.WINDOW.value DEVICE_CLASS_WINDOW = CoverDeviceClass.WINDOW.value
class CoverEntityFeature(IntEnum):
"""Supported features of the cover entity."""
OPEN = 1
CLOSE = 2
SET_POSITION = 4
STOP = 8
OPEN_TILT = 16
CLOSE_TILT = 32
STOP_TILT = 64
SET_TILT_POSITION = 128
# These SUPPORT_* constants are deprecated as of Home Assistant 2022.5.
# Please use the CoverEntityFeature enum instead.
SUPPORT_OPEN = 1 SUPPORT_OPEN = 1
SUPPORT_CLOSE = 2 SUPPORT_CLOSE = 2
SUPPORT_SET_POSITION = 4 SUPPORT_SET_POSITION = 4
@ -109,11 +126,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
await component.async_setup(config) await component.async_setup(config)
component.async_register_entity_service( component.async_register_entity_service(
SERVICE_OPEN_COVER, {}, "async_open_cover", [SUPPORT_OPEN] SERVICE_OPEN_COVER, {}, "async_open_cover", [CoverEntityFeature.OPEN]
) )
component.async_register_entity_service( component.async_register_entity_service(
SERVICE_CLOSE_COVER, {}, "async_close_cover", [SUPPORT_CLOSE] SERVICE_CLOSE_COVER, {}, "async_close_cover", [CoverEntityFeature.CLOSE]
) )
component.async_register_entity_service( component.async_register_entity_service(
@ -124,27 +141,39 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
) )
}, },
"async_set_cover_position", "async_set_cover_position",
[SUPPORT_SET_POSITION], [CoverEntityFeature.SET_POSITION],
) )
component.async_register_entity_service( component.async_register_entity_service(
SERVICE_STOP_COVER, {}, "async_stop_cover", [SUPPORT_STOP] SERVICE_STOP_COVER, {}, "async_stop_cover", [CoverEntityFeature.STOP]
) )
component.async_register_entity_service( component.async_register_entity_service(
SERVICE_TOGGLE, {}, "async_toggle", [SUPPORT_OPEN | SUPPORT_CLOSE] SERVICE_TOGGLE,
{},
"async_toggle",
[CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE],
) )
component.async_register_entity_service( component.async_register_entity_service(
SERVICE_OPEN_COVER_TILT, {}, "async_open_cover_tilt", [SUPPORT_OPEN_TILT] SERVICE_OPEN_COVER_TILT,
{},
"async_open_cover_tilt",
[CoverEntityFeature.OPEN_TILT],
) )
component.async_register_entity_service( component.async_register_entity_service(
SERVICE_CLOSE_COVER_TILT, {}, "async_close_cover_tilt", [SUPPORT_CLOSE_TILT] SERVICE_CLOSE_COVER_TILT,
{},
"async_close_cover_tilt",
[CoverEntityFeature.CLOSE_TILT],
) )
component.async_register_entity_service( component.async_register_entity_service(
SERVICE_STOP_COVER_TILT, {}, "async_stop_cover_tilt", [SUPPORT_STOP_TILT] SERVICE_STOP_COVER_TILT,
{},
"async_stop_cover_tilt",
[CoverEntityFeature.STOP_TILT],
) )
component.async_register_entity_service( component.async_register_entity_service(
@ -155,14 +184,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
) )
}, },
"async_set_cover_tilt_position", "async_set_cover_tilt_position",
[SUPPORT_SET_TILT_POSITION], [CoverEntityFeature.SET_TILT_POSITION],
) )
component.async_register_entity_service( component.async_register_entity_service(
SERVICE_TOGGLE_COVER_TILT, SERVICE_TOGGLE_COVER_TILT,
{}, {},
"async_toggle_tilt", "async_toggle_tilt",
[SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT], [CoverEntityFeature.OPEN_TILT | CoverEntityFeature.CLOSE_TILT],
) )
return True return True
@ -262,17 +291,19 @@ class CoverEntity(Entity):
if self._attr_supported_features is not None: if self._attr_supported_features is not None:
return self._attr_supported_features return self._attr_supported_features
supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP supported_features = (
CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP
)
if self.current_cover_position is not None: if self.current_cover_position is not None:
supported_features |= SUPPORT_SET_POSITION supported_features |= CoverEntityFeature.SET_POSITION
if self.current_cover_tilt_position is not None: if self.current_cover_tilt_position is not None:
supported_features |= ( supported_features |= (
SUPPORT_OPEN_TILT CoverEntityFeature.OPEN_TILT
| SUPPORT_CLOSE_TILT | CoverEntityFeature.CLOSE_TILT
| SUPPORT_STOP_TILT | CoverEntityFeature.STOP_TILT
| SUPPORT_SET_TILT_POSITION | CoverEntityFeature.SET_TILT_POSITION
) )
return supported_features return supported_features
@ -395,7 +426,7 @@ class CoverEntity(Entity):
await self.async_close_cover_tilt(**kwargs) await self.async_close_cover_tilt(**kwargs)
def _get_toggle_function(self, fns): def _get_toggle_function(self, fns):
if SUPPORT_STOP | self.supported_features and ( if CoverEntityFeature.STOP | self.supported_features and (
self.is_closing or self.is_opening self.is_closing or self.is_opening
): ):
return fns["stop"] return fns["stop"]

View File

@ -4,14 +4,9 @@ from __future__ import annotations
from homeassistant.components.cover import ( from homeassistant.components.cover import (
ATTR_POSITION, ATTR_POSITION,
ATTR_TILT_POSITION, ATTR_TILT_POSITION,
SUPPORT_CLOSE,
SUPPORT_CLOSE_TILT,
SUPPORT_OPEN,
SUPPORT_OPEN_TILT,
SUPPORT_SET_TILT_POSITION,
SUPPORT_STOP_TILT,
CoverDeviceClass, CoverDeviceClass,
CoverEntity, CoverEntity,
CoverEntityFeature,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -40,7 +35,7 @@ async def async_setup_platform(
"cover_4", "cover_4",
"Garage Door", "Garage Door",
device_class=CoverDeviceClass.GARAGE, device_class=CoverDeviceClass.GARAGE,
supported_features=(SUPPORT_OPEN | SUPPORT_CLOSE), supported_features=(CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE),
), ),
DemoCover( DemoCover(
hass, hass,
@ -48,10 +43,10 @@ async def async_setup_platform(
"Pergola Roof", "Pergola Roof",
tilt_position=60, tilt_position=60,
supported_features=( supported_features=(
SUPPORT_OPEN_TILT CoverEntityFeature.OPEN_TILT
| SUPPORT_STOP_TILT | CoverEntityFeature.STOP_TILT
| SUPPORT_CLOSE_TILT | CoverEntityFeature.CLOSE_TILT
| SUPPORT_SET_TILT_POSITION | CoverEntityFeature.SET_TILT_POSITION
), ),
), ),
] ]