diff --git a/homeassistant/components/mqtt/climate.py b/homeassistant/components/mqtt/climate.py index 0216302c651..91e30c7b1b1 100644 --- a/homeassistant/components/mqtt/climate.py +++ b/homeassistant/components/mqtt/climate.py @@ -37,6 +37,7 @@ from homeassistant.const import ( ATTR_TEMPERATURE, CONF_DEVICE, CONF_NAME, + CONF_TEMPERATURE_UNIT, CONF_VALUE_TEMPLATE, PRECISION_HALVES, PRECISION_TENTHS, @@ -223,6 +224,7 @@ PLATFORM_SCHEMA = ( vol.Optional(CONF_TEMP_LOW_STATE_TOPIC): mqtt.valid_subscribe_topic, vol.Optional(CONF_TEMP_STATE_TEMPLATE): cv.template, vol.Optional(CONF_TEMP_STATE_TOPIC): mqtt.valid_subscribe_topic, + vol.Optional(CONF_TEMPERATURE_UNIT): cv.temperature_unit, vol.Optional(CONF_UNIQUE_ID): cv.string, vol.Optional(CONF_VALUE_TEMPLATE): cv.template, } @@ -294,7 +296,6 @@ class MqttClimate( self._target_temp_high = None self._target_temp_low = None self._topic = None - self._unit_of_measurement = hass.config.units.temperature_unit self._value_templates = None self._setup_from_config(config) @@ -583,7 +584,9 @@ class MqttClimate( @property def temperature_unit(self): """Return the unit of measurement.""" - return self._unit_of_measurement + if self._config.get(CONF_TEMPERATURE_UNIT): + return self._config.get(CONF_TEMPERATURE_UNIT) + return self.hass.config.units.temperature_unit @property def current_temperature(self): diff --git a/tests/components/mqtt/test_climate.py b/tests/components/mqtt/test_climate.py index 03e22cd72e2..1d485c4be13 100644 --- a/tests/components/mqtt/test_climate.py +++ b/tests/components/mqtt/test_climate.py @@ -793,6 +793,20 @@ async def test_temp_step_custom(hass, mqtt_mock): assert temp_step == 0.01 +async def test_temperature_unit(hass, mqtt_mock): + """Test that setting temperature unit converts temperature values.""" + config = copy.deepcopy(DEFAULT_CONFIG) + config["climate"]["temperature_unit"] = "F" + config["climate"]["current_temperature_topic"] = "current_temperature" + + assert await async_setup_component(hass, CLIMATE_DOMAIN, config) + + async_fire_mqtt_message(hass, "current_temperature", "77") + + state = hass.states.get(ENTITY_CLIMATE) + assert state.attributes.get("current_temperature") == 25 + + async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_via_mqtt_json_message(