Improve feature setter robustness (#870)

This adds a test to check that all feature.set_value() calls will cause
a query, i.e., that there are no self.call()s that are not awaited, and
fixes existing code in this context.

This also fixes an issue where it was not possible to print out the
feature if the value threw an exception.
This commit is contained in:
Teemu R
2024-05-02 15:32:06 +02:00
committed by GitHub
parent 9dcd8ec91b
commit 5ef81f4669
8 changed files with 91 additions and 19 deletions

View File

@@ -55,9 +55,9 @@ class AutoOffModule(SmartModule):
"""Return True if enabled."""
return self.data["enable"]
def set_enabled(self, enable: bool):
async def set_enabled(self, enable: bool):
"""Enable/disable auto off."""
return self.call(
return await self.call(
"set_auto_off_config",
{"enable": enable, "delay_min": self.data["delay_min"]},
)
@@ -67,9 +67,9 @@ class AutoOffModule(SmartModule):
"""Return time until auto off."""
return self.data["delay_min"]
def set_delay(self, delay: int):
async def set_delay(self, delay: int):
"""Set time until auto off."""
return self.call(
return await self.call(
"set_auto_off_config", {"delay_min": delay, "enable": self.data["enable"]}
)

View File

@@ -34,6 +34,7 @@ class ColorTemperatureModule(SmartModule):
attribute_setter="set_color_temp",
range_getter="valid_temperature_range",
category=Feature.Category.Primary,
type=Feature.Type.Number,
)
)

View File

@@ -88,9 +88,9 @@ class LightTransitionModule(SmartModule):
return self.data["off_state"]
def set_enabled_v1(self, enable: bool):
async def set_enabled_v1(self, enable: bool):
"""Enable gradual on/off."""
return self.call("set_on_off_gradually_info", {"enable": enable})
return await self.call("set_on_off_gradually_info", {"enable": enable})
@property
def enabled_v1(self) -> bool:

View File

@@ -48,6 +48,7 @@ class TemperatureSensor(SmartModule):
attribute_getter="temperature_unit",
attribute_setter="set_temperature_unit",
type=Feature.Type.Choice,
choices=["celsius", "fahrenheit"],
)
)
# TODO: use temperature_unit for feature creation