Add EntityFeature enum to Lock (#69118)

This commit is contained in:
Franck Nijhof 2022-04-03 05:57:32 +02:00 committed by GitHub
parent ea148a1b8e
commit 721db6d962
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 9 deletions

View file

@ -3,7 +3,7 @@ from __future__ import annotations
import asyncio import asyncio
from homeassistant.components.lock import SUPPORT_OPEN, LockEntity from homeassistant.components.lock import LockEntity, LockEntityFeature
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
STATE_JAMMED, STATE_JAMMED,
@ -60,7 +60,7 @@ class DemoLock(LockEntity):
"""Initialize the lock.""" """Initialize the lock."""
self._attr_name = name self._attr_name = name
if openable: if openable:
self._attr_supported_features = SUPPORT_OPEN self._attr_supported_features = LockEntityFeature.OPEN
self._state = state self._state = state
self._openable = openable self._openable = openable
self._jam_on_operation = jam_on_operation self._jam_on_operation = jam_on_operation
@ -113,5 +113,5 @@ class DemoLock(LockEntity):
def supported_features(self): def supported_features(self):
"""Flag supported features.""" """Flag supported features."""
if self._openable: if self._openable:
return SUPPORT_OPEN return LockEntityFeature.OPEN
return 0 return 0

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
@ -46,7 +47,15 @@ MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
LOCK_SERVICE_SCHEMA = make_entity_service_schema({vol.Optional(ATTR_CODE): cv.string}) LOCK_SERVICE_SCHEMA = make_entity_service_schema({vol.Optional(ATTR_CODE): cv.string})
# Bitfield of features supported by the lock entity
class LockEntityFeature(IntEnum):
"""Supported features of the lock entity."""
OPEN = 1
# The SUPPORT_OPEN constant is deprecated as of Home Assistant 2022.5.
# Please use the LockEntityFeature enum instead.
SUPPORT_OPEN = 1 SUPPORT_OPEN = 1
PROP_TO_ATTR = {"changed_by": ATTR_CHANGED_BY, "code_format": ATTR_CODE_FORMAT} PROP_TO_ATTR = {"changed_by": ATTR_CHANGED_BY, "code_format": ATTR_CODE_FORMAT}

View file

@ -18,7 +18,7 @@ from homeassistant.helpers import entity_registry
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import get_supported_features from homeassistant.helpers.entity import get_supported_features
from . import DOMAIN, SUPPORT_OPEN from . import DOMAIN, LockEntityFeature
ACTION_TYPES = {"lock", "unlock", "open"} ACTION_TYPES = {"lock", "unlock", "open"}
@ -54,7 +54,7 @@ async def async_get_actions(
actions.append({**base_action, CONF_TYPE: "lock"}) actions.append({**base_action, CONF_TYPE: "lock"})
actions.append({**base_action, CONF_TYPE: "unlock"}) actions.append({**base_action, CONF_TYPE: "unlock"})
if supported_features & (SUPPORT_OPEN): if supported_features & (LockEntityFeature.OPEN):
actions.append({**base_action, CONF_TYPE: "open"}) actions.append({**base_action, CONF_TYPE: "open"})
return actions return actions

View file

@ -3,7 +3,7 @@ import pytest
import homeassistant.components.automation as automation import homeassistant.components.automation as automation
from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.components.lock import DOMAIN, SUPPORT_OPEN from homeassistant.components.lock import DOMAIN, LockEntityFeature
from homeassistant.helpers import device_registry from homeassistant.helpers import device_registry
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -34,9 +34,9 @@ def entity_reg(hass):
"set_state,features_reg,features_state,expected_action_types", "set_state,features_reg,features_state,expected_action_types",
[ [
(False, 0, 0, []), (False, 0, 0, []),
(False, SUPPORT_OPEN, 0, ["open"]), (False, LockEntityFeature.OPEN, 0, ["open"]),
(True, 0, 0, []), (True, 0, 0, []),
(True, 0, SUPPORT_OPEN, ["open"]), (True, 0, LockEntityFeature.OPEN, ["open"]),
], ],
) )
async def test_get_actions( async def test_get_actions(