Skip to content

Commit

Permalink
Add YoLink lock V2 support (home-assistant#124202)
Browse files Browse the repository at this point in the history
* Add Lock V2 Support

* Change as suggestions
  • Loading branch information
matrixd2 authored Aug 28, 2024
1 parent c4e5d67 commit f8ac952
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions homeassistant/components/yolink/lock.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""YoLink Lock."""
"""YoLink Lock V1/V2."""

from __future__ import annotations

from typing import Any

from yolink.client_request import ClientRequest
from yolink.const import ATTR_DEVICE_LOCK
from yolink.const import ATTR_DEVICE_LOCK, ATTR_DEVICE_LOCK_V2

from homeassistant.components.lock import LockEntity
from homeassistant.config_entries import ConfigEntry
Expand All @@ -27,7 +27,8 @@ async def async_setup_entry(
entities = [
YoLinkLockEntity(config_entry, device_coordinator)
for device_coordinator in device_coordinators.values()
if device_coordinator.device.device_type == ATTR_DEVICE_LOCK
if device_coordinator.device.device_type
in [ATTR_DEVICE_LOCK, ATTR_DEVICE_LOCK_V2]
]
async_add_entities(entities)

Expand All @@ -50,21 +51,41 @@ def __init__(
def update_entity_state(self, state: dict[str, Any]) -> None:
"""Update HA Entity State."""
state_value = state.get("state")
self._attr_is_locked = (
state_value == "locked" if state_value is not None else None
)
if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2:
self._attr_is_locked = (
state_value["lock"] == "locked" if state_value is not None else None
)
else:
self._attr_is_locked = (
state_value == "locked" if state_value is not None else None
)
self.async_write_ha_state()

async def call_lock_state_change(self, state: str) -> None:
"""Call setState api to change lock state."""
await self.call_device(ClientRequest("setState", {"state": state}))
if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2:
await self.call_device(
ClientRequest("setState", {"state": {"lock": state}})
)
else:
await self.call_device(ClientRequest("setState", {"state": state}))
self._attr_is_locked = state == "lock"
self.async_write_ha_state()

async def async_lock(self, **kwargs: Any) -> None:
"""Lock device."""
await self.call_lock_state_change("lock")
state_param = (
"locked"
if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2
else "lock"
)
await self.call_lock_state_change(state_param)

async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock device."""
await self.call_lock_state_change("unlock")
state_param = (
"unlocked"
if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2
else "unlock"
)
await self.call_lock_state_change(state_param)

0 comments on commit f8ac952

Please sign in to comment.