Continue on invalid platforms and new setup_component unit tests (#3736)

This commit is contained in:
Johann Kellerman authored and Paulus Schoutsen committed 2016-10-08 11:27:35 -07:00
1 parent 1b26b5ad14
commit 7b40a641ec
23 files changed
+842 -672

No files matched your search

+113 -98
View File
@@ -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)
+36 -34
View File
@@ -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