test resume program service

This commit is contained in:
Derek Brooks 2017-12-27 12:42:56 -08:00
parent f0244d7982
commit 63d9bd4a9c
3 changed files with 50 additions and 17 deletions

View File

@ -19,7 +19,7 @@ from homeassistant.components.climate import (
STATE_AUTO, STATE_AUTO,
STATE_HEAT, STATE_HEAT,
STATE_IDLE) 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.config import load_yaml_config_file
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
@ -64,7 +64,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
return return
temperature_unit = hass.config.units.temperature_unit temperature_unit = hass.config.units.temperature_unit
api, serial_numbers = hass.data[DATA_NUHEAT] api, serial_numbers = hass.data[NUHEAT_DOMAIN]
thermostats = [ thermostats = [
NuHeatThermostat(api, serial_number, temperature_unit) NuHeatThermostat(api, serial_number, temperature_unit)
for serial_number in serial_numbers 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): def resume_program_set_service(service):
"""Resume the program on the target thermostats.""" """Resume the program on the target thermostats."""
entity_id = service.data.get(ATTR_ENTITY_ID) entity_id = service.data.get(ATTR_ENTITY_ID)
if entity_id: if entity_id:
target_thermostats = [device for device in thermostats target_thermostats = [device for device in thermostats
if device.entity_id in entity_id] 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), descriptions.get(SERVICE_RESUME_PROGRAM),
schema=RESUME_PROGRAM_SCHEMA) schema=RESUME_PROGRAM_SCHEMA)
return True
class NuHeatThermostat(ClimateDevice): class NuHeatThermostat(ClimateDevice):
"""Representation of a NuHeat Thermostat.""" """Representation of a NuHeat Thermostat."""

View File

@ -1,6 +1,7 @@
"""The test for the NuHeat thermostat module.""" """The test for the NuHeat thermostat module."""
import unittest import unittest
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from tests.common import get_test_home_assistant
from homeassistant.components.climate import ( from homeassistant.components.climate import (
SUPPORT_HOLD_MODE, SUPPORT_HOLD_MODE,
@ -21,7 +22,7 @@ class TestNuHeat(unittest.TestCase):
# pylint: disable=protected-access, no-self-use # pylint: disable=protected-access, no-self-use
def setUp(self): def setUp(self): # pylint: disable=invalid-name
"""Set up test variables.""" """Set up test variables."""
serial_number = "12345" serial_number = "12345"
temperature_unit = "F" temperature_unit = "F"
@ -45,31 +46,62 @@ class TestNuHeat(unittest.TestCase):
thermostat.get_data = Mock() thermostat.get_data = Mock()
thermostat.resume_schedule = Mock() thermostat.resume_schedule = Mock()
api = Mock() self.api = Mock()
api.get_thermostat.return_value = thermostat self.api.get_thermostat.return_value = thermostat
self.hass = get_test_home_assistant()
self.thermostat = nuheat.NuHeatThermostat( 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") @patch("homeassistant.components.climate.nuheat.NuHeatThermostat")
def test_setup_platform(self, mocked_thermostat): def test_setup_platform(self, mocked_thermostat):
"""Test setup_platform.""" """Test setup_platform."""
api = Mock() mocked_thermostat.return_value = self.thermostat
data = {"nuheat": (api, ["12345"])} thermostat = mocked_thermostat(self.api, "12345", "F")
thermostats = [thermostat]
hass = Mock() self.hass.data[nuheat.NUHEAT_DOMAIN] = (self.api, ["12345"])
hass.config.units.temperature_unit.return_value = "F"
hass.data = Mock()
hass.data.__getitem__ = Mock(side_effect=data.__getitem__)
config = {} config = {}
add_devices = Mock() add_devices = Mock()
discovery_info = {} discovery_info = {}
nuheat.setup_platform(hass, config, add_devices, discovery_info) nuheat.setup_platform(self.hass, config, add_devices, discovery_info)
thermostats = [mocked_thermostat(api, "12345", "F")]
add_devices.assert_called_once_with(thermostats, True) 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): def test_name(self):
"""Test name property.""" """Test name property."""
self.assertEqual(self.thermostat.name, "Master bathroom") self.assertEqual(self.thermostat.name, "Master bathroom")

View File

@ -18,12 +18,12 @@ VALID_CONFIG = {
class TestNuHeat(unittest.TestCase): class TestNuHeat(unittest.TestCase):
"""Test the NuHeat component.""" """Test the NuHeat component."""
def setUp(self): def setUp(self): # pylint: disable=invalid-name
"""Initialize the values for this test class.""" """Initialize the values for this test class."""
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()
self.config = VALID_CONFIG self.config = VALID_CONFIG
def tearDown(self): def tearDown(self): # pylint: disable=invalid-name
"""Teardown this test class. Stop hass.""" """Teardown this test class. Stop hass."""
self.hass.stop() self.hass.stop()