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 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.components.lock import LockEntity, LockEntityFeature
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_CODE 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 homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry 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): class EsphomeLock(EsphomeEntity[LockInfo, LockEntityState], LockEntity):
"""A lock implementation for ESPHome.""" """A lock implementation for ESPHome."""
@property @callback
def assumed_state(self) -> bool: def _on_static_info_update(self, static_info: EntityInfo) -> None:
"""Return True if unable to access real state of the entity.""" """Set attrs from static info."""
return self._static_info.assumed_state super()._on_static_info_update(static_info)
static_info = self._static_info
@property self._attr_assumed_state = static_info.assumed_state
def supported_features(self) -> LockEntityFeature: self._attr_supported_features = LockEntityFeature(0)
"""Flag supported features.""" if static_info.supports_open:
if self._static_info.supports_open: self._attr_supported_features |= LockEntityFeature.OPEN
return LockEntityFeature.OPEN if static_info.requires_code:
return LockEntityFeature(0) self._attr_code_format = static_info.code_format
else:
@property self._attr_code_format = None
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
@property @property
@esphome_state_property @esphome_state_property
@ -77,13 +72,13 @@ class EsphomeLock(EsphomeEntity[LockInfo, LockEntityState], LockEntity):
async def async_lock(self, **kwargs: Any) -> None: async def async_lock(self, **kwargs: Any) -> None:
"""Lock the lock.""" """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: async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the lock.""" """Unlock the lock."""
code = kwargs.get(ATTR_CODE, None) 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: async def async_open(self, **kwargs: Any) -> None:
"""Open the door latch.""" """Open the door latch."""
await self._client.lock_command(self._static_info.key, LockCommand.OPEN) await self._client.lock_command(self._key, LockCommand.OPEN)