humidifier.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. import time
  4. from .device import REQ_HEADER, SwitchbotDevice
  5. HUMIDIFIER_COMMAND_HEADER = "4381"
  6. HUMIDIFIER_REQUEST = f"{REQ_HEADER}4481"
  7. HUMIDIFIER_COMMAND = f"{REQ_HEADER}{HUMIDIFIER_COMMAND_HEADER}"
  8. HUMIDIFIER_OFF_KEY = f"{HUMIDIFIER_COMMAND}010080FFFFFFFF"
  9. HUMIDIFIER_ON_KEY = f"{HUMIDIFIER_COMMAND}010180FFFFFFFF"
  10. ##
  11. # OFF 570F 4381 0100 80FF FFFF FF
  12. # ON 570F 4381 0101 80FF FFFF FF
  13. # AUTO 570F 4381 0101 80FF FFFF FF
  14. # 1. 570F 4381 0101 22FF FFFF FF
  15. # 2. 570F 4381 0101 43FF FFFF FF
  16. # 3 . 570F 4381 0101 64FF FFFF FF
  17. MANUAL_BUTTON_PRESSES_TO_LEVEL = {
  18. 101: 33,
  19. 102: 66,
  20. 103: 100,
  21. }
  22. class SwitchbotHumidifier(SwitchbotDevice):
  23. """Representation of a Switchbot humidifier."""
  24. async def update(self, interface: int | None = None) -> None:
  25. """Update state of device."""
  26. # No battery here
  27. self._last_full_update = time.monotonic()
  28. def _generate_command(
  29. self, on: bool | None = None, level: int | None = None
  30. ) -> str:
  31. """Generate command."""
  32. if level is None:
  33. level = self.get_target_humidity() or 128
  34. if on is None:
  35. on = self.is_on()
  36. on_hex = "01" if on else "00"
  37. return f"{HUMIDIFIER_COMMAND}01{on_hex}{level:02X}FFFFFFFF"
  38. async def _async_set_state(self, state: bool) -> bool:
  39. level = self.get_target_humidity() or 128
  40. result = await self._send_command(self._generate_command(on=state, level=level))
  41. ret = self._check_command_result(result, 0, {0x01})
  42. self._override_state({"isOn": state, "level": level})
  43. self._fire_callbacks()
  44. return ret
  45. async def turn_on(self) -> bool:
  46. """Turn device on."""
  47. await self._async_set_state(True)
  48. async def turn_off(self) -> bool:
  49. """Turn device off."""
  50. await self._async_set_state(False)
  51. async def set_level(self, level: int) -> bool:
  52. """Set level."""
  53. assert 1 <= level <= 100, "Level must be between 1 and 100"
  54. await self._set_level(level)
  55. async def _set_level(self, level: int) -> bool:
  56. """Set level."""
  57. result = await self._send_command(self._generate_command(level=level))
  58. ret = self._check_command_result(result, 0, {0x01})
  59. self._override_state({"level": level})
  60. self._fire_callbacks()
  61. return ret
  62. async def async_set_auto(self) -> bool:
  63. """Set auto mode."""
  64. await self._set_level(128)
  65. async def async_set_manual(self) -> bool:
  66. """Set manual mode."""
  67. await self._set_level(50)
  68. def is_auto(self) -> bool:
  69. """Return auto state from cache."""
  70. return self.get_level() in (228, 128)
  71. def get_level(self) -> int | None:
  72. """Return level state from cache."""
  73. return self._get_adv_value("level")
  74. def is_on(self) -> bool | None:
  75. """Return switch state from cache."""
  76. return self._get_adv_value("isOn")
  77. def get_target_humidity(self) -> int | None:
  78. """Return target humidity from cache."""
  79. level = self.get_level()
  80. if self.is_auto():
  81. return None
  82. return MANUAL_BUTTON_PRESSES_TO_LEVEL.get(level, level)
  83. def poll_needed(self, last_poll_time: float | None) -> bool:
  84. """Return if device needs polling."""
  85. return False