فهرست منبع

Add support for Kogan Garage Door Opener

Issue #143

Extend detection of door position to handle a single action dp that includes opened and closed along with opening and closing.
Jason Rumney 3 سال پیش
والد
کامیت
75918c2351

+ 42 - 0
custom_components/tuya_local/devices/kogan_garage_opener.yaml

@@ -0,0 +1,42 @@
+name: Kogan Garage Door
+primary_entity:
+  entity: cover
+  class: garage
+  dps:
+    - id: 101
+      name: control
+      type: string
+      mapping:
+        - dps_val: "fopen"
+          value: "open"
+        - dps_val: "fclose"
+          value: "close"
+    - id: 102
+      name: action
+      type: string
+      mapping:
+        - dps_val: openning
+          value: opening
+        - dps_val: opened
+          value: opened
+        - dps_val: closing
+          value: closing
+        - dps_val: closed
+          value: closed
+secondary_entities:
+  - entity: sensor
+    name: Battery
+    class: battery
+    category: diagnostic
+    dps:
+      - id: 104
+        name: sensor
+        type: integer
+  - entity: binary_sensor
+    name: Door Open
+    class: garage_door
+    category: diagnostic
+    dps:
+      - id: 105
+        name: sensor
+        type: boolean

+ 4 - 0
custom_components/tuya_local/generic/cover.py

@@ -74,6 +74,10 @@ class TuyaLocalCover(TuyaLocalEntity, CoverEntity):
             if state is not None:
                 return 100 if state else 0
 
+        if self._action_dps:
+            state = self._action_dps.get_value(self._device)
+            return 100 if state == "opened" else 0 if state == "closed" else 50
+
     @property
     def is_opening(self):
         """Return if the cover is opening or not."""

+ 4 - 0
custom_components/tuya_local/translations/en.json

@@ -35,6 +35,7 @@
 		    "vacuum": "Include a vacuum entity",
                     "binary_sensor_continuous_heat": "Include continuous heat alarm as a binary_sensor entity",
                     "binary_sensor_defrost": "Include defrost as a binary_sensor entity",
+		    "binary_sensor_door_open": "Include door open as a binary sensor",
                     "binary_sensor_error": "Include error as a binary_sensor entity.",
                     "binary_sensor_high_temperature": "Include high temperature alarm as a binary_sensor entity",
                     "binary_sensor_low_temperature": "Include low temperature alarm as a binary_sensor entity",
@@ -103,6 +104,7 @@
                     "sensor_active_filter_life": "Include active filter life as a sensor entity",
                     "sensor_air_quality": "Include air quality as a sensor entity",
 		    "sensor_balance_energy": "Include balance energy as a sensor_entity",
+		    "sensor_battery": "Include battery as a sensor",
                     "sensor_charcoal_filter_life": "Include charcoal filter life as a sensor entity",
 		    "sensor_clean_area": "Include clean area as a sensor entity",
 		    "sensor_clean_time": "Include clean time as a sensor entity",
@@ -188,6 +190,7 @@
 		    "vacuum": "Include a vacuum entity",
                     "binary_sensor_continuous_heat": "Include continuous heat alarm as a binary_sensor entity",
                     "binary_sensor_defrost": "Include defrost as a binary_sensor entity",
+		    "binary_sensor_door_open": "Include door open as a binary sensor",
                     "binary_sensor_error": "Include error as a binary_sensor entity.",
                     "binary_sensor_high_temperature": "Include high temperature alarm as a binary_sensor entity",
                     "binary_sensor_low_temperature": "Include low temperature alarm as a binary_sensor entity",
@@ -256,6 +259,7 @@
                     "sensor_active_filter_life": "Include active filter life as a sensor entity",
                     "sensor_air_quality": "Include air quality as a sensor entity",
 		    "sensor_balance_energy": "Include balance energy as a sensor_entity",
+		    "sensor_battery": "Include battery as a sensor",
                     "sensor_charcoal_filter_life": "Include charcoal filter life as a sensor entity",
 		    "sensor_clean_area": "Include clean area as a sensor entity",
 		    "sensor_clean_time": "Include clean time as a sensor entity",

+ 7 - 0
tests/const.py

@@ -1031,3 +1031,10 @@ VORK_VK6067_PURIFIER_PAYLOAD = {
     "21": "good",
     "22": 0,
 }
+
+KOGAN_GARAGE_DOOR_PAYLOAD = {
+    "101": "fopen",
+    "102": "opening",
+    "104": 100,
+    "105": True,
+}

+ 113 - 0
tests/devices/test_kogan_garage_door_opener.py

@@ -0,0 +1,113 @@
+"""Tests for the simple garage door opener."""
+from homeassistant.components.binary_sensor import DEVICE_CLASS_GARAGE_DOOR
+from homeassistant.components.cover import (
+    DEVICE_CLASS_GARAGE,
+    SUPPORT_CLOSE,
+    SUPPORT_OPEN,
+)
+from homeassistant.components.sensor import DEVICE_CLASS_BATTERY
+
+from ..const import KOGAN_GARAGE_DOOR_PAYLOAD
+from ..helpers import assert_device_properties_set
+from ..mixins.binary_sensor import BasicBinarySensorTests
+from ..mixins.sensor import BasicSensorTests
+from .base_device_tests import TuyaDeviceTestCase
+
+CONTROL_DPS = "101"
+ACTION_DPS = "102"
+BATTERY_DPS = "104"
+LEFTOPEN_DPS = "105"
+
+
+class TestKoganGarageOpener(
+    BasicBinarySensorTests,
+    BasicSensorTests,
+    TuyaDeviceTestCase,
+):
+    __test__ = True
+
+    def setUp(self):
+        self.setUpForConfig("kogan_garage_opener.yaml", KOGAN_GARAGE_DOOR_PAYLOAD)
+        self.subject = self.entities["cover"]
+        self.setUpBasicBinarySensor(
+            LEFTOPEN_DPS,
+            self.entities.get("binary_sensor_door_open"),
+            device_class=DEVICE_CLASS_GARAGE_DOOR,
+        )
+        self.setUpBasicSensor(
+            BATTERY_DPS,
+            self.entities.get("sensor_battery"),
+            device_class=DEVICE_CLASS_BATTERY,
+        )
+        self.mark_secondary(["binary_sensor_door_open", "sensor_battery"])
+
+    def test_device_class_is_garage(self):
+        self.assertEqual(self.subject.device_class, DEVICE_CLASS_GARAGE)
+
+    def test_supported_features(self):
+        self.assertEqual(
+            self.subject.supported_features,
+            SUPPORT_OPEN | SUPPORT_CLOSE,
+        )
+
+    def test_current_cover_position(self):
+        self.dps[ACTION_DPS] = "opened"
+        self.dps[CONTROL_DPS] = "fopen"
+        self.assertEqual(self.subject.current_cover_position, 100)
+        self.dps[ACTION_DPS] = "closed"
+        self.dps[CONTROL_DPS] = "fclose"
+        self.assertEqual(self.subject.current_cover_position, 0)
+        self.dps[ACTION_DPS] = "closing"
+        self.assertEqual(self.subject.current_cover_position, 50)
+
+    def test_is_opening(self):
+        self.dps[ACTION_DPS] = "opened"
+        self.assertFalse(self.subject.is_opening)
+        self.dps[ACTION_DPS] = "closed"
+        self.assertFalse(self.subject.is_opening)
+        self.dps[ACTION_DPS] = "closing"
+        self.assertFalse(self.subject.is_opening)
+        self.dps[ACTION_DPS] = "openning"
+        self.assertTrue(self.subject.is_opening)
+
+    def test_is_closing(self):
+        self.dps[ACTION_DPS] = "opened"
+        self.assertFalse(self.subject.is_closing)
+        self.dps[ACTION_DPS] = "closed"
+        self.assertFalse(self.subject.is_closing)
+        self.dps[ACTION_DPS] = "openning"
+        self.assertFalse(self.subject.is_closing)
+        self.dps[ACTION_DPS] = "closing"
+        self.assertTrue(self.subject.is_closing)
+
+    def test_is_closed(self):
+        self.dps[CONTROL_DPS] = "fclose"
+        self.dps[ACTION_DPS] = "closing"
+        self.assertFalse(self.subject.is_closed)
+        self.dps[ACTION_DPS] = "closed"
+        self.assertTrue(self.subject.is_closed)
+
+    async def test_open_cover(self):
+        async with assert_device_properties_set(
+            self.subject._device,
+            {CONTROL_DPS: "fopen"},
+        ):
+            await self.subject.async_open_cover()
+
+    async def test_close_cover(self):
+        async with assert_device_properties_set(
+            self.subject._device,
+            {CONTROL_DPS: "fclose"},
+        ):
+            await self.subject.async_close_cover()
+
+    async def test_set_cover_position_not_supported(self):
+        with self.assertRaises(NotImplementedError):
+            await self.subject.async_set_cover_position(50)
+
+    async def test_stop_cover_not_supported(self):
+        with self.assertRaises(NotImplementedError):
+            await self.subject.async_stop_cover()
+
+    def test_extra_state_attributes(self):
+        self.assertEqual(self.subject.extra_state_attributes, {})