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

Only switch lights on in turn_on if they are off.

Some Tuya devices seem to have problems with multiple dp sent at once.

Since lights require all settings to be set via the turn_on call, the
current behaviour of always setting the switch on when turn_on is
called triggers such problems when setting other parameters.

Only implicitly turn the switch on when it is off, so that other
parameters can go through to the light alone.

Possible workaround for Issue #236 (has also come up before)
Jason Rumney 3 лет назад
Родитель
Сommit
1fd2fdfa72

+ 8 - 7
custom_components/tuya_local/generic/light.py

@@ -293,12 +293,6 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
                 ),
             }
 
-        if self._switch_dps:
-            settings = {
-                **settings,
-                **self._switch_dps.get_values_to_set(self._device, True),
-            }
-
         if self._effect_dps:
             effect = params.get(ATTR_EFFECT, None)
             if effect:
@@ -311,7 +305,14 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
                     ),
                 }
 
-        await self._device.async_set_properties(settings)
+        if self._switch_dps and not self.is_on:
+            settings = {
+                **settings,
+                **self._switch_dps.get_values_to_set(self._device, True),
+            }
+
+        if settings:
+            await self._device.async_set_properties(settings)
 
     async def async_turn_off(self):
         if self._switch_dps:

+ 1 - 2
tests/devices/test_digoo_dgsp01_dual_nightlight_switch.py

@@ -115,6 +115,7 @@ class TestDigooNightlightSwitch(BasicSwitchTests, TuyaDeviceTestCase):
         self.assertEqual(self.light.supported_features, LightEntityFeature.EFFECT)
 
     async def test_turn_on(self):
+        self.dps[LIGHTSW_DPS] = False
         async with assert_device_properties_set(
             self.light._device, {LIGHTSW_DPS: True}
         ):
@@ -130,7 +131,6 @@ class TestDigooNightlightSwitch(BasicSwitchTests, TuyaDeviceTestCase):
         async with assert_device_properties_set(
             self.light._device,
             {
-                LIGHTSW_DPS: True,
                 COLORMODE_DPS: "white",
                 BRIGHTNESS_DPS: 128,
             },
@@ -142,7 +142,6 @@ class TestDigooNightlightSwitch(BasicSwitchTests, TuyaDeviceTestCase):
         async with assert_device_properties_set(
             self.light._device,
             {
-                LIGHTSW_DPS: True,
                 COLORMODE_DPS: "colour",
                 RGBW_DPS: "ff000000006464",
             },

+ 1 - 0
tests/devices/test_goldair_dehumidifier.py

@@ -212,6 +212,7 @@ class TestGoldairDehumidifier(
         self.assertEqual(self.light.extra_state_attributes, {})
 
     async def test_light_turn_on(self):
+        self.dps[LIGHTOFF_DPS] = True
         async with assert_device_properties_set(
             self.light._device, {LIGHTOFF_DPS: False}
         ):

+ 4 - 2
tests/devices/test_moes_rgb_socket.py

@@ -167,6 +167,7 @@ class TestMoesRGBWSocket(
         self.assertEqual(self.light.supported_features, LightEntityFeature.EFFECT)
 
     async def test_turn_on(self):
+        self.dps[LIGHT_DPS] = False
         async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
             await self.light.async_turn_on()
 
@@ -175,10 +176,10 @@ class TestMoesRGBWSocket(
             await self.light.async_turn_off()
 
     async def test_set_brightness(self):
+        self.dps[LIGHT_DPS] = True
         async with assert_device_properties_set(
             self.light._device,
             {
-                LIGHT_DPS: True,
                 MODE_DPS: "white",
                 BRIGHTNESS_DPS: 128,
             },
@@ -187,10 +188,11 @@ class TestMoesRGBWSocket(
 
     async def test_set_rgbw(self):
         self.dps[BRIGHTNESS_DPS] = 255
+        self.dps[LIGHT_DPS] = True
+
         async with assert_device_properties_set(
             self.light._device,
             {
-                LIGHT_DPS: True,
                 MODE_DPS: "colour",
                 RGBW_DPS: "ff00000000ffff",
             },

+ 3 - 2
tests/devices/test_rgbcw_lightbulb.py

@@ -98,6 +98,7 @@ class TestRGBCWLightbulb(BasicNumberTests, TuyaDeviceTestCase):
         self.assertEqual(self.subject.supported_features, LightEntityFeature.EFFECT)
 
     async def test_turn_on(self):
+        self.dps[SWITCH_DPS] = False
         async with assert_device_properties_set(
             self.subject._device,
             {SWITCH_DPS: True},
@@ -112,10 +113,10 @@ class TestRGBCWLightbulb(BasicNumberTests, TuyaDeviceTestCase):
             await self.subject.async_turn_off()
 
     async def test_set_brightness(self):
+        self.dps[SWITCH_DPS] = True
         async with assert_device_properties_set(
             self.subject._device,
             {
-                SWITCH_DPS: True,
                 MODE_DPS: "white",
                 BRIGHTNESS_DPS: 502,
             },
@@ -124,10 +125,10 @@ class TestRGBCWLightbulb(BasicNumberTests, TuyaDeviceTestCase):
 
     async def test_set_rgbw(self):
         self.dps[BRIGHTNESS_DPS] = 1000
+        self.dps[SWITCH_DPS] = True
         async with assert_device_properties_set(
             self.subject._device,
             {
-                SWITCH_DPS: True,
                 MODE_DPS: "colour",
                 HSV_DPS: "000003e803e8",
             },

+ 3 - 0
tests/mixins/light.py

@@ -39,12 +39,14 @@ class BasicLightTests:
         self.assertFalse(self.basicLight.is_on)
 
     async def test_basic_light_turn_on(self):
+        self.dps[self.basicLightDps] = self.basicLightOff
         async with assert_device_properties_set(
             self.basicLight._device, {self.basicLightDps: self.basicLightOn}
         ):
             await self.basicLight.async_turn_on()
 
     async def test_basic_light_turn_off(self):
+        self.dps[self.basicLightDps] = self.basicLightOn
         async with assert_device_properties_set(
             self.basicLight._device, {self.basicLightDps: self.basicLightOff}
         ):
@@ -123,6 +125,7 @@ class MultiLightTests:
     async def test_multi_light_turn_on(self):
         for key, light in self.multiLight.items():
             with self.subTest(key):
+                self.dps[self.multiLightDps[key]] = self.multiLightOff[key]
                 async with assert_device_properties_set(
                     light._device, {self.multiLightDps[key]: self.multiLightOn[key]}
                 ):