From a1f33a093cfb35fcb36a6ade814e8a5a9bd77ca0 Mon Sep 17 00:00:00 2001 From: arantes555 Date: Wed, 20 Apr 2022 00:19:59 +0200 Subject: [PATCH] Generic thermostat temp step (#70303) * Add target_temp_step to generic_thermostat * Add target_temp_step to generic_thermostat : fix default + add tests * Add target_temp_step to generic_thermostat : fix test by using new 'units_imperial' fixture --- .../components/generic_thermostat/climate.py | 13 +++++++++++-- tests/components/generic_thermostat/test_climate.py | 10 +++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py index 56b0dad3416..081bf1900d6 100644 --- a/homeassistant/components/generic_thermostat/climate.py +++ b/homeassistant/components/generic_thermostat/climate.py @@ -75,6 +75,7 @@ CONF_HOT_TOLERANCE = "hot_tolerance" CONF_KEEP_ALIVE = "keep_alive" CONF_INITIAL_HVAC_MODE = "initial_hvac_mode" CONF_PRECISION = "precision" +CONF_TEMP_STEP = "target_temp_step" CONF_PRESETS = { p: f"{p}_temp" @@ -106,6 +107,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( vol.Optional(CONF_PRECISION): vol.In( [PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE] ), + vol.Optional(CONF_TEMP_STEP): vol.In( + [PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE] + ), vol.Optional(CONF_UNIQUE_ID): cv.string, } ).extend({vol.Optional(v): vol.Coerce(float) for (k, v) in CONF_PRESETS.items()}) @@ -137,6 +141,7 @@ async def async_setup_platform( key: config[value] for key, value in CONF_PRESETS.items() if value in config } precision = config.get(CONF_PRECISION) + target_temperature_step = config.get(CONF_TEMP_STEP) unit = hass.config.units.temperature_unit unique_id = config.get(CONF_UNIQUE_ID) @@ -157,6 +162,7 @@ async def async_setup_platform( initial_hvac_mode, presets, precision, + target_temperature_step, unit, unique_id, ) @@ -183,6 +189,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity): initial_hvac_mode, presets, precision, + target_temperature_step, unit, unique_id, ): @@ -198,6 +205,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity): self._hvac_mode = initial_hvac_mode self._saved_target_temp = target_temp or next(iter(presets.values()), None) self._temp_precision = precision + self._temp_target_temperature_step = target_temperature_step if self.ac_mode: self._hvac_list = [HVAC_MODE_COOL, HVAC_MODE_OFF] else: @@ -325,8 +333,9 @@ class GenericThermostat(ClimateEntity, RestoreEntity): @property def target_temperature_step(self): """Return the supported step of target temperature.""" - # Since this integration does not yet have a step size parameter - # we have to re-use the precision as the step size for now. + if self._temp_target_temperature_step is not None: + return self._temp_target_temperature_step + # if a target_temperature_step is not defined, fallback to equal the precision return self.precision @property diff --git a/tests/components/generic_thermostat/test_climate.py b/tests/components/generic_thermostat/test_climate.py index 1720a54d973..23d2078648e 100644 --- a/tests/components/generic_thermostat/test_climate.py +++ b/tests/components/generic_thermostat/test_climate.py @@ -33,7 +33,6 @@ from homeassistant.const import ( STATE_UNAVAILABLE, STATE_UNKNOWN, TEMP_CELSIUS, - TEMP_FAHRENHEIT, ) import homeassistant.core as ha from homeassistant.core import DOMAIN as HASS_DOMAIN, CoreState, State, callback @@ -62,6 +61,7 @@ MAX_TEMP = 65.0 TARGET_TEMP = 42.0 COLD_TOLERANCE = 0.5 HOT_TOLERANCE = 0.5 +TARGET_TEMP_STEP = 0.5 async def test_setup_missing_conf(hass): @@ -276,6 +276,7 @@ async def test_default_setup_params(hass, setup_comp_2): assert state.attributes.get("min_temp") == 7 assert state.attributes.get("max_temp") == 35 assert state.attributes.get("temperature") == 7 + assert state.attributes.get("target_temp_step") == 0.1 async def test_get_hvac_modes(hass, setup_comp_2): @@ -1179,7 +1180,6 @@ async def test_temp_change_heater_trigger_off_long_enough_2(hass, setup_comp_8): @pytest.fixture async def setup_comp_9(hass): """Initialize components.""" - hass.config.temperature_unit = TEMP_FAHRENHEIT assert await async_setup_component( hass, DOMAIN, @@ -1201,11 +1201,13 @@ async def setup_comp_9(hass): await hass.async_block_till_done() -async def test_precision(hass, setup_comp_9): +async def test_precision(hass, units_imperial, setup_comp_9): """Test that setting precision to tenths works as intended.""" await common.async_set_temperature(hass, 23.27) state = hass.states.get(ENTITY) assert state.attributes.get("temperature") == 23.3 + # check that target_temp_step defaults to precision + assert state.attributes.get("target_temp_step") == 0.1 async def test_custom_setup_params(hass): @@ -1222,6 +1224,7 @@ async def test_custom_setup_params(hass): "min_temp": MIN_TEMP, "max_temp": MAX_TEMP, "target_temp": TARGET_TEMP, + "target_temp_step": 0.5, } }, ) @@ -1231,6 +1234,7 @@ async def test_custom_setup_params(hass): assert state.attributes.get("min_temp") == MIN_TEMP assert state.attributes.get("max_temp") == MAX_TEMP assert state.attributes.get("temperature") == TARGET_TEMP + assert state.attributes.get("target_temp_step") == TARGET_TEMP_STEP @pytest.mark.parametrize("hvac_mode", [HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_COOL])