Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions homeassistant/components/mqtt/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
ATTR_TEMPERATURE,
CONF_DEVICE,
CONF_NAME,
CONF_TEMPERATURE_UNIT,
CONF_VALUE_TEMPLATE,
PRECISION_HALVES,
PRECISION_TENTHS,
Expand Down Expand Up @@ -223,6 +224,7 @@
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,
}
Expand Down Expand Up @@ -294,7 +296,6 @@ def __init__(self, hass, config, config_entry, discovery_data):
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)
Expand Down Expand Up @@ -583,7 +584,9 @@ def unique_id(self):
@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):
Expand Down
14 changes: 14 additions & 0 deletions tests/components/mqtt/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down