From 9197316b574c0328931893fdefec8b3430a5de62 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Sat, 22 Apr 2023 13:55:43 +0200 Subject: [PATCH] Add tests mqtt light with single supported color_mode (#91811) --- tests/components/mqtt/test_light_json.py | 43 ++++++++++++++++++++ tests/components/mqtt/test_light_template.py | 39 ++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index 4bac39ed8ef..6c653045aa2 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -268,6 +268,49 @@ async def test_fail_setup_if_color_modes_invalid( assert error in caplog.text +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "schema": "json", + "name": "test", + "command_topic": "test_light/set", + "state_topic": "test_light", + "color_mode": True, + "supported_color_modes": "color_temp", + } + } + } + ], +) +async def test_single_color_mode( + hass: HomeAssistant, + mqtt_mock_entry: MqttMockHAClientGenerator, +) -> None: + """Test setup with single color_mode.""" + await mqtt_mock_entry() + state = hass.states.get("light.test") + assert state.state == STATE_UNKNOWN + + await common.async_turn_on(hass, "light.test", brightness=50, color_temp=192) + + async_fire_mqtt_message( + hass, + "test_light", + '{"state": "ON", "brightness": 50, "color_mode": "color_temp", "color_temp": 192}', + ) + color_modes = [light.ColorMode.COLOR_TEMP] + state = hass.states.get("light.test") + assert state.state == STATE_ON + + assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes + assert state.attributes.get(light.ATTR_COLOR_TEMP) == 192 + assert state.attributes.get(light.ATTR_BRIGHTNESS) == 50 + assert state.attributes.get(light.ATTR_COLOR_MODE) == color_modes[0] + + @pytest.mark.parametrize( "hass_config", [ diff --git a/tests/components/mqtt/test_light_template.py b/tests/components/mqtt/test_light_template.py index 0dffe6368e2..ba8414e252a 100644 --- a/tests/components/mqtt/test_light_template.py +++ b/tests/components/mqtt/test_light_template.py @@ -183,6 +183,45 @@ async def test_rgb_light( assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "schema": "template", + "name": "test", + "command_topic": "test_light/set", + "command_on_template": "on,{{ brightness|d }},{{ color_temp|d }}", + "command_off_template": "off", + "brightness_template": "{{ value.split(',')[1] }}", + "color_temp_template": "{{ value.split(',')[2] }}", + } + } + } + ], +) +async def test_single_color_mode( + hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator +) -> None: + """Test the color mode when we only have one supported color_mode.""" + await mqtt_mock_entry() + + state = hass.states.get("light.test") + assert state.state == STATE_UNKNOWN + + await common.async_turn_on(hass, "light.test", brightness=50, color_temp=192) + async_fire_mqtt_message(hass, "test_light", "on,50,192") + color_modes = [light.ColorMode.COLOR_TEMP] + state = hass.states.get("light.test") + assert state.state == STATE_ON + + assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes + assert state.attributes.get(light.ATTR_COLOR_TEMP) == 192 + assert state.attributes.get(light.ATTR_BRIGHTNESS) == 50 + assert state.attributes.get(light.ATTR_COLOR_MODE) == color_modes[0] + + @pytest.mark.parametrize( "hass_config", [