Ver Fonte

fix: standing fan night light not turning off (send 0x00 for OFF) (#547)

McMoobud há 2 dias atrás
pai
commit
4ecabc840b
2 ficheiros alterados com 11 adições e 3 exclusões
  1. 5 1
      switchbot/devices/fan.py
  2. 6 2
      tests/test_fan.py

+ 5 - 1
switchbot/devices/fan.py

@@ -241,7 +241,11 @@ class SwitchbotStandingFan(SwitchbotFan):
     @update_after_operation
     async def set_night_light(self, state: NightLightState | int) -> bool:
         """Set night-light state (LEVEL_1, LEVEL_2, OFF)."""
-        value = NightLightState(state).value
+        state = NightLightState(state)
+        # The Standing Fan firmware ignores the OFF byte defined by
+        # NightLightState (0x03) and only turns the night light off when it
+        # receives 0x00. Map OFF -> 0x00 here; LEVEL_1/LEVEL_2 are unchanged.
+        value = 0 if state is NightLightState.OFF else state.value
         cmd = f"{COMMAND_SET_NIGHT_LIGHT}{value:02X}FFFF"
         result = await self._send_command(cmd)
         return self._check_command_result(result, 0, {1})

+ 6 - 2
tests/test_fan.py

@@ -560,7 +560,9 @@ async def test_standing_fan_set_night_light(state):
     await standing_fan.set_night_light(state)
     standing_fan._send_command.assert_called_once()
     cmd = standing_fan._send_command.call_args[0][0]
-    assert cmd == f"{fan.COMMAND_SET_NIGHT_LIGHT}{state.value:02X}FFFF"
+    # OFF is sent as 0x00 (firmware ignores NightLightState.OFF's 0x03).
+    expected = 0 if state is NightLightState.OFF else state.value
+    assert cmd == f"{fan.COMMAND_SET_NIGHT_LIGHT}{expected:02X}FFFF"
 
 
 @pytest.mark.asyncio
@@ -570,7 +572,9 @@ async def test_standing_fan_set_night_light_int(state):
     standing_fan = create_standing_fan_for_testing()
     await standing_fan.set_night_light(state)
     cmd = standing_fan._send_command.call_args[0][0]
-    assert cmd == f"{fan.COMMAND_SET_NIGHT_LIGHT}{state:02X}FFFF"
+    # OFF (3) is sent as 0x00 (firmware ignores NightLightState.OFF's 0x03).
+    expected = 0 if state == NightLightState.OFF.value else state
+    assert cmd == f"{fan.COMMAND_SET_NIGHT_LIGHT}{expected:02X}FFFF"
 
 
 @pytest.mark.asyncio