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
13 changes: 12 additions & 1 deletion homeassistant/components/mqtt/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
SUPPORT_TARGET_TEMPERATURE_RANGE)
from homeassistant.components.fan import SPEED_HIGH, SPEED_LOW, SPEED_MEDIUM
from homeassistant.const import (
ATTR_TEMPERATURE, CONF_DEVICE, CONF_NAME, CONF_VALUE_TEMPLATE, STATE_ON)
ATTR_TEMPERATURE, CONF_DEVICE, CONF_NAME, CONF_VALUE_TEMPLATE,
PRECISION_HALVES, PRECISION_TENTHS, PRECISION_WHOLE, STATE_ON)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
Expand Down Expand Up @@ -58,6 +59,7 @@
CONF_POWER_COMMAND_TOPIC = 'power_command_topic'
CONF_POWER_STATE_TEMPLATE = 'power_state_template'
CONF_POWER_STATE_TOPIC = 'power_state_topic'
CONF_PRECISION = 'precision'
CONF_SEND_IF_OFF = 'send_if_off'
CONF_SWING_MODE_COMMAND_TOPIC = 'swing_mode_command_topic'
CONF_SWING_MODE_LIST = 'swing_modes'
Expand Down Expand Up @@ -151,6 +153,8 @@
vol.Optional(CONF_POWER_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_POWER_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_POWER_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_PRECISION): vol.In(
[PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE]),
vol.Optional(CONF_RETAIN, default=mqtt.DEFAULT_RETAIN): cv.boolean,
vol.Optional(CONF_SEND_IF_OFF, default=True): cv.boolean,
vol.Optional(CONF_ACTION_TEMPLATE): cv.template,
Expand Down Expand Up @@ -779,3 +783,10 @@ def min_temp(self):
def max_temp(self):
"""Return the maximum temperature."""
return self._config[CONF_TEMP_MAX]

@property
def precision(self):
"""Return the precision of the system."""
if self._config.get(CONF_PRECISION) is not None:
return self._config.get(CONF_PRECISION)
return super().precision
37 changes: 37 additions & 0 deletions tests/components/mqtt/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,3 +1012,40 @@ async def test_entity_id_update(hass, mqtt_mock):
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('avty-topic', ANY, 0, 'utf-8')


async def test_precision_default(hass, mqtt_mock):
"""Test that setting precision to tenths works as intended."""
assert await async_setup_component(hass, CLIMATE_DOMAIN, DEFAULT_CONFIG)

await common.async_set_temperature(hass, temperature=23.67,
entity_id=ENTITY_CLIMATE)
state = hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('temperature') == 23.7
mqtt_mock.async_publish.reset_mock()


async def test_precision_halves(hass, mqtt_mock):
"""Test that setting precision to halves works as intended."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['precision'] = 0.5
assert await async_setup_component(hass, CLIMATE_DOMAIN, config)

await common.async_set_temperature(hass, temperature=23.67,
entity_id=ENTITY_CLIMATE)
state = hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('temperature') == 23.5
mqtt_mock.async_publish.reset_mock()


async def test_precision_whole(hass, mqtt_mock):
"""Test that setting precision to whole works as intended."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['precision'] = 1.0
assert await async_setup_component(hass, CLIMATE_DOMAIN, config)

await common.async_set_temperature(hass, temperature=23.67,
entity_id=ENTITY_CLIMATE)
state = hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get('temperature') == 24.0
mqtt_mock.async_publish.reset_mock()