Use dict[key] for required config keys and keys with default values of MQTT light (#22834)

* Use dict[key] for required config keys and keys with default values.

* Improve tests

* Lint

* Improve tests of JSON data
This commit is contained in:
Erik Montnemery authored and Fabian Affolter committed 2019-04-10 11:16:41 +02:00
1 parent bbedf091aa
commit bc5f0ff0b3
4 files changed
+357 -85

No files matched your search

+280 -2
View File
@@ -88,6 +88,7 @@ light:
brightness_scale: 99
"""
import json
from unittest import mock
from unittest.mock import ANY, patch
from homeassistant.components import light, mqtt
@@ -101,6 +102,19 @@ from homeassistant.setup import async_setup_component
from tests.common import (
MockConfigEntry, async_fire_mqtt_message, async_mock_mqtt_component,
mock_coro, mock_registry)
from tests.components.light import common
class JsonValidator(object):
"""Helper to compare JSON."""
def __init__(self, jsondata):
"""Constructor."""
self.jsondata = jsondata
def __eq__(self, other):
"""Compare JSON data."""
return json.loads(self.jsondata) == json.loads(other)
async def test_fail_setup_if_no_command_topic(hass, mqtt_mock):
@@ -295,7 +309,9 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
'brightness': True,
'color_temp': True,
'effect': True,
'hs': True,
'rgb': True,
'xy': True,
'white_value': True,
'qos': 2
}
@@ -311,6 +327,65 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
assert 191 == state.attributes.get(ATTR_SUPPORTED_FEATURES)
assert state.attributes.get(ATTR_ASSUMED_STATE)
common.async_turn_on(hass, 'light.test')
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
'test_light_rgb/set', '{"state": "ON"}', 2, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get('light.test')
assert STATE_ON == state.state
common.async_turn_off(hass, 'light.test')
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
'test_light_rgb/set', '{"state": "OFF"}', 2, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get('light.test')
assert STATE_OFF == state.state
mqtt_mock.reset_mock()
common.async_turn_on(hass, 'light.test',
brightness=50, xy_color=[0.123, 0.123])
common.async_turn_on(hass, 'light.test',
brightness=50, hs_color=[359, 78])
common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0],
white_value=80)
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_has_calls([
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"r": 0, "g": 123, "b": 255,'
' "x": 0.14, "y": 0.131, "h": 210.824, "s": 100.0},'
' "brightness": 50}'),
2, False),
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"r": 255, "g": 56, "b": 59,'
' "x": 0.654, "y": 0.301, "h": 359.0, "s": 78.0},'
' "brightness": 50}'),
2, False),
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"r": 255, "g": 128, "b": 0,'
' "x": 0.611, "y": 0.375, "h": 30.118, "s": 100.0},'
' "white_value": 80}'),
2, False),
], any_order=True)
state = hass.states.get('light.test')
assert STATE_ON == state.state
assert (255, 128, 0) == state.attributes['rgb_color']
assert 50 == state.attributes['brightness']
assert (30.118, 100) == state.attributes['hs_color']
assert 80 == state.attributes['white_value']
assert (0.611, 0.375) == state.attributes['xy_color']
async def test_sending_hs_color(hass, mqtt_mock):
"""Test light.turn_on with hs color sends hs color parameters."""
@@ -320,6 +395,7 @@ async def test_sending_hs_color(hass, mqtt_mock):
'schema': 'json',
'name': 'test',
'command_topic': 'test_light_rgb/set',
'brightness': True,
'hs': True,
}
})
@@ -327,6 +403,170 @@ async def test_sending_hs_color(hass, mqtt_mock):
state = hass.states.get('light.test')
assert STATE_OFF == state.state
mqtt_mock.reset_mock()
common.async_turn_on(hass, 'light.test',
brightness=50, xy_color=[0.123, 0.123])
common.async_turn_on(hass, 'light.test',
brightness=50, hs_color=[359, 78])
common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0],
white_value=80)
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_has_calls([
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"h": 210.824, "s": 100.0},'
' "brightness": 50}'),
0, False),
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"h": 359.0, "s": 78.0},'
' "brightness": 50}'),
0, False),
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"h": 30.118, "s": 100.0},'
' "white_value": 80}'),
0, False),
], any_order=True)
async def test_sending_rgb_color_no_brightness(hass, mqtt_mock):
"""Test light.turn_on with hs color sends rgb color parameters."""
assert await async_setup_component(hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt',
'schema': 'json',
'name': 'test',
'command_topic': 'test_light_rgb/set',
'rgb': True,
}
})
state = hass.states.get('light.test')
assert STATE_OFF == state.state
common.async_turn_on(hass, 'light.test',
brightness=50, xy_color=[0.123, 0.123])
common.async_turn_on(hass, 'light.test',
brightness=50, hs_color=[359, 78])
common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0],
brightness=255)
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_has_calls([
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"r": 0, "g": 24, "b": 50}}'),
0, False),
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"r": 50, "g": 11, "b": 11}}'),
0, False),
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"r": 255, "g": 128, "b": 0}}'),
0, False),
], any_order=True)
async def test_sending_rgb_color_with_brightness(hass, mqtt_mock):
"""Test light.turn_on with hs color sends rgb color parameters."""
assert await async_setup_component(hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt',
'schema': 'json',
'name': 'test',
'command_topic': 'test_light_rgb/set',
'brightness': True,
'rgb': True,
}
})
state = hass.states.get('light.test')
assert STATE_OFF == state.state
common.async_turn_on(hass, 'light.test',
brightness=50, xy_color=[0.123, 0.123])
common.async_turn_on(hass, 'light.test',
brightness=50, hs_color=[359, 78])
common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0],
white_value=80)
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_has_calls([
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"r": 0, "g": 123, "b": 255},'
' "brightness": 50}'),
0, False),
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"r": 255, "g": 56, "b": 59},'
' "brightness": 50}'),
0, False),
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"r": 255, "g": 128, "b": 0},'
' "white_value": 80}'),
0, False),
], any_order=True)
async def test_sending_xy_color(hass, mqtt_mock):
"""Test light.turn_on with hs color sends xy color parameters."""
assert await async_setup_component(hass, light.DOMAIN, {
light.DOMAIN: {
'platform': 'mqtt',
'schema': 'json',
'name': 'test',
'command_topic': 'test_light_rgb/set',
'brightness': True,
'xy': True,
}
})
state = hass.states.get('light.test')
assert STATE_OFF == state.state
common.async_turn_on(hass, 'light.test',
brightness=50, xy_color=[0.123, 0.123])
common.async_turn_on(hass, 'light.test',
brightness=50, hs_color=[359, 78])
common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0],
white_value=80)
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_has_calls([
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"x": 0.14, "y": 0.131},'
' "brightness": 50}'),
0, False),
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"x": 0.654, "y": 0.301},'
' "brightness": 50}'),
0, False),
mock.call(
'test_light_rgb/set',
JsonValidator(
'{"state": "ON", "color": {"x": 0.611, "y": 0.375},'
' "white_value": 80}'),
0, False),
], any_order=True)
async def test_flash_short_and_long(hass, mqtt_mock):
"""Test for flash length being sent when included."""
@@ -335,7 +575,6 @@ async def test_flash_short_and_long(hass, mqtt_mock):
'platform': 'mqtt',
'schema': 'json',
'name': 'test',
'state_topic': 'test_light_rgb',
'command_topic': 'test_light_rgb/set',
'flash_time_short': 5,
'flash_time_long': 15,
@@ -347,6 +586,26 @@ async def test_flash_short_and_long(hass, mqtt_mock):
assert STATE_OFF == state.state
assert 40 == state.attributes.get(ATTR_SUPPORTED_FEATURES)
common.async_turn_on(hass, 'light.test', flash='short')
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
'test_light_rgb/set', JsonValidator(
'{"state": "ON", "flash": 5}'), 0, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get('light.test')
assert STATE_ON == state.state
common.async_turn_on(hass, 'light.test', flash='long')
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
'test_light_rgb/set', JsonValidator(
'{"state": "ON", "flash": 15}'), 0, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get('light.test')
assert STATE_ON == state.state
async def test_transition(hass, mqtt_mock):
"""Test for transition time being sent when included."""
@@ -355,7 +614,6 @@ async def test_transition(hass, mqtt_mock):
'platform': 'mqtt',
'schema': 'json',
'name': 'test',
'state_topic': 'test_light_rgb',
'command_topic': 'test_light_rgb/set',
'qos': 0
}
@@ -365,6 +623,26 @@ async def test_transition(hass, mqtt_mock):
assert STATE_OFF == state.state
assert 40 == state.attributes.get(ATTR_SUPPORTED_FEATURES)
common.async_turn_on(hass, 'light.test', transition=15)
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
'test_light_rgb/set', JsonValidator(
'{"state": "ON", "transition": 15}'), 0, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get('light.test')
assert STATE_ON == state.state
common.async_turn_off(hass, 'light.test', transition=30)
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
'test_light_rgb/set', JsonValidator(
'{"state": "OFF", "transition": 30}'), 0, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get('light.test')
assert STATE_OFF == state.state
async def test_brightness_scale(hass, mqtt_mock):
"""Test for brightness scaling."""