From 63d9bd4a9cb034501d855f691eb167704536b8e1 Mon Sep 17 00:00:00 2001 From: Derek Brooks Date: Wed, 27 Dec 2017 12:42:56 -0800 Subject: [PATCH] test resume program service --- homeassistant/components/climate/nuheat.py | 7 +-- tests/components/climate/test_nuheat.py | 56 +++++++++++++++++----- tests/components/test_nuheat.py | 4 +- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/climate/nuheat.py b/homeassistant/components/climate/nuheat.py index a4d1cad68a4..67540a2347d 100644 --- a/homeassistant/components/climate/nuheat.py +++ b/homeassistant/components/climate/nuheat.py @@ -19,7 +19,7 @@ from homeassistant.components.climate import ( STATE_AUTO, STATE_HEAT, STATE_IDLE) -from homeassistant.components.nuheat import DATA_NUHEAT +from homeassistant.components.nuheat import DOMAIN as NUHEAT_DOMAIN from homeassistant.config import load_yaml_config_file from homeassistant.const import ( ATTR_ENTITY_ID, @@ -64,7 +64,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): return temperature_unit = hass.config.units.temperature_unit - api, serial_numbers = hass.data[DATA_NUHEAT] + api, serial_numbers = hass.data[NUHEAT_DOMAIN] thermostats = [ NuHeatThermostat(api, serial_number, temperature_unit) for serial_number in serial_numbers @@ -74,7 +74,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None): def resume_program_set_service(service): """Resume the program on the target thermostats.""" entity_id = service.data.get(ATTR_ENTITY_ID) - if entity_id: target_thermostats = [device for device in thermostats if device.entity_id in entity_id] @@ -94,6 +93,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): descriptions.get(SERVICE_RESUME_PROGRAM), schema=RESUME_PROGRAM_SCHEMA) + return True + class NuHeatThermostat(ClimateDevice): """Representation of a NuHeat Thermostat.""" diff --git a/tests/components/climate/test_nuheat.py b/tests/components/climate/test_nuheat.py index b2ad57731ba..6ec63646bec 100644 --- a/tests/components/climate/test_nuheat.py +++ b/tests/components/climate/test_nuheat.py @@ -1,6 +1,7 @@ """The test for the NuHeat thermostat module.""" import unittest from unittest.mock import Mock, patch +from tests.common import get_test_home_assistant from homeassistant.components.climate import ( SUPPORT_HOLD_MODE, @@ -21,7 +22,7 @@ class TestNuHeat(unittest.TestCase): # pylint: disable=protected-access, no-self-use - def setUp(self): + def setUp(self): # pylint: disable=invalid-name """Set up test variables.""" serial_number = "12345" temperature_unit = "F" @@ -45,31 +46,62 @@ class TestNuHeat(unittest.TestCase): thermostat.get_data = Mock() thermostat.resume_schedule = Mock() - api = Mock() - api.get_thermostat.return_value = thermostat + self.api = Mock() + self.api.get_thermostat.return_value = thermostat + self.hass = get_test_home_assistant() self.thermostat = nuheat.NuHeatThermostat( - api, serial_number, temperature_unit) + self.api, serial_number, temperature_unit) + + def tearDown(self): # pylint: disable=invalid-name + """Stop hass.""" + self.hass.stop() @patch("homeassistant.components.climate.nuheat.NuHeatThermostat") def test_setup_platform(self, mocked_thermostat): """Test setup_platform.""" - api = Mock() - data = {"nuheat": (api, ["12345"])} + mocked_thermostat.return_value = self.thermostat + thermostat = mocked_thermostat(self.api, "12345", "F") + thermostats = [thermostat] - hass = Mock() - hass.config.units.temperature_unit.return_value = "F" - hass.data = Mock() - hass.data.__getitem__ = Mock(side_effect=data.__getitem__) + self.hass.data[nuheat.NUHEAT_DOMAIN] = (self.api, ["12345"]) config = {} add_devices = Mock() discovery_info = {} - nuheat.setup_platform(hass, config, add_devices, discovery_info) - thermostats = [mocked_thermostat(api, "12345", "F")] + nuheat.setup_platform(self.hass, config, add_devices, discovery_info) add_devices.assert_called_once_with(thermostats, True) + @patch("homeassistant.components.climate.nuheat.NuHeatThermostat") + def test_resume_program_service(self, mocked_thermostat): + """Test resume program service.""" + mocked_thermostat.return_value = self.thermostat + thermostat = mocked_thermostat(self.api, "12345", "F") + thermostat.resume_program = Mock() + thermostat.schedule_update_ha_state = Mock() + thermostat.entity_id = "climate.master_bathroom" + + self.hass.data[nuheat.NUHEAT_DOMAIN] = (self.api, ["12345"]) + nuheat.setup_platform(self.hass, {}, Mock(), {}) + + # Explicit entity + self.hass.services.call(nuheat.DOMAIN, nuheat.SERVICE_RESUME_PROGRAM, + {"entity_id": "climate.master_bathroom"}, True) + + thermostat.resume_program.assert_called_with() + thermostat.schedule_update_ha_state.assert_called_with(True) + + thermostat.resume_program.reset_mock() + thermostat.schedule_update_ha_state.reset_mock() + + # All entities + self.hass.services.call( + nuheat.DOMAIN, nuheat.SERVICE_RESUME_PROGRAM, {}, True) + + thermostat.resume_program.assert_called_with() + thermostat.schedule_update_ha_state.assert_called_with(True) + def test_name(self): """Test name property.""" self.assertEqual(self.thermostat.name, "Master bathroom") diff --git a/tests/components/test_nuheat.py b/tests/components/test_nuheat.py index 6b091b8df35..6b486c6afcc 100644 --- a/tests/components/test_nuheat.py +++ b/tests/components/test_nuheat.py @@ -18,12 +18,12 @@ VALID_CONFIG = { class TestNuHeat(unittest.TestCase): """Test the NuHeat component.""" - def setUp(self): + def setUp(self): # pylint: disable=invalid-name """Initialize the values for this test class.""" self.hass = get_test_home_assistant() self.config = VALID_CONFIG - def tearDown(self): + def tearDown(self): # pylint: disable=invalid-name """Teardown this test class. Stop hass.""" self.hass.stop()