1
mirror of https://github.com/home-assistant/core synced 2024-10-01 05:30:36 +02:00

Add MQTT climate temperature unit (#34066)

* Add MQTT temperature unit
This commit is contained in:
presslab-us 2020-04-15 14:11:04 -04:00 committed by GitHub
parent 86f09a42a2
commit e41753556c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -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):

View File

@ -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(