plug.py 1.2 KB

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