mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Define LockEntity entity attributes as class variables (#51909)
This commit is contained in:
parent
d3724355cf
commit
927b5361a3
@ -22,44 +22,26 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
class DemoLock(LockEntity):
|
class DemoLock(LockEntity):
|
||||||
"""Representation of a Demo lock."""
|
"""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."""
|
"""Initialize the lock."""
|
||||||
self._name = name
|
self._attr_name = name
|
||||||
self._state = state
|
self._attr_is_locked = state == STATE_LOCKED
|
||||||
self._openable = openable
|
if openable:
|
||||||
|
self._attr_supported_features = SUPPORT_OPEN
|
||||||
@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
|
|
||||||
|
|
||||||
def lock(self, **kwargs):
|
def lock(self, **kwargs):
|
||||||
"""Lock the device."""
|
"""Lock the device."""
|
||||||
self._state = STATE_LOCKED
|
self._attr_is_locked = True
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
def unlock(self, **kwargs):
|
def unlock(self, **kwargs):
|
||||||
"""Unlock the device."""
|
"""Unlock the device."""
|
||||||
self._state = STATE_UNLOCKED
|
self._attr_is_locked = False
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
def open(self, **kwargs):
|
def open(self, **kwargs):
|
||||||
"""Open the door latch."""
|
"""Open the door latch."""
|
||||||
self._state = STATE_UNLOCKED
|
self._attr_is_locked = False
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
@property
|
|
||||||
def supported_features(self):
|
|
||||||
"""Flag supported features."""
|
|
||||||
if self._openable:
|
|
||||||
return SUPPORT_OPEN
|
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
"""Component to interface with locks that can be controlled remotely."""
|
"""Component to interface with locks that can be controlled remotely."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import functools as ft
|
import functools as ft
|
||||||
import logging
|
import logging
|
||||||
@ -83,20 +85,25 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
class LockEntity(Entity):
|
class LockEntity(Entity):
|
||||||
"""Base class for lock entities."""
|
"""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
|
@property
|
||||||
def changed_by(self):
|
def changed_by(self) -> str | None:
|
||||||
"""Last change triggered by."""
|
"""Last change triggered by."""
|
||||||
return None
|
return self._attr_changed_by
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def code_format(self):
|
def code_format(self) -> str | None:
|
||||||
"""Regex for code format or None if no code is required."""
|
"""Regex for code format or None if no code is required."""
|
||||||
return None
|
return self._attr_code_format
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_locked(self):
|
def is_locked(self) -> bool | None:
|
||||||
"""Return true if the lock is locked."""
|
"""Return true if the lock is locked."""
|
||||||
return None
|
return self._attr_is_locked
|
||||||
|
|
||||||
def lock(self, **kwargs):
|
def lock(self, **kwargs):
|
||||||
"""Lock the lock."""
|
"""Lock the lock."""
|
||||||
@ -133,8 +140,9 @@ class LockEntity(Entity):
|
|||||||
state_attr[attr] = value
|
state_attr[attr] = value
|
||||||
return state_attr
|
return state_attr
|
||||||
|
|
||||||
|
@final
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self) -> str | None:
|
||||||
"""Return the state."""
|
"""Return the state."""
|
||||||
locked = self.is_locked
|
locked = self.is_locked
|
||||||
if locked is None:
|
if locked is None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user