Просмотр исходного кода

Add newer type of Kogan switch.

The newer type of Kogan switch, model KASPEMHUSBA, has current, voltage
and power readings on different DPS ids than the older ones.
Add detection of such switches, and fallbacks for the readings.
Jason Rumney 5 лет назад
Родитель
Сommit
b495f29422

+ 3 - 0
custom_components/tuya_local/device.py

@@ -100,6 +100,9 @@ class TuyaLocalDevice(object):
         if "5" in cached_state and "3" not in cached_state:
             _LOGGER.info(f"Detecting {self.name} as Kogan Switch")
             return CONF_TYPE_KOGAN_SWITCH
+        if "18" in cached_state:
+            _LOGGER.info(f"Detecting {self.name} as newer type of Kogan Switch")
+            return CONF_TYPE_KOGAN_SWITCH
         if "106" in cached_state:
             _LOGGER.info(f"Detecting {self.name} as Goldair GPPH Heater")
             return CONF_TYPE_GPPH_HEATER

+ 6 - 0
custom_components/tuya_local/kogan_socket/const.py

@@ -4,6 +4,9 @@ ATTR_SWITCH = "switch"
 ATTR_TIMER = "timer"
 ATTR_CURRENT_A = "current_a"
 ATTR_VOLTAGE_V = "voltage_v"
+ATTR_ALT_CURRENT_A = "alt_currrent_a"
+ATTR_ALT_CURRENT_POWER_W = "alt_power_w"
+ATTR_ALT_VOLTAGE_V = "alt_voltage_v"
 
 PROPERTY_TO_DPS_ID = {
     ATTR_SWITCH: "1",
@@ -11,4 +14,7 @@ PROPERTY_TO_DPS_ID = {
     ATTR_CURRENT_A: "4",
     ATTR_CURRENT_POWER_W: "5",
     ATTR_VOLTAGE_V: "6",
+    ATTR_ALT_CURRENT_A: "18",
+    ATTR_ALT_CURRENT_POWER_W: "19",
+    ATTR_ALT_VOLTAGE_V: "20",
 }

+ 19 - 3
custom_components/tuya_local/kogan_socket/switch.py

@@ -14,6 +14,9 @@ from .const import (
     ATTR_SWITCH,
     ATTR_TIMER,
     ATTR_VOLTAGE_V,
+    ATTR_ALT_CURRENT_A,
+    ATTR_ALT_CURRENT_POWER_W,
+    ATTR_ALT_VOLTAGE_V,
     PROPERTY_TO_DPS_ID,
 )
 
@@ -66,9 +69,14 @@ class KoganSocketSwitch(SwitchEntity):
         """Return the current power consumption in Watts"""
         pwr = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W])
         if pwr is None:
-            return STATE_UNAVAILABLE
-        else:
-            return pwr / 10.0
+            # Some newer plugs have the measurements on different DPS ids
+            pwr = self._device.get_property(
+                PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_POWER_W]
+            )
+            if pwr is None:
+                return STATE_UNAVAILABLE
+
+        return pwr / 10.0
 
     @property
     def device_state_attributes(self):
@@ -76,6 +84,14 @@ class KoganSocketSwitch(SwitchEntity):
         timer = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_TIMER])
         voltage = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_VOLTAGE_V])
         current = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_A])
+
+        # Some newer plugs have the measurements on different DPS ids
+        if voltage is None:
+            voltage = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_ALT_VOLTAGE_V])
+
+        if current is None:
+            current = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_A])
+
         return {
             ATTR_CURRENT_POWER_W: self.current_power_w,
             ATTR_CURRENT_A: None if current is None else current / 1000.0,