ソースを参照

Protect numeric values against None responses

Early on, the numeric attributes are coming back as None.
Handle this to avoid errors that cause the device to fail.
Jason Rumney 5 年 前
コミット
3af197afc3
1 ファイル変更11 行追加7 行削除
  1. 11 7
      custom_components/tuya_local/kogan_socket/switch.py

+ 11 - 7
custom_components/tuya_local/kogan_socket/switch.py

@@ -68,20 +68,24 @@ class KoganSocketSwitch(SwitchEntity):
     @property
     def current_power_w(self):
         """Return the current power consumption in Watts"""
-        return (
-            self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W]) / 10.0
-        )
+        pwr = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W])
+        if pwr is None:
+            return STATE_UNAVAILABLE
+        else:
+            return (
+                self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W]) / 10.0
+            )
 
     @property
     def device_state_attributes(self):
         """Get additional attributes that HA doesn't naturally support."""
         timer = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_TIMER])
-        voltage = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_VOLTAGE_V]) / 10.0
-        current = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_A]) / 1000.0
+        voltage = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_VOLTAGE_V])
+        current = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_A])
         return {
             ATTR_CURRENT_POWER_W: self.current_power_w,
-            ATTR_CURRENT_A: current,
-            ATTR_VOLTAGE_V: voltage,
+            ATTR_CURRENT_A: None if current is None else current / 1000.0,
+            ATTR_VOLTAGE_V: None if current is None else voltage / 10.0,
             ATTR_TIMER: timer,
         }