mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 18:54:08 +00:00
Allow erroring modules to recover (#1080)
Re-query failed modules after some delay instead of immediately disabling them. Changes to features so they can still be created when modules are erroring.
This commit is contained in:
@@ -69,7 +69,7 @@ class Alarm(SmartModule):
|
||||
attribute_setter="set_alarm_volume",
|
||||
category=Feature.Category.Config,
|
||||
type=Feature.Type.Choice,
|
||||
choices=["low", "normal", "high"],
|
||||
choices_getter=lambda: ["low", "normal", "high"],
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
|
@@ -39,7 +39,7 @@ class AutoOff(SmartModule):
|
||||
attribute_getter="delay",
|
||||
attribute_setter="set_delay",
|
||||
type=Feature.Type.Number,
|
||||
unit="min", # ha-friendly unit, see UnitOfTime.MINUTES
|
||||
unit_getter=lambda: "min", # ha-friendly unit, see UnitOfTime.MINUTES
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
|
@@ -37,7 +37,7 @@ class BatterySensor(SmartModule):
|
||||
container=self,
|
||||
attribute_getter="battery",
|
||||
icon="mdi:battery",
|
||||
unit="%",
|
||||
unit_getter=lambda: "%",
|
||||
category=Feature.Category.Info,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
|
@@ -27,8 +27,7 @@ class Brightness(SmartModule):
|
||||
container=self,
|
||||
attribute_getter="brightness",
|
||||
attribute_setter="set_brightness",
|
||||
minimum_value=BRIGHTNESS_MIN,
|
||||
maximum_value=BRIGHTNESS_MAX,
|
||||
range_getter=lambda: (BRIGHTNESS_MIN, BRIGHTNESS_MAX),
|
||||
type=Feature.Type.Number,
|
||||
category=Feature.Category.Primary,
|
||||
)
|
||||
|
@@ -18,13 +18,6 @@ class Cloud(SmartModule):
|
||||
REQUIRED_COMPONENT = "cloud_connect"
|
||||
MINIMUM_UPDATE_INTERVAL_SECS = 60
|
||||
|
||||
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 the logic here is to treat that as not connected.
|
||||
"""
|
||||
|
||||
def __init__(self, device: SmartDevice, module: str):
|
||||
super().__init__(device, module)
|
||||
|
||||
|
@@ -5,7 +5,7 @@ from __future__ import annotations
|
||||
from ...emeterstatus import EmeterStatus
|
||||
from ...exceptions import KasaException
|
||||
from ...interfaces.energy import Energy as EnergyInterface
|
||||
from ..smartmodule import SmartModule
|
||||
from ..smartmodule import SmartModule, raise_if_update_error
|
||||
|
||||
|
||||
class Energy(SmartModule, EnergyInterface):
|
||||
@@ -23,6 +23,7 @@ class Energy(SmartModule, EnergyInterface):
|
||||
return req
|
||||
|
||||
@property
|
||||
@raise_if_update_error
|
||||
def current_consumption(self) -> float | None:
|
||||
"""Current power in watts."""
|
||||
if (power := self.energy.get("current_power")) is not None:
|
||||
@@ -30,6 +31,7 @@ class Energy(SmartModule, EnergyInterface):
|
||||
return None
|
||||
|
||||
@property
|
||||
@raise_if_update_error
|
||||
def energy(self):
|
||||
"""Return get_energy_usage results."""
|
||||
if en := self.data.get("get_energy_usage"):
|
||||
@@ -45,6 +47,7 @@ class Energy(SmartModule, EnergyInterface):
|
||||
)
|
||||
|
||||
@property
|
||||
@raise_if_update_error
|
||||
def status(self):
|
||||
"""Get the emeter status."""
|
||||
return self._get_status_from_energy(self.energy)
|
||||
@@ -55,26 +58,31 @@ class Energy(SmartModule, EnergyInterface):
|
||||
return self._get_status_from_energy(res["get_energy_usage"])
|
||||
|
||||
@property
|
||||
@raise_if_update_error
|
||||
def consumption_this_month(self) -> float | None:
|
||||
"""Get the emeter value for this month in kWh."""
|
||||
return self.energy.get("month_energy") / 1_000
|
||||
|
||||
@property
|
||||
@raise_if_update_error
|
||||
def consumption_today(self) -> float | None:
|
||||
"""Get the emeter value for today in kWh."""
|
||||
return self.energy.get("today_energy") / 1_000
|
||||
|
||||
@property
|
||||
@raise_if_update_error
|
||||
def consumption_total(self) -> float | None:
|
||||
"""Return total consumption since last reboot in kWh."""
|
||||
return None
|
||||
|
||||
@property
|
||||
@raise_if_update_error
|
||||
def current(self) -> float | None:
|
||||
"""Return the current in A."""
|
||||
return None
|
||||
|
||||
@property
|
||||
@raise_if_update_error
|
||||
def voltage(self) -> float | None:
|
||||
"""Get the current voltage in V."""
|
||||
return None
|
||||
|
@@ -30,8 +30,7 @@ class Fan(SmartModule, FanInterface):
|
||||
attribute_setter="set_fan_speed_level",
|
||||
icon="mdi:fan",
|
||||
type=Feature.Type.Number,
|
||||
minimum_value=0,
|
||||
maximum_value=4,
|
||||
range_getter=lambda: (0, 4),
|
||||
category=Feature.Category.Primary,
|
||||
)
|
||||
)
|
||||
|
@@ -27,7 +27,7 @@ class HumiditySensor(SmartModule):
|
||||
container=self,
|
||||
attribute_getter="humidity",
|
||||
icon="mdi:water-percent",
|
||||
unit="%",
|
||||
unit_getter=lambda: "%",
|
||||
category=Feature.Category.Primary,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
|
@@ -73,7 +73,7 @@ class LightTransition(SmartModule):
|
||||
attribute_setter="set_turn_on_transition",
|
||||
icon=icon,
|
||||
type=Feature.Type.Number,
|
||||
maximum_value=self._turn_on_transition_max,
|
||||
range_getter=lambda: (0, self._turn_on_transition_max),
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
@@ -86,7 +86,7 @@ class LightTransition(SmartModule):
|
||||
attribute_setter="set_turn_off_transition",
|
||||
icon=icon,
|
||||
type=Feature.Type.Number,
|
||||
maximum_value=self._turn_off_transition_max,
|
||||
range_getter=lambda: (0, self._turn_off_transition_max),
|
||||
)
|
||||
)
|
||||
|
||||
|
@@ -26,7 +26,7 @@ class ReportMode(SmartModule):
|
||||
name="Report interval",
|
||||
container=self,
|
||||
attribute_getter="report_interval",
|
||||
unit="s",
|
||||
unit_getter=lambda: "s",
|
||||
category=Feature.Category.Debug,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
|
@@ -51,8 +51,7 @@ class TemperatureControl(SmartModule):
|
||||
container=self,
|
||||
attribute_getter="temperature_offset",
|
||||
attribute_setter="set_temperature_offset",
|
||||
minimum_value=-10,
|
||||
maximum_value=10,
|
||||
range_getter=lambda: (-10, 10),
|
||||
type=Feature.Type.Number,
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
|
@@ -54,7 +54,7 @@ class TemperatureSensor(SmartModule):
|
||||
attribute_getter="temperature_unit",
|
||||
attribute_setter="set_temperature_unit",
|
||||
type=Feature.Type.Choice,
|
||||
choices=["celsius", "fahrenheit"],
|
||||
choices_getter=lambda: ["celsius", "fahrenheit"],
|
||||
)
|
||||
)
|
||||
|
||||
|
Reference in New Issue
Block a user