outdoor_plug.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from typing import Any
  2. from ..const import SwitchbotModel
  3. from ..models import SwitchBotAdvertisement
  4. from .relay_switch import COMMAND_GET_CHANNEL2_INFO, SwitchbotRelaySwitch
  5. class SwitchbotOutdoorPlug(SwitchbotRelaySwitch):
  6. """An Outdoor Plug EU with bidirectional metering."""
  7. _model = SwitchbotModel.OUTDOOR_PLUG_EU
  8. def update_from_advertisement(self, advertisement: SwitchBotAdvertisement) -> None:
  9. """Preserve polled metering values alongside advertised power."""
  10. data = advertisement.data["data"]
  11. for key in (
  12. "voltage",
  13. "current",
  14. "energy",
  15. "current_production",
  16. "energy_production",
  17. ):
  18. data[key] = self._get_adv_value(key) or 0
  19. super().update_from_advertisement(advertisement)
  20. async def get_basic_info(self) -> dict[str, Any] | None:
  21. """Read consumption and production for the single relay."""
  22. if not (data := await super().get_basic_info()):
  23. return None
  24. now, day_start = self.get_current_time_and_start_time()
  25. raw = await self._get_basic_info(
  26. COMMAND_GET_CHANNEL2_INFO.format(now, day_start)
  27. )
  28. if not raw or len(raw) < 15:
  29. return None
  30. production = self._parse_user_data(raw)
  31. if not data["isOn"]:
  32. self._reset_power_data(production)
  33. return data | {
  34. "power_production": production["power"],
  35. "current_production": production["current"],
  36. "energy_production": production["energy"],
  37. }