Make sure MQTT light brightness is not rounded to 0 (#35207)

This commit is contained in:
Erik Montnemery 2020-05-05 00:54:49 +02:00 committed by GitHub
parent 94b6130af6
commit f9b420a5a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 209 additions and 12 deletions

View file

@ -980,7 +980,6 @@ async def test_on_command_first(hass, mqtt_mock):
call("test_light/set", "ON", 0, False),
call("test_light/bright", 50, 0, False),
],
any_order=True,
)
mqtt_mock.async_publish.reset_mock()
@ -1015,7 +1014,6 @@ async def test_on_command_last(hass, mqtt_mock):
call("test_light/bright", 50, 0, False),
call("test_light/set", "ON", 0, False),
],
any_order=True,
)
mqtt_mock.async_publish.reset_mock()
@ -1063,7 +1061,7 @@ async def test_on_command_brightness(hass, mqtt_mock):
await common.async_turn_off(hass, "light.test")
# Turn on w/ just a color to insure brightness gets
# Turn on w/ just a color to ensure brightness gets
# added and sent.
await common.async_turn_on(hass, "light.test", rgb_color=[255, 128, 0])
@ -1076,6 +1074,71 @@ async def test_on_command_brightness(hass, mqtt_mock):
)
async def test_on_command_brightness_scaled(hass, mqtt_mock):
"""Test brightness scale."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"brightness_command_topic": "test_light/bright",
"brightness_scale": 100,
"rgb_command_topic": "test_light/rgb",
"on_command_type": "brightness",
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
state = hass.states.get("light.test")
assert state.state == STATE_OFF
# Turn on w/ no brightness - should set to max
await common.async_turn_on(hass, "light.test")
# Should get the following MQTT messages.
# test_light/bright: 100
mqtt_mock.async_publish.assert_called_once_with("test_light/bright", 100, 0, False)
mqtt_mock.async_publish.reset_mock()
await common.async_turn_off(hass, "light.test")
mqtt_mock.async_publish.assert_called_once_with("test_light/set", "OFF", 0, False)
mqtt_mock.async_publish.reset_mock()
# Turn on w/ brightness
await common.async_turn_on(hass, "light.test", brightness=50)
mqtt_mock.async_publish.assert_called_once_with("test_light/bright", 20, 0, False)
mqtt_mock.async_publish.reset_mock()
# Turn on w/ max brightness
await common.async_turn_on(hass, "light.test", brightness=255)
mqtt_mock.async_publish.assert_called_once_with("test_light/bright", 100, 0, False)
mqtt_mock.async_publish.reset_mock()
# Turn on w/ min brightness
await common.async_turn_on(hass, "light.test", brightness=1)
mqtt_mock.async_publish.assert_called_once_with("test_light/bright", 1, 0, False)
mqtt_mock.async_publish.reset_mock()
await common.async_turn_off(hass, "light.test")
# Turn on w/ just a color to ensure brightness gets
# added and sent.
await common.async_turn_on(hass, "light.test", rgb_color=[255, 128, 0])
mqtt_mock.async_publish.assert_has_calls(
[
call("test_light/rgb", "255,128,0", 0, False),
call("test_light/bright", 1, 0, False),
],
any_order=True,
)
async def test_on_command_rgb(hass, mqtt_mock):
"""Test on command in RGB brightness mode."""
config = {
@ -1106,10 +1169,64 @@ async def test_on_command_rgb(hass, mqtt_mock):
)
mqtt_mock.async_publish.reset_mock()
await common.async_turn_on(hass, "light.test", brightness=255)
# Should get the following MQTT messages.
# test_light/rgb: '255,255,255'
# test_light/set: 'ON'
mqtt_mock.async_publish.assert_has_calls(
[
call("test_light/rgb", "255,255,255", 0, False),
call("test_light/set", "ON", 0, False),
],
any_order=True,
)
mqtt_mock.async_publish.reset_mock()
await common.async_turn_on(hass, "light.test", brightness=1)
# Should get the following MQTT messages.
# test_light/rgb: '1,1,1'
# test_light/set: 'ON'
mqtt_mock.async_publish.assert_has_calls(
[
call("test_light/rgb", "1,1,1", 0, False),
call("test_light/set", "ON", 0, False),
],
any_order=True,
)
mqtt_mock.async_publish.reset_mock()
await common.async_turn_off(hass, "light.test")
mqtt_mock.async_publish.assert_called_once_with("test_light/set", "OFF", 0, False)
# Ensure color gets scaled with brightness.
await common.async_turn_on(hass, "light.test", rgb_color=[255, 128, 0])
mqtt_mock.async_publish.assert_has_calls(
[
call("test_light/rgb", "1,0,0", 0, False),
call("test_light/set", "ON", 0, False),
],
any_order=True,
)
mqtt_mock.async_publish.reset_mock()
await common.async_turn_on(hass, "light.test", brightness=255)
# Should get the following MQTT messages.
# test_light/rgb: '255,128,0'
# test_light/set: 'ON'
mqtt_mock.async_publish.assert_has_calls(
[
call("test_light/rgb", "255,128,0", 0, False),
call("test_light/set", "ON", 0, False),
],
any_order=True,
)
mqtt_mock.async_publish.reset_mock()
async def test_on_command_rgb_template(hass, mqtt_mock):
"""Test on command in RGB brightness mode with RGB template."""