Log error for servicecall without required data
* Log error for services called without required attributes, in media_player, notify and thermostat platforms. * Add fan property and methods in thermostat demo component. * Add tests for notify file and thermostat demo component. * Increase coverage of tests for media_player, notify and thermostat platforms. * Fix some PEP issues, but not all. Tests still have old linting errors.
This commit is contained in:
1 parent
72fdd94b29
commit
4e4b24fcff
14 files changed
+489
-90
No files matched your search
@@ -0,0 +1,101 @@
|
||||
"""The tests for the demo thermostat."""
|
||||
import unittest
|
||||
|
||||
from homeassistant.const import (
|
||||
TEMP_CELCIUS,
|
||||
)
|
||||
from homeassistant.components import thermostat
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
ENTITY_NEST = 'thermostat.nest'
|
||||
|
||||
|
||||
class TestDemoThermostat(unittest.TestCase):
|
||||
"""Test the Heat Control thermostat."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.hass.config.temperature_unit = TEMP_CELCIUS
|
||||
self.assertTrue(thermostat.setup(self.hass, {'thermostat': {
|
||||
'platform': 'demo',
|
||||
}}))
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
"""Stop down everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_setup_params(self):
|
||||
"""Test the inititial parameters."""
|
||||
state = self.hass.states.get(ENTITY_NEST)
|
||||
self.assertEqual(21, state.attributes.get('temperature'))
|
||||
self.assertEqual('off', state.attributes.get('away_mode'))
|
||||
self.assertEqual(19, state.attributes.get('current_temperature'))
|
||||
self.assertEqual('off', state.attributes.get('fan'))
|
||||
|
||||
def test_default_setup_params(self):
|
||||
"""Test the setup with default parameters."""
|
||||
state = self.hass.states.get(ENTITY_NEST)
|
||||
self.assertEqual(7, state.attributes.get('min_temp'))
|
||||
self.assertEqual(35, state.attributes.get('max_temp'))
|
||||
|
||||
def test_set_target_temp_bad_attr(self):
|
||||
"""Test setting the target temperature without required attribute."""
|
||||
self.assertEqual('21', self.hass.states.get(ENTITY_NEST).state)
|
||||
thermostat.set_temperature(self.hass, None, ENTITY_NEST)
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual('21', self.hass.states.get(ENTITY_NEST).state)
|
||||
|
||||
def test_set_target_temp(self):
|
||||
"""Test the setting of the target temperature."""
|
||||
thermostat.set_temperature(self.hass, 30, ENTITY_NEST)
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual('30.0', self.hass.states.get(ENTITY_NEST).state)
|
||||
|
||||
def test_set_away_mode_bad_attr(self):
|
||||
"""Test setting the away mode without required attribute."""
|
||||
state = self.hass.states.get(ENTITY_NEST)
|
||||
self.assertEqual('off', state.attributes.get('away_mode'))
|
||||
thermostat.set_away_mode(self.hass, None, ENTITY_NEST)
|
||||
self.hass.pool.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_NEST)
|
||||
self.assertEqual('off', state.attributes.get('away_mode'))
|
||||
|
||||
def test_set_away_mode_on(self):
|
||||
"""Test setting the away mode on/true."""
|
||||
thermostat.set_away_mode(self.hass, True, ENTITY_NEST)
|
||||
self.hass.pool.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_NEST)
|
||||
self.assertEqual('on', state.attributes.get('away_mode'))
|
||||
|
||||
def test_set_away_mode_off(self):
|
||||
"""Test setting the away mode off/false."""
|
||||
thermostat.set_away_mode(self.hass, False, ENTITY_NEST)
|
||||
self.hass.pool.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_NEST)
|
||||
self.assertEqual('off', state.attributes.get('away_mode'))
|
||||
|
||||
def test_set_fan_mode_on_bad_attr(self):
|
||||
"""Test setting the fan mode on/true without required attribute."""
|
||||
state = self.hass.states.get(ENTITY_NEST)
|
||||
self.assertEqual('off', state.attributes.get('fan'))
|
||||
thermostat.set_fan_mode(self.hass, None, ENTITY_NEST)
|
||||
self.hass.pool.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_NEST)
|
||||
self.assertEqual('off', state.attributes.get('fan'))
|
||||
|
||||
def test_set_fan_mode_on(self):
|
||||
"""Test setting the fan mode on/true."""
|
||||
thermostat.set_fan_mode(self.hass, True, ENTITY_NEST)
|
||||
self.hass.pool.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_NEST)
|
||||
self.assertEqual('on', state.attributes.get('fan'))
|
||||
|
||||
def test_set_fan_mode_off(self):
|
||||
"""Test setting the fan mode off/false."""
|
||||
thermostat.set_fan_mode(self.hass, False, ENTITY_NEST)
|
||||
self.hass.pool.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_NEST)
|
||||
self.assertEqual('off', state.attributes.get('fan'))
|
||||
@@ -1,5 +1,6 @@
|
||||
"""The tests for the heat control thermostat."""
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
@@ -10,16 +11,55 @@ from homeassistant.const import (
|
||||
TEMP_CELCIUS,
|
||||
)
|
||||
from homeassistant.components import thermostat
|
||||
import homeassistant.components.thermostat.heat_control as heat_control
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
entity = 'thermostat.test'
|
||||
ent_sensor = 'sensor.test'
|
||||
ent_switch = 'switch.test'
|
||||
min_temp = 3.0
|
||||
max_temp = 65.0
|
||||
target_temp = 42.0
|
||||
ENTITY = 'thermostat.test'
|
||||
ENT_SENSOR = 'sensor.test'
|
||||
ENT_SWITCH = 'switch.test'
|
||||
MIN_TEMP = 3.0
|
||||
MAX_TEMP = 65.0
|
||||
TARGET_TEMP = 42.0
|
||||
|
||||
|
||||
class TestSetupThermostatHeatControl(unittest.TestCase):
|
||||
"""Test the Heat Control thermostat with custom config."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
"""Stop down everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_setup_missing_conf(self):
|
||||
"""Test set up heat_control with missing config values."""
|
||||
config = {
|
||||
'name': 'test',
|
||||
'target_sensor': ENT_SENSOR
|
||||
}
|
||||
add_devices = mock.MagicMock()
|
||||
result = heat_control.setup_platform(self.hass, config, add_devices)
|
||||
self.assertEqual(False, result)
|
||||
|
||||
def test_setup_with_sensor(self):
|
||||
"""Test set up heat_control with sensor to trigger update at init."""
|
||||
self.hass.states.set(ENT_SENSOR, 22.0, {
|
||||
ATTR_UNIT_OF_MEASUREMENT: TEMP_CELCIUS
|
||||
})
|
||||
thermostat.setup(self.hass, {'thermostat': {
|
||||
'platform': 'heat_control',
|
||||
'name': 'test',
|
||||
'heater': ENT_SWITCH,
|
||||
'target_sensor': ENT_SENSOR
|
||||
}})
|
||||
state = self.hass.states.get(ENTITY)
|
||||
self.assertEqual(
|
||||
TEMP_CELCIUS, state.attributes.get('unit_of_measurement'))
|
||||
self.assertEqual(22.0, state.attributes.get('current_temperature'))
|
||||
|
||||
|
||||
class TestThermostatHeatControl(unittest.TestCase):
|
||||
@@ -32,8 +72,8 @@ class TestThermostatHeatControl(unittest.TestCase):
|
||||
thermostat.setup(self.hass, {'thermostat': {
|
||||
'platform': 'heat_control',
|
||||
'name': 'test',
|
||||
'heater': ent_switch,
|
||||
'target_sensor': ent_sensor
|
||||
'heater': ENT_SWITCH,
|
||||
'target_sensor': ENT_SENSOR
|
||||
}})
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
@@ -42,11 +82,11 @@ class TestThermostatHeatControl(unittest.TestCase):
|
||||
|
||||
def test_setup_defaults_to_unknown(self):
|
||||
"""Test the setting of defaults to unknown."""
|
||||
self.assertEqual('unknown', self.hass.states.get(entity).state)
|
||||
self.assertEqual('unknown', self.hass.states.get(ENTITY).state)
|
||||
|
||||
def test_default_setup_params(self):
|
||||
"""Test the setup with default parameters."""
|
||||
state = self.hass.states.get(entity)
|
||||
state = self.hass.states.get(ENTITY)
|
||||
self.assertEqual(7, state.attributes.get('min_temp'))
|
||||
self.assertEqual(35, state.attributes.get('max_temp'))
|
||||
self.assertEqual(None, state.attributes.get('temperature'))
|
||||
@@ -56,25 +96,41 @@ class TestThermostatHeatControl(unittest.TestCase):
|
||||
thermostat.setup(self.hass, {'thermostat': {
|
||||
'platform': 'heat_control',
|
||||
'name': 'test',
|
||||
'heater': ent_switch,
|
||||
'target_sensor': ent_sensor,
|
||||
'min_temp': min_temp,
|
||||
'max_temp': max_temp,
|
||||
'target_temp': target_temp
|
||||
'heater': ENT_SWITCH,
|
||||
'target_sensor': ENT_SENSOR,
|
||||
'min_temp': MIN_TEMP,
|
||||
'max_temp': MAX_TEMP,
|
||||
'target_temp': TARGET_TEMP
|
||||
}})
|
||||
state = self.hass.states.get(entity)
|
||||
self.assertEqual(min_temp, state.attributes.get('min_temp'))
|
||||
self.assertEqual(max_temp, state.attributes.get('max_temp'))
|
||||
self.assertEqual(target_temp, state.attributes.get('temperature'))
|
||||
self.assertEqual(str(target_temp), self.hass.states.get(entity).state)
|
||||
state = self.hass.states.get(ENTITY)
|
||||
self.assertEqual(MIN_TEMP, state.attributes.get('min_temp'))
|
||||
self.assertEqual(MAX_TEMP, state.attributes.get('max_temp'))
|
||||
self.assertEqual(TARGET_TEMP, state.attributes.get('temperature'))
|
||||
self.assertEqual(str(TARGET_TEMP), self.hass.states.get(ENTITY).state)
|
||||
|
||||
def test_set_target_temp(self):
|
||||
"""Test the setting of the target temperature."""
|
||||
thermostat.set_temperature(self.hass, 30)
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual('30.0', self.hass.states.get(entity).state)
|
||||
self.assertEqual('30.0', self.hass.states.get(ENTITY).state)
|
||||
|
||||
def test_set_target_temp_turns_on_heater(self):
|
||||
def test_sensor_bad_unit(self):
|
||||
"""Test sensor that have bad unit."""
|
||||
self._setup_sensor(22.0, unit='bad_unit')
|
||||
self.hass.pool.block_till_done()
|
||||
state = self.hass.states.get(ENTITY)
|
||||
self.assertEqual(None, state.attributes.get('unit_of_measurement'))
|
||||
self.assertEqual(None, state.attributes.get('current_temperature'))
|
||||
|
||||
def test_sensor_bad_value(self):
|
||||
"""Test sensor that have None as state."""
|
||||
self._setup_sensor(None)
|
||||
self.hass.pool.block_till_done()
|
||||
state = self.hass.states.get(ENTITY)
|
||||
self.assertEqual(None, state.attributes.get('unit_of_measurement'))
|
||||
self.assertEqual(None, state.attributes.get('current_temperature'))
|
||||
|
||||
def test_set_target_temp_heater_on(self):
|
||||
"""Test if target temperature turn heater on."""
|
||||
self._setup_switch(False)
|
||||
self._setup_sensor(25)
|
||||
@@ -85,9 +141,9 @@ class TestThermostatHeatControl(unittest.TestCase):
|
||||
call = self.calls[0]
|
||||
self.assertEqual('switch', call.domain)
|
||||
self.assertEqual(SERVICE_TURN_ON, call.service)
|
||||
self.assertEqual(ent_switch, call.data['entity_id'])
|
||||
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
||||
|
||||
def test_set_target_temp_turns_off_heater(self):
|
||||
def test_set_target_temp_heater_off(self):
|
||||
"""Test if target temperature turn heater off."""
|
||||
self._setup_switch(True)
|
||||
self._setup_sensor(30)
|
||||
@@ -98,9 +154,9 @@ class TestThermostatHeatControl(unittest.TestCase):
|
||||
call = self.calls[0]
|
||||
self.assertEqual('switch', call.domain)
|
||||
self.assertEqual(SERVICE_TURN_OFF, call.service)
|
||||
self.assertEqual(ent_switch, call.data['entity_id'])
|
||||
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
||||
|
||||
def test_set_temp_change_turns_on_heater(self):
|
||||
def test_set_temp_change_heater_on(self):
|
||||
"""Test if temperature change turn heater on."""
|
||||
self._setup_switch(False)
|
||||
thermostat.set_temperature(self.hass, 30)
|
||||
@@ -111,9 +167,9 @@ class TestThermostatHeatControl(unittest.TestCase):
|
||||
call = self.calls[0]
|
||||
self.assertEqual('switch', call.domain)
|
||||
self.assertEqual(SERVICE_TURN_ON, call.service)
|
||||
self.assertEqual(ent_switch, call.data['entity_id'])
|
||||
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
||||
|
||||
def test_temp_change_turns_off_heater(self):
|
||||
def test_temp_change_heater_off(self):
|
||||
"""Test if temperature change turn heater off."""
|
||||
self._setup_switch(True)
|
||||
thermostat.set_temperature(self.hass, 25)
|
||||
@@ -124,17 +180,17 @@ class TestThermostatHeatControl(unittest.TestCase):
|
||||
call = self.calls[0]
|
||||
self.assertEqual('switch', call.domain)
|
||||
self.assertEqual(SERVICE_TURN_OFF, call.service)
|
||||
self.assertEqual(ent_switch, call.data['entity_id'])
|
||||
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
||||
|
||||
def _setup_sensor(self, temp, unit=TEMP_CELCIUS):
|
||||
"""Setup the test sensor."""
|
||||
self.hass.states.set(ent_sensor, temp, {
|
||||
self.hass.states.set(ENT_SENSOR, temp, {
|
||||
ATTR_UNIT_OF_MEASUREMENT: unit
|
||||
})
|
||||
|
||||
def _setup_switch(self, is_on):
|
||||
"""Setup the test switch."""
|
||||
self.hass.states.set(ent_switch, STATE_ON if is_on else STATE_OFF)
|
||||
self.hass.states.set(ENT_SWITCH, STATE_ON if is_on else STATE_OFF)
|
||||
self.calls = []
|
||||
|
||||
def log_call(call):
|
||||
|
||||
@@ -23,6 +23,15 @@ class TestHoneywell(unittest.TestCase):
|
||||
CONF_PASSWORD: 'pass',
|
||||
'region': 'us',
|
||||
}
|
||||
bad_pass_config = {
|
||||
CONF_USERNAME: 'user',
|
||||
'region': 'us',
|
||||
}
|
||||
bad_region_config = {
|
||||
CONF_USERNAME: 'user',
|
||||
CONF_PASSWORD: 'pass',
|
||||
'region': 'un',
|
||||
}
|
||||
hass = mock.MagicMock()
|
||||
add_devices = mock.MagicMock()
|
||||
|
||||
@@ -37,6 +46,10 @@ class TestHoneywell(unittest.TestCase):
|
||||
locations[0].devices_by_id.values.return_value = devices_1
|
||||
locations[1].devices_by_id.values.return_value = devices_2
|
||||
|
||||
result = honeywell.setup_platform(hass, bad_pass_config, add_devices)
|
||||
self.assertFalse(result)
|
||||
result = honeywell.setup_platform(hass, bad_region_config, add_devices)
|
||||
self.assertFalse(result)
|
||||
result = honeywell.setup_platform(hass, config, add_devices)
|
||||
self.assertTrue(result)
|
||||
mock_sc.assert_called_once_with('user', 'pass')
|
||||
|
||||
Reference in new issue
Block a user