فهرست منبع

Fan: use percentage and preset_mode arguments to turn_on.

When arguments are given, use them.

Issue #1381
Jason Rumney 2 سال پیش
والد
کامیت
2c71e28a17
2فایلهای تغییر یافته به همراه30 افزوده شده و 4 حذف شده
  1. 20 4
      custom_components/tuya_local/fan.py
  2. 10 0
      tests/devices/test_anko_fan.py

+ 20 - 4
custom_components/tuya_local/fan.py

@@ -73,11 +73,27 @@ class TuyaLocalFan(TuyaLocalEntity, FanEntity):
         preset_mode: str | None = None,
         **kwargs: Any,
     ):
-        """Turn the switch on"""
+        """Turn the fan on, setting any other parameters given"""
+        settings = {}
         if self._switch_dps:
-            await self._switch_dps.async_set_value(self._device, True)
-        # TODO: handle percentage and preset_mode parameters, and potentially
-        # other kwargs.
+            settings = {
+                **settings,
+                **self._switch_dps.get_values_to_set(self._device, True),
+            }
+
+        if percentage is not None and self._speed_dps:
+            settings = {
+                **settings,
+                **self._speed_dps.get_values_to_set(self._device, percentage),
+            }
+        if preset_mode and self._preset_dps:
+            settings = {
+                **settings,
+                **self._preset_dps.get_values_to_set(self._device, preset_mode),
+            }
+        # TODO: potentially handle other kwargs.
+        if settings:
+            await self._device.async_set_properties(settings)
 
     async def async_turn_off(self, **kwargs):
         """Turn the switch off"""

+ 10 - 0
tests/devices/test_anko_fan.py

@@ -118,3 +118,13 @@ class TestAnkoFan(SwitchableTests, BasicNumberTests, TuyaDeviceTestCase):
     def test_extra_state_attributes(self):
         self.dps[TIMER_DPS] = "5"
         self.assertEqual(self.subject.extra_state_attributes, {"timer": 5})
+
+    async def test_turn_on_with_params(self):
+        self.dps[SWITCH_DPS] = False
+        self.dps[SPEED_DPS] = "1"
+        self.dps[PRESET_DPS] = "normal"
+        async with assert_device_properties_set(
+            self.subject._device,
+            {SWITCH_DPS: True, SPEED_DPS: "6", PRESET_DPS: "nature"},
+        ):
+            await self.subject.async_turn_on(80, "nature")