mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Cleanup use of MQTT in emulated_hue tests (#4068)
* Use unix newlines on test_emulated_hue This commit switches the test_emulated_hue module to use unix newlines instead of the DOS style that were there before. (using dos2unix on the file) This makes it consistent with the other files in the repo. * Cleanup emulated_hue tests Previously these tests relied on the mqtt light platform as test devices to control with the emulated hue. However, this was pretty heavyweight and required running an MQTT broker in the tests. Instead this commit switches it to use the demo light platform which is strictly in memory. Fixes #3549
This commit is contained in:
parent
4fb0b27310
commit
5d3956ea98
@ -9,7 +9,7 @@ import requests
|
||||
|
||||
from homeassistant import bootstrap, const, core
|
||||
import homeassistant.components as core_components
|
||||
from homeassistant.components import emulated_hue, http, light, mqtt
|
||||
from homeassistant.components import emulated_hue, http, light
|
||||
from homeassistant.const import STATE_ON, STATE_OFF
|
||||
from homeassistant.components.emulated_hue import (
|
||||
HUE_API_STATE_ON, HUE_API_STATE_BRI)
|
||||
@ -18,28 +18,10 @@ from tests.common import get_test_instance_port, get_test_home_assistant
|
||||
|
||||
HTTP_SERVER_PORT = get_test_instance_port()
|
||||
BRIDGE_SERVER_PORT = get_test_instance_port()
|
||||
MQTT_BROKER_PORT = get_test_instance_port()
|
||||
|
||||
BRIDGE_URL_BASE = "http://127.0.0.1:{}".format(BRIDGE_SERVER_PORT) + "{}"
|
||||
JSON_HEADERS = {const.HTTP_HEADER_CONTENT_TYPE: const.CONTENT_TYPE_JSON}
|
||||
|
||||
mqtt_broker = None
|
||||
|
||||
|
||||
def setUpModule():
|
||||
"""Setup things to be run when tests are started."""
|
||||
global mqtt_broker
|
||||
|
||||
mqtt_broker = MQTTBroker('127.0.0.1', MQTT_BROKER_PORT)
|
||||
mqtt_broker.start()
|
||||
|
||||
|
||||
def tearDownModule():
|
||||
"""Stop everything that was started."""
|
||||
global mqtt_broker
|
||||
|
||||
mqtt_broker.stop()
|
||||
|
||||
|
||||
def setup_hass_instance(emulated_hue_config):
|
||||
"""Setup the Home Assistant instance to test."""
|
||||
@ -140,41 +122,10 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
}
|
||||
})
|
||||
|
||||
bootstrap.setup_component(cls.hass, mqtt.DOMAIN, {
|
||||
'mqtt': {
|
||||
'broker': '127.0.0.1',
|
||||
'port': MQTT_BROKER_PORT
|
||||
}
|
||||
})
|
||||
|
||||
bootstrap.setup_component(cls.hass, light.DOMAIN, {
|
||||
'light': [
|
||||
{
|
||||
'platform': 'mqtt',
|
||||
'name': 'Office light',
|
||||
'state_topic': 'office/rgb1/light/status',
|
||||
'command_topic': 'office/rgb1/light/switch',
|
||||
'brightness_state_topic': 'office/rgb1/brightness/status',
|
||||
'brightness_command_topic': 'office/rgb1/brightness/set',
|
||||
'optimistic': True
|
||||
},
|
||||
{
|
||||
'platform': 'mqtt',
|
||||
'name': 'Bedroom light',
|
||||
'state_topic': 'bedroom/rgb1/light/status',
|
||||
'command_topic': 'bedroom/rgb1/light/switch',
|
||||
'brightness_state_topic': 'bedroom/rgb1/brightness/status',
|
||||
'brightness_command_topic': 'bedroom/rgb1/brightness/set',
|
||||
'optimistic': True
|
||||
},
|
||||
{
|
||||
'platform': 'mqtt',
|
||||
'name': 'Kitchen light',
|
||||
'state_topic': 'kitchen/rgb1/light/status',
|
||||
'command_topic': 'kitchen/rgb1/light/switch',
|
||||
'brightness_state_topic': 'kitchen/rgb1/brightness/status',
|
||||
'brightness_command_topic': 'kitchen/rgb1/brightness/set',
|
||||
'optimistic': True
|
||||
'platform': 'demo',
|
||||
}
|
||||
]
|
||||
})
|
||||
@ -182,7 +133,7 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
start_hass_instance(cls.hass)
|
||||
|
||||
# Kitchen light is explicitly excluded from being exposed
|
||||
kitchen_light_entity = cls.hass.states.get('light.kitchen_light')
|
||||
kitchen_light_entity = cls.hass.states.get('light.kitchen_lights')
|
||||
attrs = dict(kitchen_light_entity.attributes)
|
||||
attrs[emulated_hue.ATTR_EMULATED_HUE] = False
|
||||
cls.hass.states.set(
|
||||
@ -205,9 +156,9 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
result_json = result.json()
|
||||
|
||||
# Make sure the lights we added to the config are there
|
||||
self.assertTrue('light.office_light' in result_json)
|
||||
self.assertTrue('light.bedroom_light' in result_json)
|
||||
self.assertTrue('light.kitchen_light' not in result_json)
|
||||
self.assertTrue('light.ceiling_lights' in result_json)
|
||||
self.assertTrue('light.bed_light' in result_json)
|
||||
self.assertTrue('light.kitchen_lights' not in result_json)
|
||||
|
||||
def test_get_light_state(self):
|
||||
"""Test the getting of light state."""
|
||||
@ -215,12 +166,12 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
self.hass.services.call(
|
||||
light.DOMAIN, const.SERVICE_TURN_ON,
|
||||
{
|
||||
const.ATTR_ENTITY_ID: 'light.office_light',
|
||||
const.ATTR_ENTITY_ID: 'light.ceiling_lights',
|
||||
light.ATTR_BRIGHTNESS: 127
|
||||
},
|
||||
blocking=True)
|
||||
|
||||
office_json = self.perform_get_light_state('light.office_light', 200)
|
||||
office_json = self.perform_get_light_state('light.ceiling_lights', 200)
|
||||
|
||||
self.assertEqual(office_json['state'][HUE_API_STATE_ON], True)
|
||||
self.assertEqual(office_json['state'][HUE_API_STATE_BRI], 127)
|
||||
@ -229,17 +180,17 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
self.hass.services.call(
|
||||
light.DOMAIN, const.SERVICE_TURN_OFF,
|
||||
{
|
||||
const.ATTR_ENTITY_ID: 'light.bedroom_light'
|
||||
const.ATTR_ENTITY_ID: 'light.bed_light'
|
||||
},
|
||||
blocking=True)
|
||||
|
||||
bedroom_json = self.perform_get_light_state('light.bedroom_light', 200)
|
||||
bedroom_json = self.perform_get_light_state('light.bed_light', 200)
|
||||
|
||||
self.assertEqual(bedroom_json['state'][HUE_API_STATE_ON], False)
|
||||
self.assertEqual(bedroom_json['state'][HUE_API_STATE_BRI], 0)
|
||||
|
||||
# Make sure kitchen light isn't accessible
|
||||
kitchen_url = '/api/username/lights/{}'.format('light.kitchen_light')
|
||||
kitchen_url = '/api/username/lights/{}'.format('light.kitchen_lights')
|
||||
kitchen_result = requests.get(
|
||||
BRIDGE_URL_BASE.format(kitchen_url), timeout=5)
|
||||
|
||||
@ -247,22 +198,22 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
|
||||
def test_put_light_state(self):
|
||||
"""Test the seeting of light states."""
|
||||
self.perform_put_test_on_office_light()
|
||||
self.perform_put_test_on_ceiling_lights()
|
||||
|
||||
# Turn the bedroom light on first
|
||||
self.hass.services.call(
|
||||
light.DOMAIN, const.SERVICE_TURN_ON,
|
||||
{const.ATTR_ENTITY_ID: 'light.bedroom_light',
|
||||
{const.ATTR_ENTITY_ID: 'light.bed_light',
|
||||
light.ATTR_BRIGHTNESS: 153},
|
||||
blocking=True)
|
||||
|
||||
bedroom_light = self.hass.states.get('light.bedroom_light')
|
||||
self.assertEqual(bedroom_light.state, STATE_ON)
|
||||
self.assertEqual(bedroom_light.attributes[light.ATTR_BRIGHTNESS], 153)
|
||||
bed_light = self.hass.states.get('light.bed_light')
|
||||
self.assertEqual(bed_light.state, STATE_ON)
|
||||
self.assertEqual(bed_light.attributes[light.ATTR_BRIGHTNESS], 153)
|
||||
|
||||
# Go through the API to turn it off
|
||||
bedroom_result = self.perform_put_light_state(
|
||||
'light.bedroom_light', False)
|
||||
'light.bed_light', False)
|
||||
|
||||
bedroom_result_json = bedroom_result.json()
|
||||
|
||||
@ -273,8 +224,8 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
self.assertEqual(len(bedroom_result_json), 1)
|
||||
|
||||
# Check to make sure the state changed
|
||||
bedroom_light = self.hass.states.get('light.bedroom_light')
|
||||
self.assertEqual(bedroom_light.state, STATE_OFF)
|
||||
bed_light = self.hass.states.get('light.bed_light')
|
||||
self.assertEqual(bed_light.state, STATE_OFF)
|
||||
|
||||
# Make sure we can't change the kitchen light state
|
||||
kitchen_result = self.perform_put_light_state(
|
||||
@ -284,15 +235,15 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
def test_put_with_form_urlencoded_content_type(self):
|
||||
"""Test the form with urlencoded content."""
|
||||
# Needed for Alexa
|
||||
self.perform_put_test_on_office_light(
|
||||
self.perform_put_test_on_ceiling_lights(
|
||||
'application/x-www-form-urlencoded')
|
||||
|
||||
# Make sure we fail gracefully when we can't parse the data
|
||||
data = {'key1': 'value1', 'key2': 'value2'}
|
||||
result = requests.put(
|
||||
BRIDGE_URL_BASE.format(
|
||||
'/api/username/lights/{}/state'.format("light.office_light")),
|
||||
data=data)
|
||||
'/api/username/lights/{}/state'.format(
|
||||
"light.ceiling_lights")), data=data)
|
||||
|
||||
self.assertEqual(result.status_code, 400)
|
||||
|
||||
@ -316,13 +267,14 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
"""Test the allowed methods."""
|
||||
result = requests.get(
|
||||
BRIDGE_URL_BASE.format(
|
||||
'/api/username/lights/{}/state'.format("light.office_light")))
|
||||
'/api/username/lights/{}/state'.format(
|
||||
"light.ceiling_lights")))
|
||||
|
||||
self.assertEqual(result.status_code, 405)
|
||||
|
||||
result = requests.put(
|
||||
BRIDGE_URL_BASE.format(
|
||||
'/api/username/lights/{}'.format("light.office_light")),
|
||||
'/api/username/lights/{}'.format("light.ceiling_lights")),
|
||||
data={'key1': 'value1'})
|
||||
|
||||
self.assertEqual(result.status_code, 405)
|
||||
@ -338,7 +290,8 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
# Test proper on value parsing
|
||||
result = requests.put(
|
||||
BRIDGE_URL_BASE.format(
|
||||
'/api/username/lights/{}/state'.format("light.office_light")),
|
||||
'/api/username/lights/{}/state'.format(
|
||||
"light.ceiling_lights")),
|
||||
data=json.dumps({HUE_API_STATE_ON: 1234}))
|
||||
|
||||
self.assertEqual(result.status_code, 400)
|
||||
@ -346,29 +299,29 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
# Test proper brightness value parsing
|
||||
result = requests.put(
|
||||
BRIDGE_URL_BASE.format(
|
||||
'/api/username/lights/{}/state'.format("light.office_light")),
|
||||
data=json.dumps({
|
||||
'/api/username/lights/{}/state'.format(
|
||||
"light.ceiling_lights")), data=json.dumps({
|
||||
HUE_API_STATE_ON: True,
|
||||
HUE_API_STATE_BRI: 'Hello world!'
|
||||
}))
|
||||
|
||||
self.assertEqual(result.status_code, 400)
|
||||
|
||||
def perform_put_test_on_office_light(self,
|
||||
def perform_put_test_on_ceiling_lights(self,
|
||||
content_type='application/json'):
|
||||
"""Test the setting of a light."""
|
||||
# Turn the office light off first
|
||||
self.hass.services.call(
|
||||
light.DOMAIN, const.SERVICE_TURN_OFF,
|
||||
{const.ATTR_ENTITY_ID: 'light.office_light'},
|
||||
{const.ATTR_ENTITY_ID: 'light.ceiling_lights'},
|
||||
blocking=True)
|
||||
|
||||
office_light = self.hass.states.get('light.office_light')
|
||||
self.assertEqual(office_light.state, STATE_OFF)
|
||||
ceiling_lights = self.hass.states.get('light.ceiling_lights')
|
||||
self.assertEqual(ceiling_lights.state, STATE_OFF)
|
||||
|
||||
# Go through the API to turn it on
|
||||
office_result = self.perform_put_light_state(
|
||||
'light.office_light', True, 56, content_type)
|
||||
'light.ceiling_lights', True, 56, content_type)
|
||||
|
||||
office_result_json = office_result.json()
|
||||
|
||||
@ -379,9 +332,9 @@ class TestEmulatedHueExposedByDefault(unittest.TestCase):
|
||||
self.assertEqual(len(office_result_json), 2)
|
||||
|
||||
# Check to make sure the state changed
|
||||
office_light = self.hass.states.get('light.office_light')
|
||||
self.assertEqual(office_light.state, STATE_ON)
|
||||
self.assertEqual(office_light.attributes[light.ATTR_BRIGHTNESS], 56)
|
||||
ceiling_lights = self.hass.states.get('light.ceiling_lights')
|
||||
self.assertEqual(ceiling_lights.state, STATE_ON)
|
||||
self.assertEqual(ceiling_lights.attributes[light.ATTR_BRIGHTNESS], 56)
|
||||
|
||||
def perform_get_light_state(self, entity_id, expected_status):
|
||||
"""Test the gettting of a light state."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user