plug.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. from .device import REQ_HEADER, SwitchbotDeviceOverrideStateDuringConnection
  4. # Plug Mini keys
  5. PLUG_ON_KEY = f"{REQ_HEADER}50010180"
  6. PLUG_OFF_KEY = f"{REQ_HEADER}50010100"
  7. class SwitchbotPlugMini(SwitchbotDeviceOverrideStateDuringConnection):
  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. ret = self._check_command_result(result, 1, {0x80})
  16. self._override_adv_data = {"isOn": True}
  17. self._fire_callbacks()
  18. return ret
  19. async def turn_off(self) -> bool:
  20. """Turn device off."""
  21. result = await self._send_command(PLUG_OFF_KEY)
  22. ret = self._check_command_result(result, 1, {0x80})
  23. self._override_adv_data = {"isOn": False}
  24. self._fire_callbacks()
  25. return ret
  26. def is_on(self) -> bool | None:
  27. """Return switch state from cache."""
  28. return self._get_adv_value("isOn")