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

fan: turn off when speed is set to 0 and on when speed set otherwise

Speed 0 turning the fan off used to be the behaviour of HA, but it
changed recently, probably becaue fans exist somewhere that really
have a 0 speed.  Many of our configs give a minimum speed of 0 when 0
is not a valid speed step, so we are relying on this implicit
behavior.  I think HA anyway doesn't check the range of speeds, only
the number of steps, and always offers Off as an option.

Also, the new UI buttons result in the on switch being hidden when
more info is open, even though inclusion of Off in the speed buttons
implies that it is possible to switch the fan off and on from here.
So the reverse behaviour of implicitly turning the fan on when speed
is set is also implemented by this commit.

Issue #936
Jason Rumney 2 лет назад
Родитель
Сommit
82a108cc2a
1 измененных файлов с 9 добавлено и 1 удалено
  1. 9 1
      custom_components/tuya_local/fan.py

+ 9 - 1
custom_components/tuya_local/fan.py

@@ -105,6 +105,10 @@ class TuyaLocalFan(TuyaLocalEntity, FanEntity):
 
     async def async_set_percentage(self, percentage):
         """Set the fan speed as a percentage."""
+        # If speed is 0, turn the fan off
+        if percentage == 0 and self._switch_dps:
+            return self.async_turn_off(self)
+
         if self._speed_dps is None:
             return None
         # If there is a fixed list of values, snap to the closest one
@@ -114,7 +118,11 @@ class TuyaLocalFan(TuyaLocalEntity, FanEntity):
                 key=lambda x: abs(x - percentage),
             )
 
-        await self._speed_dps.async_set_value(self._device, percentage)
+        values_to_set = self._speed_dps.get_values_to_set(self._device, percentage)
+        if not self.is_on and self._switch_dps:
+            values_to_set.update(self._switch_dps.get_values_to_set(self._device, true))
+
+        await self._device.async_set_properties(values_to_set)
 
     @property
     def preset_mode(self):