From 2cfdc15c380772be23f4af6c04518b9c527f1f07 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 7 Sep 2022 09:54:21 -0500 Subject: [PATCH] Handle stale switchbot advertisement data while connected (#77956) --- homeassistant/components/switchbot/cover.py | 3 +++ homeassistant/components/switchbot/manifest.json | 2 +- homeassistant/components/switchbot/switch.py | 13 +++++++------ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/switchbot/cover.py b/homeassistant/components/switchbot/cover.py index c2b6cb1a4c7..df716be6ff3 100644 --- a/homeassistant/components/switchbot/cover.py +++ b/homeassistant/components/switchbot/cover.py @@ -4,6 +4,8 @@ from __future__ import annotations import logging from typing import Any +import switchbot + from homeassistant.components.cover import ( ATTR_CURRENT_POSITION, ATTR_POSITION, @@ -36,6 +38,7 @@ async def async_setup_entry( class SwitchBotCurtainEntity(SwitchbotEntity, CoverEntity, RestoreEntity): """Representation of a Switchbot.""" + _device: switchbot.SwitchbotCurtain _attr_device_class = CoverDeviceClass.CURTAIN _attr_supported_features = ( CoverEntityFeature.OPEN diff --git a/homeassistant/components/switchbot/manifest.json b/homeassistant/components/switchbot/manifest.json index 040e76391bd..f322734ba54 100644 --- a/homeassistant/components/switchbot/manifest.json +++ b/homeassistant/components/switchbot/manifest.json @@ -2,7 +2,7 @@ "domain": "switchbot", "name": "SwitchBot", "documentation": "https://www.home-assistant.io/integrations/switchbot", - "requirements": ["PySwitchbot==0.18.25"], + "requirements": ["PySwitchbot==0.18.27"], "config_flow": true, "dependencies": ["bluetooth"], "codeowners": [ diff --git a/homeassistant/components/switchbot/switch.py b/homeassistant/components/switchbot/switch.py index 17235135cfa..d524a7100f0 100644 --- a/homeassistant/components/switchbot/switch.py +++ b/homeassistant/components/switchbot/switch.py @@ -4,6 +4,8 @@ from __future__ import annotations import logging from typing import Any +import switchbot + from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import STATE_ON @@ -34,6 +36,7 @@ class SwitchBotSwitch(SwitchbotEntity, SwitchEntity, RestoreEntity): """Representation of a Switchbot switch.""" _attr_device_class = SwitchDeviceClass.SWITCH + _device: switchbot.Switchbot def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None: """Initialize the Switchbot.""" @@ -69,21 +72,19 @@ class SwitchBotSwitch(SwitchbotEntity, SwitchEntity, RestoreEntity): @property def assumed_state(self) -> bool: """Return true if unable to access real state of entity.""" - if not self.data["data"]["switchMode"]: - return True - return False + return not self._device.switch_mode() @property def is_on(self) -> bool | None: """Return true if device is on.""" - if not self.data["data"]["switchMode"]: + if not self._device.switch_mode(): return self._attr_is_on - return self.data["data"]["isOn"] + return self._device.is_on() @property def extra_state_attributes(self) -> dict: """Return the state attributes.""" return { **super().extra_state_attributes, - "switch_mode": self.data["data"]["switchMode"], + "switch_mode": self._device.switch_mode(), } diff --git a/requirements_all.txt b/requirements_all.txt index 34aecea532e..f96fbc6d141 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -37,7 +37,7 @@ PyRMVtransport==0.3.3 PySocks==1.7.1 # homeassistant.components.switchbot -PySwitchbot==0.18.25 +PySwitchbot==0.18.27 # homeassistant.components.transport_nsw PyTransportNSW==0.1.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 5e90f44f381..2f295442c9e 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -33,7 +33,7 @@ PyRMVtransport==0.3.3 PySocks==1.7.1 # homeassistant.components.switchbot -PySwitchbot==0.18.25 +PySwitchbot==0.18.27 # homeassistant.components.transport_nsw PyTransportNSW==0.1.1