Continue on invalid platforms and new setup_component unit tests (#3736)
This commit is contained in:
1 parent
1b26b5ad14
commit
7b40a641ec
23 files changed
+842
-672
No files matched your search
@@ -1,14 +1,15 @@
|
||||
"""The tests the MQTT alarm control panel component."""
|
||||
import unittest
|
||||
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
from homeassistant.bootstrap import setup_component
|
||||
from homeassistant.const import (
|
||||
STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY,
|
||||
STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED, STATE_UNKNOWN)
|
||||
from homeassistant.components import alarm_control_panel
|
||||
|
||||
from tests.common import (
|
||||
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant)
|
||||
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant,
|
||||
assert_setup_component)
|
||||
|
||||
CODE = 'HELLO_CODE'
|
||||
|
||||
@@ -16,7 +17,9 @@ CODE = 'HELLO_CODE'
|
||||
class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
"""Test the manual alarm module."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
def setUp(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.mock_publish = mock_mqtt_component(self.hass)
|
||||
@@ -28,27 +31,30 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
def test_fail_setup_without_state_topic(self):
|
||||
"""Test for failing with no state topic."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert not _setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'command_topic': 'alarm/command'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(0) as config:
|
||||
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'command_topic': 'alarm/command'
|
||||
}
|
||||
})
|
||||
assert not config[alarm_control_panel.DOMAIN]
|
||||
|
||||
def test_fail_setup_without_command_topic(self):
|
||||
"""Test failing with no command topic."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert not _setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'state_topic': 'alarm/state'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'state_topic': 'alarm/state'
|
||||
}
|
||||
})
|
||||
|
||||
def test_update_state_via_state_topic(self):
|
||||
"""Test updating with via state topic."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
@@ -72,7 +78,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
def test_ignore_update_state_if_unknown_via_state_topic(self):
|
||||
"""Test ignoring updates via state topic."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
@@ -93,7 +99,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
def test_arm_home_publishes_mqtt(self):
|
||||
"""Test publishing of MQTT messages while armed."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
@@ -110,7 +116,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
def test_arm_home_not_publishes_mqtt_with_invalid_code(self):
|
||||
"""Test not publishing of MQTT messages with invalid code."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
@@ -128,7 +134,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
def test_arm_away_publishes_mqtt(self):
|
||||
"""Test publishing of MQTT messages while armed."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
@@ -145,7 +151,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
def test_arm_away_not_publishes_mqtt_with_invalid_code(self):
|
||||
"""Test not publishing of MQTT messages with invalid code."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
@@ -163,7 +169,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
def test_disarm_publishes_mqtt(self):
|
||||
"""Test publishing of MQTT messages while disarmed."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
@@ -180,7 +186,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
def test_disarm_not_publishes_mqtt_with_invalid_code(self):
|
||||
"""Test not publishing of MQTT messages with invalid code."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
|
||||
alarm_control_panel.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
|
||||
@@ -8,19 +8,22 @@ from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, assert_setup_component
|
||||
|
||||
|
||||
class TestAutomation(unittest.TestCase):
|
||||
"""Test the event automation."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
def setUp(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.hass.config.components.append('group')
|
||||
self.calls = []
|
||||
|
||||
def record_call(service):
|
||||
"""Record call."""
|
||||
self.calls.append(service)
|
||||
|
||||
self.hass.services.register('test', 'automation', record_call)
|
||||
@@ -31,18 +34,19 @@ class TestAutomation(unittest.TestCase):
|
||||
|
||||
def test_service_data_not_a_dict(self):
|
||||
"""Test service data not dict."""
|
||||
assert not setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'data': 100,
|
||||
with assert_setup_component(0):
|
||||
assert not setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'data': 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
def test_service_specify_data(self):
|
||||
"""Test service data."""
|
||||
@@ -70,7 +74,7 @@ class TestAutomation(unittest.TestCase):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
assert len(self.calls) == 1
|
||||
assert 'event - test_event' == self.calls[0].data['some']
|
||||
assert self.calls[0].data['some'] == 'event - test_event'
|
||||
state = self.hass.states.get('automation.hello')
|
||||
assert state is not None
|
||||
assert state.attributes.get('last_triggered') == time
|
||||
@@ -444,21 +448,22 @@ class TestAutomation(unittest.TestCase):
|
||||
})
|
||||
def test_reload_config_when_invalid_config(self, mock_load_yaml):
|
||||
"""Test the reload config service handling invalid config."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'data_template': {
|
||||
'event': '{{ trigger.event.event_type }}'
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'data_template': {
|
||||
'event': '{{ trigger.event.event_type }}'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.get('automation.hello') is not None
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
@@ -470,11 +475,11 @@ class TestAutomation(unittest.TestCase):
|
||||
automation.reload(self.hass)
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.states.get('automation.hello') is not None
|
||||
assert self.hass.states.get('automation.hello') is None
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
assert len(self.calls) == 2
|
||||
assert len(self.calls) == 1
|
||||
|
||||
def test_reload_config_handles_load_fails(self):
|
||||
"""Test the reload config service."""
|
||||
|
||||
@@ -4,10 +4,11 @@ from unittest.mock import MagicMock
|
||||
|
||||
import requests_mock
|
||||
|
||||
from homeassistant import core as ha
|
||||
from homeassistant.bootstrap import setup_component
|
||||
from homeassistant.components.binary_sensor import sleepiq
|
||||
|
||||
from tests.components.test_sleepiq import mock_responses
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
class TestSleepIQBinarySensorSetup(unittest.TestCase):
|
||||
@@ -22,7 +23,7 @@ class TestSleepIQBinarySensorSetup(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Initialize values for this testcase class."""
|
||||
self.hass = ha.HomeAssistant()
|
||||
self.hass = get_test_home_assistant()
|
||||
self.username = 'foo'
|
||||
self.password = 'bar'
|
||||
self.config = {
|
||||
@@ -35,6 +36,9 @@ class TestSleepIQBinarySensorSetup(unittest.TestCase):
|
||||
"""Test for successfully setting up the SleepIQ platform."""
|
||||
mock_responses(mock)
|
||||
|
||||
setup_component(self.hass, 'sleepiq', {
|
||||
'sleepiq': self.config})
|
||||
|
||||
sleepiq.setup_platform(self.hass,
|
||||
self.config,
|
||||
self.add_devices,
|
||||
|
||||
@@ -9,12 +9,15 @@ from homeassistant.components.binary_sensor import template
|
||||
from homeassistant.exceptions import TemplateError
|
||||
from homeassistant.helpers import template as template_hlpr
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, assert_setup_component
|
||||
|
||||
|
||||
class TestBinarySensorTemplate(unittest.TestCase):
|
||||
"""Test for Binary sensor template platform."""
|
||||
|
||||
hass = None
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
@@ -47,53 +50,53 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
||||
|
||||
def test_setup_no_sensors(self):
|
||||
""""Test setup with no sensors."""
|
||||
result = bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template'
|
||||
}
|
||||
})
|
||||
self.assertFalse(result)
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template'
|
||||
}
|
||||
})
|
||||
|
||||
def test_setup_invalid_device(self):
|
||||
""""Test the setup with invalid devices."""
|
||||
result = bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'foo bar': {},
|
||||
},
|
||||
}
|
||||
})
|
||||
self.assertFalse(result)
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'foo bar': {},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
def test_setup_invalid_sensor_class(self):
|
||||
""""Test setup with invalid sensor class."""
|
||||
result = bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test': {
|
||||
'value_template': '{{ foo }}',
|
||||
'sensor_class': 'foobarnotreal',
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test': {
|
||||
'value_template': '{{ foo }}',
|
||||
'sensor_class': 'foobarnotreal',
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
self.assertFalse(result)
|
||||
}
|
||||
})
|
||||
|
||||
def test_setup_invalid_missing_template(self):
|
||||
""""Test setup with invalid and missing template."""
|
||||
result = bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test': {
|
||||
'sensor_class': 'motion',
|
||||
},
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test': {
|
||||
'sensor_class': 'motion',
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
self.assertFalse(result)
|
||||
})
|
||||
|
||||
def test_attributes(self):
|
||||
""""Test the attributes."""
|
||||
@@ -107,7 +110,9 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
||||
vs.update()
|
||||
self.assertFalse(vs.is_on)
|
||||
|
||||
# pylint: disable=protected-access
|
||||
vs._template = template_hlpr.Template("{{ 2 > 1 }}", self.hass)
|
||||
|
||||
vs.update()
|
||||
self.assertTrue(vs.is_on)
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
"""The test for the Trend sensor platform."""
|
||||
import homeassistant.bootstrap as bootstrap
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, assert_setup_component
|
||||
|
||||
|
||||
class TestTrendBinarySensor:
|
||||
"""Test the Trend sensor."""
|
||||
|
||||
hass = None
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
@@ -189,41 +191,46 @@ class TestTrendBinarySensor:
|
||||
state = self.hass.states.get('binary_sensor.test_trend_sensor')
|
||||
assert state.state == 'off'
|
||||
|
||||
def test_invalid_name_does_not_create(self):
|
||||
def test_invalid_name_does_not_create(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test invalid name."""
|
||||
assert not bootstrap.setup_component(self.hass, 'binary_sensor', {
|
||||
'binary_sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test INVALID sensor': {
|
||||
'entity_id':
|
||||
"sensor.test_state"
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'binary_sensor', {
|
||||
'binary_sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test INVALID sensor': {
|
||||
'entity_id':
|
||||
"sensor.test_state"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_invalid_sensor_does_not_create(self):
|
||||
def test_invalid_sensor_does_not_create(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test invalid sensor."""
|
||||
assert not bootstrap.setup_component(self.hass, 'binary_sensor', {
|
||||
'binary_sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_trend_sensor': {
|
||||
'not_entity_id':
|
||||
"sensor.test_state"
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'binary_sensor', {
|
||||
'binary_sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_trend_sensor': {
|
||||
'not_entity_id':
|
||||
"sensor.test_state"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_no_sensors_does_not_create(self):
|
||||
"""Test no sensors."""
|
||||
assert not bootstrap.setup_component(self.hass, 'binary_sensor', {
|
||||
'binary_sensor': {
|
||||
'platform': 'trend'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'binary_sensor', {
|
||||
'binary_sensor': {
|
||||
'platform': 'trend'
|
||||
}
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
@@ -8,7 +8,7 @@ from werkzeug.test import EnvironBuilder
|
||||
from homeassistant.bootstrap import setup_component
|
||||
from homeassistant.components.http import request_class
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, assert_setup_component
|
||||
|
||||
|
||||
class TestLocalCamera(unittest.TestCase):
|
||||
@@ -28,21 +28,21 @@ class TestLocalCamera(unittest.TestCase):
|
||||
"""Test that it loads image from disk."""
|
||||
self.hass.wsgi = mock.MagicMock()
|
||||
|
||||
with NamedTemporaryFile() as fp:
|
||||
fp.write('hello'.encode('utf-8'))
|
||||
fp.flush()
|
||||
with NamedTemporaryFile() as fptr:
|
||||
fptr.write('hello'.encode('utf-8'))
|
||||
fptr.flush()
|
||||
|
||||
assert setup_component(self.hass, 'camera', {
|
||||
'camera': {
|
||||
'name': 'config_test',
|
||||
'platform': 'local_file',
|
||||
'file_path': fp.name,
|
||||
'file_path': fptr.name,
|
||||
}})
|
||||
|
||||
image_view = self.hass.wsgi.mock_calls[0][1][0]
|
||||
|
||||
builder = EnvironBuilder(method='GET')
|
||||
Request = request_class()
|
||||
Request = request_class() # pylint: disable=invalid-name
|
||||
request = Request(builder.get_environ())
|
||||
request.authenticated = True
|
||||
resp = image_view.get(request, 'camera.config_test')
|
||||
@@ -54,16 +54,17 @@ class TestLocalCamera(unittest.TestCase):
|
||||
"""Test local file will not setup when file is not readable."""
|
||||
self.hass.wsgi = mock.MagicMock()
|
||||
|
||||
with NamedTemporaryFile() as fp:
|
||||
fp.write('hello'.encode('utf-8'))
|
||||
fp.flush()
|
||||
with NamedTemporaryFile() as fptr:
|
||||
fptr.write('hello'.encode('utf-8'))
|
||||
fptr.flush()
|
||||
|
||||
with mock.patch('os.access', return_value=False):
|
||||
assert not setup_component(self.hass, 'camera', {
|
||||
with mock.patch('os.access', return_value=False), \
|
||||
assert_setup_component(0):
|
||||
assert setup_component(self.hass, 'camera', {
|
||||
'camera': {
|
||||
'name': 'config_test',
|
||||
'platform': 'local_file',
|
||||
'file_path': fp.name,
|
||||
'file_path': fptr.name,
|
||||
}})
|
||||
|
||||
assert [] == self.hass.states.all()
|
||||
@@ -16,7 +16,7 @@ from homeassistant.const import (
|
||||
from homeassistant.util.unit_system import METRIC_SYSTEM
|
||||
from homeassistant.components import climate
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import assert_setup_component, get_test_home_assistant
|
||||
|
||||
|
||||
ENTITY = 'climate.test'
|
||||
@@ -44,8 +44,9 @@ class TestSetupClimateGenericThermostat(unittest.TestCase):
|
||||
'name': 'test',
|
||||
'target_sensor': ENT_SENSOR
|
||||
}
|
||||
self.assertFalse(setup_component(self.hass, 'climate', {
|
||||
'climate': config}))
|
||||
with assert_setup_component(0):
|
||||
setup_component(self.hass, 'climate', {
|
||||
'climate': config})
|
||||
|
||||
def test_valid_conf(self):
|
||||
"""Test set up genreic_thermostat with valid config values."""
|
||||
|
||||
@@ -5,14 +5,15 @@ from unittest import mock
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
from homeassistant.bootstrap import setup_component
|
||||
from homeassistant.components import device_tracker
|
||||
from homeassistant.components.device_tracker.asuswrt import (
|
||||
CONF_PROTOCOL, CONF_MODE, CONF_PUB_KEY, PLATFORM_SCHEMA, DOMAIN)
|
||||
from homeassistant.const import (CONF_PLATFORM, CONF_PASSWORD, CONF_USERNAME,
|
||||
CONF_HOST)
|
||||
|
||||
from tests.common import get_test_home_assistant, get_test_config_dir
|
||||
from tests.common import (
|
||||
get_test_home_assistant, get_test_config_dir, assert_setup_component)
|
||||
|
||||
FAKEFILE = None
|
||||
|
||||
@@ -32,6 +33,7 @@ def teardown_module():
|
||||
|
||||
class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
|
||||
"""Tests for the ASUSWRT device tracker platform."""
|
||||
|
||||
hass = None
|
||||
|
||||
def setup_method(self, _):
|
||||
@@ -49,12 +51,13 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
|
||||
def test_password_or_pub_key_required(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test creating an AsusWRT scanner without a pass or pubkey."""
|
||||
self.assertFalse(_setup_component(
|
||||
self.hass, DOMAIN, {DOMAIN: {
|
||||
CONF_PLATFORM: 'asuswrt',
|
||||
CONF_HOST: 'fake_host',
|
||||
CONF_USERNAME: 'fake_user'
|
||||
}}))
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(
|
||||
self.hass, DOMAIN, {DOMAIN: {
|
||||
CONF_PLATFORM: 'asuswrt',
|
||||
CONF_HOST: 'fake_host',
|
||||
CONF_USERNAME: 'fake_user'
|
||||
}})
|
||||
|
||||
@mock.patch(
|
||||
'homeassistant.components.device_tracker.asuswrt.AsusWrtDeviceScanner',
|
||||
@@ -70,7 +73,10 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
|
||||
CONF_PASSWORD: 'fake_pass'
|
||||
}
|
||||
}
|
||||
self.assertIsNotNone(_setup_component(self.hass, DOMAIN, conf_dict))
|
||||
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, DOMAIN, conf_dict)
|
||||
|
||||
conf_dict[DOMAIN][CONF_MODE] = 'router'
|
||||
conf_dict[DOMAIN][CONF_PROTOCOL] = 'ssh'
|
||||
asuswrt_mock.assert_called_once_with(conf_dict[DOMAIN])
|
||||
@@ -90,7 +96,8 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
|
||||
}
|
||||
}
|
||||
|
||||
self.assertIsNotNone(_setup_component(self.hass, DOMAIN, conf_dict))
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, DOMAIN, conf_dict)
|
||||
|
||||
conf_dict[DOMAIN][CONF_MODE] = 'router'
|
||||
conf_dict[DOMAIN][CONF_PROTOCOL] = 'ssh'
|
||||
@@ -163,6 +170,7 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
|
||||
update_mock.start()
|
||||
self.addCleanup(update_mock.stop)
|
||||
|
||||
self.assertFalse(_setup_component(self.hass, DOMAIN,
|
||||
{DOMAIN: conf_dict}))
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, DOMAIN,
|
||||
{DOMAIN: conf_dict})
|
||||
ssh.login.assert_not_called()
|
||||
@@ -17,7 +17,7 @@ from homeassistant.exceptions import HomeAssistantError
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, fire_time_changed, fire_service_discovered,
|
||||
patch_yaml_files)
|
||||
patch_yaml_files, assert_setup_component)
|
||||
|
||||
TEST_PLATFORM = {device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}
|
||||
|
||||
@@ -336,6 +336,7 @@ class TestComponentsDeviceTracker(unittest.TestCase):
|
||||
@patch('homeassistant.components.device_tracker.log_exception')
|
||||
def test_config_failure(self, mock_ex):
|
||||
"""Test that the device tracker see failures."""
|
||||
assert not setup_component(self.hass, device_tracker.DOMAIN,
|
||||
{device_tracker.DOMAIN: {
|
||||
device_tracker.CONF_CONSIDER_HOME: -1}})
|
||||
with assert_setup_component(0, device_tracker.DOMAIN):
|
||||
setup_component(self.hass, device_tracker.DOMAIN,
|
||||
{device_tracker.DOMAIN: {
|
||||
device_tracker.CONF_CONSIDER_HOME: -1}})
|
||||
@@ -1,18 +1,21 @@
|
||||
"""The tests for the MQTT Garge door platform."""
|
||||
import unittest
|
||||
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
from homeassistant.bootstrap import setup_component
|
||||
from homeassistant.const import STATE_OPEN, STATE_CLOSED, ATTR_ASSUMED_STATE
|
||||
|
||||
import homeassistant.components.garage_door as garage_door
|
||||
from tests.common import (
|
||||
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant)
|
||||
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant,
|
||||
assert_setup_component)
|
||||
|
||||
|
||||
class TestGarageDoorMQTT(unittest.TestCase):
|
||||
"""Test the MQTT Garage door."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
def setUp(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.mock_publish = mock_mqtt_component(self.hass)
|
||||
@@ -24,29 +27,31 @@ class TestGarageDoorMQTT(unittest.TestCase):
|
||||
def test_fail_setup_if_no_command_topic(self):
|
||||
"""Test if command fails with command topic."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert not _setup_component(self.hass, garage_door.DOMAIN, {
|
||||
garage_door.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': '/home/garage_door/door'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, garage_door.DOMAIN, {
|
||||
garage_door.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': '/home/garage_door/door'
|
||||
}
|
||||
})
|
||||
self.assertIsNone(self.hass.states.get('garage_door.test'))
|
||||
|
||||
def test_controlling_state_via_topic(self):
|
||||
"""Test the controlling state via topic."""
|
||||
assert _setup_component(self.hass, garage_door.DOMAIN, {
|
||||
garage_door.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'state-topic',
|
||||
'command_topic': 'command-topic',
|
||||
'state_open': 1,
|
||||
'state_closed': 0,
|
||||
'service_open': 1,
|
||||
'service_close': 0
|
||||
}
|
||||
})
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, garage_door.DOMAIN, {
|
||||
garage_door.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'state-topic',
|
||||
'command_topic': 'command-topic',
|
||||
'state_open': 1,
|
||||
'state_closed': 0,
|
||||
'service_open': 1,
|
||||
'service_close': 0
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('garage_door.test')
|
||||
self.assertEqual(STATE_CLOSED, state.state)
|
||||
@@ -66,18 +71,19 @@ class TestGarageDoorMQTT(unittest.TestCase):
|
||||
|
||||
def test_sending_mqtt_commands_and_optimistic(self):
|
||||
"""Test the sending MQTT commands in optimistic mode."""
|
||||
assert _setup_component(self.hass, garage_door.DOMAIN, {
|
||||
garage_door.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'command_topic': 'command-topic',
|
||||
'state_open': 'beer state open',
|
||||
'state_closed': 'beer state closed',
|
||||
'service_open': 'beer open',
|
||||
'service_close': 'beer close',
|
||||
'qos': '2'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, garage_door.DOMAIN, {
|
||||
garage_door.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'command_topic': 'command-topic',
|
||||
'state_open': 'beer state open',
|
||||
'state_closed': 'beer state closed',
|
||||
'service_open': 'beer open',
|
||||
'service_close': 'beer close',
|
||||
'qos': '2'
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('garage_door.test')
|
||||
self.assertEqual(STATE_CLOSED, state.state)
|
||||
@@ -101,19 +107,20 @@ class TestGarageDoorMQTT(unittest.TestCase):
|
||||
|
||||
def test_controlling_state_via_topic_and_json_message(self):
|
||||
"""Test the controlling state via topic and JSON message."""
|
||||
assert _setup_component(self.hass, garage_door.DOMAIN, {
|
||||
garage_door.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'state-topic',
|
||||
'command_topic': 'command-topic',
|
||||
'state_open': 'beer open',
|
||||
'state_closed': 'beer closed',
|
||||
'service_open': 'beer service open',
|
||||
'service_close': 'beer service close',
|
||||
'value_template': '{{ value_json.val }}'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, garage_door.DOMAIN, {
|
||||
garage_door.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'state-topic',
|
||||
'command_topic': 'command-topic',
|
||||
'state_open': 'beer open',
|
||||
'state_closed': 'beer closed',
|
||||
'service_open': 'beer service open',
|
||||
'service_close': 'beer service close',
|
||||
'value_template': '{{ value_json.val }}'
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('garage_door.test')
|
||||
self.assertEqual(STATE_CLOSED, state.state)
|
||||
|
||||
@@ -75,17 +75,20 @@ light:
|
||||
"""
|
||||
import unittest
|
||||
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
from homeassistant.bootstrap import setup_component
|
||||
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ASSUMED_STATE
|
||||
import homeassistant.components.light as light
|
||||
from tests.common import (
|
||||
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message)
|
||||
assert_setup_component, get_test_home_assistant, mock_mqtt_component,
|
||||
fire_mqtt_message)
|
||||
|
||||
|
||||
class TestLightMQTT(unittest.TestCase):
|
||||
"""Test the MQTT light."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
def setUp(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.mock_publish = mock_mqtt_component(self.hass)
|
||||
@@ -97,25 +100,28 @@ class TestLightMQTT(unittest.TestCase):
|
||||
def test_fail_setup_if_no_command_topic(self):
|
||||
"""Test if command fails with command topic."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert not _setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
}
|
||||
})
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
}
|
||||
})
|
||||
self.assertIsNone(self.hass.states.get('light.test'))
|
||||
|
||||
def test_no_color_or_brightness_or_color_temp_if_no_topics(self):
|
||||
def test_no_color_or_brightness_or_color_temp_if_no_topics(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test if there is no color and brightness if no topic."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb/status',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
}
|
||||
})
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb/status',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
@@ -132,26 +138,28 @@ class TestLightMQTT(unittest.TestCase):
|
||||
self.assertIsNone(state.attributes.get('brightness'))
|
||||
self.assertIsNone(state.attributes.get('color_temp'))
|
||||
|
||||
def test_controlling_state_via_topic(self):
|
||||
def test_controlling_state_via_topic(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test the controlling of the state via topic."""
|
||||
config = {light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb/status',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'brightness_state_topic': 'test_light_rgb/brightness/status',
|
||||
'brightness_command_topic': 'test_light_rgb/brightness/set',
|
||||
'rgb_state_topic': 'test_light_rgb/rgb/status',
|
||||
'rgb_command_topic': 'test_light_rgb/rgb/set',
|
||||
'color_temp_state_topic': 'test_light_rgb/color_temp/status',
|
||||
'color_temp_command_topic': 'test_light_rgb/color_temp/set',
|
||||
'qos': '0',
|
||||
'payload_on': 1,
|
||||
'payload_off': 0
|
||||
}}
|
||||
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb/status',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'brightness_state_topic': 'test_light_rgb/brightness/status',
|
||||
'brightness_command_topic': 'test_light_rgb/brightness/set',
|
||||
'rgb_state_topic': 'test_light_rgb/rgb/status',
|
||||
'rgb_command_topic': 'test_light_rgb/rgb/set',
|
||||
'color_temp_state_topic': 'test_light_rgb/color_temp/status',
|
||||
'color_temp_command_topic': 'test_light_rgb/color_temp/set',
|
||||
'qos': '0',
|
||||
'payload_on': 1,
|
||||
'payload_off': 0
|
||||
}
|
||||
})
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, light.DOMAIN, config)
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
@@ -206,20 +214,21 @@ class TestLightMQTT(unittest.TestCase):
|
||||
def test_controlling_scale(self):
|
||||
"""Test the controlling scale."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_scale/status',
|
||||
'command_topic': 'test_scale/set',
|
||||
'brightness_state_topic': 'test_scale/brightness/status',
|
||||
'brightness_command_topic': 'test_scale/brightness/set',
|
||||
'brightness_scale': '99',
|
||||
'qos': 0,
|
||||
'payload_on': 'on',
|
||||
'payload_off': 'off'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_scale/status',
|
||||
'command_topic': 'test_scale/set',
|
||||
'brightness_state_topic': 'test_scale/brightness/status',
|
||||
'brightness_command_topic': 'test_scale/brightness/set',
|
||||
'brightness_scale': '99',
|
||||
'qos': 0,
|
||||
'payload_on': 'on',
|
||||
'payload_off': 'off'
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
@@ -250,24 +259,26 @@ class TestLightMQTT(unittest.TestCase):
|
||||
self.assertEqual(255,
|
||||
light_state.attributes['brightness'])
|
||||
|
||||
def test_controlling_state_via_topic_with_templates(self):
|
||||
def test_controlling_state_via_topic_with_templates(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test the setting og the state with a template."""
|
||||
config = {light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb/status',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'brightness_state_topic': 'test_light_rgb/brightness/status',
|
||||
'color_temp_state_topic': 'test_light_rgb/color_temp/status',
|
||||
'rgb_state_topic': 'test_light_rgb/rgb/status',
|
||||
'state_value_template': '{{ value_json.hello }}',
|
||||
'brightness_value_template': '{{ value_json.hello }}',
|
||||
'color_temp_value_template': '{{ value_json.hello }}',
|
||||
'rgb_value_template': '{{ value_json.hello | join(",") }}',
|
||||
}}
|
||||
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb/status',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'brightness_state_topic': 'test_light_rgb/brightness/status',
|
||||
'color_temp_state_topic': 'test_light_rgb/color_temp/status',
|
||||
'rgb_state_topic': 'test_light_rgb/rgb/status',
|
||||
'state_value_template': '{{ value_json.hello }}',
|
||||
'brightness_value_template': '{{ value_json.hello }}',
|
||||
'color_temp_value_template': '{{ value_json.hello }}',
|
||||
'rgb_value_template': '{{ value_json.hello | join(",") }}',
|
||||
}
|
||||
})
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, light.DOMAIN, config)
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
@@ -290,22 +301,24 @@ class TestLightMQTT(unittest.TestCase):
|
||||
self.assertEqual([1, 2, 3], state.attributes.get('rgb_color'))
|
||||
self.assertEqual(300, state.attributes.get('color_temp'))
|
||||
|
||||
def test_sending_mqtt_commands_and_optimistic(self):
|
||||
def test_sending_mqtt_commands_and_optimistic(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test the sending of command in optimistic mode."""
|
||||
config = {light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'brightness_command_topic': 'test_light_rgb/brightness/set',
|
||||
'rgb_command_topic': 'test_light_rgb/rgb/set',
|
||||
'color_temp_command_topic': 'test_light_rgb/color_temp/set',
|
||||
'qos': 2,
|
||||
'payload_on': 'on',
|
||||
'payload_off': 'off'
|
||||
}}
|
||||
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'brightness_command_topic': 'test_light_rgb/brightness/set',
|
||||
'rgb_command_topic': 'test_light_rgb/rgb/set',
|
||||
'color_temp_command_topic': 'test_light_rgb/color_temp/set',
|
||||
'qos': 2,
|
||||
'payload_on': 'on',
|
||||
'payload_off': 'off'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, light.DOMAIN, config)
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
@@ -352,16 +365,17 @@ class TestLightMQTT(unittest.TestCase):
|
||||
|
||||
def test_show_brightness_if_only_command_topic(self):
|
||||
"""Test the brightness if only a command topic is present."""
|
||||
config = {light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'brightness_command_topic': 'test_light_rgb/brightness/set',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'state_topic': 'test_light_rgb/status',
|
||||
}}
|
||||
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'brightness_command_topic': 'test_light_rgb/brightness/set',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'state_topic': 'test_light_rgb/status',
|
||||
}
|
||||
})
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, light.DOMAIN, config)
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
@@ -376,16 +390,17 @@ class TestLightMQTT(unittest.TestCase):
|
||||
|
||||
def test_show_color_temp_only_if_command_topic(self):
|
||||
"""Test the color temp only if a command topic is present."""
|
||||
config = {light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'color_temp_command_topic': 'test_light_rgb/brightness/set',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'state_topic': 'test_light_rgb/status'
|
||||
}}
|
||||
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'color_temp_command_topic': 'test_light_rgb/brightness/set',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'state_topic': 'test_light_rgb/status'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, light.DOMAIN, config)
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
|
||||
@@ -30,11 +30,12 @@ light:
|
||||
import json
|
||||
import unittest
|
||||
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
from homeassistant.bootstrap import _setup_component, setup_component
|
||||
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ASSUMED_STATE
|
||||
import homeassistant.components.light as light
|
||||
from tests.common import (
|
||||
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message)
|
||||
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message,
|
||||
assert_setup_component)
|
||||
|
||||
|
||||
class TestLightMQTTJSON(unittest.TestCase):
|
||||
@@ -49,18 +50,21 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_fail_setup_if_no_command_topic(self):
|
||||
def test_fail_setup_if_no_command_topic(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test if setup fails with no command topic."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert not _setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'name': 'test',
|
||||
}
|
||||
})
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'name': 'test',
|
||||
}
|
||||
})
|
||||
self.assertIsNone(self.hass.states.get('light.test'))
|
||||
|
||||
def test_no_color_or_brightness_if_no_config(self):
|
||||
def test_no_color_or_brightness_if_no_config(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test if there is no color and brightness if they aren't defined."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
@@ -85,7 +89,8 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
self.assertIsNone(state.attributes.get('rgb_color'))
|
||||
self.assertIsNone(state.attributes.get('brightness'))
|
||||
|
||||
def test_controlling_state_via_topic(self):
|
||||
def test_controlling_state_via_topic(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test the controlling of the state via topic."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
@@ -108,10 +113,9 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
|
||||
# Turn on the light, full white
|
||||
fire_mqtt_message(self.hass, 'test_light_rgb',
|
||||
'{"state":"ON",' +
|
||||
'"color":{"r":255,"g":255,"b":255},' +
|
||||
'"brightness":255}'
|
||||
)
|
||||
'{"state":"ON",'
|
||||
'"color":{"r":255,"g":255,"b":255},'
|
||||
'"brightness":255}')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
@@ -127,9 +131,8 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
|
||||
fire_mqtt_message(self.hass, 'test_light_rgb',
|
||||
'{"state":"ON",' +
|
||||
'"brightness":100}'
|
||||
)
|
||||
'{"state":"ON",'
|
||||
'"brightness":100}')
|
||||
self.hass.block_till_done()
|
||||
|
||||
light_state = self.hass.states.get('light.test')
|
||||
@@ -138,16 +141,16 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
light_state.attributes['brightness'])
|
||||
|
||||
fire_mqtt_message(self.hass, 'test_light_rgb',
|
||||
'{"state":"ON",' +
|
||||
'"color":{"r":125,"g":125,"b":125}}'
|
||||
)
|
||||
'{"state":"ON",'
|
||||
'"color":{"r":125,"g":125,"b":125}}')
|
||||
self.hass.block_till_done()
|
||||
|
||||
light_state = self.hass.states.get('light.test')
|
||||
self.assertEqual([125, 125, 125],
|
||||
light_state.attributes.get('rgb_color'))
|
||||
|
||||
def test_sending_mqtt_commands_and_optimistic(self):
|
||||
def test_sending_mqtt_commands_and_optimistic(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test the sending of command in optimistic mode."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
@@ -202,7 +205,8 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
|
||||
self.assertEqual(50, state.attributes['brightness'])
|
||||
|
||||
def test_flash_short_and_long(self):
|
||||
def test_flash_short_and_long(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test for flash length being sent when included."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
@@ -285,7 +289,8 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
self.assertEqual(10, message_json["transition"])
|
||||
self.assertEqual("OFF", message_json["state"])
|
||||
|
||||
def test_invalid_color_and_brightness_values(self):
|
||||
def test_invalid_color_and_brightness_values(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test that invalid color/brightness values are ignored."""
|
||||
self.hass.config.components = ['mqtt']
|
||||
assert _setup_component(self.hass, light.DOMAIN, {
|
||||
@@ -308,10 +313,9 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
|
||||
# Turn on the light
|
||||
fire_mqtt_message(self.hass, 'test_light_rgb',
|
||||
'{"state":"ON",' +
|
||||
'"color":{"r":255,"g":255,"b":255},' +
|
||||
'"brightness": 255}'
|
||||
)
|
||||
'{"state":"ON",'
|
||||
'"color":{"r":255,"g":255,"b":255},'
|
||||
'"brightness": 255}')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
@@ -321,9 +325,8 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
|
||||
# Bad color values
|
||||
fire_mqtt_message(self.hass, 'test_light_rgb',
|
||||
'{"state":"ON",' +
|
||||
'"color":{"r":"bad","g":"val","b":"test"}}'
|
||||
)
|
||||
'{"state":"ON",'
|
||||
'"color":{"r":"bad","g":"val","b":"test"}}')
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Color should not have changed
|
||||
@@ -333,9 +336,8 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
|
||||
# Bad brightness values
|
||||
fire_mqtt_message(self.hass, 'test_light_rgb',
|
||||
'{"state":"ON",' +
|
||||
'"brightness": "badValue"}'
|
||||
)
|
||||
'{"state":"ON",'
|
||||
'"brightness": "badValue"}')
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Brightness should not have changed
|
||||
|
||||
@@ -5,7 +5,7 @@ from homeassistant.bootstrap import setup_component
|
||||
import homeassistant.components.notify as notify
|
||||
from homeassistant.components.notify import group
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import assert_setup_component, get_test_home_assistant
|
||||
|
||||
|
||||
class TestNotifyGroup(unittest.TestCase):
|
||||
@@ -15,15 +15,16 @@ class TestNotifyGroup(unittest.TestCase):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.events = []
|
||||
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
|
||||
'notify': [{
|
||||
'name': 'demo1',
|
||||
'platform': 'demo'
|
||||
}, {
|
||||
'name': 'demo2',
|
||||
'platform': 'demo'
|
||||
}]
|
||||
}))
|
||||
with assert_setup_component(2):
|
||||
setup_component(self.hass, notify.DOMAIN, {
|
||||
'notify': [{
|
||||
'name': 'demo1',
|
||||
'platform': 'demo'
|
||||
}, {
|
||||
'name': 'demo2',
|
||||
'platform': 'demo'
|
||||
}]
|
||||
})
|
||||
|
||||
self.service = group.get_service(self.hass, {'services': [
|
||||
{'service': 'demo1'},
|
||||
|
||||
@@ -57,12 +57,12 @@ class TestDarkSkySetup(unittest.TestCase):
|
||||
|
||||
@requests_mock.Mocker()
|
||||
@patch('forecastio.api.get_forecast', wraps=forecastio.api.get_forecast)
|
||||
def test_setup(self, m, mock_get_forecast):
|
||||
def test_setup(self, mock_req, mock_get_forecast):
|
||||
"""Test for successfully setting up the forecast.io platform."""
|
||||
uri = ('https://api.darksky.net\/forecast\/(\w+)\/'
|
||||
'(-?\d+\.?\d*),(-?\d+\.?\d*)')
|
||||
m.get(re.compile(uri),
|
||||
text=load_fixture('darksky.json'))
|
||||
uri = (r'https://api.(darksky.net|forecast.io)\/forecast\/(\w+)\/'
|
||||
r'(-?\d+\.?\d*),(-?\d+\.?\d*)')
|
||||
mock_req.get(re.compile(uri),
|
||||
text=load_fixture('darksky.json'))
|
||||
darksky.setup_platform(self.hass, self.config, MagicMock())
|
||||
self.assertTrue(mock_get_forecast.called)
|
||||
self.assertEqual(mock_get_forecast.call_count, 1)
|
||||
@@ -4,10 +4,10 @@ from unittest.mock import MagicMock
|
||||
|
||||
import requests_mock
|
||||
|
||||
from homeassistant import core as ha
|
||||
from homeassistant.components.sensor import sleepiq
|
||||
|
||||
from tests.components.test_sleepiq import mock_responses
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
class TestSleepIQSensorSetup(unittest.TestCase):
|
||||
@@ -22,7 +22,7 @@ class TestSleepIQSensorSetup(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Initialize values for this testcase class."""
|
||||
self.hass = ha.HomeAssistant()
|
||||
self.hass = get_test_home_assistant()
|
||||
self.username = 'foo'
|
||||
self.password = 'bar'
|
||||
self.config = {
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
"""The test for the Template sensor platform."""
|
||||
import homeassistant.bootstrap as bootstrap
|
||||
from homeassistant.bootstrap import setup_component
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, assert_setup_component
|
||||
|
||||
|
||||
class TestTemplateSensor:
|
||||
"""Test the Template sensor."""
|
||||
|
||||
hass = None
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
@@ -17,17 +20,18 @@ class TestTemplateSensor:
|
||||
|
||||
def test_template(self):
|
||||
"""Test template."""
|
||||
assert bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template':
|
||||
"It {{ states.sensor.test_state.state }}."
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template':
|
||||
"It {{ states.sensor.test_state.state }}."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
state = self.hass.states.get('sensor.test_template_sensor')
|
||||
assert state.state == 'It .'
|
||||
@@ -39,83 +43,89 @@ class TestTemplateSensor:
|
||||
|
||||
def test_template_syntax_error(self):
|
||||
"""Test templating syntax error."""
|
||||
assert not bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template':
|
||||
"{% if rubbish %}"
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template':
|
||||
"{% if rubbish %}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_template_attribute_missing(self):
|
||||
"""Test missing attribute template."""
|
||||
assert bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template':
|
||||
"It {{ states.sensor.test_state.attributes.missing }}."
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template': 'It {{ states.sensor.test_state'
|
||||
'.attributes.missing }}.'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
state = self.hass.states.get('sensor.test_template_sensor')
|
||||
assert state.state == 'unknown'
|
||||
|
||||
def test_invalid_name_does_not_create(self):
|
||||
"""Test invalid name."""
|
||||
assert not bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test INVALID sensor': {
|
||||
'value_template':
|
||||
"{{ states.sensor.test_state.state }}"
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test INVALID sensor': {
|
||||
'value_template':
|
||||
"{{ states.sensor.test_state.state }}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_invalid_sensor_does_not_create(self):
|
||||
"""Test invalid sensor."""
|
||||
assert not bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': 'invalid'
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': 'invalid'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_no_sensors_does_not_create(self):
|
||||
"""Test no sensors."""
|
||||
assert not bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template'
|
||||
}
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_missing_template_does_not_create(self):
|
||||
"""Test missing template."""
|
||||
assert not bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'not_value_template':
|
||||
"{{ states.sensor.test_state.state }}"
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'not_value_template':
|
||||
"{{ states.sensor.test_state.state }}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
@@ -1,6 +1,6 @@
|
||||
"""The tests for the Flux switch platform."""
|
||||
import unittest
|
||||
from datetime import timedelta
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.bootstrap import setup_component
|
||||
@@ -8,8 +8,10 @@ from homeassistant.components import switch, light
|
||||
from homeassistant.const import CONF_PLATFORM, STATE_ON, SERVICE_TURN_ON
|
||||
import homeassistant.loader as loader
|
||||
import homeassistant.util.dt as dt_util
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import fire_time_changed, mock_service
|
||||
|
||||
from tests.common import (
|
||||
assert_setup_component, get_test_home_assistant, fire_time_changed,
|
||||
mock_service)
|
||||
|
||||
|
||||
class TestSwitchFlux(unittest.TestCase):
|
||||
@@ -50,21 +52,23 @@ class TestSwitchFlux(unittest.TestCase):
|
||||
|
||||
def test_valid_config_no_name(self):
|
||||
"""Test configuration."""
|
||||
assert setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'flux',
|
||||
'lights': ['light.desk', 'light.lamp']
|
||||
}
|
||||
})
|
||||
with assert_setup_component(1, 'switch'):
|
||||
assert setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'flux',
|
||||
'lights': ['light.desk', 'light.lamp']
|
||||
}
|
||||
})
|
||||
|
||||
def test_invalid_config_no_lights(self):
|
||||
"""Test configuration."""
|
||||
assert not setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'flux',
|
||||
'name': 'flux'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(0, 'switch'):
|
||||
assert setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'flux',
|
||||
'name': 'flux'
|
||||
}
|
||||
})
|
||||
|
||||
def test_flux_when_switch_is_off(self):
|
||||
"""Test the flux switch when it is off."""
|
||||
|
||||
@@ -6,12 +6,16 @@ from homeassistant.const import (
|
||||
STATE_ON,
|
||||
STATE_OFF)
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, assert_setup_component
|
||||
|
||||
|
||||
class TestTemplateSwitch:
|
||||
"""Test the Template switch."""
|
||||
|
||||
hass = None
|
||||
calls = None
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
@@ -29,25 +33,26 @@ class TestTemplateSwitch:
|
||||
|
||||
def test_template_state_text(self):
|
||||
""""Test the state text of a template."""
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
with assert_setup_component(1):
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
state = self.hass.states.set('switch.test_state', STATE_ON)
|
||||
self.hass.block_till_done()
|
||||
@@ -63,188 +68,197 @@ class TestTemplateSwitch:
|
||||
|
||||
def test_template_state_boolean_on(self):
|
||||
"""Test the setting of the state with boolean on."""
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ 1 == 1 }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
with assert_setup_component(1):
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ 1 == 1 }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.state == STATE_ON
|
||||
|
||||
def test_template_state_boolean_off(self):
|
||||
"""Test the setting of the state with off."""
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ 1 == 2 }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
with assert_setup_component(1):
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ 1 == 2 }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
def test_template_syntax_error(self):
|
||||
"""Test templating syntax error."""
|
||||
assert not bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{% if rubbish %}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{% if rubbish %}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_invalid_name_does_not_create(self):
|
||||
"""Test invalid name."""
|
||||
assert not bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test INVALID switch': {
|
||||
'value_template':
|
||||
"{{ rubbish }",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test INVALID switch': {
|
||||
'value_template':
|
||||
"{{ rubbish }",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_invalid_switch_does_not_create(self):
|
||||
"""Test invalid switch."""
|
||||
assert not bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': 'Invalid'
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': 'Invalid'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_no_switches_does_not_create(self):
|
||||
"""Test if there are no switches no creation."""
|
||||
assert not bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template'
|
||||
}
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_missing_template_does_not_create(self):
|
||||
"""Test missing template."""
|
||||
assert not bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'not_value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'not_value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_missing_on_does_not_create(self):
|
||||
"""Test missing on."""
|
||||
assert not bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'not_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'not_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_missing_off_does_not_create(self):
|
||||
"""Test missing off."""
|
||||
assert not bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'not_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
with assert_setup_component(0):
|
||||
assert bootstrap.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'not_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_on_action(self):
|
||||
|
||||
@@ -4,7 +4,7 @@ import unittest
|
||||
from unittest import mock
|
||||
|
||||
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
from homeassistant.bootstrap import setup_component
|
||||
from homeassistant.const import (
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
SERVICE_TURN_OFF,
|
||||
@@ -16,7 +16,7 @@ from homeassistant.const import (
|
||||
from homeassistant.util.unit_system import METRIC_SYSTEM
|
||||
from homeassistant.components import thermostat
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import assert_setup_component, get_test_home_assistant
|
||||
|
||||
|
||||
ENTITY = 'thermostat.test'
|
||||
@@ -44,12 +44,13 @@ class TestSetupThermostatHeatControl(unittest.TestCase):
|
||||
'name': 'test',
|
||||
'target_sensor': ENT_SENSOR
|
||||
}
|
||||
self.assertFalse(_setup_component(self.hass, 'thermostat', {
|
||||
'thermostat': config}))
|
||||
with assert_setup_component(0):
|
||||
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',
|
||||
self.assertTrue(setup_component(self.hass, 'thermostat',
|
||||
{'thermostat': {
|
||||
'platform': 'heat_control',
|
||||
'name': 'test',
|
||||
|
||||
Reference in new issue
Block a user