plug.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. import time
  4. from .device import REQ_HEADER, SwitchbotDeviceOverrideStateDuringConnection
  5. # Plug Mini keys
  6. PLUG_ON_KEY = f"{REQ_HEADER}50010180"
  7. PLUG_OFF_KEY = f"{REQ_HEADER}50010100"
  8. class SwitchbotPlugMini(SwitchbotDeviceOverrideStateDuringConnection):
  9. """Representation of a Switchbot plug mini."""
  10. async def update(self, interface: int | None = None) -> None:
  11. """Update state of device."""
  12. # No battery here
  13. self._last_full_update = time.monotonic()
  14. async def turn_on(self) -> bool:
  15. """Turn device on."""
  16. result = await self._send_command(PLUG_ON_KEY)
  17. ret = self._check_command_result(result, 1, {0x80})
  18. self._override_state({"isOn": True})
  19. self._fire_callbacks()
  20. return ret
  21. async def turn_off(self) -> bool:
  22. """Turn device off."""
  23. result = await self._send_command(PLUG_OFF_KEY)
  24. ret = self._check_command_result(result, 1, {0x80})
  25. self._override_state({"isOn": False})
  26. self._fire_callbacks()
  27. return ret
  28. def is_on(self) -> bool | None:
  29. """Return switch state from cache."""
  30. return self._get_adv_value("isOn")
  31. def poll_needed(self, last_poll_time: float | None) -> bool:
  32. """Return if device needs polling."""
  33. return False