diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index e916276cb4a..b34406f42bc 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -2,169 +2,170 @@ Configuration for RGB Version with brightness: -light: - platform: mqtt - name: "Office Light RGB" - state_topic: "office/rgb1/light/status" - command_topic: "office/rgb1/light/switch" - brightness_state_topic: "office/rgb1/brightness/status" - brightness_command_topic: "office/rgb1/brightness/set" - rgb_state_topic: "office/rgb1/rgb/status" - rgb_command_topic: "office/rgb1/rgb/set" - qos: 0 - payload_on: "on" - payload_off: "off" +mqtt: + light: + - name: "Office Light RGB" + state_topic: "office/rgb1/light/status" + command_topic: "office/rgb1/light/switch" + brightness_state_topic: "office/rgb1/brightness/status" + brightness_command_topic: "office/rgb1/brightness/set" + rgb_state_topic: "office/rgb1/rgb/status" + rgb_command_topic: "office/rgb1/rgb/set" + qos: 0 + payload_on: "on" + payload_off: "off" Configuration for XY Version with brightness: -light: - platform: mqtt - name: "Office Light XY" - state_topic: "office/xy1/light/status" - command_topic: "office/xy1/light/switch" - brightness_state_topic: "office/xy1/brightness/status" - brightness_command_topic: "office/xy1/brightness/set" - xy_state_topic: "office/xy1/xy/status" - xy_command_topic: "office/xy1/xy/set" - qos: 0 - payload_on: "on" - payload_off: "off" +mqtt: + light: + - platform: mqtt + name: "Office Light XY" + state_topic: "office/xy1/light/status" + command_topic: "office/xy1/light/switch" + brightness_state_topic: "office/xy1/brightness/status" + brightness_command_topic: "office/xy1/brightness/set" + xy_state_topic: "office/xy1/xy/status" + xy_command_topic: "office/xy1/xy/set" + qos: 0 + payload_on: "on" + payload_off: "off" config without RGB: -light: - platform: mqtt - name: "Office Light" - state_topic: "office/rgb1/light/status" - command_topic: "office/rgb1/light/switch" - brightness_state_topic: "office/rgb1/brightness/status" - brightness_command_topic: "office/rgb1/brightness/set" - qos: 0 - payload_on: "on" - payload_off: "off" +mqtt: + light: + - name: "Office Light" + state_topic: "office/rgb1/light/status" + command_topic: "office/rgb1/light/switch" + brightness_state_topic: "office/rgb1/brightness/status" + brightness_command_topic: "office/rgb1/brightness/set" + qos: 0 + payload_on: "on" + payload_off: "off" config without RGB and brightness: -light: - platform: mqtt - name: "Office Light" - state_topic: "office/rgb1/light/status" - command_topic: "office/rgb1/light/switch" - qos: 0 - payload_on: "on" - payload_off: "off" +mqtt: + light: + - name: "Office Light" + state_topic: "office/rgb1/light/status" + command_topic: "office/rgb1/light/switch" + qos: 0 + payload_on: "on" + payload_off: "off" config for RGB Version with brightness and scale: -light: - platform: mqtt - name: "Office Light RGB" - state_topic: "office/rgb1/light/status" - command_topic: "office/rgb1/light/switch" - brightness_state_topic: "office/rgb1/brightness/status" - brightness_command_topic: "office/rgb1/brightness/set" - brightness_scale: 99 - rgb_state_topic: "office/rgb1/rgb/status" - rgb_command_topic: "office/rgb1/rgb/set" - rgb_scale: 99 - qos: 0 - payload_on: "on" - payload_off: "off" +mqtt: + light: + - name: "Office Light RGB" + state_topic: "office/rgb1/light/status" + command_topic: "office/rgb1/light/switch" + brightness_state_topic: "office/rgb1/brightness/status" + brightness_command_topic: "office/rgb1/brightness/set" + brightness_scale: 99 + rgb_state_topic: "office/rgb1/rgb/status" + rgb_command_topic: "office/rgb1/rgb/set" + rgb_scale: 99 + qos: 0 + payload_on: "on" + payload_off: "off" config with brightness and color temp -light: - platform: mqtt - name: "Office Light Color Temp" - state_topic: "office/rgb1/light/status" - command_topic: "office/rgb1/light/switch" - brightness_state_topic: "office/rgb1/brightness/status" - brightness_command_topic: "office/rgb1/brightness/set" - brightness_scale: 99 - color_temp_state_topic: "office/rgb1/color_temp/status" - color_temp_command_topic: "office/rgb1/color_temp/set" - qos: 0 - payload_on: "on" - payload_off: "off" +mqtt: + light: + - name: "Office Light Color Temp" + state_topic: "office/rgb1/light/status" + command_topic: "office/rgb1/light/switch" + brightness_state_topic: "office/rgb1/brightness/status" + brightness_command_topic: "office/rgb1/brightness/set" + brightness_scale: 99 + color_temp_state_topic: "office/rgb1/color_temp/status" + color_temp_command_topic: "office/rgb1/color_temp/set" + qos: 0 + payload_on: "on" + payload_off: "off" config with brightness and effect -light: - platform: mqtt - name: "Office Light Color Temp" - state_topic: "office/rgb1/light/status" - command_topic: "office/rgb1/light/switch" - brightness_state_topic: "office/rgb1/brightness/status" - brightness_command_topic: "office/rgb1/brightness/set" - brightness_scale: 99 - effect_state_topic: "office/rgb1/effect/status" - effect_command_topic: "office/rgb1/effect/set" - effect_list: - - rainbow - - colorloop - qos: 0 - payload_on: "on" - payload_off: "off" +mqtt: + light: + - name: "Office Light Color Temp" + state_topic: "office/rgb1/light/status" + command_topic: "office/rgb1/light/switch" + brightness_state_topic: "office/rgb1/brightness/status" + brightness_command_topic: "office/rgb1/brightness/set" + brightness_scale: 99 + effect_state_topic: "office/rgb1/effect/status" + effect_command_topic: "office/rgb1/effect/set" + effect_list: + - rainbow + - colorloop + qos: 0 + payload_on: "on" + payload_off: "off" config for RGB Version with RGB command template: -light: - platform: mqtt - name: "Office Light RGB" - state_topic: "office/rgb1/light/status" - command_topic: "office/rgb1/light/switch" - rgb_state_topic: "office/rgb1/rgb/status" - rgb_command_topic: "office/rgb1/rgb/set" - rgb_command_template: "{{ '#%02x%02x%02x' | format(red, green, blue)}}" - qos: 0 - payload_on: "on" - payload_off: "off" +mqtt: + light: + - name: "Office Light RGB" + state_topic: "office/rgb1/light/status" + command_topic: "office/rgb1/light/switch" + rgb_state_topic: "office/rgb1/rgb/status" + rgb_command_topic: "office/rgb1/rgb/set" + rgb_command_template: "{{ '#%02x%02x%02x' | format(red, green, blue)}}" + qos: 0 + payload_on: "on" + payload_off: "off" Configuration for HS Version with brightness: -light: - platform: mqtt - name: "Office Light HS" - state_topic: "office/hs1/light/status" - command_topic: "office/hs1/light/switch" - brightness_state_topic: "office/hs1/brightness/status" - brightness_command_topic: "office/hs1/brightness/set" - hs_state_topic: "office/hs1/hs/status" - hs_command_topic: "office/hs1/hs/set" - qos: 0 - payload_on: "on" - payload_off: "off" +mqtt: + light: + - name: "Office Light HS" + state_topic: "office/hs1/light/status" + command_topic: "office/hs1/light/switch" + brightness_state_topic: "office/hs1/brightness/status" + brightness_command_topic: "office/hs1/brightness/set" + hs_state_topic: "office/hs1/hs/status" + hs_command_topic: "office/hs1/hs/set" + qos: 0 + payload_on: "on" + payload_off: "off" Configuration with brightness command template: -light: - platform: mqtt - name: "Office Light" - state_topic: "office/rgb1/light/status" - command_topic: "office/rgb1/light/switch" - brightness_state_topic: "office/rgb1/brightness/status" - brightness_command_topic: "office/rgb1/brightness/set" - brightness_command_template: '{ "brightness": "{{ value }}" }' - qos: 0 - payload_on: "on" - payload_off: "off" +mqtt: + light: + - name: "Office Light" + state_topic: "office/rgb1/light/status" + command_topic: "office/rgb1/light/switch" + brightness_state_topic: "office/rgb1/brightness/status" + brightness_command_topic: "office/rgb1/brightness/set" + brightness_command_template: '{ "brightness": "{{ value }}" }' + qos: 0 + payload_on: "on" + payload_off: "off" Configuration with effect command template: -light: - platform: mqtt - name: "Office Light Color Temp" - state_topic: "office/rgb1/light/status" - command_topic: "office/rgb1/light/switch" - effect_state_topic: "office/rgb1/effect/status" - effect_command_topic: "office/rgb1/effect/set" - effect_command_template: '{ "effect": "{{ value }}" }' - effect_list: - - rainbow - - colorloop - qos: 0 - payload_on: "on" - payload_off: "off" +mqtt: + light: + - name: "Office Light Color Temp" + state_topic: "office/rgb1/light/status" + command_topic: "office/rgb1/light/switch" + effect_state_topic: "office/rgb1/effect/status" + effect_command_topic: "office/rgb1/effect/set" + effect_command_template: '{ "effect": "{{ value }}" }' + effect_list: + - rainbow + - colorloop + qos: 0 + payload_on: "on" + payload_off: "off" """ import copy @@ -172,7 +173,7 @@ from unittest.mock import call, patch import pytest -from homeassistant.components import light +from homeassistant.components import light, mqtt from homeassistant.components.mqtt.light.schema_basic import ( CONF_BRIGHTNESS_COMMAND_TOPIC, CONF_COLOR_TEMP_COMMAND_TOPIC, @@ -226,17 +227,18 @@ from .test_common import ( help_test_update_with_json_attrs_not_dict, ) -from tests.common import ( - assert_setup_component, - async_fire_mqtt_message, - mock_restore_cache, -) +from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.components.light import common DEFAULT_CONFIG = { - light.DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"} + mqtt.DOMAIN: {light.DOMAIN: {"name": "test", "command_topic": "test-topic"}} } +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) +DEFAULT_CONFIG_LEGACY[light.DOMAIN]["platform"] = mqtt.DOMAIN + @pytest.fixture(autouse=True) def light_platform_only(): @@ -245,14 +247,15 @@ def light_platform_only(): yield -async def test_fail_setup_if_no_command_topic(hass, mqtt_mock_entry_no_yaml_config): +async def test_fail_setup_if_no_command_topic(hass, caplog): """Test if command fails with command topic.""" - assert await async_setup_component( - hass, light.DOMAIN, {light.DOMAIN: {"platform": "mqtt", "name": "test"}} + assert not await async_setup_component( + hass, mqtt.DOMAIN, {mqtt.DOMAIN: {light.DOMAIN: {"name": "test"}}} + ) + assert ( + "Invalid config for [mqtt]: required key not provided @ data['mqtt']['light'][0]['command_topic']. Got None." + in caplog.text ) - await hass.async_block_till_done() - await mqtt_mock_entry_no_yaml_config() - assert hass.states.get("light.test") is None async def test_no_color_brightness_color_temp_hs_white_xy_if_no_topics( @@ -261,13 +264,14 @@ async def test_no_color_brightness_color_temp_hs_white_xy_if_no_topics( """Test if there is no color and brightness if no topic.""" assert await async_setup_component( hass, - light.DOMAIN, + mqtt.DOMAIN, { - light.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test_light_rgb/status", - "command_topic": "test_light_rgb/set", + mqtt.DOMAIN: { + light.DOMAIN: { + "name": "test", + "state_topic": "test_light_rgb/status", + "command_topic": "test_light_rgb/set", + } } }, ) @@ -317,7 +321,6 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi """Test the controlling of the state via topic.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "state_topic": "test_light_rgb/status", "command_topic": "test_light_rgb/set", @@ -344,7 +347,7 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi } color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"] - assert await async_setup_component(hass, light.DOMAIN, config) + assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -437,7 +440,6 @@ async def test_invalid_state_via_topic(hass, mqtt_mock_entry_with_yaml_config, c """Test handling of empty data via topic.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "state_topic": "test_light_rgb/status", "command_topic": "test_light_rgb/set", @@ -464,7 +466,7 @@ async def test_invalid_state_via_topic(hass, mqtt_mock_entry_with_yaml_config, c } } - assert await async_setup_component(hass, light.DOMAIN, config) + assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -570,13 +572,12 @@ async def test_invalid_state_via_topic(hass, mqtt_mock_entry_with_yaml_config, c async def test_brightness_controlling_scale(hass, mqtt_mock_entry_with_yaml_config): """Test the brightness controlling scale.""" - with assert_setup_component(1, light.DOMAIN): - assert await async_setup_component( - hass, - light.DOMAIN, - { + assert await async_setup_component( + hass, + mqtt.DOMAIN, + { + mqtt.DOMAIN: { light.DOMAIN: { - "platform": "mqtt", "name": "test", "state_topic": "test_scale/status", "command_topic": "test_scale/set", @@ -587,10 +588,11 @@ async def test_brightness_controlling_scale(hass, mqtt_mock_entry_with_yaml_conf "payload_on": "on", "payload_off": "off", } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + }, + ) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -620,13 +622,12 @@ async def test_brightness_from_rgb_controlling_scale( hass, mqtt_mock_entry_with_yaml_config ): """Test the brightness controlling scale.""" - with assert_setup_component(1, light.DOMAIN): - assert await async_setup_component( - hass, - light.DOMAIN, - { + assert await async_setup_component( + hass, + mqtt.DOMAIN, + { + mqtt.DOMAIN: { light.DOMAIN: { - "platform": "mqtt", "name": "test", "state_topic": "test_scale_rgb/status", "command_topic": "test_scale_rgb/set", @@ -636,10 +637,11 @@ async def test_brightness_from_rgb_controlling_scale( "payload_on": "on", "payload_off": "off", } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + }, + ) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -664,7 +666,6 @@ async def test_controlling_state_via_topic_with_templates( """Test the setting of the state with a template.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "state_topic": "test_light_rgb/status", "command_topic": "test_light_rgb/set", @@ -697,7 +698,7 @@ async def test_controlling_state_via_topic_with_templates( } color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"] - assert await async_setup_component(hass, light.DOMAIN, config) + assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -767,7 +768,6 @@ async def test_sending_mqtt_commands_and_optimistic( """Test the sending of command in optimistic mode.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light_rgb/set", "brightness_command_topic": "test_light_rgb/brightness/set", @@ -798,10 +798,9 @@ async def test_sending_mqtt_commands_and_optimistic( ) mock_restore_cache(hass, (fake_state,)) - with assert_setup_component(1, light.DOMAIN): - assert await async_setup_component(hass, light.DOMAIN, config) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + 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() state = hass.states.get("light.test") assert state.state == STATE_ON @@ -958,7 +957,6 @@ async def test_sending_mqtt_rgb_command_with_template( """Test the sending of RGB command with template.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light_rgb/set", "rgb_command_topic": "test_light_rgb/rgb/set", @@ -970,7 +968,7 @@ async def test_sending_mqtt_rgb_command_with_template( } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -998,7 +996,6 @@ async def test_sending_mqtt_rgbw_command_with_template( """Test the sending of RGBW command with template.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light_rgb/set", "rgbw_command_topic": "test_light_rgb/rgbw/set", @@ -1010,7 +1007,7 @@ async def test_sending_mqtt_rgbw_command_with_template( } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1038,7 +1035,6 @@ async def test_sending_mqtt_rgbww_command_with_template( """Test the sending of RGBWW command with template.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light_rgb/set", "rgbww_command_topic": "test_light_rgb/rgbww/set", @@ -1050,7 +1046,7 @@ async def test_sending_mqtt_rgbww_command_with_template( } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1078,7 +1074,6 @@ async def test_sending_mqtt_color_temp_command_with_template( """Test the sending of Color Temp command with template.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light_color_temp/set", "color_temp_command_topic": "test_light_color_temp/color_temp/set", @@ -1089,7 +1084,7 @@ async def test_sending_mqtt_color_temp_command_with_template( } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1115,7 +1110,6 @@ async def test_on_command_first(hass, mqtt_mock_entry_with_yaml_config): """Test on command being sent before brightness.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light/set", "brightness_command_topic": "test_light/bright", @@ -1123,7 +1117,7 @@ async def test_on_command_first(hass, mqtt_mock_entry_with_yaml_config): } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1152,14 +1146,13 @@ async def test_on_command_last(hass, mqtt_mock_entry_with_yaml_config): """Test on command being sent after brightness.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light/set", "brightness_command_topic": "test_light/bright", } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1188,7 +1181,6 @@ async def test_on_command_brightness(hass, mqtt_mock_entry_with_yaml_config): """Test on command being sent as only brightness.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light/set", "brightness_command_topic": "test_light/bright", @@ -1197,7 +1189,7 @@ async def test_on_command_brightness(hass, mqtt_mock_entry_with_yaml_config): } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1244,7 +1236,6 @@ async def test_on_command_brightness_scaled(hass, mqtt_mock_entry_with_yaml_conf """Test brightness scale.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light/set", "brightness_command_topic": "test_light/bright", @@ -1254,7 +1245,7 @@ async def test_on_command_brightness_scaled(hass, mqtt_mock_entry_with_yaml_conf } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1315,14 +1306,13 @@ async def test_on_command_rgb(hass, mqtt_mock_entry_with_yaml_config): """Test on command in RGB brightness mode.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light/set", "rgb_command_topic": "test_light/rgb", } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1406,14 +1396,13 @@ async def test_on_command_rgbw(hass, mqtt_mock_entry_with_yaml_config): """Test on command in RGBW brightness mode.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light/set", "rgbw_command_topic": "test_light/rgbw", } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1497,14 +1486,13 @@ async def test_on_command_rgbww(hass, mqtt_mock_entry_with_yaml_config): """Test on command in RGBWW brightness mode.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light/set", "rgbww_command_topic": "test_light/rgbww", } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1588,7 +1576,6 @@ async def test_on_command_rgb_template(hass, mqtt_mock_entry_with_yaml_config): """Test on command in RGB brightness mode with RGB template.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light/set", "rgb_command_topic": "test_light/rgb", @@ -1596,7 +1583,7 @@ async def test_on_command_rgb_template(hass, mqtt_mock_entry_with_yaml_config): } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1626,7 +1613,6 @@ async def test_on_command_rgbw_template(hass, mqtt_mock_entry_with_yaml_config): """Test on command in RGBW brightness mode with RGBW template.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light/set", "rgbw_command_topic": "test_light/rgbw", @@ -1634,7 +1620,7 @@ async def test_on_command_rgbw_template(hass, mqtt_mock_entry_with_yaml_config): } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1663,7 +1649,6 @@ async def test_on_command_rgbww_template(hass, mqtt_mock_entry_with_yaml_config) """Test on command in RGBWW brightness mode with RGBWW template.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light/set", "rgbww_command_topic": "test_light/rgbww", @@ -1671,7 +1656,7 @@ async def test_on_command_rgbww_template(hass, mqtt_mock_entry_with_yaml_config) } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1701,7 +1686,6 @@ async def test_on_command_white(hass, mqtt_mock_entry_with_yaml_config): """Test sending commands for RGB + white light.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "tasmota_B94927/cmnd/POWER", "state_value_template": "{{ value_json.POWER }}", @@ -1721,7 +1705,7 @@ async def test_on_command_white(hass, mqtt_mock_entry_with_yaml_config): } color_modes = ["rgb", "white"] - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -1779,7 +1763,6 @@ async def test_explicit_color_mode(hass, mqtt_mock_entry_with_yaml_config): """Test explicit color mode over mqtt.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "state_topic": "test_light_rgb/status", "command_topic": "test_light_rgb/set", @@ -1807,7 +1790,7 @@ async def test_explicit_color_mode(hass, mqtt_mock_entry_with_yaml_config): } color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"] - assert await async_setup_component(hass, light.DOMAIN, config) + assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -1928,7 +1911,6 @@ async def test_explicit_color_mode_templated(hass, mqtt_mock_entry_with_yaml_con """Test templated explicit color mode over mqtt.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "state_topic": "test_light_rgb/status", "command_topic": "test_light_rgb/set", @@ -1947,7 +1929,7 @@ async def test_explicit_color_mode_templated(hass, mqtt_mock_entry_with_yaml_con } color_modes = ["color_temp", "hs"] - assert await async_setup_component(hass, light.DOMAIN, config) + assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -2010,7 +1992,6 @@ async def test_white_state_update(hass, mqtt_mock_entry_with_yaml_config): """Test state updates for RGB + white light.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "state_topic": "tasmota_B94927/tele/STATE", "command_topic": "tasmota_B94927/cmnd/POWER", @@ -2034,7 +2015,7 @@ async def test_white_state_update(hass, mqtt_mock_entry_with_yaml_config): } color_modes = ["rgb", "white"] - assert await async_setup_component(hass, light.DOMAIN, config) + assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -2075,7 +2056,6 @@ async def test_effect(hass, mqtt_mock_entry_with_yaml_config): """Test effect.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light/set", "effect_command_topic": "test_light/effect/set", @@ -2083,7 +2063,7 @@ async def test_effect(hass, mqtt_mock_entry_with_yaml_config): } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -2114,28 +2094,28 @@ async def test_availability_when_connection_lost( ): """Test availability after MQTT disconnection.""" await help_test_availability_when_connection_lost( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config): """Test availability without defined availability topic.""" await help_test_availability_without_topic( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by default payload with defined topic.""" await help_test_default_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by custom payload with defined topic.""" await help_test_custom_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -2144,7 +2124,7 @@ async def test_setting_attribute_via_mqtt_json_message( ): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -2156,7 +2136,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, - DEFAULT_CONFIG, + DEFAULT_CONFIG_LEGACY, MQTT_LIGHT_ATTRIBUTES_BLOCKED, ) @@ -2164,7 +2144,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_with_template( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -2173,7 +2153,11 @@ async def test_update_with_json_attrs_not_dict( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + light.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -2182,14 +2166,22 @@ async def test_update_with_json_attrs_bad_JSON( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + light.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test update of discovered MQTTAttributes.""" await help_test_discovery_update_attr( - hass, mqtt_mock_entry_no_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_no_yaml_config, + caplog, + light.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -2230,6 +2222,8 @@ async def test_discovery_removal_light(hass, mqtt_mock_entry_no_yaml_config, cap ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_discovery_deprecated(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test discovery of mqtt light with deprecated platform option.""" await mqtt_mock_entry_no_yaml_config() @@ -2750,42 +2744,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog): async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT light device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT light device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): """Test device registry update.""" await help_test_entity_device_info_update( - hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): """Test device registry remove.""" await help_test_entity_device_info_remove( - hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config): """Test MQTT subscriptions are managed when entity_id is updated.""" await help_test_entity_id_update_subscriptions( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT discovery update when entity_id is updated.""" await help_test_entity_id_update_discovery_update( - hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -2795,7 +2789,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, - DEFAULT_CONFIG, + DEFAULT_CONFIG_LEGACY, light.SERVICE_TURN_ON, ) @@ -2804,7 +2798,6 @@ async def test_max_mireds(hass, mqtt_mock_entry_with_yaml_config): """Test setting min_mireds and max_mireds.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_max_mireds/set", "color_temp_command_topic": "test_max_mireds/color_temp/set", @@ -2812,7 +2805,7 @@ async def test_max_mireds(hass, mqtt_mock_entry_with_yaml_config): } } - assert await async_setup_component(hass, light.DOMAIN, config) + assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -2921,7 +2914,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = light.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[domain]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) if topic == "effect_command_topic": config["effect_list"] = ["random", "color_loop"] elif topic == "white_command_topic": @@ -2946,7 +2939,7 @@ async def test_publishing_with_custom_encoding( async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = light.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) @@ -2955,7 +2948,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = light.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) @@ -3000,7 +2993,7 @@ async def test_encoding_subscribable_topics( init_payload, ): """Test handling of incoming encoded payload.""" - config = copy.deepcopy(DEFAULT_CONFIG[light.DOMAIN]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[light.DOMAIN]) config[CONF_EFFECT_COMMAND_TOPIC] = "light/CONF_EFFECT_COMMAND_TOPIC" config[CONF_RGB_COMMAND_TOPIC] = "light/CONF_RGB_COMMAND_TOPIC" config[CONF_BRIGHTNESS_COMMAND_TOPIC] = "light/CONF_BRIGHTNESS_COMMAND_TOPIC" @@ -3043,7 +3036,7 @@ async def test_encoding_subscribable_topics_brightness( init_payload, ): """Test handling of incoming encoded payload for a brightness only light.""" - config = copy.deepcopy(DEFAULT_CONFIG[light.DOMAIN]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[light.DOMAIN]) config[CONF_BRIGHTNESS_COMMAND_TOPIC] = "light/CONF_BRIGHTNESS_COMMAND_TOPIC" await help_test_encoding_subscribable_topics( @@ -3066,7 +3059,6 @@ async def test_sending_mqtt_brightness_command_with_template( """Test the sending of Brightness command with template.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light_brightness/set", "brightness_command_topic": "test_light_brightness/brightness/set", @@ -3077,7 +3069,7 @@ async def test_sending_mqtt_brightness_command_with_template( } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -3105,7 +3097,6 @@ async def test_sending_mqtt_effect_command_with_template( """Test the sending of Effect command with template.""" config = { light.DOMAIN: { - "platform": "mqtt", "name": "test", "command_topic": "test_light_brightness/set", "brightness_command_topic": "test_light_brightness/brightness/set", @@ -3118,7 +3109,7 @@ async def test_sending_mqtt_effect_command_with_template( } } - assert await async_setup_component(hass, light.DOMAIN, config) + 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() @@ -3147,7 +3138,7 @@ async def test_sending_mqtt_effect_command_with_template( async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = light.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[platform]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform]) config["name"] = "test" del config["platform"] await help_test_setup_manual_entity_from_yaml(hass, platform, config) @@ -3157,7 +3148,20 @@ async def test_setup_manual_entity_from_yaml(hass): async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = light.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) + + +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config): + """Test a setup with deprecated yaml platform schema.""" + domain = light.DOMAIN + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config["name"] = "test" + assert await async_setup_component(hass, domain, {domain: config}) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() + assert hass.states.get(f"{domain}.test") is not None diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index 8fac9092e3f..e61d4e77286 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -2,58 +2,63 @@ Configuration with RGB, brightness, color temp, effect, and XY: -light: - platform: mqtt_json - name: mqtt_json_light_1 - state_topic: "home/rgb1" - command_topic: "home/rgb1/set" - brightness: true - color_temp: true - effect: true - rgb: true - xy: true +mqtt: + light: + schema: json + name: mqtt_json_light_1 + state_topic: "home/rgb1" + command_topic: "home/rgb1/set" + brightness: true + color_temp: true + effect: true + rgb: true + xy: true Configuration with RGB, brightness, color temp and effect: -light: - platform: mqtt_json - name: mqtt_json_light_1 - state_topic: "home/rgb1" - command_topic: "home/rgb1/set" - brightness: true - color_temp: true - effect: true - rgb: true +mqtt: + light: + schema: json + name: mqtt_json_light_1 + state_topic: "home/rgb1" + command_topic: "home/rgb1/set" + brightness: true + color_temp: true + effect: true + rgb: true Configuration with RGB, brightness and color temp: -light: - platform: mqtt_json - name: mqtt_json_light_1 - state_topic: "home/rgb1" - command_topic: "home/rgb1/set" - brightness: true - rgb: true - color_temp: true +mqtt: + light: + schema: json + name: mqtt_json_light_1 + state_topic: "home/rgb1" + command_topic: "home/rgb1/set" + brightness: true + rgb: true + color_temp: true Configuration with RGB, brightness: -light: - platform: mqtt_json - name: mqtt_json_light_1 - state_topic: "home/rgb1" - command_topic: "home/rgb1/set" - brightness: true - rgb: true +mqtt: + light: + schema: json + name: mqtt_json_light_1 + state_topic: "home/rgb1" + command_topic: "home/rgb1/set" + brightness: true + rgb: true Config without RGB: -light: - platform: mqtt_json - name: mqtt_json_light_1 - state_topic: "home/rgb1" - command_topic: "home/rgb1/set" - brightness: true +mqtt: + light: + schema: json + name: mqtt_json_light_1 + state_topic: "home/rgb1" + command_topic: "home/rgb1/set" + brightness: true Config without RGB and brightness: @@ -79,7 +84,7 @@ from unittest.mock import call, patch import pytest -from homeassistant.components import light +from homeassistant.components import light, mqtt from homeassistant.components.mqtt.light.schema_basic import ( MQTT_LIGHT_ATTRIBUTES_BLOCKED, ) @@ -128,14 +133,20 @@ from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.components.light import common DEFAULT_CONFIG = { - light.DOMAIN: { - "platform": "mqtt", - "schema": "json", - "name": "test", - "command_topic": "test-topic", + mqtt.DOMAIN: { + light.DOMAIN: { + "schema": "json", + "name": "test", + "command_topic": "test-topic", + } } } +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) +DEFAULT_CONFIG_LEGACY[light.DOMAIN]["platform"] = mqtt.DOMAIN + @pytest.fixture(autouse=True) def light_platform_only(): @@ -156,22 +167,21 @@ class JsonValidator: return json.loads(self.jsondata) == json.loads(other) -async def test_fail_setup_if_no_command_topic(hass, mqtt_mock_entry_no_yaml_config): +async def test_fail_setup_if_no_command_topic(hass, caplog): """Test if setup fails with no command topic.""" - assert await async_setup_component( + assert not await async_setup_component( hass, - light.DOMAIN, - {light.DOMAIN: {"platform": "mqtt", "schema": "json", "name": "test"}}, + mqtt.DOMAIN, + {mqtt.DOMAIN: {light.DOMAIN: {"schema": "json", "name": "test"}}}, + ) + assert ( + "Invalid config for [mqtt]: required key not provided @ data['mqtt']['light'][0]['command_topic']. Got None." + in caplog.text ) - await hass.async_block_till_done() - await mqtt_mock_entry_no_yaml_config() - assert hass.states.get("light.test") is None @pytest.mark.parametrize("deprecated", ("color_temp", "hs", "rgb", "xy")) -async def test_fail_setup_if_color_mode_deprecated( - hass, mqtt_mock_entry_no_yaml_config, deprecated -): +async def test_fail_setup_if_color_mode_deprecated(hass, caplog, deprecated): """Test if setup fails if color mode is combined with deprecated config keys.""" supported_color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"] @@ -181,27 +191,32 @@ async def test_fail_setup_if_color_mode_deprecated( "color_mode": True, "command_topic": "test_light_rgb/set", "name": "test", - "platform": "mqtt", "schema": "json", "supported_color_modes": supported_color_modes, } } config[light.DOMAIN][deprecated] = True - assert await async_setup_component( + assert not await async_setup_component( hass, - light.DOMAIN, - config, + mqtt.DOMAIN, + {mqtt.DOMAIN: config}, + ) + assert ( + "Invalid config for [mqtt]: color_mode must not be combined with any of" + in caplog.text ) - await hass.async_block_till_done() - await mqtt_mock_entry_no_yaml_config() - assert hass.states.get("light.test") is None @pytest.mark.parametrize( - "supported_color_modes", [["onoff", "rgb"], ["brightness", "rgb"], ["unknown"]] + "supported_color_modes,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 [ - on,{{ brightness|d }},{{ red|d }}-{{ green|d }}-{{ blue|d }} - command_off_template: 'off' - state_template: '{{ value.split(",")[0] }}' - brightness_template: '{{ value.split(",")[1] }}' - color_temp_template: '{{ value.split(",")[2] }}' - red_template: '{{ value.split(",")[4].split("-")[0] }}' - green_template: '{{ value.split(",")[4].split("-")[1] }}' - blue_template: '{{ value.split(",")[4].split("-")[2] }}' +mqtt: + light: + schema: template + name: mqtt_template_light_1 + state_topic: 'home/rgb1' + command_topic: 'home/rgb1/set' + command_on_template: > + on,{{ brightness|d }},{{ red|d }}-{{ green|d }}-{{ blue|d }} + command_off_template: 'off' + state_template: '{{ value.split(",")[0] }}' + brightness_template: '{{ value.split(",")[1] }}' + color_temp_template: '{{ value.split(",")[2] }}' + red_template: '{{ value.split(",")[4].split("-")[0] }}' + green_template: '{{ value.split(",")[4].split("-")[1] }}' + blue_template: '{{ value.split(",")[4].split("-")[2] }}' If your light doesn't support brightness feature, omit `brightness_template`. @@ -28,7 +29,7 @@ from unittest.mock import patch import pytest -from homeassistant.components import light +from homeassistant.components import light, mqtt from homeassistant.components.mqtt.light.schema_basic import ( MQTT_LIGHT_ATTRIBUTES_BLOCKED, ) @@ -74,24 +75,26 @@ from .test_common import ( help_test_update_with_json_attrs_not_dict, ) -from tests.common import ( - assert_setup_component, - async_fire_mqtt_message, - mock_restore_cache, -) +from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.components.light import common DEFAULT_CONFIG = { - light.DOMAIN: { - "platform": "mqtt", - "schema": "template", - "name": "test", - "command_topic": "test-topic", - "command_on_template": "on,{{ transition }}", - "command_off_template": "off,{{ transition|d }}", + mqtt.DOMAIN: { + light.DOMAIN: { + "schema": "template", + "name": "test", + "command_topic": "test-topic", + "command_on_template": "on,{{ transition }}", + "command_off_template": "off,{{ transition|d }}", + } } } +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) +DEFAULT_CONFIG_LEGACY[light.DOMAIN]["platform"] = mqtt.DOMAIN + @pytest.fixture(autouse=True) def light_platform_only(): @@ -103,10 +106,9 @@ def light_platform_only(): @pytest.mark.parametrize( "test_config", [ - ({"platform": "mqtt", "schema": "template", "name": "test"},), + ({"schema": "template", "name": "test"},), ( { - "platform": "mqtt", "schema": "template", "name": "test", "command_topic": "test_topic", @@ -114,7 +116,6 @@ def light_platform_only(): ), ( { - "platform": "mqtt", "schema": "template", "name": "test", "command_topic": "test_topic", @@ -123,7 +124,6 @@ def light_platform_only(): ), ( { - "platform": "mqtt", "schema": "template", "name": "test", "command_topic": "test_topic", @@ -132,36 +132,33 @@ def light_platform_only(): ), ], ) -async def test_setup_fails(hass, mqtt_mock_entry_no_yaml_config, test_config): +async def test_setup_fails(hass, caplog, test_config): """Test that setup fails with missing required configuration items.""" - with assert_setup_component(0, light.DOMAIN) as setup_config: - assert await async_setup_component( - hass, - light.DOMAIN, - {light.DOMAIN: test_config}, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_no_yaml_config() - assert not setup_config[light.DOMAIN] - assert hass.states.get("light.test") is None + assert not await async_setup_component( + hass, + mqtt.DOMAIN, + {mqtt.DOMAIN: {light.DOMAIN: test_config}}, + ) + assert "Invalid config for [mqtt]" in caplog.text async def test_rgb_light(hass, mqtt_mock_entry_with_yaml_config): """Test RGB light flags brightness support.""" assert await async_setup_component( hass, - light.DOMAIN, + mqtt.DOMAIN, { - light.DOMAIN: { - "platform": "mqtt", - "schema": "template", - "name": "test", - "command_topic": "test_light_rgb/set", - "command_on_template": "on", - "command_off_template": "off", - "red_template": '{{ value.split(",")[4].' 'split("-")[0] }}', - "green_template": '{{ value.split(",")[4].' 'split("-")[1] }}', - "blue_template": '{{ value.split(",")[4].' 'split("-")[2] }}', + mqtt.DOMAIN: { + light.DOMAIN: { + "schema": "template", + "name": "test", + "command_topic": "test_light_rgb/set", + "command_on_template": "on", + "command_off_template": "off", + "red_template": '{{ value.split(",")[4].' 'split("-")[0] }}', + "green_template": '{{ value.split(",")[4].' 'split("-")[1] }}', + "blue_template": '{{ value.split(",")[4].' 'split("-")[2] }}', + } } }, ) @@ -181,13 +178,12 @@ async def test_rgb_light(hass, mqtt_mock_entry_with_yaml_config): async def test_state_change_via_topic(hass, mqtt_mock_entry_with_yaml_config): """Test state change via topic.""" - with assert_setup_component(1, light.DOMAIN): - assert await async_setup_component( - hass, - light.DOMAIN, - { + assert await async_setup_component( + hass, + mqtt.DOMAIN, + { + mqtt.DOMAIN: { light.DOMAIN: { - "platform": "mqtt", "schema": "template", "name": "test", "state_topic": "test_light_rgb", @@ -201,10 +197,11 @@ async def test_state_change_via_topic(hass, mqtt_mock_entry_with_yaml_config): "command_off_template": "off", "state_template": '{{ value.split(",")[0] }}', } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + }, + ) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -236,13 +233,12 @@ async def test_state_brightness_color_effect_temp_change_via_topic( hass, mqtt_mock_entry_with_yaml_config ): """Test state, bri, color, effect, color temp change.""" - with assert_setup_component(1, light.DOMAIN): - assert await async_setup_component( - hass, - light.DOMAIN, - { + assert await async_setup_component( + hass, + mqtt.DOMAIN, + { + mqtt.DOMAIN: { light.DOMAIN: { - "platform": "mqtt", "schema": "template", "name": "test", "effect_list": ["rainbow", "colorloop"], @@ -264,10 +260,11 @@ async def test_state_brightness_color_effect_temp_change_via_topic( "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() + } + }, + ) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -340,13 +337,12 @@ async def test_sending_mqtt_commands_and_optimistic( ) mock_restore_cache(hass, (fake_state,)) - with assert_setup_component(1, light.DOMAIN): - assert await async_setup_component( - hass, - light.DOMAIN, - { + assert await async_setup_component( + hass, + mqtt.DOMAIN, + { + mqtt.DOMAIN: { light.DOMAIN: { - "platform": "mqtt", "schema": "template", "name": "test", "command_topic": "test_light_rgb/set", @@ -369,10 +365,11 @@ async def test_sending_mqtt_commands_and_optimistic( "effect_template": '{{ value.split(",")[4] }}', "qos": 2, } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_ON @@ -470,13 +467,12 @@ async def test_sending_mqtt_commands_non_optimistic_brightness_template( hass, mqtt_mock_entry_with_yaml_config ): """Test the sending of command in optimistic mode.""" - with assert_setup_component(1, light.DOMAIN): - assert await async_setup_component( - hass, - light.DOMAIN, - { + assert await async_setup_component( + hass, + mqtt.DOMAIN, + { + mqtt.DOMAIN: { light.DOMAIN: { - "platform": "mqtt", "schema": "template", "name": "test", "effect_list": ["rainbow", "colorloop"], @@ -499,10 +495,11 @@ async def test_sending_mqtt_commands_non_optimistic_brightness_template( "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() + } + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -591,13 +588,12 @@ async def test_sending_mqtt_commands_non_optimistic_brightness_template( async def test_effect(hass, mqtt_mock_entry_with_yaml_config): """Test effect sent over MQTT in optimistic mode.""" - with assert_setup_component(1, light.DOMAIN): - assert await async_setup_component( - hass, - light.DOMAIN, - { + assert await async_setup_component( + hass, + mqtt.DOMAIN, + { + mqtt.DOMAIN: { light.DOMAIN: { - "platform": "mqtt", "schema": "template", "effect_list": ["rainbow", "colorloop"], "name": "test", @@ -606,10 +602,11 @@ async def test_effect(hass, mqtt_mock_entry_with_yaml_config): "command_off_template": "off", "qos": 0, } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -644,13 +641,12 @@ async def test_effect(hass, mqtt_mock_entry_with_yaml_config): async def test_flash(hass, mqtt_mock_entry_with_yaml_config): """Test flash sent over MQTT in optimistic mode.""" - with assert_setup_component(1, light.DOMAIN): - assert await async_setup_component( - hass, - light.DOMAIN, - { + assert await async_setup_component( + hass, + mqtt.DOMAIN, + { + mqtt.DOMAIN: { light.DOMAIN: { - "platform": "mqtt", "schema": "template", "name": "test", "command_topic": "test_light_rgb/set", @@ -658,10 +654,11 @@ async def test_flash(hass, mqtt_mock_entry_with_yaml_config): "command_off_template": "off", "qos": 0, } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -693,13 +690,12 @@ async def test_flash(hass, mqtt_mock_entry_with_yaml_config): async def test_transition(hass, mqtt_mock_entry_with_yaml_config): """Test for transition time being sent when included.""" - with assert_setup_component(1, light.DOMAIN): - assert await async_setup_component( - hass, - light.DOMAIN, - { + assert await async_setup_component( + hass, + mqtt.DOMAIN, + { + mqtt.DOMAIN: { light.DOMAIN: { - "platform": "mqtt", "schema": "template", "name": "test", "command_topic": "test_light_rgb/set", @@ -707,10 +703,11 @@ async def test_transition(hass, mqtt_mock_entry_with_yaml_config): "command_off_template": "off,{{ transition|int|d }}", "qos": 1, } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + } + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -735,13 +732,12 @@ async def test_transition(hass, mqtt_mock_entry_with_yaml_config): async def test_invalid_values(hass, mqtt_mock_entry_with_yaml_config): """Test that invalid values are ignored.""" - with assert_setup_component(1, light.DOMAIN): - assert await async_setup_component( - hass, - light.DOMAIN, - { + assert await async_setup_component( + hass, + mqtt.DOMAIN, + { + mqtt.DOMAIN: { light.DOMAIN: { - "platform": "mqtt", "schema": "template", "name": "test", "effect_list": ["rainbow", "colorloop"], @@ -763,10 +759,11 @@ async def test_invalid_values(hass, mqtt_mock_entry_with_yaml_config): "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() + } + }, + ) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_UNKNOWN @@ -827,28 +824,28 @@ async def test_availability_when_connection_lost( ): """Test availability after MQTT disconnection.""" await help_test_availability_when_connection_lost( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config): """Test availability without defined availability topic.""" await help_test_availability_without_topic( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by default payload with defined topic.""" await help_test_default_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by custom payload with defined topic.""" await help_test_custom_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -857,7 +854,7 @@ async def test_setting_attribute_via_mqtt_json_message( ): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -869,7 +866,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, - DEFAULT_CONFIG, + DEFAULT_CONFIG_LEGACY, MQTT_LIGHT_ATTRIBUTES_BLOCKED, ) @@ -877,7 +874,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_with_template( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -886,7 +883,11 @@ async def test_update_with_json_attrs_not_dict( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + light.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -895,14 +896,22 @@ async def test_update_with_json_attrs_bad_JSON( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + light.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test update of discovered MQTTAttributes.""" await help_test_discovery_update_attr( - hass, mqtt_mock_entry_no_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_no_yaml_config, + caplog, + light.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -1017,42 +1026,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog): async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT light device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT light device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): """Test device registry update.""" await help_test_entity_device_info_update( - hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): """Test device registry remove.""" await help_test_entity_device_info_remove( - hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config): """Test MQTT subscriptions are managed when entity_id is updated.""" await help_test_entity_id_update_subscriptions( - hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT discovery update when entity_id is updated.""" await help_test_entity_id_update_discovery_update( - hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -1082,7 +1091,6 @@ async def test_max_mireds(hass, mqtt_mock_entry_with_yaml_config): """Test setting min_mireds and max_mireds.""" config = { light.DOMAIN: { - "platform": "mqtt", "schema": "template", "name": "test", "command_topic": "test_max_mireds/set", @@ -1093,7 +1101,7 @@ async def test_max_mireds(hass, mqtt_mock_entry_with_yaml_config): } } - assert await async_setup_component(hass, light.DOMAIN, config) + assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -1139,7 +1147,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = light.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[domain]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) if topic == "effect_command_topic": config["effect_list"] = ["random", "color_loop"] @@ -1162,7 +1170,7 @@ async def test_publishing_with_custom_encoding( async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = light.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) @@ -1171,7 +1179,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = light.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) @@ -1192,7 +1200,7 @@ async def test_encoding_subscribable_topics( init_payload, ): """Test handling of incoming encoded payload.""" - config = copy.deepcopy(DEFAULT_CONFIG[light.DOMAIN]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[light.DOMAIN]) config["state_template"] = "{{ value }}" await help_test_encoding_subscribable_topics( hass, @@ -1211,7 +1219,7 @@ async def test_encoding_subscribable_topics( async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = light.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[platform]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform]) config["name"] = "test" del config["platform"] await help_test_setup_manual_entity_from_yaml(hass, platform, config) @@ -1221,7 +1229,20 @@ async def test_setup_manual_entity_from_yaml(hass): async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = light.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) + + +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config): + """Test a setup with deprecated yaml platform schema.""" + domain = light.DOMAIN + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config["name"] = "test" + assert await async_setup_component(hass, domain, {domain: config}) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() + assert hass.states.get(f"{domain}.test") is not None diff --git a/tests/components/mqtt/test_lock.py b/tests/components/mqtt/test_lock.py index f6dc4a0ed6d..de97de23a1e 100644 --- a/tests/components/mqtt/test_lock.py +++ b/tests/components/mqtt/test_lock.py @@ -4,14 +4,14 @@ from unittest.mock import patch import pytest +from homeassistant.components import lock, mqtt from homeassistant.components.lock import ( - DOMAIN as LOCK_DOMAIN, SERVICE_LOCK, SERVICE_OPEN, SERVICE_UNLOCK, STATE_LOCKED, STATE_UNLOCKED, - SUPPORT_OPEN, + LockEntityFeature, ) from homeassistant.components.mqtt.lock import MQTT_LOCK_ATTRIBUTES_BLOCKED from homeassistant.const import ( @@ -56,9 +56,14 @@ from .test_common import ( from tests.common import async_fire_mqtt_message DEFAULT_CONFIG = { - LOCK_DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"} + mqtt.DOMAIN: {lock.DOMAIN: {"name": "test", "command_topic": "test-topic"}} } +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) +DEFAULT_CONFIG_LEGACY[lock.DOMAIN]["platform"] = mqtt.DOMAIN + @pytest.fixture(autouse=True) def lock_platform_only(): @@ -71,17 +76,18 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi """Test the controlling state via topic.""" assert await async_setup_component( hass, - LOCK_DOMAIN, + mqtt.DOMAIN, { - LOCK_DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_lock": "LOCK", - "payload_unlock": "UNLOCK", - "state_locked": "LOCKED", - "state_unlocked": "UNLOCKED", + mqtt.DOMAIN: { + lock.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_lock": "LOCK", + "payload_unlock": "UNLOCK", + "state_locked": "LOCKED", + "state_unlocked": "UNLOCKED", + } } }, ) @@ -110,17 +116,18 @@ async def test_controlling_non_default_state_via_topic( """Test the controlling state via topic.""" assert await async_setup_component( hass, - LOCK_DOMAIN, + mqtt.DOMAIN, { - LOCK_DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_lock": "LOCK", - "payload_unlock": "UNLOCK", - "state_locked": "closed", - "state_unlocked": "open", + mqtt.DOMAIN: { + lock.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_lock": "LOCK", + "payload_unlock": "UNLOCK", + "state_locked": "closed", + "state_unlocked": "open", + } } }, ) @@ -148,18 +155,19 @@ async def test_controlling_state_via_topic_and_json_message( """Test the controlling state via topic and JSON message.""" assert await async_setup_component( hass, - LOCK_DOMAIN, + mqtt.DOMAIN, { - LOCK_DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_lock": "LOCK", - "payload_unlock": "UNLOCK", - "state_locked": "LOCKED", - "state_unlocked": "UNLOCKED", - "value_template": "{{ value_json.val }}", + mqtt.DOMAIN: { + lock.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_lock": "LOCK", + "payload_unlock": "UNLOCK", + "state_locked": "LOCKED", + "state_unlocked": "UNLOCKED", + "value_template": "{{ value_json.val }}", + } } }, ) @@ -186,18 +194,19 @@ async def test_controlling_non_default_state_via_topic_and_json_message( """Test the controlling state via topic and JSON message.""" assert await async_setup_component( hass, - LOCK_DOMAIN, + mqtt.DOMAIN, { - LOCK_DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_lock": "LOCK", - "payload_unlock": "UNLOCK", - "state_locked": "closed", - "state_unlocked": "open", - "value_template": "{{ value_json.val }}", + mqtt.DOMAIN: { + lock.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_lock": "LOCK", + "payload_unlock": "UNLOCK", + "state_locked": "closed", + "state_unlocked": "open", + "value_template": "{{ value_json.val }}", + } } }, ) @@ -224,16 +233,17 @@ async def test_sending_mqtt_commands_and_optimistic( """Test optimistic mode without state topic.""" assert await async_setup_component( hass, - LOCK_DOMAIN, + mqtt.DOMAIN, { - LOCK_DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "payload_lock": "LOCK", - "payload_unlock": "UNLOCK", - "state_locked": "LOCKED", - "state_unlocked": "UNLOCKED", + mqtt.DOMAIN: { + lock.DOMAIN: { + "name": "test", + "command_topic": "command-topic", + "payload_lock": "LOCK", + "payload_unlock": "UNLOCK", + "state_locked": "LOCKED", + "state_unlocked": "UNLOCKED", + } } }, ) @@ -245,7 +255,7 @@ async def test_sending_mqtt_commands_and_optimistic( assert state.attributes.get(ATTR_ASSUMED_STATE) await hass.services.async_call( - LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True + lock.DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False) @@ -255,7 +265,7 @@ async def test_sending_mqtt_commands_and_optimistic( assert state.attributes.get(ATTR_ASSUMED_STATE) await hass.services.async_call( - LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True + lock.DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False) @@ -271,18 +281,19 @@ async def test_sending_mqtt_commands_and_explicit_optimistic( """Test optimistic mode without state topic.""" assert await async_setup_component( hass, - LOCK_DOMAIN, + mqtt.DOMAIN, { - LOCK_DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_lock": "LOCK", - "payload_unlock": "UNLOCK", - "state_locked": "LOCKED", - "state_unlocked": "UNLOCKED", - "optimistic": True, + mqtt.DOMAIN: { + lock.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_lock": "LOCK", + "payload_unlock": "UNLOCK", + "state_locked": "LOCKED", + "state_unlocked": "UNLOCKED", + "optimistic": True, + } } }, ) @@ -294,7 +305,7 @@ async def test_sending_mqtt_commands_and_explicit_optimistic( assert state.attributes.get(ATTR_ASSUMED_STATE) await hass.services.async_call( - LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True + lock.DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False) @@ -304,7 +315,7 @@ async def test_sending_mqtt_commands_and_explicit_optimistic( assert state.attributes.get(ATTR_ASSUMED_STATE) await hass.services.async_call( - LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True + lock.DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False) @@ -320,17 +331,18 @@ async def test_sending_mqtt_commands_support_open_and_optimistic( """Test open function of the lock without state topic.""" assert await async_setup_component( hass, - LOCK_DOMAIN, + mqtt.DOMAIN, { - LOCK_DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "payload_lock": "LOCK", - "payload_unlock": "UNLOCK", - "payload_open": "OPEN", - "state_locked": "LOCKED", - "state_unlocked": "UNLOCKED", + mqtt.DOMAIN: { + lock.DOMAIN: { + "name": "test", + "command_topic": "command-topic", + "payload_lock": "LOCK", + "payload_unlock": "UNLOCK", + "payload_open": "OPEN", + "state_locked": "LOCKED", + "state_unlocked": "UNLOCKED", + } } }, ) @@ -340,10 +352,10 @@ async def test_sending_mqtt_commands_support_open_and_optimistic( state = hass.states.get("lock.test") assert state.state is STATE_UNLOCKED assert state.attributes.get(ATTR_ASSUMED_STATE) - assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == SUPPORT_OPEN + assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == LockEntityFeature.OPEN await hass.services.async_call( - LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True + lock.DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False) @@ -353,7 +365,7 @@ async def test_sending_mqtt_commands_support_open_and_optimistic( assert state.attributes.get(ATTR_ASSUMED_STATE) await hass.services.async_call( - LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True + lock.DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False) @@ -363,7 +375,7 @@ async def test_sending_mqtt_commands_support_open_and_optimistic( assert state.attributes.get(ATTR_ASSUMED_STATE) await hass.services.async_call( - LOCK_DOMAIN, SERVICE_OPEN, {ATTR_ENTITY_ID: "lock.test"}, blocking=True + lock.DOMAIN, SERVICE_OPEN, {ATTR_ENTITY_ID: "lock.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "OPEN", 0, False) @@ -379,19 +391,20 @@ async def test_sending_mqtt_commands_support_open_and_explicit_optimistic( """Test open function of the lock without state topic.""" assert await async_setup_component( hass, - LOCK_DOMAIN, + mqtt.DOMAIN, { - LOCK_DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_lock": "LOCK", - "payload_unlock": "UNLOCK", - "payload_open": "OPEN", - "state_locked": "LOCKED", - "state_unlocked": "UNLOCKED", - "optimistic": True, + mqtt.DOMAIN: { + lock.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_lock": "LOCK", + "payload_unlock": "UNLOCK", + "payload_open": "OPEN", + "state_locked": "LOCKED", + "state_unlocked": "UNLOCKED", + "optimistic": True, + } } }, ) @@ -401,10 +414,10 @@ async def test_sending_mqtt_commands_support_open_and_explicit_optimistic( state = hass.states.get("lock.test") assert state.state is STATE_UNLOCKED assert state.attributes.get(ATTR_ASSUMED_STATE) - assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == SUPPORT_OPEN + assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == LockEntityFeature.OPEN await hass.services.async_call( - LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True + lock.DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False) @@ -414,7 +427,7 @@ async def test_sending_mqtt_commands_support_open_and_explicit_optimistic( assert state.attributes.get(ATTR_ASSUMED_STATE) await hass.services.async_call( - LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True + lock.DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False) @@ -424,7 +437,7 @@ async def test_sending_mqtt_commands_support_open_and_explicit_optimistic( assert state.attributes.get(ATTR_ASSUMED_STATE) await hass.services.async_call( - LOCK_DOMAIN, SERVICE_OPEN, {ATTR_ENTITY_ID: "lock.test"}, blocking=True + lock.DOMAIN, SERVICE_OPEN, {ATTR_ENTITY_ID: "lock.test"}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with("command-topic", "OPEN", 0, False) @@ -439,28 +452,28 @@ async def test_availability_when_connection_lost( ): """Test availability after MQTT disconnection.""" await help_test_availability_when_connection_lost( - hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config): """Test availability without defined availability topic.""" await help_test_availability_without_topic( - hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by default payload with defined topic.""" await help_test_default_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by custom payload with defined topic.""" await help_test_custom_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -469,7 +482,7 @@ async def test_setting_attribute_via_mqtt_json_message( ): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -480,8 +493,8 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( await help_test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, - LOCK_DOMAIN, - DEFAULT_CONFIG, + lock.DOMAIN, + DEFAULT_CONFIG_LEGACY, MQTT_LOCK_ATTRIBUTES_BLOCKED, ) @@ -489,7 +502,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_with_template( - hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -498,7 +511,11 @@ async def test_update_with_json_attrs_not_dict( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock_entry_with_yaml_config, caplog, LOCK_DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + lock.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -507,21 +524,25 @@ async def test_update_with_json_attrs_bad_json( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock_entry_with_yaml_config, caplog, LOCK_DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + lock.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test update of discovered MQTTAttributes.""" await help_test_discovery_update_attr( - hass, mqtt_mock_entry_no_yaml_config, caplog, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, caplog, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one lock per unique_id.""" config = { - LOCK_DOMAIN: [ + lock.DOMAIN: [ { "platform": "mqtt", "name": "Test 1", @@ -539,7 +560,7 @@ async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): ] } await help_test_unique_id( - hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, config + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, config ) @@ -547,7 +568,7 @@ async def test_discovery_removal_lock(hass, mqtt_mock_entry_no_yaml_config, capl """Test removal of discovered lock.""" data = '{ "name": "test",' ' "command_topic": "test_topic" }' await help_test_discovery_removal( - hass, mqtt_mock_entry_no_yaml_config, caplog, LOCK_DOMAIN, data + hass, mqtt_mock_entry_no_yaml_config, caplog, lock.DOMAIN, data ) @@ -566,7 +587,7 @@ async def test_discovery_update_lock(hass, mqtt_mock_entry_no_yaml_config, caplo "availability_topic": "availability_topic2", } await help_test_discovery_update( - hass, mqtt_mock_entry_no_yaml_config, caplog, LOCK_DOMAIN, config1, config2 + hass, mqtt_mock_entry_no_yaml_config, caplog, lock.DOMAIN, config1, config2 ) @@ -586,7 +607,7 @@ async def test_discovery_update_unchanged_lock( hass, mqtt_mock_entry_no_yaml_config, caplog, - LOCK_DOMAIN, + lock.DOMAIN, data1, discovery_update, ) @@ -598,49 +619,49 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog): data1 = '{ "name": "Beer" }' data2 = '{ "name": "Milk",' ' "command_topic": "test_topic" }' await help_test_discovery_broken( - hass, mqtt_mock_entry_no_yaml_config, caplog, LOCK_DOMAIN, data1, data2 + hass, mqtt_mock_entry_no_yaml_config, caplog, lock.DOMAIN, data1, data2 ) async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT lock device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT lock device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): """Test device registry update.""" await help_test_entity_device_info_update( - hass, mqtt_mock_entry_no_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): """Test device registry remove.""" await help_test_entity_device_info_remove( - hass, mqtt_mock_entry_no_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config): """Test MQTT subscriptions are managed when entity_id is updated.""" await help_test_entity_id_update_subscriptions( - hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT discovery update when entity_id is updated.""" await help_test_entity_id_update_discovery_update( - hass, mqtt_mock_entry_no_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -649,8 +670,8 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): await help_test_entity_debug_info_message( hass, mqtt_mock_entry_no_yaml_config, - LOCK_DOMAIN, - DEFAULT_CONFIG, + lock.DOMAIN, + DEFAULT_CONFIG_LEGACY, SERVICE_LOCK, command_payload="LOCK", ) @@ -679,8 +700,8 @@ async def test_publishing_with_custom_encoding( template, ): """Test publishing MQTT payload with different encoding.""" - domain = LOCK_DOMAIN - config = DEFAULT_CONFIG[domain] + domain = lock.DOMAIN + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_publishing_with_custom_encoding( hass, @@ -698,8 +719,8 @@ async def test_publishing_with_custom_encoding( async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" - domain = LOCK_DOMAIN - config = DEFAULT_CONFIG[domain] + domain = lock.DOMAIN + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) @@ -707,8 +728,8 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" - domain = LOCK_DOMAIN - config = DEFAULT_CONFIG[domain] + domain = lock.DOMAIN + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) @@ -732,8 +753,8 @@ async def test_encoding_subscribable_topics( hass, mqtt_mock_entry_with_yaml_config, caplog, - LOCK_DOMAIN, - DEFAULT_CONFIG[LOCK_DOMAIN], + lock.DOMAIN, + DEFAULT_CONFIG_LEGACY[lock.DOMAIN], topic, value, attribute, @@ -743,8 +764,8 @@ async def test_encoding_subscribable_topics( async def test_setup_manual_entity_from_yaml(hass, caplog, tmp_path): """Test setup manual configured MQTT entity.""" - platform = LOCK_DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[platform]) + platform = lock.DOMAIN + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform]) config["name"] = "test" del config["platform"] await help_test_setup_manual_entity_from_yaml(hass, platform, config) @@ -753,8 +774,21 @@ async def test_setup_manual_entity_from_yaml(hass, caplog, tmp_path): async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" - domain = LOCK_DOMAIN - config = DEFAULT_CONFIG[domain] + domain = lock.DOMAIN + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) + + +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config): + """Test a setup with deprecated yaml platform schema.""" + domain = lock.DOMAIN + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config["name"] = "test" + assert await async_setup_component(hass, domain, {domain: config}) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() + assert hass.states.get(f"{domain}.test") is not None diff --git a/tests/components/mqtt/test_number.py b/tests/components/mqtt/test_number.py index 603984cffad..69b0473fb9d 100644 --- a/tests/components/mqtt/test_number.py +++ b/tests/components/mqtt/test_number.py @@ -66,9 +66,14 @@ from .test_common import ( from tests.common import async_fire_mqtt_message, mock_restore_cache_with_extra_data DEFAULT_CONFIG = { - number.DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"} + mqtt.DOMAIN: {number.DOMAIN: {"name": "test", "command_topic": "test-topic"}} } +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) +DEFAULT_CONFIG_LEGACY[number.DOMAIN]["platform"] = mqtt.DOMAIN + @pytest.fixture(autouse=True) def number_platform_only(): @@ -82,16 +87,17 @@ async def test_run_number_setup(hass, mqtt_mock_entry_with_yaml_config): topic = "test/number" await async_setup_component( hass, - "number", + mqtt.DOMAIN, { - "number": { - "platform": "mqtt", - "state_topic": topic, - "command_topic": topic, - "name": "Test Number", - "device_class": "temperature", - "unit_of_measurement": TEMP_FAHRENHEIT, - "payload_reset": "reset!", + mqtt.DOMAIN: { + number.DOMAIN: { + "state_topic": topic, + "command_topic": topic, + "name": "Test Number", + "device_class": "temperature", + "unit_of_measurement": TEMP_FAHRENHEIT, + "payload_reset": "reset!", + } } }, ) @@ -131,14 +137,15 @@ async def test_value_template(hass, mqtt_mock_entry_with_yaml_config): topic = "test/number" await async_setup_component( hass, - "number", + mqtt.DOMAIN, { - "number": { - "platform": "mqtt", - "state_topic": topic, - "command_topic": topic, - "name": "Test Number", - "value_template": "{{ value_json.val }}", + mqtt.DOMAIN: { + number.DOMAIN: { + "state_topic": topic, + "command_topic": topic, + "name": "Test Number", + "value_template": "{{ value_json.val }}", + } } }, ) @@ -184,14 +191,15 @@ async def test_restore_native_value(hass, mqtt_mock_entry_with_yaml_config): ) assert await async_setup_component( hass, - number.DOMAIN, + mqtt.DOMAIN, { - "number": { - "platform": "mqtt", - "command_topic": topic, - "device_class": "temperature", - "unit_of_measurement": TEMP_FAHRENHEIT, - "name": "Test Number", + mqtt.DOMAIN: { + number.DOMAIN: { + "command_topic": topic, + "device_class": "temperature", + "unit_of_measurement": TEMP_FAHRENHEIT, + "name": "Test Number", + } } }, ) @@ -220,12 +228,13 @@ async def test_run_number_service_optimistic(hass, mqtt_mock_entry_with_yaml_con ) assert await async_setup_component( hass, - number.DOMAIN, + mqtt.DOMAIN, { - "number": { - "platform": "mqtt", - "command_topic": topic, - "name": "Test Number", + mqtt.DOMAIN: { + number.DOMAIN: { + "command_topic": topic, + "name": "Test Number", + } } }, ) @@ -295,13 +304,14 @@ async def test_run_number_service_optimistic_with_command_template( ) assert await async_setup_component( hass, - number.DOMAIN, + mqtt.DOMAIN, { - "number": { - "platform": "mqtt", - "command_topic": topic, - "name": "Test Number", - "command_template": '{"number": {{ value }} }', + mqtt.DOMAIN: { + number.DOMAIN: { + "command_topic": topic, + "name": "Test Number", + "command_template": '{"number": {{ value }} }', + } } }, ) @@ -361,13 +371,14 @@ async def test_run_number_service(hass, mqtt_mock_entry_with_yaml_config): assert await async_setup_component( hass, - number.DOMAIN, + mqtt.DOMAIN, { - "number": { - "platform": "mqtt", - "command_topic": cmd_topic, - "state_topic": state_topic, - "name": "Test Number", + mqtt.DOMAIN: { + number.DOMAIN: { + "command_topic": cmd_topic, + "state_topic": state_topic, + "name": "Test Number", + } } }, ) @@ -398,14 +409,15 @@ async def test_run_number_service_with_command_template( assert await async_setup_component( hass, - number.DOMAIN, + mqtt.DOMAIN, { - "number": { - "platform": "mqtt", - "command_topic": cmd_topic, - "state_topic": state_topic, - "name": "Test Number", - "command_template": '{"number": {{ value }} }', + mqtt.DOMAIN: { + number.DOMAIN: { + "command_topic": cmd_topic, + "state_topic": state_topic, + "name": "Test Number", + "command_template": '{"number": {{ value }} }', + } } }, ) @@ -434,28 +446,28 @@ async def test_availability_when_connection_lost( ): """Test availability after MQTT disconnection.""" await help_test_availability_when_connection_lost( - hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config): """Test availability without defined availability topic.""" await help_test_availability_without_topic( - hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by default payload with defined topic.""" await help_test_default_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by custom payload with defined topic.""" await help_test_custom_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -464,7 +476,7 @@ async def test_setting_attribute_via_mqtt_json_message( ): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -476,7 +488,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, - DEFAULT_CONFIG, + DEFAULT_CONFIG_LEGACY, MQTT_NUMBER_ATTRIBUTES_BLOCKED, ) @@ -484,7 +496,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_with_template( - hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -493,7 +505,11 @@ async def test_update_with_json_attrs_not_dict( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock_entry_with_yaml_config, caplog, number.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + number.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -502,14 +518,22 @@ async def test_update_with_json_attrs_bad_JSON( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock_entry_with_yaml_config, caplog, number.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + number.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test update of discovered MQTTAttributes.""" await help_test_discovery_update_attr( - hass, mqtt_mock_entry_no_yaml_config, caplog, number.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_no_yaml_config, + caplog, + number.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -540,7 +564,7 @@ async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): async def test_discovery_removal_number(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test removal of discovered number.""" - data = json.dumps(DEFAULT_CONFIG[number.DOMAIN]) + data = json.dumps(DEFAULT_CONFIG_LEGACY[number.DOMAIN]) await help_test_discovery_removal( hass, mqtt_mock_entry_no_yaml_config, caplog, number.DOMAIN, data ) @@ -600,42 +624,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog): async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT number device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT number device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): """Test device registry update.""" await help_test_entity_device_info_update( - hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): """Test device registry remove.""" await help_test_entity_device_info_remove( - hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config): """Test MQTT subscriptions are managed when entity_id is updated.""" await help_test_entity_id_update_subscriptions( - hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT discovery update when entity_id is updated.""" await help_test_entity_id_update_discovery_update( - hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -645,7 +669,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, - DEFAULT_CONFIG, + DEFAULT_CONFIG_LEGACY, SERVICE_SET_VALUE, service_parameters={ATTR_VALUE: 45}, command_payload="45", @@ -658,16 +682,17 @@ async def test_min_max_step_attributes(hass, mqtt_mock_entry_with_yaml_config): topic = "test/number" await async_setup_component( hass, - "number", + mqtt.DOMAIN, { - "number": { - "platform": "mqtt", - "state_topic": topic, - "command_topic": topic, - "name": "Test Number", - "min": 5, - "max": 110, - "step": 20, + mqtt.DOMAIN: { + number.DOMAIN: { + "state_topic": topic, + "command_topic": topic, + "name": "Test Number", + "min": 5, + "max": 110, + "step": 20, + } } }, ) @@ -680,25 +705,24 @@ async def test_min_max_step_attributes(hass, mqtt_mock_entry_with_yaml_config): assert state.attributes.get(ATTR_STEP) == 20 -async def test_invalid_min_max_attributes(hass, caplog, mqtt_mock_entry_no_yaml_config): +async def test_invalid_min_max_attributes(hass, caplog): """Test invalid min/max attributes.""" topic = "test/number" - await async_setup_component( + assert not await async_setup_component( hass, - "number", + mqtt.DOMAIN, { - "number": { - "platform": "mqtt", - "state_topic": topic, - "command_topic": topic, - "name": "Test Number", - "min": 35, - "max": 10, + mqtt.DOMAIN: { + number.DOMAIN: { + "state_topic": topic, + "command_topic": topic, + "name": "Test Number", + "min": 35, + "max": 10, + } } }, ) - await hass.async_block_till_done() - await mqtt_mock_entry_no_yaml_config() assert f"'{CONF_MAX}' must be > '{CONF_MIN}'" in caplog.text @@ -779,15 +803,16 @@ async def test_mqtt_payload_not_a_number_warning( ): """Test warning for MQTT payload which is not a number.""" topic = "test/number" - await async_setup_component( + assert await async_setup_component( hass, - "number", + mqtt.DOMAIN, { - "number": { - "platform": "mqtt", - "state_topic": topic, - "command_topic": topic, - "name": "Test Number", + mqtt.DOMAIN: { + number.DOMAIN: { + "state_topic": topic, + "command_topic": topic, + "name": "Test Number", + } } }, ) @@ -808,15 +833,16 @@ async def test_mqtt_payload_out_of_range_error( topic = "test/number" await async_setup_component( hass, - "number", + mqtt.DOMAIN, { - "number": { - "platform": "mqtt", - "state_topic": topic, - "command_topic": topic, - "name": "Test Number", - "min": 5, - "max": 110, + mqtt.DOMAIN: { + number.DOMAIN: { + "state_topic": topic, + "command_topic": topic, + "name": "Test Number", + "min": 5, + "max": 110, + } } }, ) @@ -856,7 +882,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = NUMBER_DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_publishing_with_custom_encoding( hass, @@ -875,7 +901,7 @@ async def test_publishing_with_custom_encoding( async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = number.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) @@ -884,7 +910,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = number.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) @@ -910,7 +936,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, "number", - DEFAULT_CONFIG["number"], + DEFAULT_CONFIG_LEGACY["number"], topic, value, attribute, @@ -921,7 +947,7 @@ async def test_encoding_subscribable_topics( async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = number.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[platform]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform]) config["name"] = "test" del config["platform"] await help_test_setup_manual_entity_from_yaml(hass, platform, config) @@ -931,7 +957,20 @@ async def test_setup_manual_entity_from_yaml(hass): async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = number.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) + + +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config): + """Test a setup with deprecated yaml platform schema.""" + domain = number.DOMAIN + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config["name"] = "test" + assert await async_setup_component(hass, domain, {domain: config}) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() + assert hass.states.get(f"{domain}.test") is not None diff --git a/tests/components/mqtt/test_scene.py b/tests/components/mqtt/test_scene.py index d676429bf3e..a1c1644cc37 100644 --- a/tests/components/mqtt/test_scene.py +++ b/tests/components/mqtt/test_scene.py @@ -4,7 +4,7 @@ from unittest.mock import patch import pytest -from homeassistant.components import scene +from homeassistant.components import mqtt, scene from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON, STATE_UNKNOWN, Platform import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -28,14 +28,20 @@ from .test_common import ( from tests.common import mock_restore_cache DEFAULT_CONFIG = { - scene.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "payload_on": "test-payload-on", + mqtt.DOMAIN: { + scene.DOMAIN: { + "name": "test", + "command_topic": "test-topic", + "payload_on": "test-payload-on", + } } } +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) +DEFAULT_CONFIG_LEGACY[scene.DOMAIN]["platform"] = mqtt.DOMAIN + @pytest.fixture(autouse=True) def scene_platform_only(): @@ -51,14 +57,15 @@ async def test_sending_mqtt_commands(hass, mqtt_mock_entry_with_yaml_config): assert await async_setup_component( hass, - scene.DOMAIN, + mqtt.DOMAIN, { - scene.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "payload_on": "beer on", - }, + mqtt.DOMAIN: { + scene.DOMAIN: { + "name": "test", + "command_topic": "command-topic", + "payload_on": "beer on", + }, + } }, ) await hass.async_block_till_done() @@ -80,14 +87,14 @@ async def test_availability_when_connection_lost( ): """Test availability after MQTT disconnection.""" await help_test_availability_when_connection_lost( - hass, mqtt_mock_entry_with_yaml_config, scene.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, scene.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config): """Test availability without defined availability topic.""" await help_test_availability_without_topic( - hass, mqtt_mock_entry_with_yaml_config, scene.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, scene.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -168,8 +175,8 @@ async def test_discovery_removal_scene(hass, mqtt_mock_entry_no_yaml_config, cap async def test_discovery_update_payload(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test update of discovered scene.""" - config1 = copy.deepcopy(DEFAULT_CONFIG[scene.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG[scene.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[scene.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[scene.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["payload_on"] = "ON" @@ -216,7 +223,7 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog): async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = scene.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) @@ -225,14 +232,14 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = scene.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = scene.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[platform]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform]) config["name"] = "test" del config["platform"] await help_test_setup_manual_entity_from_yaml(hass, platform, config) @@ -242,7 +249,20 @@ async def test_setup_manual_entity_from_yaml(hass): async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = scene.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) + + +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config): + """Test a setup with deprecated yaml platform schema.""" + domain = scene.DOMAIN + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config["name"] = "test" + assert await async_setup_component(hass, domain, {domain: config}) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() + assert hass.states.get(f"{domain}.test") is not None diff --git a/tests/components/mqtt/test_select.py b/tests/components/mqtt/test_select.py index 085c4d5df00..c66638a18af 100644 --- a/tests/components/mqtt/test_select.py +++ b/tests/components/mqtt/test_select.py @@ -5,7 +5,7 @@ from unittest.mock import patch import pytest -from homeassistant.components import select +from homeassistant.components import mqtt, select from homeassistant.components.mqtt.select import MQTT_SELECT_ATTRIBUTES_BLOCKED from homeassistant.components.select import ( ATTR_OPTION, @@ -56,14 +56,20 @@ from .test_common import ( from tests.common import async_fire_mqtt_message, mock_restore_cache DEFAULT_CONFIG = { - select.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "options": ["milk", "beer"], + mqtt.DOMAIN: { + select.DOMAIN: { + "name": "test", + "command_topic": "test-topic", + "options": ["milk", "beer"], + } } } +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) +DEFAULT_CONFIG_LEGACY[select.DOMAIN]["platform"] = mqtt.DOMAIN + @pytest.fixture(autouse=True) def select_platform_only(): @@ -77,14 +83,15 @@ async def test_run_select_setup(hass, mqtt_mock_entry_with_yaml_config): topic = "test/select" await async_setup_component( hass, - "select", + mqtt.DOMAIN, { - "select": { - "platform": "mqtt", - "state_topic": topic, - "command_topic": topic, - "name": "Test Select", - "options": ["milk", "beer"], + mqtt.DOMAIN: { + select.DOMAIN: { + "state_topic": topic, + "command_topic": topic, + "name": "Test Select", + "options": ["milk", "beer"], + } } }, ) @@ -111,15 +118,16 @@ async def test_value_template(hass, mqtt_mock_entry_with_yaml_config): topic = "test/select" await async_setup_component( hass, - "select", + mqtt.DOMAIN, { - "select": { - "platform": "mqtt", - "state_topic": topic, - "command_topic": topic, - "name": "Test Select", - "options": ["milk", "beer"], - "value_template": "{{ value_json.val }}", + mqtt.DOMAIN: { + select.DOMAIN: { + "state_topic": topic, + "command_topic": topic, + "name": "Test Select", + "options": ["milk", "beer"], + "value_template": "{{ value_json.val }}", + } } }, ) @@ -157,13 +165,14 @@ async def test_run_select_service_optimistic(hass, mqtt_mock_entry_with_yaml_con assert await async_setup_component( hass, - select.DOMAIN, + mqtt.DOMAIN, { - "select": { - "platform": "mqtt", - "command_topic": topic, - "name": "Test Select", - "options": ["milk", "beer"], + mqtt.DOMAIN: { + select.DOMAIN: { + "command_topic": topic, + "name": "Test Select", + "options": ["milk", "beer"], + } } }, ) @@ -198,14 +207,15 @@ async def test_run_select_service_optimistic_with_command_template( assert await async_setup_component( hass, - select.DOMAIN, + mqtt.DOMAIN, { - "select": { - "platform": "mqtt", - "command_topic": topic, - "name": "Test Select", - "options": ["milk", "beer"], - "command_template": '{"option": "{{ value }}"}', + mqtt.DOMAIN: { + select.DOMAIN: { + "command_topic": topic, + "name": "Test Select", + "options": ["milk", "beer"], + "command_template": '{"option": "{{ value }}"}', + } } }, ) @@ -238,14 +248,15 @@ async def test_run_select_service(hass, mqtt_mock_entry_with_yaml_config): assert await async_setup_component( hass, - select.DOMAIN, + mqtt.DOMAIN, { - "select": { - "platform": "mqtt", - "command_topic": cmd_topic, - "state_topic": state_topic, - "name": "Test Select", - "options": ["milk", "beer"], + mqtt.DOMAIN: { + select.DOMAIN: { + "command_topic": cmd_topic, + "state_topic": state_topic, + "name": "Test Select", + "options": ["milk", "beer"], + } } }, ) @@ -276,15 +287,16 @@ async def test_run_select_service_with_command_template( assert await async_setup_component( hass, - select.DOMAIN, + mqtt.DOMAIN, { - "select": { - "platform": "mqtt", - "command_topic": cmd_topic, - "state_topic": state_topic, - "name": "Test Select", - "options": ["milk", "beer"], - "command_template": '{"option": "{{ value }}"}', + mqtt.DOMAIN: { + select.DOMAIN: { + "command_topic": cmd_topic, + "state_topic": state_topic, + "name": "Test Select", + "options": ["milk", "beer"], + "command_template": '{"option": "{{ value }}"}', + } } }, ) @@ -311,28 +323,28 @@ async def test_availability_when_connection_lost( ): """Test availability after MQTT disconnection.""" await help_test_availability_when_connection_lost( - hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config): """Test availability without defined availability topic.""" await help_test_availability_without_topic( - hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by default payload with defined topic.""" await help_test_default_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by custom payload with defined topic.""" await help_test_custom_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -341,7 +353,7 @@ async def test_setting_attribute_via_mqtt_json_message( ): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -353,7 +365,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, - DEFAULT_CONFIG, + DEFAULT_CONFIG_LEGACY, MQTT_SELECT_ATTRIBUTES_BLOCKED, ) @@ -361,7 +373,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_with_template( - hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -370,7 +382,11 @@ async def test_update_with_json_attrs_not_dict( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock_entry_with_yaml_config, caplog, select.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + select.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -379,14 +395,22 @@ async def test_update_with_json_attrs_bad_JSON( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock_entry_with_yaml_config, caplog, select.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + select.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test update of discovered MQTTAttributes.""" await help_test_discovery_update_attr( - hass, mqtt_mock_entry_no_yaml_config, caplog, select.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_no_yaml_config, + caplog, + select.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -419,7 +443,7 @@ async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): async def test_discovery_removal_select(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test removal of discovered select.""" - data = json.dumps(DEFAULT_CONFIG[select.DOMAIN]) + data = json.dumps(DEFAULT_CONFIG_LEGACY[select.DOMAIN]) await help_test_discovery_removal( hass, mqtt_mock_entry_no_yaml_config, caplog, select.DOMAIN, data ) @@ -477,42 +501,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog): async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT select device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT select device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): """Test device registry update.""" await help_test_entity_device_info_update( - hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): """Test device registry remove.""" await help_test_entity_device_info_remove( - hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config): """Test MQTT subscriptions are managed when entity_id is updated.""" await help_test_entity_id_update_subscriptions( - hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT discovery update when entity_id is updated.""" await help_test_entity_id_update_discovery_update( - hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -522,7 +546,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, - DEFAULT_CONFIG, + DEFAULT_CONFIG_LEGACY, select.SERVICE_SELECT_OPTION, service_parameters={ATTR_OPTION: "beer"}, command_payload="beer", @@ -536,14 +560,15 @@ async def test_options_attributes(hass, mqtt_mock_entry_with_yaml_config, option topic = "test/select" await async_setup_component( hass, - "select", + mqtt.DOMAIN, { - "select": { - "platform": "mqtt", - "state_topic": topic, - "command_topic": topic, - "name": "Test select", - "options": options, + mqtt.DOMAIN: { + select.DOMAIN: { + "state_topic": topic, + "command_topic": topic, + "name": "Test select", + "options": options, + } } }, ) @@ -561,14 +586,15 @@ async def test_mqtt_payload_not_an_option_warning( topic = "test/select" await async_setup_component( hass, - "select", + mqtt.DOMAIN, { - "select": { - "platform": "mqtt", - "state_topic": topic, - "command_topic": topic, - "name": "Test Select", - "options": ["milk", "beer"], + mqtt.DOMAIN: { + select.DOMAIN: { + "state_topic": topic, + "command_topic": topic, + "name": "Test Select", + "options": ["milk", "beer"], + } } }, ) @@ -609,7 +635,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = select.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] config["options"] = ["milk", "beer"] await help_test_publishing_with_custom_encoding( @@ -629,7 +655,7 @@ async def test_publishing_with_custom_encoding( async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = select.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) @@ -638,7 +664,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = select.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) @@ -659,7 +685,7 @@ async def test_encoding_subscribable_topics( attribute_value, ): """Test handling of incoming encoded payload.""" - config = copy.deepcopy(DEFAULT_CONFIG["select"]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY["select"]) config["options"] = ["milk", "beer"] await help_test_encoding_subscribable_topics( hass, @@ -677,7 +703,7 @@ async def test_encoding_subscribable_topics( async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = select.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[platform]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform]) config["name"] = "test" del config["platform"] await help_test_setup_manual_entity_from_yaml(hass, platform, config) @@ -687,7 +713,20 @@ async def test_setup_manual_entity_from_yaml(hass): async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = select.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) + + +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config): + """Test a setup with deprecated yaml platform schema.""" + domain = select.DOMAIN + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config["name"] = "test" + assert await async_setup_component(hass, domain, {domain: config}) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() + assert hass.states.get(f"{domain}.test") is not None diff --git a/tests/components/mqtt/test_sensor.py b/tests/components/mqtt/test_sensor.py index b446b1a8b76..c5cf377cf1a 100644 --- a/tests/components/mqtt/test_sensor.py +++ b/tests/components/mqtt/test_sensor.py @@ -6,8 +6,8 @@ from unittest.mock import MagicMock, patch import pytest +from homeassistant.components import mqtt, sensor from homeassistant.components.mqtt.sensor import MQTT_SENSOR_ATTRIBUTES_BLOCKED -import homeassistant.components.sensor as sensor from homeassistant.const import ( EVENT_STATE_CHANGED, STATE_UNAVAILABLE, @@ -64,16 +64,20 @@ from .test_common import ( ) from tests.common import ( - assert_setup_component, async_fire_mqtt_message, async_fire_time_changed, mock_restore_cache_with_extra_data, ) DEFAULT_CONFIG = { - sensor.DOMAIN: {"platform": "mqtt", "name": "test", "state_topic": "test-topic"} + mqtt.DOMAIN: {sensor.DOMAIN: {"name": "test", "state_topic": "test-topic"}} } +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) +DEFAULT_CONFIG_LEGACY[sensor.DOMAIN]["platform"] = mqtt.DOMAIN + @pytest.fixture(autouse=True) def sensor_platform_only(): @@ -88,13 +92,14 @@ async def test_setting_sensor_value_via_mqtt_message( """Test the setting of the value via MQTT.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "unit_of_measurement": "fav unit", + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "unit_of_measurement": "fav unit", + } } }, ) @@ -146,13 +151,14 @@ async def test_setting_sensor_native_value_handling_via_mqtt_message( """Test the setting of the value via MQTT.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "device_class": device_class, + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "device_class": device_class, + } } }, ) @@ -173,15 +179,16 @@ async def test_setting_sensor_value_expires_availability_topic( """Test the expiration of the value.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "expire_after": 4, - "force_update": True, - "availability_topic": "availability-topic", + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "expire_after": 4, + "force_update": True, + "availability_topic": "availability-topic", + } } }, ) @@ -206,15 +213,16 @@ async def test_setting_sensor_value_expires( """Test the expiration of the value.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "unit_of_measurement": "fav unit", - "expire_after": "4", - "force_update": True, + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "unit_of_measurement": "fav unit", + "expire_after": "4", + "force_update": True, + } } }, ) @@ -285,14 +293,15 @@ async def test_setting_sensor_value_via_mqtt_json_message( """Test the setting of the value via MQTT with JSON payload.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "unit_of_measurement": "fav unit", - "value_template": "{{ value_json.val }}", + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "unit_of_measurement": "fav unit", + "value_template": "{{ value_json.val }}", + } } }, ) @@ -311,14 +320,15 @@ async def test_setting_sensor_value_via_mqtt_json_message_and_default_current_st """Test the setting of the value via MQTT with fall back to current state.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "unit_of_measurement": "fav unit", - "value_template": "{{ value_json.val | is_defined }}-{{ value_json.par }}", + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "unit_of_measurement": "fav unit", + "value_template": "{{ value_json.val | is_defined }}-{{ value_json.par }}", + } } }, ) @@ -344,15 +354,16 @@ async def test_setting_sensor_last_reset_via_mqtt_message( """Test the setting of the last_reset property via MQTT.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_class": "total", - "state_topic": "test-topic", - "unit_of_measurement": "fav unit", - "last_reset_topic": "last-reset-topic", + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_class": "total", + "state_topic": "test-topic", + "unit_of_measurement": "fav unit", + "last_reset_topic": "last-reset-topic", + } } }, ) @@ -376,15 +387,16 @@ async def test_setting_sensor_bad_last_reset_via_mqtt_message( """Test the setting of the last_reset property via MQTT.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_class": "total", - "state_topic": "test-topic", - "unit_of_measurement": "fav unit", - "last_reset_topic": "last-reset-topic", + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_class": "total", + "state_topic": "test-topic", + "unit_of_measurement": "fav unit", + "last_reset_topic": "last-reset-topic", + } } }, ) @@ -403,15 +415,16 @@ async def test_setting_sensor_empty_last_reset_via_mqtt_message( """Test the setting of the last_reset property via MQTT.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_class": "total", - "state_topic": "test-topic", - "unit_of_measurement": "fav unit", - "last_reset_topic": "last-reset-topic", + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_class": "total", + "state_topic": "test-topic", + "unit_of_measurement": "fav unit", + "last_reset_topic": "last-reset-topic", + } } }, ) @@ -430,16 +443,17 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message( """Test the setting of the value via MQTT with JSON payload.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_class": "total", - "state_topic": "test-topic", - "unit_of_measurement": "fav unit", - "last_reset_topic": "last-reset-topic", - "last_reset_value_template": "{{ value_json.last_reset }}", + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_class": "total", + "state_topic": "test-topic", + "unit_of_measurement": "fav unit", + "last_reset_topic": "last-reset-topic", + "last_reset_value_template": "{{ value_json.last_reset }}", + } } }, ) @@ -460,19 +474,20 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message_2( """Test the setting of the value via MQTT with JSON payload.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - **{ - "platform": "mqtt", - "name": "test", - "state_class": "total", - "state_topic": "test-topic", - "unit_of_measurement": "kWh", - "value_template": "{{ value_json.value | float / 60000 }}", - "last_reset_value_template": "{{ utcnow().fromtimestamp(value_json.time / 1000, tz=utcnow().tzinfo) }}", - }, - **extra, + mqtt.DOMAIN: { + sensor.DOMAIN: { + **{ + "name": "test", + "state_class": "total", + "state_topic": "test-topic", + "unit_of_measurement": "kWh", + "value_template": "{{ value_json.value | float / 60000 }}", + "last_reset_value_template": "{{ utcnow().fromtimestamp(value_json.time / 1000, tz=utcnow().tzinfo) }}", + }, + **extra, + } } }, ) @@ -498,13 +513,14 @@ async def test_force_update_disabled(hass, mqtt_mock_entry_with_yaml_config): """Test force update option.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "unit_of_measurement": "fav unit", + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "unit_of_measurement": "fav unit", + } } }, ) @@ -532,14 +548,15 @@ async def test_force_update_enabled(hass, mqtt_mock_entry_with_yaml_config): """Test force update option.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "unit_of_measurement": "fav unit", - "force_update": True, + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "unit_of_measurement": "fav unit", + "force_update": True, + } } }, ) @@ -568,21 +585,21 @@ async def test_availability_when_connection_lost( ): """Test availability after MQTT disconnection.""" await help_test_availability_when_connection_lost( - hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config): """Test availability without defined availability topic.""" await help_test_availability_without_topic( - hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by default payload with defined topic.""" await help_test_default_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -591,7 +608,7 @@ async def test_default_availability_list_payload( ): """Test availability by default payload with defined topic.""" await help_test_default_availability_list_payload( - hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -600,7 +617,7 @@ async def test_default_availability_list_payload_all( ): """Test availability by default payload with defined topic.""" await help_test_default_availability_list_payload_all( - hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -609,7 +626,7 @@ async def test_default_availability_list_payload_any( ): """Test availability by default payload with defined topic.""" await help_test_default_availability_list_payload_any( - hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -618,21 +635,25 @@ async def test_default_availability_list_single( ): """Test availability list and availability_topic are mutually exclusive.""" await help_test_default_availability_list_single( - hass, mqtt_mock_entry_no_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_no_yaml_config, + caplog, + sensor.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by custom payload with defined topic.""" await help_test_custom_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_discovery_update_availability(hass, mqtt_mock_entry_no_yaml_config): """Test availability discovery update.""" await help_test_discovery_update_availability( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -642,11 +663,12 @@ async def test_invalid_device_class(hass, mqtt_mock_entry_no_yaml_config): hass, sensor.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "device_class": "foobarnotreal", + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "device_class": "foobarnotreal", + } } }, ) @@ -661,17 +683,18 @@ async def test_valid_device_class(hass, mqtt_mock_entry_with_yaml_config): """Test device_class option with valid values.""" assert await async_setup_component( hass, - "sensor", + mqtt.DOMAIN, { - "sensor": [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "device_class": "temperature", - }, - {"platform": "mqtt", "name": "Test 2", "state_topic": "test-topic"}, - ] + mqtt.DOMAIN: { + sensor.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "device_class": "temperature", + }, + {"name": "Test 2", "state_topic": "test-topic"}, + ] + } }, ) await hass.async_block_till_done() @@ -689,11 +712,12 @@ async def test_invalid_state_class(hass, mqtt_mock_entry_no_yaml_config): hass, sensor.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "state_class": "foobarnotreal", + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "state_class": "foobarnotreal", + } } }, ) @@ -708,17 +732,18 @@ async def test_valid_state_class(hass, mqtt_mock_entry_with_yaml_config): """Test state_class option with valid values.""" assert await async_setup_component( hass, - "sensor", + mqtt.DOMAIN, { - "sensor": [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "state_class": "measurement", - }, - {"platform": "mqtt", "name": "Test 2", "state_topic": "test-topic"}, - ] + mqtt.DOMAIN: { + sensor.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "state_class": "measurement", + }, + {"name": "Test 2", "state_topic": "test-topic"}, + ] + } }, ) await hass.async_block_till_done() @@ -735,7 +760,7 @@ async def test_setting_attribute_via_mqtt_json_message( ): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -747,7 +772,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, - DEFAULT_CONFIG, + DEFAULT_CONFIG_LEGACY, MQTT_SENSOR_ATTRIBUTES_BLOCKED, ) @@ -755,7 +780,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_with_template( - hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -764,7 +789,11 @@ async def test_update_with_json_attrs_not_dict( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock_entry_with_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + sensor.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -773,14 +802,22 @@ async def test_update_with_json_attrs_bad_JSON( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock_entry_with_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + sensor.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test update of discovered MQTTAttributes.""" await help_test_discovery_update_attr( - hass, mqtt_mock_entry_no_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_no_yaml_config, + caplog, + sensor.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -914,42 +951,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog): async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT sensor device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT sensor device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): """Test device registry update.""" await help_test_entity_device_info_update( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): """Test device registry remove.""" await help_test_entity_device_info_remove( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config): """Test MQTT subscriptions are managed when entity_id is updated.""" await help_test_entity_id_update_subscriptions( - hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT discovery update when entity_id is updated.""" await help_test_entity_id_update_discovery_update( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -967,7 +1004,6 @@ async def test_entity_device_info_with_hub(hass, mqtt_mock_entry_no_yaml_config) data = json.dumps( { - "platform": "mqtt", "name": "Test 1", "state_topic": "test-topic", "device": {"identifiers": ["helloworld"], "via_device": "hub-id"}, @@ -985,42 +1021,42 @@ async def test_entity_device_info_with_hub(hass, mqtt_mock_entry_no_yaml_config) async def test_entity_debug_info(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT sensor debug info.""" await help_test_entity_debug_info( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_debug_info_max_messages(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT sensor debug info.""" await help_test_entity_debug_info_max_messages( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT debug info.""" await help_test_entity_debug_info_message( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG, None + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY, None ) async def test_entity_debug_info_remove(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT sensor debug info.""" await help_test_entity_debug_info_remove( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_debug_info_update_entity_id(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT sensor debug info.""" await help_test_entity_debug_info_update_entity_id( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_disabled_by_default(hass, mqtt_mock_entry_no_yaml_config): """Test entity disabled by default.""" await help_test_entity_disabled_by_default( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -1028,7 +1064,7 @@ async def test_entity_disabled_by_default(hass, mqtt_mock_entry_no_yaml_config): async def test_entity_category(hass, mqtt_mock_entry_no_yaml_config): """Test entity category.""" await help_test_entity_category( - hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -1036,19 +1072,20 @@ async def test_value_template_with_entity_id(hass, mqtt_mock_entry_with_yaml_con """Test the access to attributes in value_template via the entity_id.""" assert await async_setup_component( hass, - sensor.DOMAIN, + mqtt.DOMAIN, { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "unit_of_measurement": "fav unit", - "value_template": '\ + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "unit_of_measurement": "fav unit", + "value_template": '\ {% if state_attr(entity_id, "friendly_name") == "test" %} \ {{ value | int + 1 }} \ {% else %} \ {{ value }} \ {% endif %}', + } } }, ) @@ -1064,7 +1101,7 @@ async def test_value_template_with_entity_id(hass, mqtt_mock_entry_with_yaml_con async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = sensor.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) @@ -1073,7 +1110,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = sensor.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) @@ -1082,14 +1119,14 @@ async def test_cleanup_triggers_and_restoring_state( ): """Test cleanup old triggers at reloading and restoring the state.""" domain = sensor.DOMAIN - config1 = copy.deepcopy(DEFAULT_CONFIG[domain]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain]) config1["name"] = "test1" config1["expire_after"] = 30 config1["state_topic"] = "test-topic1" config1["device_class"] = "temperature" config1["unit_of_measurement"] = TEMP_FAHRENHEIT - config2 = copy.deepcopy(DEFAULT_CONFIG[domain]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain]) config2["name"] = "test2" config2["expire_after"] = 5 config2["state_topic"] = "test-topic2" @@ -1100,8 +1137,8 @@ async def test_cleanup_triggers_and_restoring_state( assert await async_setup_component( hass, - domain, - {domain: [config1, config2]}, + mqtt.DOMAIN, + {mqtt.DOMAIN: {domain: [config1, config2]}}, ) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -1116,7 +1153,7 @@ async def test_cleanup_triggers_and_restoring_state( freezer.move_to("2022-02-02 12:01:10+01:00") await help_test_reload_with_config( - hass, caplog, tmp_path, {domain: [config1, config2]} + hass, caplog, tmp_path, {mqtt.DOMAIN: {domain: [config1, config2]}} ) await hass.async_block_till_done() @@ -1150,7 +1187,7 @@ async def test_skip_restoring_state_with_over_due_expire_trigger( freezer.move_to("2022-02-02 12:02:00+01:00") domain = sensor.DOMAIN - config3 = copy.deepcopy(DEFAULT_CONFIG[domain]) + config3 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain]) config3["name"] = "test3" config3["expire_after"] = 10 config3["state_topic"] = "test-topic3" @@ -1163,10 +1200,11 @@ async def test_skip_restoring_state_with_over_due_expire_trigger( fake_extra_data = MagicMock() mock_restore_cache_with_extra_data(hass, ((fake_state, fake_extra_data),)) - with assert_setup_component(1, domain): - assert await async_setup_component(hass, domain, {domain: config3}) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + assert await async_setup_component( + hass, mqtt.DOMAIN, {mqtt.DOMAIN: {domain: config3}} + ) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() assert "Skip state recovery after reload for sensor.test3" in caplog.text @@ -1192,7 +1230,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, sensor.DOMAIN, - DEFAULT_CONFIG[sensor.DOMAIN], + DEFAULT_CONFIG_LEGACY[sensor.DOMAIN], topic, value, attribute, @@ -1204,7 +1242,7 @@ async def test_encoding_subscribable_topics( async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = sensor.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[platform]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform]) config["name"] = "test" del config["platform"] await help_test_setup_manual_entity_from_yaml(hass, platform, config) @@ -1214,7 +1252,20 @@ async def test_setup_manual_entity_from_yaml(hass): async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = sensor.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) + + +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config): + """Test a setup with deprecated yaml platform schema.""" + domain = sensor.DOMAIN + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config["name"] = "test" + assert await async_setup_component(hass, domain, {domain: config}) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() + assert hass.states.get(f"{domain}.test") is not None diff --git a/tests/components/mqtt/test_siren.py b/tests/components/mqtt/test_siren.py index 13648f1c486..fd91847f767 100644 --- a/tests/components/mqtt/test_siren.py +++ b/tests/components/mqtt/test_siren.py @@ -4,7 +4,7 @@ from unittest.mock import patch import pytest -from homeassistant.components import siren +from homeassistant.components import mqtt, siren from homeassistant.components.siren.const import ATTR_VOLUME_LEVEL from homeassistant.const import ( ATTR_ASSUMED_STATE, @@ -53,9 +53,14 @@ from .test_common import ( from tests.common import async_fire_mqtt_message DEFAULT_CONFIG = { - siren.DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"} + mqtt.DOMAIN: {siren.DOMAIN: {"name": "test", "command_topic": "test-topic"}} } +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) +DEFAULT_CONFIG_LEGACY[siren.DOMAIN]["platform"] = mqtt.DOMAIN + @pytest.fixture(autouse=True) def siren_platform_only(): @@ -83,15 +88,16 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi """Test the controlling state via topic.""" assert await async_setup_component( hass, - siren.DOMAIN, + mqtt.DOMAIN, { - siren.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_on": 1, - "payload_off": 0, + mqtt.DOMAIN: { + siren.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_on": 1, + "payload_off": 0, + } } }, ) @@ -119,15 +125,16 @@ async def test_sending_mqtt_commands_and_optimistic( """Test the sending MQTT commands in optimistic mode.""" assert await async_setup_component( hass, - siren.DOMAIN, + mqtt.DOMAIN, { - siren.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "payload_on": "beer on", - "payload_off": "beer off", - "qos": "2", + mqtt.DOMAIN: { + siren.DOMAIN: { + "name": "test", + "command_topic": "command-topic", + "payload_on": "beer on", + "payload_off": "beer off", + "qos": "2", + } } }, ) @@ -162,16 +169,17 @@ async def test_controlling_state_via_topic_and_json_message( """Test the controlling state via topic and JSON message.""" assert await async_setup_component( hass, - siren.DOMAIN, + mqtt.DOMAIN, { - siren.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_on": "beer on", - "payload_off": "beer off", - "state_value_template": "{{ value_json.val }}", + mqtt.DOMAIN: { + siren.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_on": "beer on", + "payload_off": "beer off", + "state_value_template": "{{ value_json.val }}", + } } }, ) @@ -202,16 +210,17 @@ async def test_controlling_state_and_attributes_with_json_message_without_templa """Test the controlling state via topic and JSON message without a value template.""" assert await async_setup_component( hass, - siren.DOMAIN, + mqtt.DOMAIN, { - siren.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_on": "beer on", - "payload_off": "beer off", - "available_tones": ["ping", "siren", "bell"], + mqtt.DOMAIN: { + siren.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_on": "beer on", + "payload_off": "beer off", + "available_tones": ["ping", "siren", "bell"], + } } }, ) @@ -284,7 +293,6 @@ async def test_filtering_not_supported_attributes_optimistic( ): """Test setting attributes with support flags optimistic.""" config = { - "platform": "mqtt", "command_topic": "command-topic", "available_tones": ["ping", "siren", "bell"], } @@ -300,8 +308,8 @@ async def test_filtering_not_supported_attributes_optimistic( assert await async_setup_component( hass, - siren.DOMAIN, - {siren.DOMAIN: [config1, config2, config3]}, + mqtt.DOMAIN, + {mqtt.DOMAIN: {siren.DOMAIN: [config1, config2, config3]}}, ) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -370,7 +378,6 @@ async def test_filtering_not_supported_attributes_via_state( ): """Test setting attributes with support flags via state.""" config = { - "platform": "mqtt", "command_topic": "command-topic", "available_tones": ["ping", "siren", "bell"], } @@ -389,8 +396,8 @@ async def test_filtering_not_supported_attributes_via_state( assert await async_setup_component( hass, - siren.DOMAIN, - {siren.DOMAIN: [config1, config2, config3]}, + mqtt.DOMAIN, + {mqtt.DOMAIN: {siren.DOMAIN: [config1, config2, config3]}}, ) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -450,14 +457,14 @@ async def test_availability_when_connection_lost( ): """Test availability after MQTT disconnection.""" await help_test_availability_when_connection_lost( - hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config): """Test availability without defined availability topic.""" await help_test_availability_without_topic( - hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -513,17 +520,18 @@ async def test_custom_state_payload(hass, mqtt_mock_entry_with_yaml_config): """Test the state payload.""" assert await async_setup_component( hass, - siren.DOMAIN, + mqtt.DOMAIN, { - siren.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_on": 1, - "payload_off": 0, - "state_on": "HIGH", - "state_off": "LOW", + mqtt.DOMAIN: { + siren.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_on": 1, + "payload_off": 0, + "state_on": "HIGH", + "state_off": "LOW", + } } }, ) @@ -550,7 +558,7 @@ async def test_setting_attribute_via_mqtt_json_message( ): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -559,14 +567,14 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( ): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_blocked_attribute_via_mqtt_json_message( - hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG, {} + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY, {} ) async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_with_template( - hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -575,7 +583,11 @@ async def test_update_with_json_attrs_not_dict( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock_entry_with_yaml_config, caplog, siren.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + siren.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -584,14 +596,22 @@ async def test_update_with_json_attrs_bad_JSON( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock_entry_with_yaml_config, caplog, siren.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + siren.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test update of discovered MQTTAttributes.""" await help_test_discovery_update_attr( - hass, mqtt_mock_entry_no_yaml_config, caplog, siren.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_no_yaml_config, + caplog, + siren.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -636,8 +656,8 @@ async def test_discovery_update_siren_topic_template( hass, mqtt_mock_entry_no_yaml_config, caplog ): """Test update of discovered siren.""" - config1 = copy.deepcopy(DEFAULT_CONFIG[siren.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG[siren.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[siren.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[siren.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "siren/state1" @@ -673,8 +693,8 @@ async def test_discovery_update_siren_template( hass, mqtt_mock_entry_no_yaml_config, caplog ): """Test update of discovered siren.""" - config1 = copy.deepcopy(DEFAULT_CONFIG[siren.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG[siren.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[siren.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[siren.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "siren/state1" @@ -706,7 +726,7 @@ async def test_discovery_update_siren_template( async def test_command_templates(hass, mqtt_mock_entry_with_yaml_config, caplog): """Test siren with command templates optimistic.""" - config1 = copy.deepcopy(DEFAULT_CONFIG[siren.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][siren.DOMAIN]) config1["name"] = "Beer" config1["available_tones"] = ["ping", "chimes"] config1[ @@ -719,8 +739,8 @@ async def test_command_templates(hass, mqtt_mock_entry_with_yaml_config, caplog) assert await async_setup_component( hass, - siren.DOMAIN, - {siren.DOMAIN: [config1, config2]}, + mqtt.DOMAIN, + {mqtt.DOMAIN: {siren.DOMAIN: [config1, config2]}}, ) await hass.async_block_till_done() mqtt_mock = await mqtt_mock_entry_with_yaml_config() @@ -824,42 +844,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog): async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT siren device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT siren device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): """Test device registry update.""" await help_test_entity_device_info_update( - hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): """Test device registry remove.""" await help_test_entity_device_info_remove( - hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config): """Test MQTT subscriptions are managed when entity_id is updated.""" await help_test_entity_id_update_subscriptions( - hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT discovery update when entity_id is updated.""" await help_test_entity_id_update_discovery_update( - hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -869,7 +889,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, - DEFAULT_CONFIG, + DEFAULT_CONFIG_LEGACY, siren.SERVICE_TURN_ON, command_payload='{"state":"ON"}', ) @@ -906,7 +926,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with command templates and different encoding.""" domain = siren.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[domain]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) config[siren.ATTR_AVAILABLE_TONES] = ["siren", "xylophone"] await help_test_publishing_with_custom_encoding( @@ -926,7 +946,7 @@ async def test_publishing_with_custom_encoding( async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = siren.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) @@ -935,7 +955,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = siren.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) @@ -960,7 +980,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, siren.DOMAIN, - DEFAULT_CONFIG[siren.DOMAIN], + DEFAULT_CONFIG_LEGACY[siren.DOMAIN], topic, value, attribute, @@ -971,7 +991,7 @@ async def test_encoding_subscribable_topics( async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = siren.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[platform]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform]) config["name"] = "test" del config["platform"] await help_test_setup_manual_entity_from_yaml(hass, platform, config) @@ -981,7 +1001,20 @@ async def test_setup_manual_entity_from_yaml(hass): async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = siren.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) + + +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config): + """Test a setup with deprecated yaml platform schema.""" + domain = siren.DOMAIN + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config["name"] = "test" + assert await async_setup_component(hass, domain, {domain: config}) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() + assert hass.states.get(f"{domain}.test") is not None diff --git a/tests/components/mqtt/test_state_vacuum.py b/tests/components/mqtt/test_state_vacuum.py index b0b89c28646..4c2fbf3a596 100644 --- a/tests/components/mqtt/test_state_vacuum.py +++ b/tests/components/mqtt/test_state_vacuum.py @@ -5,7 +5,7 @@ from unittest.mock import patch import pytest -from homeassistant.components import vacuum +from homeassistant.components import mqtt, vacuum from homeassistant.components.mqtt.const import CONF_COMMAND_TOPIC, CONF_STATE_TOPIC from homeassistant.components.mqtt.vacuum import CONF_SCHEMA, schema_state as mqttvacuum from homeassistant.components.mqtt.vacuum.const import MQTT_VACUUM_ATTRIBUTES_BLOCKED @@ -73,19 +73,27 @@ SEND_COMMAND_TOPIC = "vacuum/send_command" STATE_TOPIC = "vacuum/state" DEFAULT_CONFIG = { - CONF_PLATFORM: "mqtt", - CONF_SCHEMA: "state", - CONF_NAME: "mqtttest", - CONF_COMMAND_TOPIC: COMMAND_TOPIC, - mqttvacuum.CONF_SEND_COMMAND_TOPIC: SEND_COMMAND_TOPIC, - CONF_STATE_TOPIC: STATE_TOPIC, - mqttvacuum.CONF_SET_FAN_SPEED_TOPIC: "vacuum/set_fan_speed", - mqttvacuum.CONF_FAN_SPEED_LIST: ["min", "medium", "high", "max"], + mqtt.DOMAIN: { + vacuum.DOMAIN: { + CONF_SCHEMA: "state", + CONF_NAME: "mqtttest", + CONF_COMMAND_TOPIC: COMMAND_TOPIC, + mqttvacuum.CONF_SEND_COMMAND_TOPIC: SEND_COMMAND_TOPIC, + CONF_STATE_TOPIC: STATE_TOPIC, + mqttvacuum.CONF_SET_FAN_SPEED_TOPIC: "vacuum/set_fan_speed", + mqttvacuum.CONF_FAN_SPEED_LIST: ["min", "medium", "high", "max"], + } + } } -DEFAULT_CONFIG_2 = { - vacuum.DOMAIN: {"platform": "mqtt", "schema": "state", "name": "test"} -} +DEFAULT_CONFIG_2 = {mqtt.DOMAIN: {vacuum.DOMAIN: {"schema": "state", "name": "test"}}} + +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +DEFAULT_CONFIG_LEGACY = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) +DEFAULT_CONFIG_LEGACY[vacuum.DOMAIN][CONF_PLATFORM] = mqtt.DOMAIN +DEFAULT_CONFIG_2_LEGACY = deepcopy(DEFAULT_CONFIG_2[mqtt.DOMAIN]) +DEFAULT_CONFIG_2_LEGACY[vacuum.DOMAIN][CONF_PLATFORM] = mqtt.DOMAIN @pytest.fixture(autouse=True) @@ -97,9 +105,7 @@ def vacuum_platform_only(): async def test_default_supported_features(hass, mqtt_mock_entry_with_yaml_config): """Test that the correct supported features.""" - assert await async_setup_component( - hass, vacuum.DOMAIN, {vacuum.DOMAIN: DEFAULT_CONFIG} - ) + assert await async_setup_component(hass, mqtt.DOMAIN, DEFAULT_CONFIG) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() entity = hass.states.get("vacuum.mqtttest") @@ -111,12 +117,14 @@ async def test_default_supported_features(hass, mqtt_mock_entry_with_yaml_config async def test_all_commands(hass, mqtt_mock_entry_with_yaml_config): """Test simple commands send to the vacuum.""" - config = deepcopy(DEFAULT_CONFIG) + config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN]) config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING ) - assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config}) + assert await async_setup_component( + hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}} + ) await hass.async_block_till_done() mqtt_mock = await mqtt_mock_entry_with_yaml_config() @@ -185,13 +193,15 @@ async def test_commands_without_supported_features( hass, mqtt_mock_entry_with_yaml_config ): """Test commands which are not supported by the vacuum.""" - config = deepcopy(DEFAULT_CONFIG) + config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN]) services = mqttvacuum.STRING_TO_SERVICE["status"] config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( services, SERVICE_TO_STRING ) - assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config}) + assert await async_setup_component( + hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}} + ) await hass.async_block_till_done() mqtt_mock = await mqtt_mock_entry_with_yaml_config() @@ -243,12 +253,14 @@ async def test_commands_without_supported_features( async def test_status(hass, mqtt_mock_entry_with_yaml_config): """Test status updates from the vacuum.""" - config = deepcopy(DEFAULT_CONFIG) + config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN]) config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING ) - assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config}) + assert await async_setup_component( + hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}} + ) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() state = hass.states.get("vacuum.mqtttest") @@ -288,13 +300,15 @@ async def test_status(hass, mqtt_mock_entry_with_yaml_config): async def test_no_fan_vacuum(hass, mqtt_mock_entry_with_yaml_config): """Test status updates from the vacuum when fan is not supported.""" - config = deepcopy(DEFAULT_CONFIG) + config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN]) del config[mqttvacuum.CONF_FAN_SPEED_LIST] config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( mqttvacuum.DEFAULT_SERVICES, SERVICE_TO_STRING ) - assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config}) + assert await async_setup_component( + hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}} + ) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -340,12 +354,14 @@ async def test_no_fan_vacuum(hass, mqtt_mock_entry_with_yaml_config): @pytest.mark.no_fail_on_log_exception async def test_status_invalid_json(hass, mqtt_mock_entry_with_yaml_config): """Test to make sure nothing breaks if the vacuum sends bad JSON.""" - config = deepcopy(DEFAULT_CONFIG) + config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN]) config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING ) - assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config}) + assert await async_setup_component( + hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}} + ) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -359,28 +375,28 @@ async def test_availability_when_connection_lost( ): """Test availability after MQTT disconnection.""" await help_test_availability_when_connection_lost( - hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config): """Test availability without defined availability topic.""" await help_test_availability_without_topic( - hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by default payload with defined topic.""" await help_test_default_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by custom payload with defined topic.""" await help_test_custom_availability_payload( - hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) @@ -389,7 +405,7 @@ async def test_setting_attribute_via_mqtt_json_message( ): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) @@ -401,7 +417,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, - DEFAULT_CONFIG_2, + DEFAULT_CONFIG_2_LEGACY, MQTT_VACUUM_ATTRIBUTES_BLOCKED, ) @@ -409,7 +425,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_with_template( - hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) @@ -418,7 +434,11 @@ async def test_update_with_json_attrs_not_dict( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock_entry_with_yaml_config, caplog, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + vacuum.DOMAIN, + DEFAULT_CONFIG_2_LEGACY, ) @@ -427,14 +447,22 @@ async def test_update_with_json_attrs_bad_json( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock_entry_with_yaml_config, caplog, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + vacuum.DOMAIN, + DEFAULT_CONFIG_2_LEGACY, ) async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test update of discovered MQTTAttributes.""" await help_test_discovery_update_attr( - hass, mqtt_mock_entry_no_yaml_config, caplog, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, + mqtt_mock_entry_no_yaml_config, + caplog, + vacuum.DOMAIN, + DEFAULT_CONFIG_2_LEGACY, ) @@ -511,42 +539,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog): async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT vacuum device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT vacuum device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): """Test device registry update.""" await help_test_entity_device_info_update( - hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): """Test device registry remove.""" await help_test_entity_device_info_remove( - hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config): """Test MQTT subscriptions are managed when entity_id is updated.""" await help_test_entity_id_update_subscriptions( - hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT discovery update when entity_id is updated.""" await help_test_entity_id_update_discovery_update( - hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY ) @@ -556,7 +584,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, - DEFAULT_CONFIG_2, + DEFAULT_CONFIG_2_LEGACY, vacuum.SERVICE_START, command_payload="start", state_payload="{}", @@ -615,7 +643,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = vacuum.DOMAIN - config = deepcopy(DEFAULT_CONFIG) + config = deepcopy(DEFAULT_CONFIG_LEGACY[domain]) config["supported_features"] = [ "battery", "clean_spot", @@ -646,7 +674,7 @@ async def test_publishing_with_custom_encoding( async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = vacuum.DOMAIN - config = DEFAULT_CONFIG + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) @@ -655,7 +683,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = vacuum.DOMAIN - config = DEFAULT_CONFIG + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) @@ -691,7 +719,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, vacuum.DOMAIN, - DEFAULT_CONFIG, + DEFAULT_CONFIG_LEGACY[vacuum.DOMAIN], topic, value, attribute, @@ -703,8 +731,21 @@ async def test_encoding_subscribable_topics( async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = vacuum.DOMAIN - config = deepcopy(DEFAULT_CONFIG) + config = deepcopy(DEFAULT_CONFIG_LEGACY[platform]) config["name"] = "test" del config["platform"] await help_test_setup_manual_entity_from_yaml(hass, platform, config) assert hass.states.get(f"{platform}.test") is not None + + +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config): + """Test a setup with deprecated yaml platform schema.""" + domain = vacuum.DOMAIN + config = deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config["name"] = "test" + assert await async_setup_component(hass, domain, {domain: config}) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() + assert hass.states.get(f"{domain}.test") is not None diff --git a/tests/components/mqtt/test_switch.py b/tests/components/mqtt/test_switch.py index ac69b17e18e..87d919f41c5 100644 --- a/tests/components/mqtt/test_switch.py +++ b/tests/components/mqtt/test_switch.py @@ -4,7 +4,7 @@ from unittest.mock import patch import pytest -from homeassistant.components import switch +from homeassistant.components import mqtt, switch from homeassistant.const import ( ATTR_ASSUMED_STATE, ATTR_DEVICE_CLASS, @@ -51,9 +51,14 @@ from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.components.switch import common DEFAULT_CONFIG = { - switch.DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"} + mqtt.DOMAIN: {switch.DOMAIN: {"name": "test", "command_topic": "test-topic"}} } +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) +DEFAULT_CONFIG_LEGACY[switch.DOMAIN]["platform"] = mqtt.DOMAIN + @pytest.fixture(autouse=True) def switch_platform_only(): @@ -66,16 +71,17 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi """Test the controlling state via topic.""" assert await async_setup_component( hass, - switch.DOMAIN, + mqtt.DOMAIN, { - switch.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_on": 1, - "payload_off": 0, - "device_class": "switch", + mqtt.DOMAIN: { + switch.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_on": 1, + "payload_off": 0, + "device_class": "switch", + } } }, ) @@ -112,15 +118,16 @@ async def test_sending_mqtt_commands_and_optimistic( assert await async_setup_component( hass, - switch.DOMAIN, + mqtt.DOMAIN, { - switch.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "payload_on": "beer on", - "payload_off": "beer off", - "qos": "2", + mqtt.DOMAIN: { + switch.DOMAIN: { + "name": "test", + "command_topic": "command-topic", + "payload_on": "beer on", + "payload_off": "beer off", + "qos": "2", + } } }, ) @@ -155,12 +162,13 @@ async def test_sending_inital_state_and_optimistic( """Test the initial state in optimistic mode.""" assert await async_setup_component( hass, - switch.DOMAIN, + mqtt.DOMAIN, { - switch.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", + mqtt.DOMAIN: { + switch.DOMAIN: { + "name": "test", + "command_topic": "command-topic", + } } }, ) @@ -178,16 +186,17 @@ async def test_controlling_state_via_topic_and_json_message( """Test the controlling state via topic and JSON message.""" assert await async_setup_component( hass, - switch.DOMAIN, + mqtt.DOMAIN, { - switch.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_on": "beer on", - "payload_off": "beer off", - "value_template": "{{ value_json.val }}", + mqtt.DOMAIN: { + switch.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_on": "beer on", + "payload_off": "beer off", + "value_template": "{{ value_json.val }}", + } } }, ) @@ -218,14 +227,14 @@ async def test_availability_when_connection_lost( ): """Test availability after MQTT disconnection.""" await help_test_availability_when_connection_lost( - hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config): """Test availability without defined availability topic.""" await help_test_availability_without_topic( - hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -281,17 +290,18 @@ async def test_custom_state_payload(hass, mqtt_mock_entry_with_yaml_config): """Test the state payload.""" assert await async_setup_component( hass, - switch.DOMAIN, + mqtt.DOMAIN, { - switch.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_on": 1, - "payload_off": 0, - "state_on": "HIGH", - "state_off": "LOW", + mqtt.DOMAIN: { + switch.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_on": 1, + "payload_off": 0, + "state_on": "HIGH", + "state_off": "LOW", + } } }, ) @@ -318,7 +328,7 @@ async def test_setting_attribute_via_mqtt_json_message( ): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -327,14 +337,14 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( ): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_blocked_attribute_via_mqtt_json_message( - hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG, {} + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY, {} ) async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_with_template( - hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -343,7 +353,11 @@ async def test_update_with_json_attrs_not_dict( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock_entry_with_yaml_config, caplog, switch.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + switch.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -352,14 +366,22 @@ async def test_update_with_json_attrs_bad_JSON( ): """Test attributes get extracted from a JSON result.""" await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock_entry_with_yaml_config, caplog, switch.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_with_yaml_config, + caplog, + switch.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test update of discovered MQTTAttributes.""" await help_test_discovery_update_attr( - hass, mqtt_mock_entry_no_yaml_config, caplog, switch.DOMAIN, DEFAULT_CONFIG + hass, + mqtt_mock_entry_no_yaml_config, + caplog, + switch.DOMAIN, + DEFAULT_CONFIG_LEGACY, ) @@ -404,8 +426,8 @@ async def test_discovery_update_switch_topic_template( hass, mqtt_mock_entry_no_yaml_config, caplog ): """Test update of discovered switch.""" - config1 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "switch/state1" @@ -441,8 +463,8 @@ async def test_discovery_update_switch_template( hass, mqtt_mock_entry_no_yaml_config, caplog ): """Test update of discovered switch.""" - config1 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "switch/state1" @@ -512,42 +534,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog): async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT switch device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT switch device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): """Test device registry update.""" await help_test_entity_device_info_update( - hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): """Test device registry remove.""" await help_test_entity_device_info_remove( - hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config): """Test MQTT subscriptions are managed when entity_id is updated.""" await help_test_entity_id_update_subscriptions( - hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY ) async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT discovery update when entity_id is updated.""" await help_test_entity_id_update_discovery_update( - hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY ) @@ -557,7 +579,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, - DEFAULT_CONFIG, + DEFAULT_CONFIG_LEGACY, switch.SERVICE_TURN_ON, ) @@ -593,7 +615,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = switch.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_publishing_with_custom_encoding( hass, @@ -612,7 +634,7 @@ async def test_publishing_with_custom_encoding( async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = switch.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) @@ -621,7 +643,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = switch.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) @@ -646,7 +668,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, switch.DOMAIN, - DEFAULT_CONFIG[switch.DOMAIN], + DEFAULT_CONFIG_LEGACY[switch.DOMAIN], topic, value, attribute, @@ -657,7 +679,7 @@ async def test_encoding_subscribable_topics( async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = switch.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG[platform]) + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform]) config["name"] = "test" del config["platform"] await help_test_setup_manual_entity_from_yaml(hass, platform, config) @@ -667,7 +689,20 @@ async def test_setup_manual_entity_from_yaml(hass): async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = switch.DOMAIN - config = DEFAULT_CONFIG[domain] + config = DEFAULT_CONFIG_LEGACY[domain] await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) + + +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 +async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config): + """Test a setup with deprecated yaml platform schema.""" + domain = switch.DOMAIN + config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config["name"] = "test" + assert await async_setup_component(hass, domain, {domain: config}) + await hass.async_block_till_done() + await mqtt_mock_entry_with_yaml_config() + assert hass.states.get(f"{domain}.test") is not None