humidifier.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. from .device import REQ_HEADER, SwitchbotDevice
  4. HUMIDIFIER_COMMAND_HEADER = "4381"
  5. HUMIDIFIER_REQUEST = f"{REQ_HEADER}4481"
  6. HUMIDIFIER_COMMAND = f"{REQ_HEADER}{HUMIDIFIER_COMMAND_HEADER}"
  7. HUMIDIFIER_OFF_KEY = f"{HUMIDIFIER_COMMAND}010080FFFFFFFF"
  8. HUMIDIFIER_ON_KEY = f"{HUMIDIFIER_COMMAND}010180FFFFFFFF"
  9. ##
  10. # OFF 570F 4381 0100 80FF FFFF FF
  11. # ON 570F 4381 0101 80FF FFFF FF
  12. # AUTO 570F 4381 0101 80FF FFFF FF
  13. # 1. 570F 4381 0101 22FF FFFF FF
  14. # 2. 570F 4381 0101 43FF FFFF FF
  15. # 3 . 570F 4381 0101 64FF FFFF FF
  16. class SwitchbotHumidifier(SwitchbotDevice):
  17. """Representation of a Switchbot humidifier."""
  18. async def update(self, interface: int | None = None) -> None:
  19. """Update state of device."""
  20. await self.get_device_data(retry=self._retry_count, interface=interface)
  21. async def turn_on(self) -> bool:
  22. """Turn device on."""
  23. result = await self._send_command(HUMIDIFIER_ON_KEY)
  24. ret = self._check_command_result(result, 0, {0x01})
  25. self._override_adv_data = {"isOn": True}
  26. self._fire_callbacks()
  27. return ret
  28. async def turn_off(self) -> bool:
  29. """Turn device off."""
  30. result = await self._send_command(HUMIDIFIER_OFF_KEY)
  31. ret = self._check_command_result(result, 0, {0x01})
  32. self._override_adv_data = {"isOn": False}
  33. self._fire_callbacks()
  34. return ret
  35. async def set_level(self, level: int) -> bool:
  36. """Set level."""
  37. assert 1 <= level <= 100, "Level must be between 1 and 100"
  38. result = await self._send_command(
  39. f"{HUMIDIFIER_COMMAND}0101{level:02X}FFFFFFFF"
  40. )
  41. ret = self._check_command_result(result, 0, {0x01})
  42. self._override_adv_data = {"isOn": False, "level": level}
  43. self._fire_callbacks()
  44. return ret
  45. def is_on(self) -> bool | None:
  46. """Return switch state from cache."""
  47. return self._get_adv_value("isOn")