diff --git a/homeassistant/components/demo/lock.py b/homeassistant/components/demo/lock.py index 63f2d218957..cafc0e3f748 100644 --- a/homeassistant/components/demo/lock.py +++ b/homeassistant/components/demo/lock.py @@ -22,44 +22,26 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class DemoLock(LockEntity): """Representation of a Demo lock.""" - def __init__(self, name, state, openable=False): + _attr_should_poll = False + + def __init__(self, name: str, state: str, openable: bool = False) -> None: """Initialize the lock.""" - self._name = name - self._state = state - self._openable = openable - - @property - def should_poll(self): - """No polling needed for a demo lock.""" - return False - - @property - def name(self): - """Return the name of the lock if any.""" - return self._name - - @property - def is_locked(self): - """Return true if lock is locked.""" - return self._state == STATE_LOCKED + self._attr_name = name + self._attr_is_locked = state == STATE_LOCKED + if openable: + self._attr_supported_features = SUPPORT_OPEN def lock(self, **kwargs): """Lock the device.""" - self._state = STATE_LOCKED + self._attr_is_locked = True self.schedule_update_ha_state() def unlock(self, **kwargs): """Unlock the device.""" - self._state = STATE_UNLOCKED + self._attr_is_locked = False self.schedule_update_ha_state() def open(self, **kwargs): """Open the door latch.""" - self._state = STATE_UNLOCKED + self._attr_is_locked = False self.schedule_update_ha_state() - - @property - def supported_features(self): - """Flag supported features.""" - if self._openable: - return SUPPORT_OPEN diff --git a/homeassistant/components/lock/__init__.py b/homeassistant/components/lock/__init__.py index c74289b8794..1ca7404d56a 100644 --- a/homeassistant/components/lock/__init__.py +++ b/homeassistant/components/lock/__init__.py @@ -1,4 +1,6 @@ """Component to interface with locks that can be controlled remotely.""" +from __future__ import annotations + from datetime import timedelta import functools as ft import logging @@ -83,20 +85,25 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: class LockEntity(Entity): """Base class for lock entities.""" + _attr_changed_by: str | None = None + _attr_code_format: str | None = None + _attr_is_locked: bool | None = None + _attr_state: None = None + @property - def changed_by(self): + def changed_by(self) -> str | None: """Last change triggered by.""" - return None + return self._attr_changed_by @property - def code_format(self): + def code_format(self) -> str | None: """Regex for code format or None if no code is required.""" - return None + return self._attr_code_format @property - def is_locked(self): + def is_locked(self) -> bool | None: """Return true if the lock is locked.""" - return None + return self._attr_is_locked def lock(self, **kwargs): """Lock the lock.""" @@ -133,8 +140,9 @@ class LockEntity(Entity): state_attr[attr] = value return state_attr + @final @property - def state(self): + def state(self) -> str | None: """Return the state.""" locked = self.is_locked if locked is None: