1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-30 04:05:01 +01:00
Files
2026-04-30 21:14:48 +02:00

94 lines
2.9 KiB
Python

"""Support for ESPHome locks."""
from functools import partial
from typing import Any
from aioesphomeapi import EntityInfo, LockCommand, LockEntityState, LockInfo, LockState
from homeassistant.components.lock import LockEntity, LockEntityFeature
from homeassistant.const import ATTR_CODE
from homeassistant.core import callback
from .entity import (
EsphomeEntity,
convert_api_error_ha_error,
esphome_state_property,
platform_async_setup_entry,
)
PARALLEL_UPDATES = 0
class EsphomeLock(EsphomeEntity[LockInfo, LockEntityState], LockEntity):
"""A lock implementation for ESPHome."""
@callback
def _on_static_info_update(self, static_info: EntityInfo) -> None:
"""Set attrs from static info."""
super()._on_static_info_update(static_info)
static_info = self._static_info
self._attr_assumed_state = static_info.assumed_state
self._attr_supported_features = LockEntityFeature(0)
if static_info.supports_open:
self._attr_supported_features |= LockEntityFeature.OPEN
if static_info.requires_code:
self._attr_code_format = static_info.code_format
else:
self._attr_code_format = None
@property
@esphome_state_property
def is_locked(self) -> bool | None:
"""Return true if the lock is locked."""
if self._state.state is LockState.NONE:
return None
return self._state.state is LockState.LOCKED
@property
@esphome_state_property
def is_locking(self) -> bool:
"""Return true if the lock is locking."""
return self._state.state is LockState.LOCKING
@property
@esphome_state_property
def is_unlocking(self) -> bool:
"""Return true if the lock is unlocking."""
return self._state.state is LockState.UNLOCKING
@property
@esphome_state_property
def is_jammed(self) -> bool:
"""Return true if the lock is jammed (incomplete locking)."""
return self._state.state is LockState.JAMMED
@convert_api_error_ha_error
async def async_lock(self, **kwargs: Any) -> None:
"""Lock the lock."""
self._client.lock_command(
self._key, LockCommand.LOCK, device_id=self._static_info.device_id
)
@convert_api_error_ha_error
async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the lock."""
code = kwargs.get(ATTR_CODE)
self._client.lock_command(
self._key, LockCommand.UNLOCK, code, device_id=self._static_info.device_id
)
@convert_api_error_ha_error
async def async_open(self, **kwargs: Any) -> None:
"""Open the door latch."""
self._client.lock_command(
self._key, LockCommand.OPEN, device_id=self._static_info.device_id
)
async_setup_entry = partial(
platform_async_setup_entry,
info_type=LockInfo,
entity_type=EsphomeLock,
state_type=LockEntityState,
)