1
0

plug.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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) -> Any:
  26. """Return switch state from cache."""
  27. # To get actual position call update() first.
  28. value = self._get_adv_value("isOn")
  29. if value is None:
  30. return None
  31. return value