plug.py 1008 B

123456789101112131415161718192021222324252627282930
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. from .device import REQ_HEADER, SwitchbotDevice
  4. # Plug Mini keys
  5. PLUG_ON_KEY = f"{REQ_HEADER}50010180"
  6. PLUG_OFF_KEY = f"{REQ_HEADER}50010100"
  7. class SwitchbotPlugMini(SwitchbotDevice):
  8. """Representation of a Switchbot plug mini."""
  9. async def update(self, interface: int | None = None) -> None:
  10. """Update state of device."""
  11. await self.get_device_data(retry=self._retry_count, interface=interface)
  12. async def turn_on(self) -> bool:
  13. """Turn device on."""
  14. result = await self._send_command(PLUG_ON_KEY)
  15. return self._check_command_result(result, 1, {0x80})
  16. async def turn_off(self) -> bool:
  17. """Turn device off."""
  18. result = await self._send_command(PLUG_OFF_KEY)
  19. return self._check_command_result(result, 1, {0x00})
  20. def is_on(self) -> bool | None:
  21. """Return switch state from cache."""
  22. return self._get_adv_value("isOn")