瀏覽代碼

Implement light effects.

Effects are like presets for lights, a fixed list of options.  To be used when the light settings don't exactly map directly to brightness, rgb, color temperature or on/off.
Jason Rumney 4 年之前
父節點
當前提交
48bc164cb3
共有 2 個文件被更改,包括 39 次插入0 次删除
  1. 35 0
      custom_components/tuya_local/generic/light.py
  2. 4 0
      tests/devices/test_goldair_gpph_heater.py

+ 35 - 0
custom_components/tuya_local/generic/light.py

@@ -9,6 +9,7 @@ from homeassistant.components.light import (
     COLOR_MODE_BRIGHTNESS,
     COLOR_MODE_ONOFF,
     COLOR_MODE_UNKNOWN,
+    SUPPORT_EFFECT,
 )
 
 from ..device import TuyaLocalDevice
@@ -31,6 +32,7 @@ class TuyaLocalLight(LightEntity):
         dps_map = {c.name: c for c in config.dps()}
         self._switch_dps = dps_map.pop("switch", None)
         self._brightness_dps = dps_map.pop("brightness", None)
+        self._effect_dps = dps_map.pop("effect", None)
 
         for d in dps_map.values():
             if not d.hidden:
@@ -80,6 +82,14 @@ class TuyaLocalLight(LightEntity):
         else:
             return []
 
+    @property
+    def supported_features(self):
+        """Return the supported features for this light."""
+        if self._effect_dps:
+            return SUPPORT_EFFECT
+        else:
+            return 0
+
     @property
     def color_mode(self):
         """Return the color mode of the light"""
@@ -109,6 +119,20 @@ class TuyaLocalLight(LightEntity):
             return None
         return self._brightness_dps.get_value(self._device)
 
+    @property
+    def effect_list(self):
+        """Return the list of valid effects for the light"""
+        if self._effect_dps is None:
+            return None
+        return self._effect_dps.values(self._device)
+
+    @property
+    def effect(self):
+        """Return the current effect setting of this light"""
+        if self._effect_dps is None:
+            return None
+        return self._effect_dps.get_value(self._device)
+
     @property
     def device_state_attributes(self):
         """Get additional attributes that the integration itself does not support."""
@@ -132,6 +156,15 @@ class TuyaLocalLight(LightEntity):
                 **settings,
                 **bright_values,
             }
+        if self._effect_dps:
+            effect = params.get(ATTR_EFFECT, None)
+            if effect:
+                effect_values = self._effect_dps.get_values_to_set(self._device, effect)
+                settings = {
+                    **settings,
+                    **effect_values,
+                }
+
         await self._device.async_set_properties(settings)
 
     async def async_turn_off(self):
@@ -139,6 +172,8 @@ class TuyaLocalLight(LightEntity):
             await self._switch_dps.async_set_value(self._device, False)
         elif self._brightness_dps:
             await self._brightness_dps.async_set_value(self._device, 0)
+        else:
+            raise NotImplementedError()
 
     async def async_toggle(self):
         dps_display_on = self.is_on

+ 4 - 0
tests/devices/test_goldair_gpph_heater.py

@@ -369,6 +369,10 @@ class TestGoldairHeater(TuyaDeviceTestCase):
     def test_light_has_no_brightness(self):
         self.assertIsNone(self.light.brightness)
 
+    def test_light_has_no_effects(self):
+        self.assertIsNone(self.light.effect_list)
+        self.assertIsNone(self.light.effect)
+
     def test_light_icon(self):
         self.dps[LIGHT_DPS] = True
         self.assertEqual(self.light.icon, "mdi:led-on")