Skip to content

Commit

Permalink
Add min_temp and max_temp to MQTT climate device (#14690)
Browse files Browse the repository at this point in the history
* Add min_temp and max_temp to MQTT climate device

* Add unit tests

* Remove blank line

* Fix unit tests & temp return values

* PEP-8 fixes

* Remove unused import
  • Loading branch information
PhilRW authored and balloob committed Jun 7, 2018
1 parent a6c1192 commit bb4d177
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
32 changes: 28 additions & 4 deletions homeassistant/components/climate/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
PLATFORM_SCHEMA as CLIMATE_PLATFORM_SCHEMA, STATE_AUTO,
ATTR_OPERATION_MODE, SUPPORT_TARGET_TEMPERATURE, SUPPORT_OPERATION_MODE,
SUPPORT_SWING_MODE, SUPPORT_FAN_MODE, SUPPORT_AWAY_MODE, SUPPORT_HOLD_MODE,
SUPPORT_AUX_HEAT)
SUPPORT_AUX_HEAT, DEFAULT_MIN_TEMP, DEFAULT_MAX_TEMP)
from homeassistant.const import (
STATE_ON, STATE_OFF, ATTR_TEMPERATURE, CONF_NAME, CONF_VALUE_TEMPLATE)
from homeassistant.components.mqtt import (
Expand Down Expand Up @@ -70,6 +70,9 @@
CONF_INITIAL = 'initial'
CONF_SEND_IF_OFF = 'send_if_off'

CONF_MIN_TEMP = 'min_temp'
CONF_MAX_TEMP = 'max_temp'

SCHEMA_BASE = CLIMATE_PLATFORM_SCHEMA.extend(MQTT_BASE_PLATFORM_SCHEMA.schema)
PLATFORM_SCHEMA = SCHEMA_BASE.extend({
vol.Optional(CONF_POWER_COMMAND_TOPIC): mqtt.valid_publish_topic,
Expand Down Expand Up @@ -116,6 +119,10 @@
vol.Optional(CONF_SEND_IF_OFF, default=True): cv.boolean,
vol.Optional(CONF_PAYLOAD_ON, default="ON"): cv.string,
vol.Optional(CONF_PAYLOAD_OFF, default="OFF"): cv.string,

vol.Optional(CONF_MIN_TEMP, default=DEFAULT_MIN_TEMP): vol.Coerce(float),
vol.Optional(CONF_MAX_TEMP, default=DEFAULT_MAX_TEMP): vol.Coerce(float)

}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)


Expand Down Expand Up @@ -181,19 +188,22 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
config.get(CONF_PAYLOAD_OFF),
config.get(CONF_AVAILABILITY_TOPIC),
config.get(CONF_PAYLOAD_AVAILABLE),
config.get(CONF_PAYLOAD_NOT_AVAILABLE))
config.get(CONF_PAYLOAD_NOT_AVAILABLE),
config.get(CONF_MIN_TEMP),
config.get(CONF_MAX_TEMP))
])


class MqttClimate(MqttAvailability, ClimateDevice):
"""Representation of a demo climate device."""
"""Representation of an MQTT climate device."""

def __init__(self, hass, name, topic, value_templates, qos, retain,
mode_list, fan_mode_list, swing_mode_list,
target_temperature, away, hold, current_fan_mode,
current_swing_mode, current_operation, aux, send_if_off,
payload_on, payload_off, availability_topic,
payload_available, payload_not_available):
payload_available, payload_not_available,
min_temp, max_temp):
"""Initialize the climate device."""
super().__init__(availability_topic, qos, payload_available,
payload_not_available)
Expand All @@ -219,6 +229,8 @@ def __init__(self, hass, name, topic, value_templates, qos, retain,
self._send_if_off = send_if_off
self._payload_on = payload_on
self._payload_off = payload_off
self._min_temp = min_temp
self._max_temp = max_temp

@asyncio.coroutine
def async_added_to_hass(self):
Expand Down Expand Up @@ -619,3 +631,15 @@ def supported_features(self):
support |= SUPPORT_AUX_HEAT

return support

@property
def min_temp(self):
"""Return the minimum temperature."""
# pylint: disable=no-member
return self._min_temp

@property
def max_temp(self):
"""Return the maximum temperature."""
# pylint: disable=no-member
return self._max_temp
34 changes: 31 additions & 3 deletions tests/components/climate/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from homeassistant.components import climate
from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE
from homeassistant.components.climate import (
SUPPORT_OPERATION_MODE, SUPPORT_TARGET_TEMPERATURE,
SUPPORT_FAN_MODE, SUPPORT_SWING_MODE, SUPPORT_HOLD_MODE,
SUPPORT_AWAY_MODE, SUPPORT_AUX_HEAT)
SUPPORT_OPERATION_MODE, SUPPORT_TARGET_TEMPERATURE,
SUPPORT_FAN_MODE, SUPPORT_SWING_MODE, SUPPORT_HOLD_MODE,
SUPPORT_AWAY_MODE, SUPPORT_AUX_HEAT, DEFAULT_MIN_TEMP, DEFAULT_MAX_TEMP)
from tests.common import (get_test_home_assistant, mock_mqtt_component,
fire_mqtt_message, mock_component)

Expand Down Expand Up @@ -53,6 +53,8 @@ def test_setup_params(self):
self.assertEqual("low", state.attributes.get('fan_mode'))
self.assertEqual("off", state.attributes.get('swing_mode'))
self.assertEqual("off", state.attributes.get('operation_mode'))
self.assertEqual(DEFAULT_MIN_TEMP, state.attributes.get('min_temp'))
self.assertEqual(DEFAULT_MAX_TEMP, state.attributes.get('max_temp'))

def test_supported_features(self):
"""Test the supported_features."""
Expand Down Expand Up @@ -541,3 +543,29 @@ def test_set_with_templates(self):
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_CLIMATE)
self.assertEqual(74656, state.attributes.get('current_temperature'))

def test_min_temp_custom(self):
"""Test a custom min temp."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['min_temp'] = 26

assert setup_component(self.hass, climate.DOMAIN, config)

state = self.hass.states.get(ENTITY_CLIMATE)
min_temp = state.attributes.get('min_temp')

self.assertIsInstance(min_temp, float)
self.assertEqual(26, state.attributes.get('min_temp'))

def test_max_temp_custom(self):
"""Test a custom max temp."""
config = copy.deepcopy(DEFAULT_CONFIG)
config['climate']['max_temp'] = 60

assert setup_component(self.hass, climate.DOMAIN, config)

state = self.hass.states.get(ENTITY_CLIMATE)
max_temp = state.attributes.get('max_temp')

self.assertIsInstance(max_temp, float)
self.assertEqual(60, max_temp)

0 comments on commit bb4d177

Please sign in to comment.