1
0

humidifier.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. MANUAL_BUTTON_PRESSES_TO_LEVEL = {
  17. 101: 33,
  18. 102: 66,
  19. 103: 100,
  20. }
  21. class SwitchbotHumidifier(SwitchbotDevice):
  22. """Representation of a Switchbot humidifier."""
  23. async def update(self, interface: int | None = None) -> None:
  24. """Update state of device."""
  25. await self.get_device_data(retry=self._retry_count, interface=interface)
  26. def _generate_command(
  27. self, on: bool | None = None, level: int | None = None
  28. ) -> str:
  29. """Generate command."""
  30. if level is None:
  31. level = self.get_target_humidity() or 128
  32. if on is None:
  33. on = self.is_on()
  34. on_hex = "01" if on else "00"
  35. return f"{HUMIDIFIER_COMMAND}01{on_hex}{level:02X}FFFFFFFF"
  36. async def _async_set_state(self, state: bool) -> bool:
  37. level = self.get_target_humidity() or 128
  38. result = await self._send_command(self._generate_command(on=state, level=level))
  39. ret = self._check_command_result(result, 0, {0x01})
  40. self._override_state({"isOn": state, "level": level})
  41. self._fire_callbacks()
  42. return ret
  43. async def turn_on(self) -> bool:
  44. """Turn device on."""
  45. await self._async_set_state(True)
  46. async def turn_off(self) -> bool:
  47. """Turn device off."""
  48. await self._async_set_state(False)
  49. async def set_level(self, level: int) -> bool:
  50. """Set level."""
  51. assert 1 <= level <= 100, "Level must be between 1 and 100"
  52. await self._set_level(level)
  53. async def _set_level(self, level: int) -> bool:
  54. """Set level."""
  55. result = await self._send_command(self._generate_command(level=level))
  56. ret = self._check_command_result(result, 0, {0x01})
  57. self._override_state({"level": level})
  58. self._fire_callbacks()
  59. return ret
  60. async def async_set_auto(self) -> bool:
  61. """Set auto mode."""
  62. await self._set_level(128)
  63. async def async_set_manual(self) -> bool:
  64. """Set manual mode."""
  65. await self._set_level(50)
  66. def is_auto(self) -> bool:
  67. """Return auto state from cache."""
  68. return self.get_level() in (228, 128)
  69. def get_level(self) -> int | None:
  70. """Return level state from cache."""
  71. return self._get_adv_value("level")
  72. def is_on(self) -> bool | None:
  73. """Return switch state from cache."""
  74. return self._get_adv_value("isOn")
  75. def get_target_humidity(self) -> int | None:
  76. """Return target humidity from cache."""
  77. level = self.get_level()
  78. if self.is_auto():
  79. return None
  80. return MANUAL_BUTTON_PRESSES_TO_LEVEL.get(level, level)