Explorar el Código

Add a generic lock component.

Jason Rumney hace 4 años
padre
commit
2adffb065b

+ 5 - 0
custom_components/tuya_local/dehumidifier/lock.py

@@ -28,6 +28,11 @@ class GoldairDehumidifierChildLock(LockEntity):
         """Return the name of the lock."""
         return self._device.name
 
+    @property
+    def friendly_name(self):
+        """Return the friendly name of the lock, for the UI."""
+        return self._config.name
+
     @property
     def unique_id(self):
         """Return the unique id for this dehumidifier child lock."""

+ 93 - 0
custom_components/tuya_local/generic/lock.py

@@ -0,0 +1,93 @@
+"""
+Platform to control Tuya lock devices.
+
+Initial implementation is based on the secondary child-lock feature of Goldair
+climate devices.
+"""
+from homeassistant.components.lock import LockEntity, STATE_LOCKED, STATE_UNLOCKED
+from homeassistant.const import STATE_UNAVAILABLE
+
+from ..device import TuyaLocalDevice
+from ..helpers.device_config import TuyaEntityConfig
+
+
+class TuyaLocalLock(LockEntity):
+    """Representation of a Tuya Wi-Fi connected lock."""
+
+    def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
+        """
+        Initialise the lock.
+        Args:
+          device (TuyaLocalDevice): The device API instance.
+          config (TuyaEntityConfig): The configuration for this entity.
+        """
+        self._device = device
+        self._config = config
+        self._attr_dps = []
+        for d in config.dps():
+            if d.name == "lock":
+                self._lock_dps = d
+            else:
+                self._attr_dps.append(d)
+
+    @property
+    def should_poll(self):
+        return True
+
+    @property
+    def name(self):
+        """Return the name for this entity."""
+        return self._device.name
+
+    @property
+    def friendly_name(self):
+        """Return the friendly name for this entity."""
+        return self._config.name
+
+    @property
+    def unique_id(self):
+        """Return the device unique ID."""
+        return self._device.unique_id
+
+    @property
+    def device_info(self):
+        """Return the device information."""
+        return self._device.device_info
+
+    @property
+    def state(self):
+        """Return the current state."""
+        lock = self._lock_dps.map_from_dps(self._device.get_property(self._lock_dps.id))
+
+        if lock is None:
+            return STATE_UNAVAILABLE
+        else:
+            return STATE_LOCKED if lock else STATE_UNLOCKED
+
+    @property
+    def is_locked(self):
+        """Return the a boolean representing whether the lock is locked."""
+        return self.state == STATE_LOCKED
+
+    @property
+    def device_state_attributes(self):
+        """Get additional attributes that the integration itself does not support."""
+        attr = {}
+        for a in self._attr_dps:
+            attr[a.name] = a.map_from_dps(self._device.get_property(a.id))
+        return attr
+
+    async def async_lock(self, **kwargs):
+        """Lock the lock."""
+        await self._device.async_set_property(
+            self._lock_dps.id, self._lock_dps.map_to_dps(True)
+        )
+
+    async def async_unlock(self, **kwargs):
+        """Unlock the lock."""
+        await self._device.async_set_property(
+            self._lock_dps.id, self._lock_dps.map_to_dps(False)
+        )
+
+    async def async_update(self):
+        await self._device.async_refresh()

+ 77 - 1
tests/test_lock.py

@@ -1,7 +1,11 @@
 """Tests for the lock entity."""
 import pytest
 from pytest_homeassistant_custom_component.common import MockConfigEntry
-from unittest.mock import AsyncMock, Mock
+from unittest import IsolatedAsyncioTestCase
+from unittest.mock import AsyncMock, Mock, patch
+
+from homeassistant.components.lock import STATE_LOCKED, STATE_UNLOCKED
+from homeassistant.const import STATE_UNAVAILABLE
 
 from custom_components.tuya_local.const import (
     CONF_CHILD_LOCK,
@@ -11,9 +15,17 @@ from custom_components.tuya_local.const import (
     CONF_TYPE_GPPH_HEATER,
     DOMAIN,
 )
+from custom_components.tuya_local.generic.lock import TuyaLocalLock
 from custom_components.tuya_local.heater.lock import GoldairHeaterChildLock
+from custom_components.tuya_local.helpers.device_config import config_for_legacy_use
 from custom_components.tuya_local.lock import async_setup_entry
 
+from .const import GPPH_HEATER_PAYLOAD
+from .helpers import assert_device_properties_set
+
+
+GPPH_LOCK_DPS = "6"
+
 
 async def test_init_entry(hass):
     """Test the initialisation."""
@@ -35,3 +47,67 @@ async def test_init_entry(hass):
     await async_setup_entry(hass, entry, m_add_entities)
     assert type(hass.data[DOMAIN]["dummy"][CONF_CHILD_LOCK]) == GoldairHeaterChildLock
     m_add_entities.assert_called_once()
+
+
+class TestTuyaLocalLock(IsolatedAsyncioTestCase):
+    def setUp(self):
+        device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
+        self.addCleanup(device_patcher.stop)
+        self.mock_device = device_patcher.start()
+        gpph_config = config_for_legacy_use(CONF_TYPE_GPPH_HEATER)
+        for lock in gpph_config.secondary_entities():
+            if lock.entity == "lock":
+                break
+        self.subject = TuyaLocalLock(self.mock_device(), lock)
+        self.dps = GPPH_HEATER_PAYLOAD.copy()
+        self.subject._device.get_property.side_effect = lambda id: self.dps[id]
+
+    def test_should_poll(self):
+        self.assertTrue(self.subject.should_poll)
+
+    def test_name_returns_device_name(self):
+        self.assertEqual(self.subject.name, self.subject._device.name)
+
+    def test_unique_id_returns_device_unique_id(self):
+        self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
+
+    def test_device_info_returns_device_info_from_device(self):
+        self.assertEqual(self.subject.device_info, self.subject._device.device_info)
+
+    def test_state(self):
+        self.dps[GPPH_LOCK_DPS] = True
+        self.assertEqual(self.subject.state, STATE_LOCKED)
+
+        self.dps[GPPH_LOCK_DPS] = False
+        self.assertEqual(self.subject.state, STATE_UNLOCKED)
+
+        self.dps[GPPH_LOCK_DPS] = None
+        self.assertEqual(self.subject.state, STATE_UNAVAILABLE)
+
+    def test_is_locked(self):
+        self.dps[GPPH_LOCK_DPS] = True
+        self.assertTrue(self.subject.is_locked)
+
+        self.dps[GPPH_LOCK_DPS] = False
+        self.assertFalse(self.subject.is_locked)
+
+    async def test_lock(self):
+        async with assert_device_properties_set(
+            self.subject._device, {GPPH_LOCK_DPS: True}
+        ):
+            await self.subject.async_lock()
+
+    async def test_unlock(self):
+        async with assert_device_properties_set(
+            self.subject._device, {GPPH_LOCK_DPS: False}
+        ):
+            await self.subject.async_unlock()
+
+    async def test_update(self):
+        result = AsyncMock()
+        self.subject._device.async_refresh.return_value = result()
+
+        await self.subject.async_update()
+
+        self.subject._device.async_refresh.assert_called_once()
+        result.assert_awaited()