From 721db6d962adef9a74308f3ca66277482e3e3e8c Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sun, 3 Apr 2022 05:57:32 +0200 Subject: [PATCH] Add EntityFeature enum to Lock (#69118) --- homeassistant/components/demo/lock.py | 6 +++--- homeassistant/components/lock/__init__.py | 11 ++++++++++- homeassistant/components/lock/device_action.py | 4 ++-- tests/components/lock/test_device_action.py | 6 +++--- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/demo/lock.py b/homeassistant/components/demo/lock.py index c44d10ed8f1..86188e8b935 100644 --- a/homeassistant/components/demo/lock.py +++ b/homeassistant/components/demo/lock.py @@ -3,7 +3,7 @@ from __future__ import annotations 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.const import ( STATE_JAMMED, @@ -60,7 +60,7 @@ class DemoLock(LockEntity): """Initialize the lock.""" self._attr_name = name if openable: - self._attr_supported_features = SUPPORT_OPEN + self._attr_supported_features = LockEntityFeature.OPEN self._state = state self._openable = openable self._jam_on_operation = jam_on_operation @@ -113,5 +113,5 @@ class DemoLock(LockEntity): def supported_features(self): """Flag supported features.""" if self._openable: - return SUPPORT_OPEN + return LockEntityFeature.OPEN return 0 diff --git a/homeassistant/components/lock/__init__.py b/homeassistant/components/lock/__init__.py index 60c7c91152f..b94cd33a015 100644 --- a/homeassistant/components/lock/__init__.py +++ b/homeassistant/components/lock/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations from dataclasses import dataclass from datetime import timedelta +from enum import IntEnum import functools as ft import logging 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}) -# 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 PROP_TO_ATTR = {"changed_by": ATTR_CHANGED_BY, "code_format": ATTR_CODE_FORMAT} diff --git a/homeassistant/components/lock/device_action.py b/homeassistant/components/lock/device_action.py index 50c205d113a..092aff8878d 100644 --- a/homeassistant/components/lock/device_action.py +++ b/homeassistant/components/lock/device_action.py @@ -18,7 +18,7 @@ from homeassistant.helpers import entity_registry import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import get_supported_features -from . import DOMAIN, SUPPORT_OPEN +from . import DOMAIN, LockEntityFeature 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: "unlock"}) - if supported_features & (SUPPORT_OPEN): + if supported_features & (LockEntityFeature.OPEN): actions.append({**base_action, CONF_TYPE: "open"}) return actions diff --git a/tests/components/lock/test_device_action.py b/tests/components/lock/test_device_action.py index 4ee03bcbb0f..1f533e8d222 100644 --- a/tests/components/lock/test_device_action.py +++ b/tests/components/lock/test_device_action.py @@ -3,7 +3,7 @@ import pytest import homeassistant.components.automation as automation 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.setup import async_setup_component @@ -34,9 +34,9 @@ def entity_reg(hass): "set_state,features_reg,features_state,expected_action_types", [ (False, 0, 0, []), - (False, SUPPORT_OPEN, 0, ["open"]), + (False, LockEntityFeature.OPEN, 0, ["open"]), (True, 0, 0, []), - (True, 0, SUPPORT_OPEN, ["open"]), + (True, 0, LockEntityFeature.OPEN, ["open"]), ], ) async def test_get_actions(