Migrate esphome lock platform to use _on_static_info_update (#95030)

This commit is contained in:
J. Nick Koston 2023-06-22 11:07:51 +02:00 committed by GitHub
parent 69c2ac1fac
commit 8f6cde5b32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,12 +3,12 @@ from __future__ import annotations
from typing import Any
from aioesphomeapi import LockCommand, LockEntityState, LockInfo, LockState
from aioesphomeapi import EntityInfo, LockCommand, LockEntityState, LockInfo, LockState
from homeassistant.components.lock import LockEntity, LockEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_CODE
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
@ -32,24 +32,19 @@ async def async_setup_entry(
class EsphomeLock(EsphomeEntity[LockInfo, LockEntityState], LockEntity):
"""A lock implementation for ESPHome."""
@property
def assumed_state(self) -> bool:
"""Return True if unable to access real state of the entity."""
return self._static_info.assumed_state
@property
def supported_features(self) -> LockEntityFeature:
"""Flag supported features."""
if self._static_info.supports_open:
return LockEntityFeature.OPEN
return LockEntityFeature(0)
@property
def code_format(self) -> str | None:
"""Regex for code format or None if no code is required."""
if self._static_info.requires_code:
return self._static_info.code_format
return None
@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
@ -77,13 +72,13 @@ class EsphomeLock(EsphomeEntity[LockInfo, LockEntityState], LockEntity):
async def async_lock(self, **kwargs: Any) -> None:
"""Lock the lock."""
await self._client.lock_command(self._static_info.key, LockCommand.LOCK)
await self._client.lock_command(self._key, LockCommand.LOCK)
async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the lock."""
code = kwargs.get(ATTR_CODE, None)
await self._client.lock_command(self._static_info.key, LockCommand.UNLOCK, code)
await self._client.lock_command(self._key, LockCommand.UNLOCK, code)
async def async_open(self, **kwargs: Any) -> None:
"""Open the door latch."""
await self._client.lock_command(self._static_info.key, LockCommand.OPEN)
await self._client.lock_command(self._key, LockCommand.OPEN)