diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index 7d0c0333b7e..6ad52103a06 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -196,7 +196,6 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant, State -from homeassistant.setup import async_setup_component from .test_common import ( help_test_availability_when_connection_lost, @@ -243,26 +242,26 @@ def light_platform_only(): yield +@pytest.mark.parametrize( + "hass_config", [{mqtt.DOMAIN: {light.DOMAIN: {"name": "test"}}}] +) async def test_fail_setup_if_no_command_topic( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, ) -> None: """Test if command fails with command topic.""" - assert not await async_setup_component( - hass, mqtt.DOMAIN, {mqtt.DOMAIN: {light.DOMAIN: {"name": "test"}}} - ) + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert ( "Invalid config for [mqtt]: required key not provided @ data['mqtt']['light'][0]['command_topic']. Got None." in caplog.text ) -async def test_no_color_brightness_color_temp_hs_white_xy_if_no_topics( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test if there is no color and brightness if no topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -271,10 +270,14 @@ async def test_no_color_brightness_color_temp_hs_white_xy_if_no_topics( "command_topic": "test_light_rgb/set", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_no_color_brightness_color_temp_hs_white_xy_if_no_topics( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test if there is no color and brightness if no topic.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -315,41 +318,46 @@ async def test_no_color_brightness_color_temp_hs_white_xy_if_no_topics( assert state.state == STATE_UNKNOWN +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "state_topic": "test_light_rgb/status", + "command_topic": "test_light_rgb/set", + "brightness_state_topic": "test_light_rgb/brightness/status", + "brightness_command_topic": "test_light_rgb/brightness/set", + "rgb_state_topic": "test_light_rgb/rgb/status", + "rgb_command_topic": "test_light_rgb/rgb/set", + "rgbw_state_topic": "test_light_rgb/rgbw/status", + "rgbw_command_topic": "test_light_rgb/rgbw/set", + "rgbww_state_topic": "test_light_rgb/rgbww/status", + "rgbww_command_topic": "test_light_rgb/rgbww/set", + "color_temp_state_topic": "test_light_rgb/color_temp/status", + "color_temp_command_topic": "test_light_rgb/color_temp/set", + "effect_state_topic": "test_light_rgb/effect/status", + "effect_command_topic": "test_light_rgb/effect/set", + "hs_state_topic": "test_light_rgb/hs/status", + "hs_command_topic": "test_light_rgb/hs/set", + "xy_state_topic": "test_light_rgb/xy/status", + "xy_command_topic": "test_light_rgb/xy/set", + "qos": "0", + "payload_on": 1, + "payload_off": 0, + } + } + } + ], +) async def test_controlling_state_via_topic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the controlling of the state via topic.""" - config = { - light.DOMAIN: { - "name": "test", - "state_topic": "test_light_rgb/status", - "command_topic": "test_light_rgb/set", - "brightness_state_topic": "test_light_rgb/brightness/status", - "brightness_command_topic": "test_light_rgb/brightness/set", - "rgb_state_topic": "test_light_rgb/rgb/status", - "rgb_command_topic": "test_light_rgb/rgb/set", - "rgbw_state_topic": "test_light_rgb/rgbw/status", - "rgbw_command_topic": "test_light_rgb/rgbw/set", - "rgbww_state_topic": "test_light_rgb/rgbww/status", - "rgbww_command_topic": "test_light_rgb/rgbww/set", - "color_temp_state_topic": "test_light_rgb/color_temp/status", - "color_temp_command_topic": "test_light_rgb/color_temp/set", - "effect_state_topic": "test_light_rgb/effect/status", - "effect_command_topic": "test_light_rgb/effect/set", - "hs_state_topic": "test_light_rgb/hs/status", - "hs_command_topic": "test_light_rgb/hs/set", - "xy_state_topic": "test_light_rgb/xy/status", - "xy_command_topic": "test_light_rgb/xy/set", - "qos": "0", - "payload_on": 1, - "payload_off": 0, - } - } color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"] - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -436,43 +444,47 @@ async def test_controlling_state_via_topic( assert light_state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "state_topic": "test_light_rgb/status", + "command_topic": "test_light_rgb/set", + "brightness_state_topic": "test_light_rgb/brightness/status", + "brightness_command_topic": "test_light_rgb/brightness/set", + "color_mode_state_topic": "test_light_rgb/color_mode/status", + "rgb_state_topic": "test_light_rgb/rgb/status", + "rgb_command_topic": "test_light_rgb/rgb/set", + "rgbw_state_topic": "test_light_rgb/rgbw/status", + "rgbw_command_topic": "test_light_rgb/rgbw/set", + "rgbww_state_topic": "test_light_rgb/rgbww/status", + "rgbww_command_topic": "test_light_rgb/rgbww/set", + "color_temp_state_topic": "test_light_rgb/color_temp/status", + "color_temp_command_topic": "test_light_rgb/color_temp/set", + "effect_state_topic": "test_light_rgb/effect/status", + "effect_command_topic": "test_light_rgb/effect/set", + "hs_state_topic": "test_light_rgb/hs/status", + "hs_command_topic": "test_light_rgb/hs/set", + "xy_state_topic": "test_light_rgb/xy/status", + "xy_command_topic": "test_light_rgb/xy/set", + "qos": "0", + "payload_on": 1, + "payload_off": 0, + } + } + } + ], +) async def test_invalid_state_via_topic( hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, caplog: pytest.LogCaptureFixture, ) -> None: """Test handling of empty data via topic.""" - config = { - light.DOMAIN: { - "name": "test", - "state_topic": "test_light_rgb/status", - "command_topic": "test_light_rgb/set", - "brightness_state_topic": "test_light_rgb/brightness/status", - "brightness_command_topic": "test_light_rgb/brightness/set", - "color_mode_state_topic": "test_light_rgb/color_mode/status", - "rgb_state_topic": "test_light_rgb/rgb/status", - "rgb_command_topic": "test_light_rgb/rgb/set", - "rgbw_state_topic": "test_light_rgb/rgbw/status", - "rgbw_command_topic": "test_light_rgb/rgbw/set", - "rgbww_state_topic": "test_light_rgb/rgbww/status", - "rgbww_command_topic": "test_light_rgb/rgbww/set", - "color_temp_state_topic": "test_light_rgb/color_temp/status", - "color_temp_command_topic": "test_light_rgb/color_temp/set", - "effect_state_topic": "test_light_rgb/effect/status", - "effect_command_topic": "test_light_rgb/effect/set", - "hs_state_topic": "test_light_rgb/hs/status", - "hs_command_topic": "test_light_rgb/hs/set", - "xy_state_topic": "test_light_rgb/xy/status", - "xy_command_topic": "test_light_rgb/xy/set", - "qos": "0", - "payload_on": 1, - "payload_off": 0, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -564,13 +576,9 @@ async def test_invalid_state_via_topic( assert light_state.attributes["color_temp"] == 153 -async def test_brightness_controlling_scale( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the brightness controlling scale.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -585,10 +593,14 @@ async def test_brightness_controlling_scale( "payload_off": "off", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_brightness_controlling_scale( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the brightness controlling scale.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -614,13 +626,9 @@ async def test_brightness_controlling_scale( assert light_state.attributes["brightness"] == 255 -async def test_brightness_from_rgb_controlling_scale( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the brightness controlling scale.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -634,9 +642,14 @@ async def test_brightness_from_rgb_controlling_scale( "payload_off": "off", } } - }, - ) - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_brightness_from_rgb_controlling_scale( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the brightness controlling scale.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.async_block_till_done() state = hass.states.get("light.test") @@ -675,47 +688,52 @@ async def test_brightness_from_rgb_controlling_scale( assert state.attributes.get("rgb_color") == (255, 127, 63) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "state_topic": "test_light_rgb/status", + "command_topic": "test_light_rgb/set", + "brightness_command_topic": "test_light_rgb/brightness/set", + "rgb_command_topic": "test_light_rgb/rgb/set", + "rgbw_command_topic": "test_light_rgb/rgbw/set", + "rgbww_command_topic": "test_light_rgb/rgbw/set", + "color_temp_command_topic": "test_light_rgb/color_temp/set", + "effect_command_topic": "test_light_rgb/effect/set", + "hs_command_topic": "test_light_rgb/hs/set", + "xy_command_topic": "test_light_rgb/xy/set", + "brightness_state_topic": "test_light_rgb/brightness/status", + "color_temp_state_topic": "test_light_rgb/color_temp/status", + "effect_state_topic": "test_light_rgb/effect/status", + "hs_state_topic": "test_light_rgb/hs/status", + "rgb_state_topic": "test_light_rgb/rgb/status", + "rgbw_state_topic": "test_light_rgb/rgbw/status", + "rgbww_state_topic": "test_light_rgb/rgbww/status", + "xy_state_topic": "test_light_rgb/xy/status", + "state_value_template": "{{ value_json.hello }}", + "brightness_value_template": "{{ value_json.hello }}", + "color_temp_value_template": "{{ value_json.hello }}", + "effect_value_template": "{{ value_json.hello }}", + "hs_value_template": '{{ value_json.hello | join(",") }}', + "rgb_value_template": '{{ value_json.hello | join(",") }}', + "rgbw_value_template": '{{ value_json.hello | join(",") }}', + "rgbww_value_template": '{{ value_json.hello | join(",") }}', + "xy_value_template": '{{ value_json.hello | join(",") }}', + } + } + } + ], +) async def test_controlling_state_via_topic_with_templates( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the setting of the state with a template.""" - config = { - light.DOMAIN: { - "name": "test", - "state_topic": "test_light_rgb/status", - "command_topic": "test_light_rgb/set", - "brightness_command_topic": "test_light_rgb/brightness/set", - "rgb_command_topic": "test_light_rgb/rgb/set", - "rgbw_command_topic": "test_light_rgb/rgbw/set", - "rgbww_command_topic": "test_light_rgb/rgbw/set", - "color_temp_command_topic": "test_light_rgb/color_temp/set", - "effect_command_topic": "test_light_rgb/effect/set", - "hs_command_topic": "test_light_rgb/hs/set", - "xy_command_topic": "test_light_rgb/xy/set", - "brightness_state_topic": "test_light_rgb/brightness/status", - "color_temp_state_topic": "test_light_rgb/color_temp/status", - "effect_state_topic": "test_light_rgb/effect/status", - "hs_state_topic": "test_light_rgb/hs/status", - "rgb_state_topic": "test_light_rgb/rgb/status", - "rgbw_state_topic": "test_light_rgb/rgbw/status", - "rgbww_state_topic": "test_light_rgb/rgbww/status", - "xy_state_topic": "test_light_rgb/xy/status", - "state_value_template": "{{ value_json.hello }}", - "brightness_value_template": "{{ value_json.hello }}", - "color_temp_value_template": "{{ value_json.hello }}", - "effect_value_template": "{{ value_json.hello }}", - "hs_value_template": '{{ value_json.hello | join(",") }}', - "rgb_value_template": '{{ value_json.hello | join(",") }}', - "rgbw_value_template": '{{ value_json.hello | join(",") }}', - "rgbww_value_template": '{{ value_json.hello | join(",") }}', - "xy_value_template": '{{ value_json.hello | join(",") }}', - } - } color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"] - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -777,28 +795,35 @@ async def test_controlling_state_via_topic_with_templates( assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light_rgb/set", + "brightness_command_topic": "test_light_rgb/brightness/set", + "rgb_command_topic": "test_light_rgb/rgb/set", + "rgbw_command_topic": "test_light_rgb/rgbw/set", + "rgbww_command_topic": "test_light_rgb/rgbww/set", + "color_temp_command_topic": "test_light_rgb/color_temp/set", + "effect_command_topic": "test_light_rgb/effect/set", + "hs_command_topic": "test_light_rgb/hs/set", + "xy_command_topic": "test_light_rgb/xy/set", + "effect_list": ["colorloop", "random"], + "qos": 2, + "payload_on": "on", + "payload_off": "off", + } + } + } + ], +) async def test_sending_mqtt_commands_and_optimistic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the sending of command in optimistic mode.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light_rgb/set", - "brightness_command_topic": "test_light_rgb/brightness/set", - "rgb_command_topic": "test_light_rgb/rgb/set", - "rgbw_command_topic": "test_light_rgb/rgbw/set", - "rgbww_command_topic": "test_light_rgb/rgbww/set", - "color_temp_command_topic": "test_light_rgb/color_temp/set", - "effect_command_topic": "test_light_rgb/effect/set", - "hs_command_topic": "test_light_rgb/hs/set", - "xy_command_topic": "test_light_rgb/xy/set", - "effect_list": ["colorloop", "random"], - "qos": 2, - "payload_on": "on", - "payload_off": "off", - } - } color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"] fake_state = State( "light.test", @@ -813,9 +838,7 @@ async def test_sending_mqtt_commands_and_optimistic( ) mock_restore_cache(hass, (fake_state,)) - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_ON @@ -966,26 +989,30 @@ async def test_sending_mqtt_commands_and_optimistic( assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light_rgb/set", + "rgb_command_topic": "test_light_rgb/rgb/set", + "rgb_command_template": '{{ "#%02x%02x%02x" | ' + "format(red, green, blue)}}", + "payload_on": "on", + "payload_off": "off", + "qos": 0, + } + } + } + ], +) async def test_sending_mqtt_rgb_command_with_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the sending of RGB command with template.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light_rgb/set", - "rgb_command_topic": "test_light_rgb/rgb/set", - "rgb_command_template": '{{ "#%02x%02x%02x" | ' - "format(red, green, blue)}}", - "payload_on": "on", - "payload_off": "off", - "qos": 0, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1005,26 +1032,30 @@ async def test_sending_mqtt_rgb_command_with_template( assert state.attributes["rgb_color"] == (255, 128, 64) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light_rgb/set", + "rgbw_command_topic": "test_light_rgb/rgbw/set", + "rgbw_command_template": '{{ "#%02x%02x%02x%02x" | ' + "format(red, green, blue, white)}}", + "payload_on": "on", + "payload_off": "off", + "qos": 0, + } + } + } + ], +) async def test_sending_mqtt_rgbw_command_with_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the sending of RGBW command with template.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light_rgb/set", - "rgbw_command_topic": "test_light_rgb/rgbw/set", - "rgbw_command_template": '{{ "#%02x%02x%02x%02x" | ' - "format(red, green, blue, white)}}", - "payload_on": "on", - "payload_off": "off", - "qos": 0, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1044,26 +1075,30 @@ async def test_sending_mqtt_rgbw_command_with_template( assert state.attributes["rgbw_color"] == (255, 128, 64, 32) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light_rgb/set", + "rgbww_command_topic": "test_light_rgb/rgbww/set", + "rgbww_command_template": '{{ "#%02x%02x%02x%02x%02x" | ' + "format(red, green, blue, cold_white, warm_white)}}", + "payload_on": "on", + "payload_off": "off", + "qos": 0, + } + } + } + ], +) async def test_sending_mqtt_rgbww_command_with_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the sending of RGBWW command with template.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light_rgb/set", - "rgbww_command_topic": "test_light_rgb/rgbww/set", - "rgbww_command_template": '{{ "#%02x%02x%02x%02x%02x" | ' - "format(red, green, blue, cold_white, warm_white)}}", - "payload_on": "on", - "payload_off": "off", - "qos": 0, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1083,25 +1118,29 @@ async def test_sending_mqtt_rgbww_command_with_template( assert state.attributes["rgbww_color"] == (255, 128, 64, 32, 16) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light_color_temp/set", + "color_temp_command_topic": "test_light_color_temp/color_temp/set", + "color_temp_command_template": "{{ (1000 / value) | round(0) }}", + "payload_on": "on", + "payload_off": "off", + "qos": 0, + } + } + } + ], +) async def test_sending_mqtt_color_temp_command_with_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the sending of Color Temp command with template.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light_color_temp/set", - "color_temp_command_topic": "test_light_color_temp/color_temp/set", - "color_temp_command_template": "{{ (1000 / value) | round(0) }}", - "payload_on": "on", - "payload_off": "off", - "qos": 0, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1121,22 +1160,26 @@ async def test_sending_mqtt_color_temp_command_with_template( assert state.attributes["color_temp"] == 100 +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light/set", + "brightness_command_topic": "test_light/bright", + "on_command_type": "first", + } + } + } + ], +) async def test_on_command_first( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test on command being sent before brightness.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light/set", - "brightness_command_topic": "test_light/bright", - "on_command_type": "first", - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1159,21 +1202,25 @@ async def test_on_command_first( mqtt_mock.async_publish.assert_called_once_with("test_light/set", "OFF", 0, False) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light/set", + "brightness_command_topic": "test_light/bright", + } + } + } + ], +) async def test_on_command_last( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test on command being sent after brightness.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light/set", - "brightness_command_topic": "test_light/bright", - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1196,23 +1243,27 @@ async def test_on_command_last( mqtt_mock.async_publish.assert_called_once_with("test_light/set", "OFF", 0, False) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light/set", + "brightness_command_topic": "test_light/bright", + "rgb_command_topic": "test_light/rgb", + "on_command_type": "brightness", + } + } + } + ], +) async def test_on_command_brightness( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test on command being sent as only brightness.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light/set", - "brightness_command_topic": "test_light/bright", - "rgb_command_topic": "test_light/rgb", - "on_command_type": "brightness", - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1253,24 +1304,28 @@ async def test_on_command_brightness( ) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "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", + } + } + } + ], +) async def test_on_command_brightness_scaled( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test brightness scale.""" - config = { - light.DOMAIN: { - "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, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1325,21 +1380,25 @@ async def test_on_command_brightness_scaled( ) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light/set", + "rgb_command_topic": "test_light/rgb", + } + } + } + ], +) async def test_on_command_rgb( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test on command in RGB brightness mode.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light/set", - "rgb_command_topic": "test_light/rgb", - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1417,21 +1476,25 @@ async def test_on_command_rgb( mqtt_mock.async_publish.reset_mock() +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light/set", + "rgbw_command_topic": "test_light/rgbw", + } + } + } + ], +) async def test_on_command_rgbw( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test on command in RGBW brightness mode.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light/set", - "rgbw_command_topic": "test_light/rgbw", - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1509,21 +1572,25 @@ async def test_on_command_rgbw( mqtt_mock.async_publish.reset_mock() +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light/set", + "rgbww_command_topic": "test_light/rgbww", + } + } + } + ], +) async def test_on_command_rgbww( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test on command in RGBWW brightness mode.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light/set", - "rgbww_command_topic": "test_light/rgbww", - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1601,22 +1668,26 @@ async def test_on_command_rgbww( mqtt_mock.async_publish.reset_mock() +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light/set", + "rgb_command_topic": "test_light/rgb", + "rgb_command_template": "{{ red }}/{{ green }}/{{ blue }}", + } + } + } + ], +) async def test_on_command_rgb_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test on command in RGB brightness mode with RGB template.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light/set", - "rgb_command_topic": "test_light/rgb", - "rgb_command_template": "{{ red }}/{{ green }}/{{ blue }}", - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1640,22 +1711,26 @@ async def test_on_command_rgb_template( mqtt_mock.async_publish.assert_called_once_with("test_light/set", "OFF", 0, False) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light/set", + "rgbw_command_topic": "test_light/rgbw", + "rgbw_command_template": "{{ red }}/{{ green }}/{{ blue }}/{{ white }}", + } + } + } + ], +) async def test_on_command_rgbw_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test on command in RGBW brightness mode with RGBW template.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light/set", - "rgbw_command_topic": "test_light/rgbw", - "rgbw_command_template": "{{ red }}/{{ green }}/{{ blue }}/{{ white }}", - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1678,22 +1753,27 @@ async def test_on_command_rgbw_template( mqtt_mock.async_publish.assert_called_once_with("test_light/set", "OFF", 0, False) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light/set", + "rgbww_command_topic": "test_light/rgbww", + "rgbww_command_template": "{{ red }}/{{ green }}/{{ blue }}" + "/{{ cold_white }}/{{ warm_white }}", + } + } + } + ], +) async def test_on_command_rgbww_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test on command in RGBWW brightness mode with RGBWW template.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light/set", - "rgbww_command_topic": "test_light/rgbww", - "rgbww_command_template": "{{ red }}/{{ green }}/{{ blue }}/{{ cold_white }}/{{ warm_white }}", - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1717,34 +1797,39 @@ async def test_on_command_rgbww_template( mqtt_mock.async_publish.assert_called_once_with("test_light/set", "OFF", 0, False) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "tasmota_B94927/cmnd/POWER", + "state_value_template": "{{ value_json.POWER }}", + "payload_off": "OFF", + "payload_on": "ON", + "brightness_command_topic": "tasmota_B94927/cmnd/Dimmer", + "brightness_scale": 100, + "on_command_type": "brightness", + "brightness_value_template": "{{ value_json.Dimmer }}", + "rgb_command_topic": "tasmota_B94927/cmnd/Color2", + "rgb_value_template": "{{value_json.Color.split(',')[0:3]|join(',')}}", + "white_command_topic": "tasmota_B94927/cmnd/White", + "white_scale": 100, + "color_mode_value_template": "{% if value_json.White %} white {% else %} rgb {% endif %}", + "qos": "0", + } + } + } + ], +) async def test_on_command_white( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test sending commands for RGB + white light.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "tasmota_B94927/cmnd/POWER", - "state_value_template": "{{ value_json.POWER }}", - "payload_off": "OFF", - "payload_on": "ON", - "brightness_command_topic": "tasmota_B94927/cmnd/Dimmer", - "brightness_scale": 100, - "on_command_type": "brightness", - "brightness_value_template": "{{ value_json.Dimmer }}", - "rgb_command_topic": "tasmota_B94927/cmnd/Color2", - "rgb_value_template": "{{value_json.Color.split(',')[0:3]|join(',')}}", - "white_command_topic": "tasmota_B94927/cmnd/White", - "white_scale": 100, - "color_mode_value_template": "{% if value_json.White %} white {% else %} rgb {% endif %}", - "qos": "0", - } - } color_modes = ["rgb", "white"] - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1796,42 +1881,47 @@ async def test_on_command_white( ) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "state_topic": "test_light_rgb/status", + "command_topic": "test_light_rgb/set", + "color_mode_state_topic": "test_light_rgb/color_mode/status", + "brightness_state_topic": "test_light_rgb/brightness/status", + "brightness_command_topic": "test_light_rgb/brightness/set", + "rgb_state_topic": "test_light_rgb/rgb/status", + "rgb_command_topic": "test_light_rgb/rgb/set", + "rgbw_state_topic": "test_light_rgb/rgbw/status", + "rgbw_command_topic": "test_light_rgb/rgbw/set", + "rgbww_state_topic": "test_light_rgb/rgbww/status", + "rgbww_command_topic": "test_light_rgb/rgbww/set", + "color_temp_state_topic": "test_light_rgb/color_temp/status", + "color_temp_command_topic": "test_light_rgb/color_temp/set", + "effect_state_topic": "test_light_rgb/effect/status", + "effect_command_topic": "test_light_rgb/effect/set", + "hs_state_topic": "test_light_rgb/hs/status", + "hs_command_topic": "test_light_rgb/hs/set", + "xy_state_topic": "test_light_rgb/xy/status", + "xy_command_topic": "test_light_rgb/xy/set", + "qos": "0", + "payload_on": 1, + "payload_off": 0, + } + } + } + ], +) async def test_explicit_color_mode( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test explicit color mode over mqtt.""" - config = { - light.DOMAIN: { - "name": "test", - "state_topic": "test_light_rgb/status", - "command_topic": "test_light_rgb/set", - "color_mode_state_topic": "test_light_rgb/color_mode/status", - "brightness_state_topic": "test_light_rgb/brightness/status", - "brightness_command_topic": "test_light_rgb/brightness/set", - "rgb_state_topic": "test_light_rgb/rgb/status", - "rgb_command_topic": "test_light_rgb/rgb/set", - "rgbw_state_topic": "test_light_rgb/rgbw/status", - "rgbw_command_topic": "test_light_rgb/rgbw/set", - "rgbww_state_topic": "test_light_rgb/rgbww/status", - "rgbww_command_topic": "test_light_rgb/rgbww/set", - "color_temp_state_topic": "test_light_rgb/color_temp/status", - "color_temp_command_topic": "test_light_rgb/color_temp/set", - "effect_state_topic": "test_light_rgb/effect/status", - "effect_command_topic": "test_light_rgb/effect/set", - "hs_state_topic": "test_light_rgb/hs/status", - "hs_command_topic": "test_light_rgb/hs/set", - "xy_state_topic": "test_light_rgb/xy/status", - "xy_command_topic": "test_light_rgb/xy/set", - "qos": "0", - "payload_on": 1, - "payload_off": 0, - } - } color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"] - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1946,33 +2036,38 @@ async def test_explicit_color_mode( assert light_state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "state_topic": "test_light_rgb/status", + "command_topic": "test_light_rgb/set", + "color_mode_state_topic": "test_light_rgb/color_mode/status", + "color_mode_value_template": "{{ value_json.color_mode }}", + "brightness_state_topic": "test_light_rgb/brightness/status", + "brightness_command_topic": "test_light_rgb/brightness/set", + "color_temp_state_topic": "test_light_rgb/color_temp/status", + "color_temp_command_topic": "test_light_rgb/color_temp/set", + "hs_state_topic": "test_light_rgb/hs/status", + "hs_command_topic": "test_light_rgb/hs/set", + "qos": "0", + "payload_on": 1, + "payload_off": 0, + } + } + } + ], +) async def test_explicit_color_mode_templated( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test templated explicit color mode over mqtt.""" - config = { - light.DOMAIN: { - "name": "test", - "state_topic": "test_light_rgb/status", - "command_topic": "test_light_rgb/set", - "color_mode_state_topic": "test_light_rgb/color_mode/status", - "color_mode_value_template": "{{ value_json.color_mode }}", - "brightness_state_topic": "test_light_rgb/brightness/status", - "brightness_command_topic": "test_light_rgb/brightness/set", - "color_temp_state_topic": "test_light_rgb/color_temp/status", - "color_temp_command_topic": "test_light_rgb/color_temp/set", - "hs_state_topic": "test_light_rgb/hs/status", - "hs_command_topic": "test_light_rgb/hs/set", - "qos": "0", - "payload_on": 1, - "payload_off": 0, - } - } color_modes = ["color_temp", "hs"] - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -2029,38 +2124,45 @@ async def test_explicit_color_mode_templated( assert light_state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "state_topic": "tasmota_B94927/tele/STATE", + "command_topic": "tasmota_B94927/cmnd/POWER", + "state_value_template": "{{ value_json.POWER }}", + "payload_off": "OFF", + "payload_on": "ON", + "brightness_command_topic": "tasmota_B94927/cmnd/Dimmer", + "brightness_state_topic": "tasmota_B94927/tele/STATE", + "brightness_scale": 100, + "on_command_type": "brightness", + "brightness_value_template": "{{ value_json.Dimmer }}", + "rgb_command_topic": "tasmota_B94927/cmnd/Color2", + "rgb_state_topic": "tasmota_B94927/tele/STATE", + "rgb_value_template": "{{value_json.Color.split(',')" + "[0:3]|join(',')}}", + "white_command_topic": "tasmota_B94927/cmnd/White", + "white_scale": 100, + "color_mode_state_topic": "tasmota_B94927/tele/STATE", + "color_mode_value_template": "{% if value_json.White %} white " + "{% else %} rgb {% endif %}", + "qos": "0", + } + } + } + ], +) async def test_white_state_update( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test state updates for RGB + white light.""" - config = { - light.DOMAIN: { - "name": "test", - "state_topic": "tasmota_B94927/tele/STATE", - "command_topic": "tasmota_B94927/cmnd/POWER", - "state_value_template": "{{ value_json.POWER }}", - "payload_off": "OFF", - "payload_on": "ON", - "brightness_command_topic": "tasmota_B94927/cmnd/Dimmer", - "brightness_state_topic": "tasmota_B94927/tele/STATE", - "brightness_scale": 100, - "on_command_type": "brightness", - "brightness_value_template": "{{ value_json.Dimmer }}", - "rgb_command_topic": "tasmota_B94927/cmnd/Color2", - "rgb_state_topic": "tasmota_B94927/tele/STATE", - "rgb_value_template": "{{value_json.Color.split(',')[0:3]|join(',')}}", - "white_command_topic": "tasmota_B94927/cmnd/White", - "white_scale": 100, - "color_mode_state_topic": "tasmota_B94927/tele/STATE", - "color_mode_value_template": "{% if value_json.White %} white {% else %} rgb {% endif %}", - "qos": "0", - } - } color_modes = ["rgb", "white"] - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -2095,22 +2197,26 @@ async def test_white_state_update( assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light/set", + "effect_command_topic": "test_light/effect/set", + "effect_list": ["rainbow", "colorloop"], + } + } + } + ], +) async def test_effect( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test effect.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light/set", - "effect_command_topic": "test_light/effect/set", - "effect_list": ["rainbow", "colorloop"], - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -2893,22 +2999,26 @@ async def test_entity_debug_info_message( ) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_max_mireds/set", + "color_temp_command_topic": "test_max_mireds/color_temp/set", + "max_mireds": 370, + } + } + } + ], +) async def test_max_mireds( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test setting min_mireds and max_mireds.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_max_mireds/set", - "color_temp_command_topic": "test_max_mireds/color_temp/set", - "max_mireds": 370, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.attributes.get("min_mireds") == 153 @@ -3145,25 +3255,29 @@ async def test_encoding_subscribable_topics_brightness( ) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light_brightness/set", + "brightness_command_topic": "test_light_brightness/brightness/set", + "brightness_command_template": "{{ (1000 / value) | round(0) }}", + "payload_on": "on", + "payload_off": "off", + "qos": 0, + } + } + } + ], +) async def test_sending_mqtt_brightness_command_with_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the sending of Brightness command with template.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light_brightness/set", - "brightness_command_topic": "test_light_brightness/brightness/set", - "brightness_command_template": "{{ (1000 / value) | round(0) }}", - "payload_on": "on", - "payload_off": "off", - "qos": 0, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -3183,27 +3297,31 @@ async def test_sending_mqtt_brightness_command_with_template( assert state.attributes["brightness"] == 100 +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light_brightness/set", + "brightness_command_topic": "test_light_brightness/brightness/set", + "effect_command_topic": "test_light_brightness/effect/set", + "effect_command_template": '{ "effect": "{{ value }}" }', + "effect_list": ["colorloop", "random"], + "payload_on": "on", + "payload_off": "off", + "qos": 0, + } + } + } + ], +) async def test_sending_mqtt_effect_command_with_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the sending of Effect command with template.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light_brightness/set", - "brightness_command_topic": "test_light_brightness/brightness/set", - "effect_command_topic": "test_light_brightness/effect/set", - "effect_command_template": '{ "effect": "{{ value }}" }', - "effect_list": ["colorloop", "random"], - "payload_on": "on", - "payload_off": "off", - "qos": 0, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -3227,23 +3345,27 @@ async def test_sending_mqtt_effect_command_with_template( assert state.attributes.get("effect") == "colorloop" +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light_hs/set", + "hs_command_topic": "test_light_hs/hs_color/set", + "hs_command_template": '{"hue": {{ hue | int }}, "sat": {{ sat | int}}}', + "qos": 0, + } + } + } + ], +) async def test_sending_mqtt_hs_command_with_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the sending of HS Color command with template.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light_hs/set", - "hs_command_topic": "test_light_hs/hs_color/set", - "hs_command_template": '{"hue": {{ hue | int }}, "sat": {{ sat | int}}}', - "qos": 0, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -3263,23 +3385,30 @@ async def test_sending_mqtt_hs_command_with_template( assert state.attributes["hs_color"] == (30, 100) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "command_topic": "test_light_xy/set", + "xy_command_topic": "test_light_xy/xy_color/set", + "xy_command_template": "{" + '"Color": "{{ (x * 65536) | round | int }},' + '{{ (y * 65536) | round | int }}"' + "}", + "qos": 0, + } + } + } + ], +) async def test_sending_mqtt_xy_command_with_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the sending of XY Color command with template.""" - config = { - light.DOMAIN: { - "name": "test", - "command_topic": "test_light_xy/set", - "xy_command_topic": "test_light_xy/xy_color/set", - "xy_command_template": '{"Color": "{{ (x * 65536) | round | int }},{{ (y * 65536) | round | int }}"}', - "qos": 0, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index db39cea7844..1b24f7636f5 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -98,10 +98,10 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant, State -from homeassistant.setup import async_setup_component from homeassistant.util.json import JsonValueType, json_loads from .test_common import ( + help_custom_config, help_test_availability_when_connection_lost, help_test_availability_without_topic, help_test_custom_availability_payload, @@ -144,6 +144,30 @@ DEFAULT_CONFIG = { } +COLOR_MODES_CONFIG = { + mqtt.DOMAIN: { + light.DOMAIN: { + "brightness": True, + "color_mode": True, + "effect": True, + "command_topic": "test_light_rgb/set", + "name": "test", + "schema": "json", + "supported_color_modes": [ + "color_temp", + "hs", + "rgb", + "rgbw", + "rgbww", + "white", + "xy", + ], + "qos": 0, + } + } +} + + @pytest.fixture(autouse=True) def light_platform_only(): """Only setup the light platform to speed up tests.""" @@ -163,44 +187,40 @@ class JsonValidator: return json_loads(self.jsondata) == json_loads(other) +@pytest.mark.parametrize( + "hass_config", [{mqtt.DOMAIN: {light.DOMAIN: {"schema": "json", "name": "test"}}}] +) async def test_fail_setup_if_no_command_topic( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, ) -> None: """Test if setup fails with no command topic.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, - {mqtt.DOMAIN: {light.DOMAIN: {"schema": "json", "name": "test"}}}, - ) + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert ( "Invalid config for [mqtt]: required key not provided @ data['mqtt']['light'][0]['command_topic']. Got None." in caplog.text ) -@pytest.mark.parametrize("deprecated", ("color_temp", "hs", "rgb", "xy")) +@pytest.mark.parametrize( + "hass_config", + [ + help_custom_config(light.DOMAIN, COLOR_MODES_CONFIG, ({"color_temp": True},)), + help_custom_config(light.DOMAIN, COLOR_MODES_CONFIG, ({"hs": True},)), + help_custom_config(light.DOMAIN, COLOR_MODES_CONFIG, ({"rgb": True},)), + help_custom_config(light.DOMAIN, COLOR_MODES_CONFIG, ({"xy": True},)), + ], +) async def test_fail_setup_if_color_mode_deprecated( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, deprecated + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, ) -> None: """Test if setup fails if color mode is combined with deprecated config keys.""" - supported_color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"] - - config = { - light.DOMAIN: { - "brightness": True, - "color_mode": True, - "command_topic": "test_light_rgb/set", - "name": "test", - "schema": "json", - "supported_color_modes": supported_color_modes, - } - } - config[light.DOMAIN][deprecated] = True - assert not await async_setup_component( - hass, - mqtt.DOMAIN, - {mqtt.DOMAIN: config}, - ) + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert ( "Invalid config for [mqtt]: color_mode must not be combined with any of" in caplog.text @@ -208,42 +228,49 @@ async def test_fail_setup_if_color_mode_deprecated( @pytest.mark.parametrize( - ("supported_color_modes", "error"), + ("hass_config", "error"), [ - (["onoff", "rgb"], "Unknown error calling mqtt CONFIG_SCHEMA"), - (["brightness", "rgb"], "Unknown error calling mqtt CONFIG_SCHEMA"), - (["unknown"], "Invalid config for [mqtt]: value must be one of [ None: """Test if setup fails if supported color modes is invalid.""" - config = { - light.DOMAIN: { - "brightness": True, - "color_mode": True, - "command_topic": "test_light_rgb/set", - "name": "test", - "schema": "json", - "supported_color_modes": supported_color_modes, - } - } - assert not await async_setup_component( - hass, - mqtt.DOMAIN, - {mqtt.DOMAIN: config}, - ) + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() assert error in caplog.text -async def test_legacy_rgb_light( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test legacy RGB light flags expected features and color modes.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -253,10 +280,14 @@ async def test_legacy_rgb_light( "rgb": True, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_legacy_rgb_light( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test legacy RGB light flags expected features and color modes.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") color_modes = [light.ColorMode.HS] @@ -265,13 +296,9 @@ async def test_legacy_rgb_light( assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features -async def test_no_color_brightness_color_temp_if_no_topics( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test for no RGB, brightness, color temp, effector XY.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -281,10 +308,14 @@ async def test_no_color_brightness_color_temp_if_no_topics( "command_topic": "test_light_rgb/set", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_no_color_brightness_color_temp_if_no_topics( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test for no RGB, brightness, color temp, effector XY.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -319,13 +350,9 @@ async def test_no_color_brightness_color_temp_if_no_topics( assert state.state == STATE_UNKNOWN -async def test_controlling_state_via_topic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the controlling of the state via topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -342,10 +369,14 @@ async def test_controlling_state_via_topic( "qos": "0", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_controlling_state_via_topic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the controlling of the state via topic.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -463,35 +494,22 @@ async def test_controlling_state_via_topic( assert light_state.attributes.get("effect") == "colorloop" +@pytest.mark.parametrize( + "hass_config", + [ + help_custom_config( + light.DOMAIN, COLOR_MODES_CONFIG, ({"state_topic": "test_light_rgb"},) + ) + ], +) async def test_controlling_state_via_topic2( hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, caplog: pytest.LogCaptureFixture, ) -> None: """Test the controlling of the state via topic for a light supporting color mode.""" supported_color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "white", "xy"] - - assert await async_setup_component( - hass, - mqtt.DOMAIN, - { - mqtt.DOMAIN: { - light.DOMAIN: { - "brightness": True, - "color_mode": True, - "command_topic": "test_light_rgb/set", - "effect": True, - "name": "test", - "qos": "0", - "schema": "json", - "state_topic": "test_light_rgb", - "supported_color_modes": supported_color_modes, - } - } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -641,25 +659,9 @@ async def test_controlling_state_via_topic2( ) -async def test_sending_mqtt_commands_and_optimistic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the sending of command in optimistic mode.""" - fake_state = State( - "light.test", - "on", - { - "brightness": 95, - "hs_color": [100, 100], - "effect": "random", - "color_temp": 100, - }, - ) - mock_restore_cache(hass, (fake_state,)) - - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -675,10 +677,26 @@ async def test_sending_mqtt_commands_and_optimistic( "qos": 2, } } + } + ], +) +async def test_sending_mqtt_commands_and_optimistic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the sending of command in optimistic mode.""" + fake_state = State( + "light.test", + "on", + { + "brightness": 95, + "hs_color": [100, 100], + "effect": "random", + "color_temp": 100, }, ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mock_restore_cache(hass, (fake_state,)) + + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_ON @@ -788,8 +806,35 @@ async def test_sending_mqtt_commands_and_optimistic( assert state.attributes["xy_color"] == (0.611, 0.375) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "brightness": True, + "color_mode": True, + "command_topic": "test_light_rgb/set", + "effect": True, + "name": "test", + "qos": 2, + "schema": "json", + "supported_color_modes": [ + "color_temp", + "hs", + "rgb", + "rgbw", + "rgbww", + "white", + "xy", + ], + } + } + } + ], +) async def test_sending_mqtt_commands_and_optimistic2( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test the sending of command in optimistic mode for a light supporting color mode.""" supported_color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "white", "xy"] @@ -806,26 +851,7 @@ async def test_sending_mqtt_commands_and_optimistic2( ) mock_restore_cache(hass, (fake_state,)) - assert await async_setup_component( - hass, - mqtt.DOMAIN, - { - mqtt.DOMAIN: { - light.DOMAIN: { - "brightness": True, - "color_mode": True, - "command_topic": "test_light_rgb/set", - "effect": True, - "name": "test", - "qos": 2, - "schema": "json", - "supported_color_modes": supported_color_modes, - } - } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_ON @@ -1020,13 +1046,9 @@ async def test_sending_mqtt_commands_and_optimistic2( mqtt_mock.async_publish.reset_mock() -async def test_sending_hs_color( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test light.turn_on with hs color sends hs color parameters.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1037,10 +1059,14 @@ async def test_sending_hs_color( "hs": True, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_hs_color( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test light.turn_on with hs color sends hs color parameters.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1083,13 +1109,9 @@ async def test_sending_hs_color( ) -async def test_sending_rgb_color_no_brightness( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test light.turn_on with hs color sends rgb color parameters.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1099,10 +1121,15 @@ async def test_sending_rgb_color_no_brightness( "rgb": True, } } - }, - ) + } + ], +) +async def test_sending_rgb_color_no_brightness( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test light.turn_on with hs color sends rgb color parameters.""" await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1140,14 +1167,9 @@ async def test_sending_rgb_color_no_brightness( ) -async def test_sending_rgb_color_no_brightness2( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test light.turn_on with hs color sends rgb color parameters.""" - supported_color_modes = ["rgb", "rgbw", "rgbww"] - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1155,13 +1177,17 @@ async def test_sending_rgb_color_no_brightness2( "command_topic": "test_light_rgb/set", "name": "test", "schema": "json", - "supported_color_modes": supported_color_modes, + "supported_color_modes": ["rgb", "rgbw", "rgbww"], } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_rgb_color_no_brightness2( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test light.turn_on with hs color sends rgb color parameters.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1221,13 +1247,9 @@ async def test_sending_rgb_color_no_brightness2( ) -async def test_sending_rgb_color_with_brightness( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test light.turn_on with hs color sends rgb color parameters.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1238,10 +1260,14 @@ async def test_sending_rgb_color_with_brightness( "rgb": True, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_rgb_color_with_brightness( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test light.turn_on with hs color sends rgb color parameters.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1289,13 +1315,9 @@ async def test_sending_rgb_color_with_brightness( ) -async def test_sending_rgb_color_with_scaled_brightness( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test light.turn_on with hs color sends rgb color parameters.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1307,10 +1329,15 @@ async def test_sending_rgb_color_with_scaled_brightness( "rgb": True, } } - }, - ) + } + ], +) +async def test_sending_rgb_color_with_scaled_brightness( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test light.turn_on with hs color sends rgb color parameters.""" await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1358,13 +1385,9 @@ async def test_sending_rgb_color_with_scaled_brightness( ) -async def test_sending_scaled_white( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test light.turn_on with scaled white.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1378,10 +1401,14 @@ async def test_sending_scaled_white( "white_scale": 50, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_scaled_white( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test light.turn_on with scaled white.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1405,13 +1432,9 @@ async def test_sending_scaled_white( mqtt_mock.async_publish.reset_mock() -async def test_sending_xy_color( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test light.turn_on with hs color sends xy color parameters.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1422,10 +1445,14 @@ async def test_sending_xy_color( "xy": True, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_xy_color( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test light.turn_on with hs color sends xy color parameters.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1467,13 +1494,9 @@ async def test_sending_xy_color( ) -async def test_effect( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test for effect being sent when included.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1484,10 +1507,14 @@ async def test_effect( "qos": 0, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_effect( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test for effect being sent when included.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1533,13 +1560,9 @@ async def test_effect( assert state.attributes.get("effect") == "colorloop" -async def test_flash_short_and_long( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test for flash length being sent when included.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1551,10 +1574,14 @@ async def test_flash_short_and_long( "qos": 0, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_flash_short_and_long( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test for flash length being sent when included.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1598,13 +1625,9 @@ async def test_flash_short_and_long( assert state.state == STATE_OFF -async def test_transition( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test for transition time being sent when included.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1614,10 +1637,14 @@ async def test_transition( "qos": 0, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_transition( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test for transition time being sent when included.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1648,13 +1675,9 @@ async def test_transition( assert state.state == STATE_OFF -async def test_brightness_scale( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test for brightness scaling.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1666,10 +1689,14 @@ async def test_brightness_scale( "brightness_scale": 99, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_brightness_scale( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test for brightness scaling.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1693,13 +1720,9 @@ async def test_brightness_scale( assert state.attributes.get("brightness") == 255 -async def test_white_scale( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test for white scaling.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1714,10 +1737,14 @@ async def test_white_scale( "white_scale": 50, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_white_scale( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test for white scaling.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1754,13 +1781,9 @@ async def test_white_scale( assert state.attributes.get("brightness") == 128 -async def test_invalid_values( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test that invalid color/brightness/etc. values are ignored.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -1774,10 +1797,14 @@ async def test_invalid_values( "qos": "0", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_invalid_values( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test that invalid color/brightness/etc. values are ignored.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -2203,23 +2230,27 @@ async def test_entity_debug_info_message( ) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "schema": "json", + "name": "test", + "command_topic": "test_max_mireds/set", + "color_temp": True, + "max_mireds": 370, + } + } + } + ], +) async def test_max_mireds( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test setting min_mireds and max_mireds.""" - config = { - light.DOMAIN: { - "schema": "json", - "name": "test", - "command_topic": "test_max_mireds/set", - "color_temp": True, - "max_mireds": 370, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.attributes.get("min_mireds") == 153 diff --git a/tests/components/mqtt/test_light_template.py b/tests/components/mqtt/test_light_template.py index c8018d4847f..166687b8595 100644 --- a/tests/components/mqtt/test_light_template.py +++ b/tests/components/mqtt/test_light_template.py @@ -44,7 +44,6 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant, State -from homeassistant.setup import async_setup_component from .test_common import ( help_test_availability_when_connection_lost, @@ -100,53 +99,60 @@ def light_platform_only(): @pytest.mark.parametrize( - "test_config", + "hass_config", [ - ({"schema": "template", "name": "test"},), + ({mqtt.DOMAIN: {light.DOMAIN: {"schema": "template", "name": "test"}}},), ( { - "schema": "template", - "name": "test", - "command_topic": "test_topic", + mqtt.DOMAIN: { + light.DOMAIN: { + "schema": "template", + "name": "test", + "command_topic": "test_topic", + } + } }, ), ( { - "schema": "template", - "name": "test", - "command_topic": "test_topic", - "command_on_template": "on", + mqtt.DOMAIN: { + light.DOMAIN: { + "schema": "template", + "name": "test", + "command_topic": "test_topic", + "command_on_template": "on", + } + } }, ), ( { - "schema": "template", - "name": "test", - "command_topic": "test_topic", - "command_off_template": "off", + mqtt.DOMAIN: { + light.DOMAIN: { + "schema": "template", + "name": "test", + "command_topic": "test_topic", + "command_off_template": "off", + } + } }, ), ], ) async def test_setup_fails( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, test_config + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, ) -> None: """Test that setup fails with missing required configuration items.""" - assert not await async_setup_component( - hass, - mqtt.DOMAIN, - {mqtt.DOMAIN: {light.DOMAIN: test_config}}, - ) - assert "Invalid config for [mqtt]" in caplog.text + with pytest.raises(AssertionError): + await mqtt_mock_entry_no_yaml_config() + assert "Invalid config" in caplog.text -async def test_rgb_light( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test RGB light flags brightness support.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -160,10 +166,14 @@ async def test_rgb_light( "blue_template": '{{ value.split(",")[4].' 'split("-")[2] }}', } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_rgb_light( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test RGB light flags brightness support.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -173,13 +183,9 @@ async def test_rgb_light( assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features -async def test_state_change_via_topic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test state change via topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -197,10 +203,14 @@ async def test_state_change_via_topic( "state_template": '{{ value.split(",")[0] }}', } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_state_change_via_topic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test state change via topic.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -228,13 +238,9 @@ async def test_state_change_via_topic( assert state.state == STATE_UNKNOWN -async def test_state_brightness_color_effect_temp_change_via_topic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test state, bri, color, effect, color temp change.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -254,16 +260,20 @@ async def test_state_brightness_color_effect_temp_change_via_topic( "state_template": '{{ value.split(",")[0] }}', "brightness_template": '{{ value.split(",")[1] }}', "color_temp_template": '{{ value.split(",")[2] }}', - "red_template": '{{ value.split(",")[3].' 'split("-")[0] }}', - "green_template": '{{ value.split(",")[3].' 'split("-")[1] }}', - "blue_template": '{{ value.split(",")[3].' 'split("-")[2] }}', + "red_template": '{{ value.split(",")[3].split("-")[0] }}', + "green_template": '{{ value.split(",")[3].split("-")[1] }}', + "blue_template": '{{ value.split(",")[3].split("-")[2] }}', "effect_template": '{{ value.split(",")[4] }}', } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_state_brightness_color_effect_temp_change_via_topic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test state, bri, color, effect, color temp change.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -339,25 +349,9 @@ async def test_state_brightness_color_effect_temp_change_via_topic( assert light_state.attributes.get("effect") == "rainbow" -async def test_sending_mqtt_commands_and_optimistic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the sending of command in optimistic mode.""" - fake_state = State( - "light.test", - "on", - { - "brightness": 95, - "hs_color": [100, 100], - "effect": "random", - "color_temp": 100, - }, - ) - mock_restore_cache(hass, (fake_state,)) - - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -384,10 +378,26 @@ async def test_sending_mqtt_commands_and_optimistic( "qos": 2, } } + } + ], +) +async def test_sending_mqtt_commands_and_optimistic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the sending of command in optimistic mode.""" + fake_state = State( + "light.test", + "on", + { + "brightness": 95, + "hs_color": [100, 100], + "effect": "random", + "color_temp": 100, }, ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mock_restore_cache(hass, (fake_state,)) + + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_ON @@ -481,13 +491,9 @@ async def test_sending_mqtt_commands_and_optimistic( assert state.attributes.get("rgb_color") == (0, 255, 127) -async def test_sending_mqtt_commands_non_optimistic_brightness_template( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the sending of command in optimistic mode.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -508,16 +514,20 @@ async def test_sending_mqtt_commands_non_optimistic_brightness_template( "state_template": '{{ value.split(",")[0] }}', "brightness_template": '{{ value.split(",")[1] }}', "color_temp_template": '{{ value.split(",")[2] }}', - "red_template": '{{ value.split(",")[3].' 'split("-")[0] }}', - "green_template": '{{ value.split(",")[3].' 'split("-")[1] }}', - "blue_template": '{{ value.split(",")[3].' 'split("-")[2] }}', + "red_template": '{{ value.split(",")[3].split("-")[0] }}', + "green_template": '{{ value.split(",")[3].split("-")[1] }}', + "blue_template": '{{ value.split(",")[3].split("-")[2] }}', "effect_template": '{{ value.split(",")[4] }}', } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_sending_mqtt_commands_non_optimistic_brightness_template( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the sending of command in optimistic mode.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -604,13 +614,9 @@ async def test_sending_mqtt_commands_non_optimistic_brightness_template( state = hass.states.get("light.test") -async def test_effect( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test effect sent over MQTT in optimistic mode.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -623,10 +629,14 @@ async def test_effect( "qos": 0, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_effect( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test effect sent over MQTT in optimistic mode.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -659,13 +669,9 @@ async def test_effect( assert state.attributes.get("effect") == "colorloop" -async def test_flash( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test flash sent over MQTT in optimistic mode.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -677,10 +683,14 @@ async def test_flash( "qos": 0, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_flash( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test flash sent over MQTT in optimistic mode.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -710,13 +720,9 @@ async def test_flash( assert state.state == STATE_ON -async def test_transition( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test for transition time being sent when included.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -728,10 +734,14 @@ async def test_transition( "qos": 1, } } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_transition( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test for transition time being sent when included.""" + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -754,13 +764,9 @@ async def test_transition( assert state.state == STATE_OFF -async def test_invalid_values( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test that invalid values are ignored.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { light.DOMAIN: { @@ -780,16 +786,21 @@ async def test_invalid_values( "state_template": '{{ value.split(",")[0] }}', "brightness_template": '{{ value.split(",")[1] }}', "color_temp_template": '{{ value.split(",")[2] }}', - "red_template": '{{ value.split(",")[3].' 'split("-")[0] }}', - "green_template": '{{ value.split(",")[3].' 'split("-")[1] }}', - "blue_template": '{{ value.split(",")[3].' 'split("-")[2] }}', + "red_template": '{{ value.split(",")[3].split("-")[0] }}', + "green_template": '{{ value.split(",")[3].split("-")[1] }}', + "blue_template": '{{ value.split(",")[3].split("-")[2] }}', "effect_template": '{{ value.split(",")[4] }}', } } - }, - ) + } + ], +) +async def test_invalid_values( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test that invalid values are ignored.""" await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -1173,25 +1184,29 @@ async def test_entity_debug_info_message( ) +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + light.DOMAIN: { + "schema": "template", + "name": "test", + "command_topic": "test_max_mireds/set", + "command_on_template": "on", + "command_off_template": "off", + "color_temp_template": "{{ value }}", + "max_mireds": 370, + } + } + } + ], +) async def test_max_mireds( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test setting min_mireds and max_mireds.""" - config = { - light.DOMAIN: { - "schema": "template", - "name": "test", - "command_topic": "test_max_mireds/set", - "command_on_template": "on", - "command_off_template": "off", - "color_temp_template": "{{ value }}", - "max_mireds": 370, - } - } - - assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("light.test") assert state.attributes.get("min_mireds") == 153