Add MQTT light unknown state support (#65308)

* Add MQTT light unknown sate support

* Update homeassistant/components/mqtt/light/schema_basic.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/mqtt/light/schema_json.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/mqtt/light/schema_template.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update tests for default unknown state

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Jan Bouwhuis
2022-02-03 16:49:57 +01:00
committed by GitHub
parent 2d011821ea
commit cf52357229
6 changed files with 116 additions and 63 deletions

View File

@ -177,6 +177,7 @@ from homeassistant.const import (
ATTR_SUPPORTED_FEATURES,
STATE_OFF,
STATE_ON,
STATE_UNKNOWN,
)
import homeassistant.core as ha
from homeassistant.setup import async_setup_component
@ -269,7 +270,7 @@ async def test_no_color_brightness_color_temp_hs_white_xy_if_no_topics(hass, mqt
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_temp") is None
@ -298,6 +299,16 @@ async def test_no_color_brightness_color_temp_hs_white_xy_if_no_topics(hass, mqt
assert state.attributes.get(light.ATTR_COLOR_MODE) == "onoff"
assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == ["onoff"]
async_fire_mqtt_message(hass, "test_light_rgb/status", "OFF")
state = hass.states.get("light.test")
assert state.state == STATE_OFF
async_fire_mqtt_message(hass, "test_light_rgb/status", "None")
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
async def test_legacy_controlling_state_via_topic(hass, mqtt_mock):
"""Test the controlling of the state via topic for legacy light (white_value)."""
@ -332,7 +343,7 @@ async def test_legacy_controlling_state_via_topic(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_temp") is None
@ -463,7 +474,7 @@ async def test_controlling_state_via_topic(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_temp") is None
@ -581,7 +592,7 @@ async def test_legacy_invalid_state_via_topic(hass, mqtt_mock, caplog):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_temp") is None
@ -700,7 +711,7 @@ async def test_invalid_state_via_topic(hass, mqtt_mock, caplog):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("rgbw_color") is None
assert state.attributes.get("rgbww_color") is None
@ -823,7 +834,7 @@ async def test_brightness_controlling_scale(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("brightness") is None
assert not state.attributes.get(ATTR_ASSUMED_STATE)
@ -869,7 +880,7 @@ async def test_brightness_from_rgb_controlling_scale(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("brightness") is None
assert not state.attributes.get(ATTR_ASSUMED_STATE)
@ -909,7 +920,7 @@ async def test_legacy_white_value_controlling_scale(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("white_value") is None
assert not state.attributes.get(ATTR_ASSUMED_STATE)
@ -969,7 +980,7 @@ async def test_legacy_controlling_state_via_topic_with_templates(hass, mqtt_mock
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("brightness") is None
assert state.attributes.get("rgb_color") is None
@ -1016,6 +1027,10 @@ async def test_legacy_controlling_state_via_topic_with_templates(hass, mqtt_mock
state = hass.states.get("light.test")
assert state.attributes.get("xy_color") == (0.14, 0.131)
async_fire_mqtt_message(hass, "test_light_rgb/status", '{"hello": null}')
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
async def test_controlling_state_via_topic_with_templates(hass, mqtt_mock):
"""Test the setting of the state with a template."""
@ -1058,7 +1073,7 @@ async def test_controlling_state_via_topic_with_templates(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("brightness") is None
assert state.attributes.get("rgb_color") is None
@ -1456,7 +1471,7 @@ async def test_sending_mqtt_rgb_command_with_template(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", rgb_color=[255, 128, 64])
@ -1493,7 +1508,7 @@ async def test_sending_mqtt_rgbw_command_with_template(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", rgbw_color=[255, 128, 64, 32])
@ -1530,7 +1545,7 @@ async def test_sending_mqtt_rgbww_command_with_template(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", rgbww_color=[255, 128, 64, 32, 16])
@ -1566,7 +1581,7 @@ async def test_sending_mqtt_color_temp_command_with_template(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", color_temp=100)
@ -1599,7 +1614,7 @@ async def test_on_command_first(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=50)
@ -1634,7 +1649,7 @@ async def test_on_command_last(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=50)
@ -1671,7 +1686,7 @@ async def test_on_command_brightness(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
# Turn on w/ no brightness - should set to max
await common.async_turn_on(hass, "light.test")
@ -1727,7 +1742,7 @@ async def test_on_command_brightness_scaled(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
# Turn on w/ no brightness - should set to max
await common.async_turn_on(hass, "light.test")
@ -1795,7 +1810,7 @@ async def test_legacy_on_command_rgb(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=127)
@ -1885,7 +1900,7 @@ async def test_on_command_rgb(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=127)
@ -1975,7 +1990,7 @@ async def test_on_command_rgbw(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=127)
@ -2065,7 +2080,7 @@ async def test_on_command_rgbww(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=127)
@ -2156,7 +2171,7 @@ async def test_on_command_rgb_template(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=127)
@ -2193,8 +2208,7 @@ async def test_on_command_rgbw_template(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=127)
# Should get the following MQTT messages.
@ -2230,7 +2244,7 @@ async def test_on_command_rgbww_template(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=127)
@ -2279,7 +2293,7 @@ async def test_on_command_white(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("brightness") is None
assert state.attributes.get("rgb_color") is None
assert state.attributes.get(light.ATTR_COLOR_MODE) is None
@ -2364,7 +2378,7 @@ async def test_explicit_color_mode(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_temp") is None
@ -2505,7 +2519,7 @@ async def test_explicit_color_mode_templated(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_temp") is None
assert state.attributes.get("hs_color") is None
@ -2591,7 +2605,7 @@ async def test_white_state_update(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get("brightness") is None
assert state.attributes.get("rgb_color") is None
assert state.attributes.get(light.ATTR_COLOR_MODE) is None
@ -2639,7 +2653,7 @@ async def test_effect(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", effect="rainbow")