plug.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from .device import SwitchbotDevice
  5. # Plug Mini keys
  6. PLUG_ON_KEY = "570f50010180"
  7. PLUG_OFF_KEY = "570f50010100"
  8. class SwitchbotPlugMini(SwitchbotDevice):
  9. """Representation of a Switchbot plug mini."""
  10. def __init__(self, *args: Any, **kwargs: Any) -> None:
  11. """Switchbot plug mini constructor."""
  12. super().__init__(*args, **kwargs)
  13. self._settings: dict[str, Any] = {}
  14. async def update(self, interface: int | None = None) -> None:
  15. """Update state of device."""
  16. await self.get_device_data(retry=self._retry_count, interface=interface)
  17. async def turn_on(self) -> bool:
  18. """Turn device on."""
  19. result = await self._sendcommand(PLUG_ON_KEY, self._retry_count)
  20. return result[1] == 0x80
  21. async def turn_off(self) -> bool:
  22. """Turn device off."""
  23. result = await self._sendcommand(PLUG_OFF_KEY, self._retry_count)
  24. return result[1] == 0x00
  25. def is_on(self) -> bool | None:
  26. """Return switch state from cache."""
  27. return self._get_adv_value("isOn")