Defer module updates for less volatile modules (#1052)

Addresses stability issues on older hw device versions

 - Handles module timeout errors better by querying modules individually on errors and disabling problematic modules like Firmware that go out to the internet to get updates.
- Addresses an issue with the Led module on P100 hardware version 1.0 which appears to have a memory leak and will cause the device to crash after approximately 500 calls.
- Delays updates of modules that do not have regular changes like LightPreset and LightEffect and enables them to be updated on the next update cycle only if required values have changed.
This commit is contained in:
Steven B
2024-07-11 16:21:59 +01:00
committed by GitHub
parent a044063526
commit 7fd5c213e6
16 changed files with 364 additions and 56 deletions

View File

@@ -16,6 +16,7 @@ class Cloud(SmartModule):
QUERY_GETTER_NAME = "get_connect_cloud_state"
REQUIRED_COMPONENT = "cloud_connect"
MINIMUM_UPDATE_INTERVAL_SECS = 60
def _post_update_hook(self):
"""Perform actions after a device update.

View File

@@ -14,7 +14,7 @@ from async_timeout import timeout as asyncio_timeout
from pydantic.v1 import BaseModel, Field, validator
from ...feature import Feature
from ..smartmodule import SmartModule
from ..smartmodule import SmartModule, allow_update_after
if TYPE_CHECKING:
from ..smartdevice import SmartDevice
@@ -66,6 +66,7 @@ class Firmware(SmartModule):
"""Implementation of firmware module."""
REQUIRED_COMPONENT = "firmware"
MINIMUM_UPDATE_INTERVAL_SECS = 60 * 60 * 24
def __init__(self, device: SmartDevice, module: str):
super().__init__(device, module)
@@ -122,13 +123,6 @@ class Firmware(SmartModule):
req["get_auto_update_info"] = None
return req
def _post_update_hook(self):
"""Perform actions after a device update.
Overrides the default behaviour to disable a module if the query returns
an error because some of the module still functions.
"""
@property
def current_firmware(self) -> str:
"""Return the current firmware version."""
@@ -162,6 +156,7 @@ class Firmware(SmartModule):
state = resp["get_fw_download_state"]
return DownloadState(**state)
@allow_update_after
async def update(
self, progress_cb: Callable[[DownloadState], Coroutine] | None = None
):
@@ -219,6 +214,7 @@ class Firmware(SmartModule):
and self.data["get_auto_update_info"]["enable"]
)
@allow_update_after
async def set_auto_update_enabled(self, enabled: bool):
"""Change autoupdate setting."""
data = {**self.data["get_auto_update_info"], "enable": enabled}

View File

@@ -3,7 +3,7 @@
from __future__ import annotations
from ...interfaces.led import Led as LedInterface
from ..smartmodule import SmartModule
from ..smartmodule import SmartModule, allow_update_after
class Led(SmartModule, LedInterface):
@@ -11,6 +11,8 @@ class Led(SmartModule, LedInterface):
REQUIRED_COMPONENT = "led"
QUERY_GETTER_NAME = "get_led_info"
# Led queries can cause device to crash on P100
MINIMUM_UPDATE_INTERVAL_SECS = 60 * 60
def query(self) -> dict:
"""Query to execute during the update cycle."""
@@ -29,6 +31,7 @@ class Led(SmartModule, LedInterface):
"""Return current led status."""
return self.data["led_rule"] != "never"
@allow_update_after
async def set_led(self, enable: bool):
"""Set led.

View File

@@ -9,7 +9,7 @@ import copy
from typing import Any
from ..effects import SmartLightEffect
from ..smartmodule import Module, SmartModule
from ..smartmodule import Module, SmartModule, allow_update_after
class LightEffect(SmartModule, SmartLightEffect):
@@ -17,6 +17,7 @@ class LightEffect(SmartModule, SmartLightEffect):
REQUIRED_COMPONENT = "light_effect"
QUERY_GETTER_NAME = "get_dynamic_light_effect_rules"
MINIMUM_UPDATE_INTERVAL_SECS = 60
AVAILABLE_BULB_EFFECTS = {
"L1": "Party",
"L2": "Relax",
@@ -130,6 +131,7 @@ class LightEffect(SmartModule, SmartLightEffect):
return brightness
@allow_update_after
async def set_brightness(
self,
brightness: int,
@@ -156,6 +158,7 @@ class LightEffect(SmartModule, SmartLightEffect):
return await self.call("edit_dynamic_light_effect_rule", new_effect)
@allow_update_after
async def set_custom_effect(
self,
effect_dict: dict,

View File

@@ -9,7 +9,7 @@ from typing import TYPE_CHECKING
from ...interfaces import LightPreset as LightPresetInterface
from ...interfaces import LightState
from ..smartmodule import SmartModule
from ..smartmodule import SmartModule, allow_update_after
if TYPE_CHECKING:
from ..smartdevice import SmartDevice
@@ -22,6 +22,7 @@ class LightPreset(SmartModule, LightPresetInterface):
REQUIRED_COMPONENT = "preset"
QUERY_GETTER_NAME = "get_preset_rules"
MINIMUM_UPDATE_INTERVAL_SECS = 60
SYS_INFO_STATE_KEY = "preset_state"
@@ -124,6 +125,7 @@ class LightPreset(SmartModule, LightPresetInterface):
raise ValueError(f"{preset_name} is not a valid preset: {self.preset_list}")
await self._device.modules[SmartModule.Light].set_state(preset)
@allow_update_after
async def save_preset(
self,
preset_name: str,

View File

@@ -5,7 +5,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from ..effects import EFFECT_MAPPING, EFFECT_NAMES, SmartLightEffect
from ..smartmodule import Module, SmartModule
from ..smartmodule import Module, SmartModule, allow_update_after
if TYPE_CHECKING:
from ..smartdevice import SmartDevice
@@ -84,6 +84,7 @@ class LightStripEffect(SmartModule, SmartLightEffect):
"""
return self._effect_list
@allow_update_after
async def set_effect(
self,
effect: str,
@@ -126,6 +127,7 @@ class LightStripEffect(SmartModule, SmartLightEffect):
await self.set_custom_effect(effect_dict)
@allow_update_after
async def set_custom_effect(
self,
effect_dict: dict,

View File

@@ -6,7 +6,7 @@ from typing import TYPE_CHECKING, TypedDict
from ...exceptions import KasaException
from ...feature import Feature
from ..smartmodule import SmartModule
from ..smartmodule import SmartModule, allow_update_after
if TYPE_CHECKING:
from ..smartdevice import SmartDevice
@@ -23,6 +23,7 @@ class LightTransition(SmartModule):
REQUIRED_COMPONENT = "on_off_gradually"
QUERY_GETTER_NAME = "get_on_off_gradually_info"
MINIMUM_UPDATE_INTERVAL_SECS = 60
MAXIMUM_DURATION = 60
# Key in sysinfo that indicates state can be retrieved from there.
@@ -136,6 +137,7 @@ class LightTransition(SmartModule):
"max_duration": off_max,
}
@allow_update_after
async def set_enabled(self, enable: bool):
"""Enable gradual on/off."""
if not self._supports_on_and_off:
@@ -168,6 +170,7 @@ class LightTransition(SmartModule):
# v3 added max_duration, we default to 60 when it's not available
return self._on_state["max_duration"]
@allow_update_after
async def set_turn_on_transition(self, seconds: int):
"""Set turn on transition in seconds.
@@ -203,6 +206,7 @@ class LightTransition(SmartModule):
# v3 added max_duration, we default to 60 when it's not available
return self._off_state["max_duration"]
@allow_update_after
async def set_turn_off_transition(self, seconds: int):
"""Set turn on transition in seconds.