Ver Fonte

fix: Infer TRV idle state when target temperature is reached with 0.5°C hysteresis (#521)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
davidrule1969 há 2 dias atrás
pai
commit
c1d77759f9

+ 12 - 0
switchbot/devices/smart_thermostat_radiator.py

@@ -41,6 +41,7 @@ MODE_TEMP_RANGE = {
 }
 
 DEFAULT_TEMP_RANGE = (5.0, 35.0)
+HYSTERESIS_IDLE_THRESHOLD = 0.5
 
 
 class SwitchbotSmartThermostatRadiator(
@@ -193,4 +194,15 @@ class SwitchbotSmartThermostatRadiator(
         """Return current action from cache."""
         if not self.is_on():
             return ClimateAction.OFF
+
+        current_temp = self.get_current_temperature()
+        target_temp = self.get_target_temperature()
+
+        if (
+            current_temp is not None
+            and target_temp is not None
+            and current_temp >= (target_temp + HYSTERESIS_IDLE_THRESHOLD)
+        ):
+            return ClimateAction.IDLE
+
         return ClimateAction.HEATING

+ 18 - 0
tests/test_smart_thermostat_radiator.py

@@ -219,3 +219,21 @@ def test_default_model_classvar():
         ble_device, "ff", "ffffffffffffffffffffffffffffffff"
     )
     assert device._model == SMART_THERMOSTAT_RADIATOR_INFO.modelName
+
+
+@pytest.mark.asyncio
+async def test_thermostat_idle_action() -> None:
+    """Test TRV action transitions exactly at the 0.5°C hysteresis boundary."""
+    # Case 1: Room temperature is exactly at target + 0.5 -> Should be IDLE
+    device_at_boundary = create_device_for_command_testing(
+        SMART_THERMOSTAT_RADIATOR_INFO,
+        {"target_temperature": 20.0, "temperature": 20.5},
+    )
+    assert device_at_boundary.hvac_action == ClimateAction.IDLE
+
+    # Case 2: Room is warm but within the 0.5 chattering band -> Should still be HEATING
+    device_in_band = create_device_for_command_testing(
+        SMART_THERMOSTAT_RADIATOR_INFO,
+        {"target_temperature": 20.0, "temperature": 20.4},
+    )
+    assert device_in_band.hvac_action == ClimateAction.HEATING