From 047c847f3ca4e5d605cf89129dc80c638dbabafe Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Fri, 16 Feb 2024 08:46:11 +0100 Subject: [PATCH] Fix rounding regulated + offset (#384) Co-authored-by: Jean-Marc Collin --- .../thermostat_climate.py | 50 +++++++++---------- tests/const.py | 3 +- tests/test_auto_regulation.py | 16 +++--- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/custom_components/versatile_thermostat/thermostat_climate.py b/custom_components/versatile_thermostat/thermostat_climate.py index 8189683..1b2b36f 100644 --- a/custom_components/versatile_thermostat/thermostat_climate.py +++ b/custom_components/versatile_thermostat/thermostat_climate.py @@ -216,7 +216,7 @@ class ThermostatOverClimate(BaseThermostat): ): offset_temp = device_temp - self.current_temperature - target_temp = self.regulated_target_temp + offset_temp + target_temp = round_to_nearest(self.regulated_target_temp + offset_temp, self._auto_regulation_dtemp) _LOGGER.debug( "%s - The device offset temp for regulation is %.2f - internal temp is %.2f. New target is %.2f", @@ -491,9 +491,9 @@ class ThermostatOverClimate(BaseThermostat): super().update_custom_attributes() self._attr_extra_state_attributes["is_over_climate"] = self.is_over_climate - self._attr_extra_state_attributes[ - "start_hvac_action_date" - ] = self._underlying_climate_start_hvac_action_date + self._attr_extra_state_attributes["start_hvac_action_date"] = ( + self._underlying_climate_start_hvac_action_date + ) self._attr_extra_state_attributes["underlying_climate_0"] = self._underlyings[ 0 ].entity_id @@ -509,32 +509,32 @@ class ThermostatOverClimate(BaseThermostat): if self.is_regulated: self._attr_extra_state_attributes["is_regulated"] = self.is_regulated - self._attr_extra_state_attributes[ - "regulated_target_temperature" - ] = self._regulated_target_temp - self._attr_extra_state_attributes[ - "auto_regulation_mode" - ] = self.auto_regulation_mode - self._attr_extra_state_attributes[ - "regulation_accumulated_error" - ] = self._regulation_algo.accumulated_error + self._attr_extra_state_attributes["regulated_target_temperature"] = ( + self._regulated_target_temp + ) + self._attr_extra_state_attributes["auto_regulation_mode"] = ( + self.auto_regulation_mode + ) + self._attr_extra_state_attributes["regulation_accumulated_error"] = ( + self._regulation_algo.accumulated_error + ) self._attr_extra_state_attributes["auto_fan_mode"] = self.auto_fan_mode - self._attr_extra_state_attributes[ - "current_auto_fan_mode" - ] = self._current_auto_fan_mode + self._attr_extra_state_attributes["current_auto_fan_mode"] = ( + self._current_auto_fan_mode + ) - self._attr_extra_state_attributes[ - "auto_activated_fan_mode" - ] = self._auto_activated_fan_mode + self._attr_extra_state_attributes["auto_activated_fan_mode"] = ( + self._auto_activated_fan_mode + ) - self._attr_extra_state_attributes[ - "auto_deactivated_fan_mode" - ] = self._auto_deactivated_fan_mode + self._attr_extra_state_attributes["auto_deactivated_fan_mode"] = ( + self._auto_deactivated_fan_mode + ) - self._attr_extra_state_attributes[ - "auto_regulation_use_device_temp" - ] = self.auto_regulation_use_device_temp + self._attr_extra_state_attributes["auto_regulation_use_device_temp"] = ( + self.auto_regulation_use_device_temp + ) self.async_write_ha_state() _LOGGER.debug( diff --git a/tests/const.py b/tests/const.py index b00569c..df32bb0 100644 --- a/tests/const.py +++ b/tests/const.py @@ -1,4 +1,5 @@ """ The commons const for all tests """ + from homeassistant.components.climate.const import ( # pylint: disable=unused-import PRESET_BOOST, PRESET_COMFORT, @@ -52,7 +53,7 @@ MOCK_TH_OVER_CLIMATE_MAIN_CONFIG = { CONF_CYCLE_MIN: 5, CONF_DEVICE_POWER: 1, CONF_USE_MAIN_CENTRAL_CONFIG: False, - CONF_USE_CENTRAL_MODE: True + CONF_USE_CENTRAL_MODE: True, # Keep default values which are False } diff --git a/tests/test_auto_regulation.py b/tests/test_auto_regulation.py index 0302f8b..7559c68 100644 --- a/tests/test_auto_regulation.py +++ b/tests/test_auto_regulation.py @@ -387,7 +387,7 @@ async def test_over_climate_regulation_use_device_temp( title="TheOverClimateMockName", unique_id="uniqueId", # This is include a medium regulation - data=PARTIAL_CLIMATE_CONFIG_USE_DEVICE_TEMP, + data=PARTIAL_CLIMATE_CONFIG_USE_DEVICE_TEMP | {CONF_AUTO_REGULATION_DTEMP: 0.5}, ) tz = get_tz(hass) # pylint: disable=invalid-name @@ -475,7 +475,7 @@ async def test_over_climate_regulation_use_device_temp( # room temp is 15 # target is 18 # internal heater temp is 20 - fake_underlying_climate.set_current_temperature(20) + fake_underlying_climate.set_current_temperature(20.1) await entity.async_set_temperature(temperature=18) await send_ext_temperature_change_event(entity, 9, event_timestamp) @@ -488,7 +488,7 @@ async def test_over_climate_regulation_use_device_temp( # the regulated temperature should be under (device offset is -2) assert entity.regulated_target_temp > entity.target_temperature - assert entity.regulated_target_temp == 19.4 # 18 + 1.4 + assert entity.regulated_target_temp == 19.5 # round(18 + 1.4, 0.5) mock_service_call.assert_has_calls( [ @@ -497,7 +497,7 @@ async def test_over_climate_regulation_use_device_temp( "set_temperature", { "entity_id": "climate.mock_climate", - "temperature": 24.4, # 19.4 + 5 + "temperature": 24.5, # round(19.5 + 5, 0.5) "target_temp_high": 30, "target_temp_low": 15, }, @@ -511,7 +511,7 @@ async def test_over_climate_regulation_use_device_temp( # internal heater temp is 27 await entity.async_set_hvac_mode(HVACMode.COOL) await entity.async_set_temperature(temperature=23) - fake_underlying_climate.set_current_temperature(27) + fake_underlying_climate.set_current_temperature(26.9) await send_ext_temperature_change_event(entity, 30, event_timestamp) event_timestamp = now - timedelta(minutes=3) @@ -521,9 +521,9 @@ async def test_over_climate_regulation_use_device_temp( ), patch("homeassistant.core.ServiceRegistry.async_call") as mock_service_call: await send_temperature_change_event(entity, 25, event_timestamp) - # the regulated temperature should be upper (device offset is +2) + # the regulated temperature should be upper (device offset is +1.9) assert entity.regulated_target_temp < entity.target_temperature - assert entity.regulated_target_temp == 22.4 + assert entity.regulated_target_temp == 22.5 mock_service_call.assert_has_calls( [ @@ -532,7 +532,7 @@ async def test_over_climate_regulation_use_device_temp( "set_temperature", { "entity_id": "climate.mock_climate", - "temperature": 24.4, # 22.4 + 2° of offset + "temperature": 24.5, # round(22.5 + 1.9° of offset) "target_temp_high": 30, "target_temp_low": 15, },