From bb4d1773d31f3681d7fa1ed14dfa9200b5b1ca3b Mon Sep 17 00:00:00 2001 From: Philip Rosenberg-Watt Date: Thu, 7 Jun 2018 11:50:12 -0600 Subject: [PATCH] Add min_temp and max_temp to MQTT climate device (#14690) * 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 --- homeassistant/components/climate/mqtt.py | 32 +++++++++++++++++++--- tests/components/climate/test_mqtt.py | 34 +++++++++++++++++++++--- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/climate/mqtt.py b/homeassistant/components/climate/mqtt.py index 1d98a5733f705..5397daeb784cf 100644 --- a/homeassistant/components/climate/mqtt.py +++ b/homeassistant/components/climate/mqtt.py @@ -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 ( @@ -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, @@ -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) @@ -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) @@ -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): @@ -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 diff --git a/tests/components/climate/test_mqtt.py b/tests/components/climate/test_mqtt.py index 663393503aca8..255d482d58439 100644 --- a/tests/components/climate/test_mqtt.py +++ b/tests/components/climate/test_mqtt.py @@ -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) @@ -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.""" @@ -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)