From 3318c55c6539100ac53c45b8d6f9c9e0d81c6be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=B8yer=20Iversen?= Date: Thu, 21 Apr 2016 16:59:35 +0200 Subject: [PATCH] Heat control config validation * heat control configuration validation * fix heat contol test --- .../components/thermostat/heat_control.py | 25 ++++++++++++------- .../thermostat/test_heat_control.py | 17 +++++++++---- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/thermostat/heat_control.py b/homeassistant/components/thermostat/heat_control.py index 12472b61d02..64f95c2e517 100644 --- a/homeassistant/components/thermostat/heat_control.py +++ b/homeassistant/components/thermostat/heat_control.py @@ -5,7 +5,9 @@ For more details about this platform, please refer to the documentation at https://home-assistant.io/components/thermostat.heat_control/ """ import logging +import voluptuous as vol +import homeassistant.helpers.config_validation as cv import homeassistant.util as util from homeassistant.components import switch from homeassistant.components.thermostat import ( @@ -28,21 +30,26 @@ CONF_TARGET_TEMP = 'target_temp' _LOGGER = logging.getLogger(__name__) +PLATFORM_SCHEMA = vol.Schema({ + vol.Required("platform"): "heat_control", + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, + vol.Required(CONF_HEATER): cv.entity_id, + vol.Required(CONF_SENSOR): cv.entity_id, + vol.Optional(CONF_MIN_TEMP): vol.Coerce(float), + vol.Optional(CONF_MAX_TEMP): vol.Coerce(float), + vol.Optional(CONF_TARGET_TEMP): vol.Coerce(float), +}) + # pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the heat control thermostat.""" - name = config.get(CONF_NAME, DEFAULT_NAME) + name = config.get(CONF_NAME) heater_entity_id = config.get(CONF_HEATER) sensor_entity_id = config.get(CONF_SENSOR) - min_temp = util.convert(config.get(CONF_MIN_TEMP), float, None) - max_temp = util.convert(config.get(CONF_MAX_TEMP), float, None) - target_temp = util.convert(config.get(CONF_TARGET_TEMP), float, None) - - if None in (heater_entity_id, sensor_entity_id): - _LOGGER.error('Missing required key %s or %s', CONF_HEATER, - CONF_SENSOR) - return False + min_temp = config.get(CONF_MIN_TEMP) + max_temp = config.get(CONF_MAX_TEMP) + target_temp = config.get(CONF_TARGET_TEMP) add_devices([HeatControl(hass, name, heater_entity_id, sensor_entity_id, min_temp, max_temp, target_temp)]) diff --git a/tests/components/thermostat/test_heat_control.py b/tests/components/thermostat/test_heat_control.py index 7ef23be85bb..ca3572d1710 100644 --- a/tests/components/thermostat/test_heat_control.py +++ b/tests/components/thermostat/test_heat_control.py @@ -1,7 +1,7 @@ """The tests for the heat control thermostat.""" import unittest -from unittest import mock +from homeassistant.bootstrap import _setup_component from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, SERVICE_TURN_OFF, @@ -11,7 +11,6 @@ from homeassistant.const import ( TEMP_CELSIUS, ) from homeassistant.components import thermostat -import homeassistant.components.thermostat.heat_control as heat_control from tests.common import get_test_home_assistant @@ -41,9 +40,17 @@ class TestSetupThermostatHeatControl(unittest.TestCase): 'name': 'test', 'target_sensor': ENT_SENSOR } - add_devices = mock.MagicMock() - result = heat_control.setup_platform(self.hass, config, add_devices) - self.assertEqual(False, result) + self.assertFalse(_setup_component(self.hass, 'thermostat', { + 'thermostat': config})) + + def test_valid_conf(self): + """Test set up heat_control with valid config values.""" + self.assertTrue(_setup_component(self.hass, 'thermostat', + {'thermostat': { + 'platform': 'heat_control', + 'name': 'test', + 'heater': ENT_SWITCH, + 'target_sensor': ENT_SENSOR}})) def test_setup_with_sensor(self): """Test set up heat_control with sensor to trigger update at init."""