From 2768baeea22ee9ff2abd1200b5ac6ba9290a4699 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 Sep 2022 14:59:14 +0200 Subject: [PATCH] Fix plug stale state while connected This is the same fix we did for the bots https://github.com/Danielhiversen/pySwitchbot/pull/102 Fixes #112 --- switchbot/devices/plug.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/switchbot/devices/plug.py b/switchbot/devices/plug.py index 30631723..ef1e0b26 100644 --- a/switchbot/devices/plug.py +++ b/switchbot/devices/plug.py @@ -1,14 +1,14 @@ """Library to handle connection with Switchbot.""" from __future__ import annotations -from .device import REQ_HEADER, SwitchbotDevice +from .device import REQ_HEADER, SwitchbotDeviceOverrideStateDuringConnection # Plug Mini keys PLUG_ON_KEY = f"{REQ_HEADER}50010180" PLUG_OFF_KEY = f"{REQ_HEADER}50010100" -class SwitchbotPlugMini(SwitchbotDevice): +class SwitchbotPlugMini(SwitchbotDeviceOverrideStateDuringConnection): """Representation of a Switchbot plug mini.""" async def update(self, interface: int | None = None) -> None: @@ -18,12 +18,18 @@ async def update(self, interface: int | None = None) -> None: async def turn_on(self) -> bool: """Turn device on.""" result = await self._send_command(PLUG_ON_KEY) - return self._check_command_result(result, 1, {0x80}) + ret = self._check_command_result(result, 1, {0x80}) + self._override_adv_data = {"isOn": True} + self._fire_callbacks() + return ret async def turn_off(self) -> bool: """Turn device off.""" result = await self._send_command(PLUG_OFF_KEY) - return self._check_command_result(result, 1, {0x00}) + ret = self._check_command_result(result, 1, {0x80}) + self._override_adv_data = {"isOn": False} + self._fire_callbacks() + return ret def is_on(self) -> bool | None: """Return switch state from cache."""