mirror of
https://github.com/home-assistant/core.git
synced 2025-05-06 23:19:18 +00:00

* Make DevonzBase a generic * Adjust alarm_control_panel * Adjust binary_sensor * Adjust climate * More platforms * Adjust light * Ignore type-var * Add space * Implement recommendation * Use type: ignore[union-attr] * Revert "Use type: ignore[union-attr]" This reverts commit 983443062aab0a9c599b2750d823d0c5148c05ce. * Adjust assert * Adjust lock * Rename type variables * type: ignore[union-attr] * Formatting Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
"""Support for deCONZ locks."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Union
|
|
|
|
from pydeconz.models.event import EventType
|
|
from pydeconz.models.light.lock import Lock
|
|
from pydeconz.models.sensor.door_lock import DoorLock
|
|
|
|
from homeassistant.components.lock import DOMAIN, LockEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant, callback
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from .deconz_device import DeconzDevice
|
|
from .gateway import get_gateway_from_config_entry
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up locks for deCONZ component."""
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
|
gateway.entities[DOMAIN] = set()
|
|
|
|
@callback
|
|
def async_add_lock_from_light(_: EventType, lock_id: str) -> None:
|
|
"""Add lock from deCONZ."""
|
|
lock = gateway.api.lights.locks[lock_id]
|
|
async_add_entities([DeconzLock(lock, gateway)])
|
|
|
|
gateway.register_platform_add_device_callback(
|
|
async_add_lock_from_light,
|
|
gateway.api.lights.locks,
|
|
)
|
|
|
|
@callback
|
|
def async_add_lock_from_sensor(_: EventType, lock_id: str) -> None:
|
|
"""Add lock from deCONZ."""
|
|
lock = gateway.api.sensors.door_lock[lock_id]
|
|
async_add_entities([DeconzLock(lock, gateway)])
|
|
|
|
gateway.register_platform_add_device_callback(
|
|
async_add_lock_from_sensor,
|
|
gateway.api.sensors.door_lock,
|
|
always_ignore_clip_sensors=True,
|
|
)
|
|
|
|
|
|
class DeconzLock(DeconzDevice[Union[DoorLock, Lock]], LockEntity):
|
|
"""Representation of a deCONZ lock."""
|
|
|
|
TYPE = DOMAIN
|
|
|
|
@property
|
|
def is_locked(self) -> bool:
|
|
"""Return true if lock is on."""
|
|
return self._device.is_locked
|
|
|
|
async def async_lock(self, **kwargs: Any) -> None:
|
|
"""Lock the lock."""
|
|
if isinstance(self._device, DoorLock):
|
|
await self.gateway.api.sensors.door_lock.set_config(
|
|
id=self._device.resource_id,
|
|
lock=True,
|
|
)
|
|
else:
|
|
await self.gateway.api.lights.locks.set_state(
|
|
id=self._device.resource_id,
|
|
lock=True,
|
|
)
|
|
|
|
async def async_unlock(self, **kwargs: Any) -> None:
|
|
"""Unlock the lock."""
|
|
if isinstance(self._device, DoorLock):
|
|
await self.gateway.api.sensors.door_lock.set_config(
|
|
id=self._device.resource_id,
|
|
lock=False,
|
|
)
|
|
else:
|
|
await self.gateway.api.lights.locks.set_state(
|
|
id=self._device.resource_id,
|
|
lock=False,
|
|
)
|