Make sure MQTT light brightness is not rounded to 0 (#35207)
This commit is contained in:
parent
94b6130af6
commit
f9b420a5a5
4 changed files with 209 additions and 12 deletions
|
@ -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."""
|
||||
|
|
|
@ -577,7 +577,8 @@ async def test_sending_rgb_color_with_brightness(hass, mqtt_mock):
|
|||
await common.async_turn_on(
|
||||
hass, "light.test", brightness=50, xy_color=[0.123, 0.123]
|
||||
)
|
||||
await common.async_turn_on(hass, "light.test", brightness=50, hs_color=[359, 78])
|
||||
await common.async_turn_on(hass, "light.test", brightness=255, hs_color=[359, 78])
|
||||
await common.async_turn_on(hass, "light.test", brightness=1)
|
||||
await common.async_turn_on(
|
||||
hass, "light.test", rgb_color=[255, 128, 0], white_value=80
|
||||
)
|
||||
|
@ -597,11 +598,86 @@ async def test_sending_rgb_color_with_brightness(hass, mqtt_mock):
|
|||
"test_light_rgb/set",
|
||||
JsonValidator(
|
||||
'{"state": "ON", "color": {"r": 255, "g": 56, "b": 59},'
|
||||
' "brightness": 50}'
|
||||
' "brightness": 255}'
|
||||
),
|
||||
0,
|
||||
False,
|
||||
),
|
||||
call(
|
||||
"test_light_rgb/set",
|
||||
JsonValidator('{"state": "ON", "brightness": 1}'),
|
||||
0,
|
||||
False,
|
||||
),
|
||||
call(
|
||||
"test_light_rgb/set",
|
||||
JsonValidator(
|
||||
'{"state": "ON", "color": {"r": 255, "g": 128, "b": 0},'
|
||||
' "white_value": 80}'
|
||||
),
|
||||
0,
|
||||
False,
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
async def test_sending_rgb_color_with_scaled_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,
|
||||
"brightness_scale": 100,
|
||||
"rgb": True,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
state = hass.states.get("light.test")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
await common.async_turn_on(
|
||||
hass, "light.test", brightness=50, xy_color=[0.123, 0.123]
|
||||
)
|
||||
await common.async_turn_on(hass, "light.test", brightness=255, hs_color=[359, 78])
|
||||
await common.async_turn_on(hass, "light.test", brightness=1)
|
||||
await common.async_turn_on(
|
||||
hass, "light.test", rgb_color=[255, 128, 0], white_value=80
|
||||
)
|
||||
|
||||
mqtt_mock.async_publish.assert_has_calls(
|
||||
[
|
||||
call(
|
||||
"test_light_rgb/set",
|
||||
JsonValidator(
|
||||
'{"state": "ON", "color": {"r": 0, "g": 123, "b": 255},'
|
||||
' "brightness": 20}'
|
||||
),
|
||||
0,
|
||||
False,
|
||||
),
|
||||
call(
|
||||
"test_light_rgb/set",
|
||||
JsonValidator(
|
||||
'{"state": "ON", "color": {"r": 255, "g": 56, "b": 59},'
|
||||
' "brightness": 100}'
|
||||
),
|
||||
0,
|
||||
False,
|
||||
),
|
||||
call(
|
||||
"test_light_rgb/set",
|
||||
JsonValidator('{"state": "ON", "brightness": 1}'),
|
||||
0,
|
||||
False,
|
||||
),
|
||||
call(
|
||||
"test_light_rgb/set",
|
||||
JsonValidator(
|
||||
|
@ -612,7 +688,6 @@ async def test_sending_rgb_color_with_brightness(hass, mqtt_mock):
|
|||
False,
|
||||
),
|
||||
],
|
||||
any_order=True,
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue