diff --git a/homeassistant/components/daikin/climate.py b/homeassistant/components/daikin/climate.py index 2b02d139dc7..f372c2a2d0e 100644 --- a/homeassistant/components/daikin/climate.py +++ b/homeassistant/components/daikin/climate.py @@ -117,6 +117,11 @@ async def async_setup_entry( async_add_entities([DaikinClimate(daikin_api)], update_before_add=True) +def format_target_temperature(target_temperature): + """Format target temperature to be sent to the Daikin unit, taking care of keeping at most 1 decimal digit.""" + return str(round(float(target_temperature), 1)).rstrip("0").rstrip(".") + + class DaikinClimate(ClimateEntity): """Representation of a Daikin HVAC.""" @@ -163,9 +168,9 @@ class DaikinClimate(ClimateEntity): # temperature elif attr == ATTR_TEMPERATURE: try: - values[HA_ATTR_TO_DAIKIN[ATTR_TARGET_TEMPERATURE]] = str( - round(float(value), 1) - ) + values[ + HA_ATTR_TO_DAIKIN[ATTR_TARGET_TEMPERATURE] + ] = format_target_temperature(value) except ValueError: _LOGGER.error("Invalid temperature %s", value) diff --git a/tests/components/daikin/test_temperature_format.py b/tests/components/daikin/test_temperature_format.py new file mode 100644 index 00000000000..d05efa98b8d --- /dev/null +++ b/tests/components/daikin/test_temperature_format.py @@ -0,0 +1,20 @@ +"""The tests for the Daikin target temperature conversion.""" +from homeassistant.components.daikin.climate import format_target_temperature + + +def test_int_conversion(): + """Check no decimal are kept when target temp is an integer.""" + formatted = format_target_temperature("16") + assert formatted == "16" + + +def test_decimal_conversion(): + """Check 1 decimal is kept when target temp is a decimal.""" + formatted = format_target_temperature("16.1") + assert formatted == "16.1" + + +def test_decimal_conversion_more_digits(): + """Check at most 1 decimal is kept when target temp is a decimal with more than 1 decimal.""" + formatted = format_target_temperature("16.09") + assert formatted == "16.1"