diff --git a/tests/components/mqtt/test_alarm_control_panel.py b/tests/components/mqtt/test_alarm_control_panel.py index e51ed9aeae9..d305d2ae7aa 100644 --- a/tests/components/mqtt/test_alarm_control_panel.py +++ b/tests/components/mqtt/test_alarm_control_panel.py @@ -61,7 +61,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -121,8 +121,6 @@ DEFAULT_CONFIG_REMOTE_CODE_TEXT = { # Scheduled to be removed in HA core 2022.12 DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) DEFAULT_CONFIG_LEGACY[alarm_control_panel.DOMAIN]["platform"] = mqtt.DOMAIN -DEFAULT_CONFIG_CODE_LEGACY = copy.deepcopy(DEFAULT_CONFIG_CODE[mqtt.DOMAIN]) -DEFAULT_CONFIG_CODE_LEGACY[alarm_control_panel.DOMAIN]["platform"] = mqtt.DOMAIN @pytest.fixture(autouse=True) @@ -588,7 +586,7 @@ async def test_availability_when_connection_lost( hass, mqtt_mock_entry_with_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_CODE_LEGACY, + DEFAULT_CONFIG_CODE, ) @@ -598,7 +596,7 @@ async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config hass, mqtt_mock_entry_with_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_CODE_LEGACY, + DEFAULT_CONFIG_CODE, ) @@ -608,7 +606,7 @@ async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_conf hass, mqtt_mock_entry_with_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_CODE_LEGACY, + DEFAULT_CONFIG_CODE, ) @@ -618,7 +616,7 @@ async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_confi hass, mqtt_mock_entry_with_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_CODE_LEGACY, + DEFAULT_CONFIG, ) @@ -630,7 +628,7 @@ async def test_setting_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_with_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -642,7 +640,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_ALARM_ATTRIBUTES_BLOCKED, ) @@ -653,7 +651,7 @@ async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_c hass, mqtt_mock_entry_with_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -666,20 +664,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -690,29 +688,29 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one alarm per unique_id.""" config = { - alarm_control_panel.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + alarm_control_panel.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, alarm_control_panel.DOMAIN, config @@ -721,7 +719,7 @@ async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): async def test_discovery_removal_alarm(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test removal of discovered alarm_control_panel.""" - data = json.dumps(DEFAULT_CONFIG_LEGACY[alarm_control_panel.DOMAIN]) + data = json.dumps(DEFAULT_CONFIG[mqtt.DOMAIN][alarm_control_panel.DOMAIN]) await help_test_discovery_removal( hass, mqtt_mock_entry_no_yaml_config, caplog, alarm_control_panel.DOMAIN, data ) @@ -731,8 +729,8 @@ async def test_discovery_update_alarm_topic_and_template( hass, mqtt_mock_entry_no_yaml_config, caplog ): """Test update of discovered alarm_control_panel.""" - config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[alarm_control_panel.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[alarm_control_panel.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][alarm_control_panel.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][alarm_control_panel.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "alarm/state1" @@ -766,8 +764,8 @@ async def test_discovery_update_alarm_template( hass, mqtt_mock_entry_no_yaml_config, caplog ): """Test update of discovered alarm_control_panel.""" - config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[alarm_control_panel.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[alarm_control_panel.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][alarm_control_panel.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][alarm_control_panel.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "alarm/state1" @@ -799,7 +797,7 @@ async def test_discovery_update_unchanged_alarm( hass, mqtt_mock_entry_no_yaml_config, caplog ): """Test update of discovered alarm_control_panel.""" - config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[alarm_control_panel.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][alarm_control_panel.DOMAIN]) config1["name"] = "Beer" data1 = json.dumps(config1) @@ -851,7 +849,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY[alarm_control_panel.DOMAIN], + DEFAULT_CONFIG[mqtt.DOMAIN][alarm_control_panel.DOMAIN], topic, value, ) @@ -863,7 +861,7 @@ async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_ hass, mqtt_mock_entry_no_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -873,7 +871,7 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_ hass, mqtt_mock_entry_no_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -883,7 +881,7 @@ async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -893,7 +891,7 @@ async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -903,7 +901,7 @@ async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_co hass, mqtt_mock_entry_with_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -913,7 +911,7 @@ async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_c hass, mqtt_mock_entry_no_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -923,7 +921,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, alarm_control_panel.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, alarm_control_panel.SERVICE_ALARM_DISARM, command_payload="DISARM", ) @@ -966,7 +964,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = alarm_control_panel.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_publishing_with_custom_encoding( hass, @@ -987,12 +985,14 @@ 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 = alarm_control_panel.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = alarm_control_panel.DOMAIN @@ -1003,17 +1003,14 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = alarm_control_panel.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) - assert hass.states.get(f"{platform}.test") is not None + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = alarm_control_panel.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index bbe8b978707..48acde5c6c9 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -45,7 +45,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -542,7 +542,7 @@ async def test_availability_when_connection_lost( hass, mqtt_mock_entry_with_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -552,7 +552,7 @@ async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config hass, mqtt_mock_entry_with_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -562,7 +562,7 @@ async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_conf hass, mqtt_mock_entry_with_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -572,7 +572,7 @@ async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_confi hass, mqtt_mock_entry_with_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -708,7 +708,7 @@ async def test_setting_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_with_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -718,7 +718,7 @@ async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_c hass, mqtt_mock_entry_with_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -731,20 +731,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -755,27 +755,27 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one sensor per unique_id.""" config = { - binary_sensor.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + binary_sensor.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, binary_sensor.DOMAIN, config @@ -786,7 +786,7 @@ async def test_discovery_removal_binary_sensor( hass, mqtt_mock_entry_no_yaml_config, caplog ): """Test removal of discovered binary_sensor.""" - data = json.dumps(DEFAULT_CONFIG_LEGACY[binary_sensor.DOMAIN]) + data = json.dumps(DEFAULT_CONFIG[mqtt.DOMAIN][binary_sensor.DOMAIN]) await help_test_discovery_removal( hass, mqtt_mock_entry_no_yaml_config, caplog, binary_sensor.DOMAIN, data ) @@ -796,8 +796,8 @@ async def test_discovery_update_binary_sensor_topic_template( hass, mqtt_mock_entry_no_yaml_config, caplog ): """Test update of discovered binary_sensor.""" - config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[binary_sensor.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[binary_sensor.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][binary_sensor.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][binary_sensor.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "sensor/state1" @@ -833,8 +833,8 @@ async def test_discovery_update_binary_sensor_template( hass, mqtt_mock_entry_no_yaml_config, caplog ): """Test update of discovered binary_sensor.""" - config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[binary_sensor.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[binary_sensor.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][binary_sensor.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][binary_sensor.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "sensor/state1" @@ -892,7 +892,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY[binary_sensor.DOMAIN], + DEFAULT_CONFIG[mqtt.DOMAIN][binary_sensor.DOMAIN], topic, value, attribute, @@ -904,7 +904,7 @@ async def test_discovery_update_unchanged_binary_sensor( hass, mqtt_mock_entry_no_yaml_config, caplog ): """Test update of discovered binary_sensor.""" - config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[binary_sensor.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][binary_sensor.DOMAIN]) config1["name"] = "Beer" data1 = json.dumps(config1) @@ -942,7 +942,7 @@ async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_ hass, mqtt_mock_entry_no_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -952,7 +952,7 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_ hass, mqtt_mock_entry_no_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -962,7 +962,7 @@ async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -972,7 +972,7 @@ async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -982,7 +982,7 @@ async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_co hass, mqtt_mock_entry_with_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -992,7 +992,7 @@ async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_c hass, mqtt_mock_entry_no_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -1002,7 +1002,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, binary_sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, None, ) @@ -1010,12 +1010,14 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = binary_sensor.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = binary_sensor.DOMAIN @@ -1040,11 +1042,11 @@ async def test_cleanup_triggers_and_restoring_state( ): """Test cleanup old triggers at reloading and restoring the state.""" domain = binary_sensor.DOMAIN - config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain]) config1["name"] = "test1" config1["expire_after"] = 30 config1["state_topic"] = "test-topic1" - config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain]) config2["name"] = "test2" config2["expire_after"] = 5 config2["state_topic"] = "test-topic2" @@ -1053,8 +1055,8 @@ async def test_cleanup_triggers_and_restoring_state( assert await async_setup_component( hass, - binary_sensor.DOMAIN, - {binary_sensor.DOMAIN: [config1, config2]}, + mqtt.DOMAIN, + {mqtt.DOMAIN: {binary_sensor.DOMAIN: [config1, config2]}}, ) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -1070,7 +1072,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]}} ) assert "Clean up expire after trigger for binary_sensor.test1" in caplog.text assert "Clean up expire after trigger for binary_sensor.test2" not in caplog.text @@ -1125,17 +1127,14 @@ async def test_skip_restoring_state_with_over_due_expire_trigger( async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = binary_sensor.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) - assert hass.states.get(f"{platform}.test") is not None + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = binary_sensor.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_button.py b/tests/components/mqtt/test_button.py index 1274c700800..80e5ce60a47 100644 --- a/tests/components/mqtt/test_button.py +++ b/tests/components/mqtt/test_button.py @@ -38,7 +38,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -140,25 +140,26 @@ 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, button.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, button.DOMAIN, DEFAULT_CONFIG ) 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, button.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, button.DOMAIN, DEFAULT_CONFIG ) async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by default payload with defined topic.""" config = { - button.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "payload_press": 1, + mqtt.DOMAIN: { + button.DOMAIN: { + "name": "test", + "command_topic": "command-topic", + "payload_press": 1, + } } } @@ -176,11 +177,12 @@ async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_conf async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by custom payload with defined topic.""" config = { - button.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "payload_press": 1, + mqtt.DOMAIN: { + button.DOMAIN: { + "name": "test", + "command_topic": "command-topic", + "payload_press": 1, + } } } @@ -200,7 +202,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, button.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, button.DOMAIN, DEFAULT_CONFIG ) @@ -209,14 +211,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, button.DOMAIN, DEFAULT_CONFIG_LEGACY, None + hass, mqtt_mock_entry_no_yaml_config, button.DOMAIN, DEFAULT_CONFIG, None ) 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, button.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, button.DOMAIN, DEFAULT_CONFIG ) @@ -229,20 +231,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, button.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, button.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -253,27 +255,27 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, button.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one button per unique_id.""" config = { - button.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + button.DOMAIN: [ + { + "name": "Test 1", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, button.DOMAIN, config @@ -290,8 +292,8 @@ async def test_discovery_removal_button(hass, mqtt_mock_entry_no_yaml_config, ca async def test_discovery_update_button(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test update of discovered button.""" - config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[button.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[button.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][button.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][button.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" @@ -340,35 +342,35 @@ 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 button device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, button.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, button.DOMAIN, DEFAULT_CONFIG ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT button device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, button.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, button.DOMAIN, DEFAULT_CONFIG ) 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, button.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, button.DOMAIN, DEFAULT_CONFIG ) 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, button.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, button.DOMAIN, DEFAULT_CONFIG ) 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, button.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, button.DOMAIN, DEFAULT_CONFIG ) @@ -378,7 +380,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, button.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, button.SERVICE_PRESS, command_payload="PRESS", state_topic=None, @@ -457,7 +459,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = button.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_publishing_with_custom_encoding( hass, @@ -476,12 +478,14 @@ 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 = button.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = button.DOMAIN @@ -492,17 +496,14 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = button.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) - assert hass.states.get(f"{platform}.test") is not None + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = button.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_camera.py b/tests/components/mqtt/test_camera.py index 4d0b2fbfca0..4cb6afb6495 100644 --- a/tests/components/mqtt/test_camera.py +++ b/tests/components/mqtt/test_camera.py @@ -37,7 +37,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -195,28 +195,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, camera.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, camera.DOMAIN, DEFAULT_CONFIG ) 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, camera.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, camera.DOMAIN, DEFAULT_CONFIG ) 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, camera.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, camera.DOMAIN, DEFAULT_CONFIG ) 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, camera.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, camera.DOMAIN, DEFAULT_CONFIG ) @@ -225,7 +225,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, camera.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, camera.DOMAIN, DEFAULT_CONFIG ) @@ -237,7 +237,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, camera.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_CAMERA_ATTRIBUTES_BLOCKED, ) @@ -245,7 +245,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, camera.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, camera.DOMAIN, DEFAULT_CONFIG ) @@ -258,20 +258,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, camera.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, camera.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -282,27 +282,27 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, camera.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one camera per unique_id.""" config = { - camera.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + camera.DOMAIN: [ + { + "name": "Test 1", + "topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, camera.DOMAIN, config @@ -311,7 +311,7 @@ async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): async def test_discovery_removal_camera(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test removal of discovered camera.""" - data = json.dumps(DEFAULT_CONFIG_LEGACY[camera.DOMAIN]) + data = json.dumps(DEFAULT_CONFIG[mqtt.DOMAIN][camera.DOMAIN]) await help_test_discovery_removal( hass, mqtt_mock_entry_no_yaml_config, caplog, camera.DOMAIN, data ) @@ -359,28 +359,28 @@ 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 camera device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, camera.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, camera.DOMAIN, DEFAULT_CONFIG ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT camera device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, camera.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, camera.DOMAIN, DEFAULT_CONFIG ) 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, camera.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, camera.DOMAIN, DEFAULT_CONFIG ) 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, camera.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, camera.DOMAIN, DEFAULT_CONFIG ) @@ -390,7 +390,7 @@ async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_co hass, mqtt_mock_entry_with_yaml_config, camera.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ["test_topic"], ) @@ -398,7 +398,7 @@ async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_co 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, camera.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, camera.DOMAIN, DEFAULT_CONFIG ) @@ -408,7 +408,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, camera.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, None, state_topic="test_topic", state_payload=b"ON", @@ -418,12 +418,14 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path): """Test reloading the MQTT platform.""" domain = camera.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = camera.DOMAIN @@ -434,17 +436,14 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = camera.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) - assert hass.states.get(f"{platform}.test") is not None + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = camera.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_climate.py b/tests/components/mqtt/test_climate.py index 14bd9084abe..ec2501e11d3 100644 --- a/tests/components/mqtt/test_climate.py +++ b/tests/components/mqtt/test_climate.py @@ -52,7 +52,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -686,28 +686,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, climate.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, climate.DOMAIN, DEFAULT_CONFIG ) 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, climate.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, climate.DOMAIN, DEFAULT_CONFIG ) 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, climate.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, climate.DOMAIN, DEFAULT_CONFIG ) 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, climate.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, climate.DOMAIN, DEFAULT_CONFIG ) @@ -994,7 +994,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, climate.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, climate.DOMAIN, DEFAULT_CONFIG ) @@ -1006,7 +1006,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, climate.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_CLIMATE_ATTRIBUTES_BLOCKED, ) @@ -1014,7 +1014,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, climate.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, climate.DOMAIN, DEFAULT_CONFIG ) @@ -1027,20 +1027,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, climate.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, climate.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -1051,29 +1051,29 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, climate.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one climate per unique_id.""" config = { - climate.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "power_state_topic": "test-topic", - "power_command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "power_state_topic": "test-topic", - "power_command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + climate.DOMAIN: [ + { + "name": "Test 1", + "power_state_topic": "test-topic", + "power_command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "power_state_topic": "test-topic", + "power_command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, climate.DOMAIN, config @@ -1106,7 +1106,7 @@ async def test_encoding_subscribable_topics( attribute_value, ): """Test handling of incoming encoded payload.""" - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[climate.DOMAIN]) + config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][climate.DOMAIN]) await help_test_encoding_subscribable_topics( hass, mqtt_mock_entry_with_yaml_config, @@ -1122,7 +1122,7 @@ async def test_encoding_subscribable_topics( async def test_discovery_removal_climate(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test removal of discovered climate.""" - data = json.dumps(DEFAULT_CONFIG_LEGACY[climate.DOMAIN]) + data = json.dumps(DEFAULT_CONFIG[mqtt.DOMAIN][climate.DOMAIN]) await help_test_discovery_removal( hass, mqtt_mock_entry_no_yaml_config, caplog, climate.DOMAIN, data ) @@ -1168,39 +1168,40 @@ 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 climate device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, climate.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, climate.DOMAIN, DEFAULT_CONFIG ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT climate device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, climate.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, climate.DOMAIN, DEFAULT_CONFIG ) 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, climate.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, climate.DOMAIN, DEFAULT_CONFIG ) 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, climate.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, climate.DOMAIN, DEFAULT_CONFIG ) async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config): """Test MQTT subscriptions are managed when entity_id is updated.""" config = { - climate.DOMAIN: { - "platform": "mqtt", - "name": "test", - "mode_state_topic": "test-topic", - "availability_topic": "avty-topic", + mqtt.DOMAIN: { + climate.DOMAIN: { + "name": "test", + "mode_state_topic": "test-topic", + "availability_topic": "avty-topic", + } } } await help_test_entity_id_update_subscriptions( @@ -1215,18 +1216,19 @@ async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_co 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, climate.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, climate.DOMAIN, DEFAULT_CONFIG ) async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT debug info.""" config = { - climate.DOMAIN: { - "platform": "mqtt", - "name": "test", - "mode_command_topic": "command-topic", - "mode_state_topic": "test-topic", + mqtt.DOMAIN: { + climate.DOMAIN: { + "name": "test", + "mode_command_topic": "command-topic", + "mode_state_topic": "test-topic", + } } } await help_test_entity_debug_info_message( @@ -1375,10 +1377,10 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = climate.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config = copy.deepcopy(DEFAULT_CONFIG) if topic != "preset_mode_command_topic": - del config["preset_mode_command_topic"] - del config["preset_modes"] + del config[mqtt.DOMAIN][domain]["preset_mode_command_topic"] + del config[mqtt.DOMAIN][domain]["preset_modes"] await help_test_publishing_with_custom_encoding( hass, @@ -1397,12 +1399,14 @@ 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 = climate.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = climate.DOMAIN @@ -1413,17 +1417,14 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = climate.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) - assert hass.states.get(f"{platform}.test") is not None + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = climate.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_common.py b/tests/components/mqtt/test_common.py index ac8ef98531e..e2411f9fc6c 100644 --- a/tests/components/mqtt/test_common.py +++ b/tests/components/mqtt/test_common.py @@ -53,7 +53,7 @@ async def help_test_availability_when_connection_lost( hass, mqtt_mock_entry_with_yaml_config, domain, config ): """Test availability after MQTT disconnection.""" - assert await async_setup_component(hass, domain, config) + assert await async_setup_component(hass, mqtt.DOMAIN, config) await hass.async_block_till_done() mqtt_mock = await mqtt_mock_entry_with_yaml_config() @@ -72,8 +72,8 @@ async def help_test_availability_without_topic( hass, mqtt_mock_entry_with_yaml_config, domain, config ): """Test availability without defined availability topic.""" - assert "availability_topic" not in config[domain] - assert await async_setup_component(hass, domain, config) + assert "availability_topic" not in config[mqtt.DOMAIN][domain] + assert await async_setup_component(hass, mqtt.DOMAIN, config) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -96,10 +96,10 @@ async def help_test_default_availability_payload( """ # Add availability settings to config config = copy.deepcopy(config) - config[domain]["availability_topic"] = "availability-topic" + config[mqtt.DOMAIN][domain]["availability_topic"] = "availability-topic" assert await async_setup_component( hass, - domain, + mqtt.DOMAIN, config, ) await hass.async_block_till_done() @@ -147,13 +147,13 @@ async def help_test_default_availability_list_payload( """ # Add availability settings to config config = copy.deepcopy(config) - config[domain]["availability"] = [ + config[mqtt.DOMAIN][domain]["availability"] = [ {"topic": "availability-topic1"}, {"topic": "availability-topic2"}, ] assert await async_setup_component( hass, - domain, + mqtt.DOMAIN, config, ) await hass.async_block_till_done() @@ -213,14 +213,14 @@ async def help_test_default_availability_list_payload_all( """ # Add availability settings to config config = copy.deepcopy(config) - config[domain]["availability_mode"] = "all" - config[domain]["availability"] = [ + config[mqtt.DOMAIN][domain]["availability_mode"] = "all" + config[mqtt.DOMAIN][domain]["availability"] = [ {"topic": "availability-topic1"}, {"topic": "availability-topic2"}, ] assert await async_setup_component( hass, - domain, + mqtt.DOMAIN, config, ) await hass.async_block_till_done() @@ -281,14 +281,14 @@ async def help_test_default_availability_list_payload_any( """ # Add availability settings to config config = copy.deepcopy(config) - config[domain]["availability_mode"] = "any" - config[domain]["availability"] = [ + config[mqtt.DOMAIN][domain]["availability_mode"] = "any" + config[mqtt.DOMAIN][domain]["availability"] = [ {"topic": "availability-topic1"}, {"topic": "availability-topic2"}, ] assert await async_setup_component( hass, - domain, + mqtt.DOMAIN, config, ) await hass.async_block_till_done() @@ -331,7 +331,6 @@ async def help_test_default_availability_list_payload_any( async def help_test_default_availability_list_single( hass, - mqtt_mock_entry_with_yaml_config, caplog, domain, config, @@ -345,22 +344,17 @@ async def help_test_default_availability_list_single( """ # Add availability settings to config config = copy.deepcopy(config) - config[domain]["availability"] = [ + config[mqtt.DOMAIN][domain]["availability"] = [ {"topic": "availability-topic1"}, ] - config[domain]["availability_topic"] = "availability-topic" - assert await async_setup_component( + config[mqtt.DOMAIN][domain]["availability_topic"] = "availability-topic" + assert not await async_setup_component( hass, - domain, + mqtt.DOMAIN, config, ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() - - state = hass.states.get(f"{domain}.test") - assert state is None assert ( - "Invalid config for [sensor.mqtt]: two or more values in the same group of exclusion 'availability'" + "Invalid config for [mqtt]: two or more values in the same group of exclusion 'availability'" in caplog.text ) @@ -380,12 +374,12 @@ async def help_test_custom_availability_payload( """ # Add availability settings to config config = copy.deepcopy(config) - config[domain]["availability_topic"] = "availability-topic" - config[domain]["payload_available"] = "good" - config[domain]["payload_not_available"] = "nogood" + config[mqtt.DOMAIN][domain]["availability_topic"] = "availability-topic" + config[mqtt.DOMAIN][domain]["payload_available"] = "good" + config[mqtt.DOMAIN][domain]["payload_not_available"] = "nogood" assert await async_setup_component( hass, - domain, + mqtt.DOMAIN, config, ) await hass.async_block_till_done() @@ -434,17 +428,17 @@ async def help_test_discovery_update_availability( await mqtt_mock_entry_no_yaml_config() # Add availability settings to config config1 = copy.deepcopy(config) - config1[domain]["availability_topic"] = "availability-topic1" + config1[mqtt.DOMAIN][domain]["availability_topic"] = "availability-topic1" config2 = copy.deepcopy(config) - config2[domain]["availability"] = [ + config2[mqtt.DOMAIN][domain]["availability"] = [ {"topic": "availability-topic2"}, {"topic": "availability-topic3"}, ] config3 = copy.deepcopy(config) - config3[domain]["availability_topic"] = "availability-topic4" - data1 = json.dumps(config1[domain]) - data2 = json.dumps(config2[domain]) - data3 = json.dumps(config3[domain]) + config3[mqtt.DOMAIN][domain]["availability_topic"] = "availability-topic4" + data1 = json.dumps(config1[mqtt.DOMAIN][domain]) + data2 = json.dumps(config2[mqtt.DOMAIN][domain]) + data3 = json.dumps(config3[mqtt.DOMAIN][domain]) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data1) await hass.async_block_till_done() @@ -508,10 +502,10 @@ async def help_test_setting_attribute_via_mqtt_json_message( """ # Add JSON attributes settings to config config = copy.deepcopy(config) - config[domain]["json_attributes_topic"] = "attr-topic" + config[mqtt.DOMAIN][domain]["json_attributes_topic"] = "attr-topic" assert await async_setup_component( hass, - domain, + mqtt.DOMAIN, config, ) await hass.async_block_till_done() @@ -535,8 +529,8 @@ async def help_test_setting_blocked_attribute_via_mqtt_json_message( # Add JSON attributes settings to config config = copy.deepcopy(config) - config[domain]["json_attributes_topic"] = "attr-topic" - data = json.dumps(config[domain]) + config[mqtt.DOMAIN][domain]["json_attributes_topic"] = "attr-topic" + data = json.dumps(config[mqtt.DOMAIN][domain]) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) await hass.async_block_till_done() val = "abc123" @@ -561,11 +555,13 @@ async def help_test_setting_attribute_with_template( """ # Add JSON attributes settings to config config = copy.deepcopy(config) - config[domain]["json_attributes_topic"] = "attr-topic" - config[domain]["json_attributes_template"] = "{{ value_json['Timer1'] | tojson }}" + config[mqtt.DOMAIN][domain]["json_attributes_topic"] = "attr-topic" + config[mqtt.DOMAIN][domain][ + "json_attributes_template" + ] = "{{ value_json['Timer1'] | tojson }}" assert await async_setup_component( hass, - domain, + mqtt.DOMAIN, config, ) await hass.async_block_till_done() @@ -589,10 +585,10 @@ async def help_test_update_with_json_attrs_not_dict( """ # Add JSON attributes settings to config config = copy.deepcopy(config) - config[domain]["json_attributes_topic"] = "attr-topic" + config[mqtt.DOMAIN][domain]["json_attributes_topic"] = "attr-topic" assert await async_setup_component( hass, - domain, + mqtt.DOMAIN, config, ) await hass.async_block_till_done() @@ -605,7 +601,7 @@ async def help_test_update_with_json_attrs_not_dict( assert "JSON result was not a dictionary" in caplog.text -async def help_test_update_with_json_attrs_bad_JSON( +async def help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, domain, config ): """Test JSON validation of attributes. @@ -614,10 +610,10 @@ async def help_test_update_with_json_attrs_bad_JSON( """ # Add JSON attributes settings to config config = copy.deepcopy(config) - config[domain]["json_attributes_topic"] = "attr-topic" + config[mqtt.DOMAIN][domain]["json_attributes_topic"] = "attr-topic" assert await async_setup_component( hass, - domain, + mqtt.DOMAIN, config, ) await hass.async_block_till_done() @@ -640,11 +636,11 @@ async def help_test_discovery_update_attr( await mqtt_mock_entry_no_yaml_config() # Add JSON attributes settings to config config1 = copy.deepcopy(config) - config1[domain]["json_attributes_topic"] = "attr-topic1" + config1[mqtt.DOMAIN][domain]["json_attributes_topic"] = "attr-topic1" config2 = copy.deepcopy(config) - config2[domain]["json_attributes_topic"] = "attr-topic2" - data1 = json.dumps(config1[domain]) - data2 = json.dumps(config2[domain]) + config2[mqtt.DOMAIN][domain]["json_attributes_topic"] = "attr-topic2" + data1 = json.dumps(config1[mqtt.DOMAIN][domain]) + data2 = json.dumps(config2[mqtt.DOMAIN][domain]) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data1) await hass.async_block_till_done() @@ -669,7 +665,7 @@ async def help_test_discovery_update_attr( async def help_test_unique_id(hass, mqtt_mock_entry_with_yaml_config, domain, config): """Test unique id option only creates one entity per unique_id.""" - assert await async_setup_component(hass, domain, config) + assert await async_setup_component(hass, mqtt.DOMAIN, config) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() assert len(hass.states.async_entity_ids(domain)) == 1 @@ -881,7 +877,7 @@ async def help_test_encoding_subscribable_topics( await hass.async_block_till_done() assert await async_setup_component( - hass, domain, {domain: [config1, config2, config3]} + hass, mqtt.DOMAIN, {mqtt.DOMAIN: {domain: [config1, config2, config3]}} ) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -944,7 +940,7 @@ async def help_test_entity_device_info_with_identifier( """ await mqtt_mock_entry_no_yaml_config() # Add device settings to config - config = copy.deepcopy(config[domain]) + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" @@ -975,7 +971,7 @@ async def help_test_entity_device_info_with_connection( """ await mqtt_mock_entry_no_yaml_config() # Add device settings to config - config = copy.deepcopy(config[domain]) + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_MAC) config["unique_id"] = "veryunique" @@ -1005,7 +1001,7 @@ async def help_test_entity_device_info_remove( """Test device registry remove.""" await mqtt_mock_entry_no_yaml_config() # Add device settings to config - config = copy.deepcopy(config[domain]) + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" @@ -1037,7 +1033,7 @@ async def help_test_entity_device_info_update( """ await mqtt_mock_entry_no_yaml_config() # Add device settings to config - config = copy.deepcopy(config[domain]) + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" @@ -1067,19 +1063,19 @@ async def help_test_entity_id_update_subscriptions( """Test MQTT subscriptions are managed when entity_id is updated.""" # Add unique_id to config config = copy.deepcopy(config) - config[domain]["unique_id"] = "TOTALLY_UNIQUE" + config[mqtt.DOMAIN][domain]["unique_id"] = "TOTALLY_UNIQUE" if topics is None: # Add default topics to config - config[domain]["availability_topic"] = "avty-topic" - config[domain]["state_topic"] = "test-topic" + config[mqtt.DOMAIN][domain]["availability_topic"] = "avty-topic" + config[mqtt.DOMAIN][domain]["state_topic"] = "test-topic" topics = ["avty-topic", "test-topic"] assert len(topics) > 0 registry = mock_registry(hass, {}) assert await async_setup_component( hass, - domain, + mqtt.DOMAIN, config, ) await hass.async_block_till_done() @@ -1111,16 +1107,16 @@ async def help_test_entity_id_update_discovery_update( # Add unique_id to config await mqtt_mock_entry_no_yaml_config() config = copy.deepcopy(config) - config[domain]["unique_id"] = "TOTALLY_UNIQUE" + config[mqtt.DOMAIN][domain]["unique_id"] = "TOTALLY_UNIQUE" if topic is None: # Add default topic to config - config[domain]["availability_topic"] = "avty-topic" + config[mqtt.DOMAIN][domain]["availability_topic"] = "avty-topic" topic = "avty-topic" ent_registry = mock_registry(hass, {}) - data = json.dumps(config[domain]) + data = json.dumps(config[mqtt.DOMAIN][domain]) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) await hass.async_block_till_done() @@ -1135,8 +1131,8 @@ async def help_test_entity_id_update_discovery_update( ent_registry.async_update_entity(f"{domain}.test", new_entity_id=f"{domain}.milk") await hass.async_block_till_done() - config[domain]["availability_topic"] = f"{topic}_2" - data = json.dumps(config[domain]) + config[mqtt.DOMAIN][domain]["availability_topic"] = f"{topic}_2" + data = json.dumps(config[mqtt.DOMAIN][domain]) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) await hass.async_block_till_done() assert len(hass.states.async_entity_ids(domain)) == 1 @@ -1155,9 +1151,10 @@ async def help_test_entity_debug_info( """ await mqtt_mock_entry_no_yaml_config() # Add device settings to config - config = copy.deepcopy(config[domain]) + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" + config["platform"] = "mqtt" registry = dr.async_get(hass) @@ -1192,7 +1189,7 @@ async def help_test_entity_debug_info_max_messages( """ await mqtt_mock_entry_no_yaml_config() # Add device settings to config - config = copy.deepcopy(config[domain]) + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" @@ -1256,7 +1253,7 @@ async def help_test_entity_debug_info_message( """ # Add device settings to config await mqtt_mock_entry_no_yaml_config() - config = copy.deepcopy(config[domain]) + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" @@ -1359,9 +1356,10 @@ async def help_test_entity_debug_info_remove( """ await mqtt_mock_entry_no_yaml_config() # Add device settings to config - config = copy.deepcopy(config[domain]) + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" + config["platform"] = "mqtt" registry = dr.async_get(hass) @@ -1405,9 +1403,10 @@ async def help_test_entity_debug_info_update_entity_id( """ await mqtt_mock_entry_no_yaml_config() # Add device settings to config - config = copy.deepcopy(config[domain]) + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" + config["platform"] = "mqtt" dev_registry = dr.async_get(hass) ent_registry = mock_registry(hass, {}) @@ -1461,7 +1460,7 @@ async def help_test_entity_disabled_by_default( """Test device registry remove.""" await mqtt_mock_entry_no_yaml_config() # Add device settings to config - config = copy.deepcopy(config[domain]) + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["enabled_by_default"] = False config["unique_id"] = "veryunique1" @@ -1500,7 +1499,7 @@ async def help_test_entity_category( """Test device registry remove.""" await mqtt_mock_entry_no_yaml_config() # Add device settings to config - config = copy.deepcopy(config[domain]) + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) ent_registry = er.async_get(hass) @@ -1564,7 +1563,7 @@ async def help_test_publishing_with_custom_encoding( setup_config = [] service_data = {} for test_id, test_data in test_config.items(): - test_config_setup = copy.deepcopy(config) + test_config_setup = copy.deepcopy(config[mqtt.DOMAIN][domain]) test_config_setup.update( { topic: f"cmd/{test_id}", @@ -1573,7 +1572,7 @@ async def help_test_publishing_with_custom_encoding( ) if test_data["encoding"] is not None: test_config_setup["encoding"] = test_data["encoding"] - if test_data["cmd_tpl"]: + if template and test_data["cmd_tpl"]: test_config_setup[ template ] = f"{{{{ (('%.1f'|format({tpl_par}))[0] if is_number({tpl_par}) else {tpl_par}[0]) | ord | pack('b') }}}}" @@ -1587,8 +1586,8 @@ async def help_test_publishing_with_custom_encoding( # setup test entities assert await async_setup_component( hass, - domain, - {domain: setup_config}, + mqtt.DOMAIN, + {mqtt.DOMAIN: {domain: setup_config}}, ) await hass.async_block_till_done() mqtt_mock = await mqtt_mock_entry_with_yaml_config() @@ -1698,24 +1697,29 @@ async def help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ): """Test reloading an MQTT platform.""" + config = copy.deepcopy(config[mqtt.DOMAIN][domain]) # Create and test an old config of 2 entities based on the config supplied old_config_1 = copy.deepcopy(config) old_config_1["name"] = "test_old_1" old_config_2 = copy.deepcopy(config) old_config_2["name"] = "test_old_2" + + # Test deprecated YAML configuration under the platform key + # Scheduled to be removed in HA core 2022.12 old_config_3 = copy.deepcopy(config) old_config_3["name"] = "test_old_3" - old_config_3.pop("platform") + old_config_3["platform"] = mqtt.DOMAIN old_config_4 = copy.deepcopy(config) old_config_4["name"] = "test_old_4" - old_config_4.pop("platform") + old_config_4["platform"] = mqtt.DOMAIN old_config = { - domain: [old_config_1, old_config_2], - "mqtt": {domain: [old_config_3, old_config_4]}, + mqtt.DOMAIN: {domain: [old_config_1, old_config_2]}, + domain: [old_config_3, old_config_4], } assert await async_setup_component(hass, domain, old_config) + assert await async_setup_component(hass, mqtt.DOMAIN, old_config) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() @@ -1731,21 +1735,24 @@ async def help_test_reloadable( new_config_1["name"] = "test_new_1" new_config_2 = copy.deepcopy(config) new_config_2["name"] = "test_new_2" + new_config_extra = copy.deepcopy(config) + new_config_extra["name"] = "test_new_5" + + # Test deprecated YAML configuration under the platform key + # Scheduled to be removed in HA core 2022.12 new_config_3 = copy.deepcopy(config) new_config_3["name"] = "test_new_3" - new_config_3.pop("platform") + new_config_3["platform"] = mqtt.DOMAIN new_config_4 = copy.deepcopy(config) new_config_4["name"] = "test_new_4" - new_config_4.pop("platform") - new_config_5 = copy.deepcopy(config) - new_config_5["name"] = "test_new_5" - new_config_6 = copy.deepcopy(config) - new_config_6["name"] = "test_new_6" - new_config_6.pop("platform") + new_config_4["platform"] = mqtt.DOMAIN + new_config_extra_legacy = copy.deepcopy(config) + new_config_extra_legacy["name"] = "test_new_6" + new_config_extra_legacy["platform"] = mqtt.DOMAIN new_config = { - domain: [new_config_1, new_config_2, new_config_5], - "mqtt": {domain: [new_config_3, new_config_4, new_config_6]}, + mqtt.DOMAIN: {domain: [new_config_1, new_config_2, new_config_extra]}, + domain: [new_config_3, new_config_4, new_config_extra_legacy], } await help_test_reload_with_config(hass, caplog, tmp_path, new_config) @@ -1760,9 +1767,12 @@ async def help_test_reloadable( assert hass.states.get(f"{domain}.test_new_6") +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def help_test_reloadable_late(hass, caplog, tmp_path, domain, config): - """Test reloading an MQTT platform when config entry is setup late.""" + """Test reloading an MQTT platform when config entry is setup is late.""" # Create and test an old config of 2 entities based on the config supplied + # using the deprecated platform schema old_config_1 = copy.deepcopy(config) old_config_1["name"] = "test_old_1" old_config_2 = copy.deepcopy(config) @@ -1815,7 +1825,7 @@ async def help_test_reloadable_late(hass, caplog, tmp_path, domain, config): assert hass.states.get(f"{domain}.test_new_3") -async def help_test_setup_manual_entity_from_yaml(hass, platform, config): +async def help_test_setup_manual_entity_from_yaml(hass, config): """Help to test setup from yaml through configuration entry.""" calls = MagicMock() @@ -1823,9 +1833,7 @@ async def help_test_setup_manual_entity_from_yaml(hass, platform, config): """Mock reload.""" calls() - config_structure = {mqtt.DOMAIN: {platform: config}} - - await async_setup_component(hass, mqtt.DOMAIN, config_structure) + assert await async_setup_component(hass, mqtt.DOMAIN, config) # Mock config entry entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"}) entry.add_to_hass(hass) @@ -1864,14 +1872,14 @@ async def help_test_unload_config_entry_with_platform( """Test unloading the MQTT config entry with a specific platform domain.""" # prepare setup through configuration.yaml config_setup = copy.deepcopy(config) - config_setup["name"] = "config_setup" + config_setup[mqtt.DOMAIN][domain]["name"] = "config_setup" config_name = config_setup - assert await async_setup_component(hass, domain, {domain: [config_setup]}) + assert await async_setup_component(hass, mqtt.DOMAIN, config_setup) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() # prepare setup through discovery - discovery_setup = copy.deepcopy(config) + discovery_setup = copy.deepcopy(config[mqtt.DOMAIN][domain]) discovery_setup["name"] = "discovery_setup" async_fire_mqtt_message( hass, f"homeassistant/{domain}/bla/config", json.dumps(discovery_setup) diff --git a/tests/components/mqtt/test_cover.py b/tests/components/mqtt/test_cover.py index a91f0ecc6ca..fb7df111d96 100644 --- a/tests/components/mqtt/test_cover.py +++ b/tests/components/mqtt/test_cover.py @@ -74,7 +74,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -2444,28 +2444,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, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) 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, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) 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, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) 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, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) @@ -2514,7 +2514,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, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) @@ -2526,7 +2526,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, cover.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_COVER_ATTRIBUTES_BLOCKED, ) @@ -2534,7 +2534,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, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) @@ -2547,7 +2547,7 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, cover.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -2555,12 +2555,12 @@ async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, cover.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -2571,27 +2571,27 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, cover.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique_id option only creates one cover per id.""" config = { - cover.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + cover.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, cover.DOMAIN, config @@ -2646,42 +2646,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 cover device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT cover device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) 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, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) 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, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) 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, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) 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, cover.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, cover.DOMAIN, DEFAULT_CONFIG ) @@ -2691,7 +2691,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, cover.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, SERVICE_OPEN_COVER, command_payload="OPEN", ) @@ -3342,8 +3342,8 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = cover.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] - config["position_topic"] = "some-position-topic" + config = DEFAULT_CONFIG + config[mqtt.DOMAIN][domain]["position_topic"] = "some-position-topic" await help_test_publishing_with_custom_encoding( hass, @@ -3362,12 +3362,14 @@ 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 = cover.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = cover.DOMAIN @@ -3399,7 +3401,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, cover.DOMAIN, - DEFAULT_CONFIG_LEGACY[cover.DOMAIN], + DEFAULT_CONFIG[mqtt.DOMAIN][cover.DOMAIN], topic, value, attribute, @@ -3411,17 +3413,14 @@ async def test_encoding_subscribable_topics( async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = cover.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) - assert hass.states.get(f"{platform}.test") is not None + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = cover.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_device_tracker_discovery.py b/tests/components/mqtt/test_device_tracker_discovery.py index 923ae7c9f75..876b66e8a4d 100644 --- a/tests/components/mqtt/test_device_tracker_discovery.py +++ b/tests/components/mqtt/test_device_tracker_discovery.py @@ -1,6 +1,5 @@ """The tests for the MQTT device_tracker platform.""" -import copy from unittest.mock import patch import pytest @@ -27,11 +26,6 @@ DEFAULT_CONFIG = { } } -# 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[device_tracker.DOMAIN]["platform"] = mqtt.DOMAIN - @pytest.fixture(autouse=True) def device_tracker_platform_only(): @@ -440,7 +434,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, device_tracker.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, None, ) @@ -451,8 +445,10 @@ async def test_setup_with_modern_schema(hass, mock_device_tracker_conf): entity_id = f"{device_tracker.DOMAIN}.{dev_id}" topic = "/location/jan" - config = {"name": dev_id, "state_topic": topic} + config = { + mqtt.DOMAIN: {device_tracker.DOMAIN: {"name": dev_id, "state_topic": topic}} + } - await help_test_setup_manual_entity_from_yaml(hass, device_tracker.DOMAIN, config) + await help_test_setup_manual_entity_from_yaml(hass, config) assert hass.states.get(entity_id) is not None diff --git a/tests/components/mqtt/test_fan.py b/tests/components/mqtt/test_fan.py index efe38234aee..1666ccee6ce 100644 --- a/tests/components/mqtt/test_fan.py +++ b/tests/components/mqtt/test_fan.py @@ -59,7 +59,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -1365,7 +1365,7 @@ async def test_encoding_subscribable_topics( attribute_value, ): """Test handling of incoming encoded payload.""" - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[fan.DOMAIN]) + config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][fan.DOMAIN]) config[ATTR_PRESET_MODES] = ["eco", "auto"] config[CONF_PRESET_MODE_COMMAND_TOPIC] = "fan/some_preset_mode_command_topic" config[CONF_PERCENTAGE_COMMAND_TOPIC] = "fan/some_percentage_command_topic" @@ -1639,14 +1639,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, fan.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, fan.DOMAIN, DEFAULT_CONFIG ) 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, fan.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, fan.DOMAIN, DEFAULT_CONFIG ) @@ -1656,7 +1656,7 @@ async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_conf hass, mqtt_mock_entry_with_yaml_config, fan.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, True, "state-topic", "1", @@ -1669,7 +1669,7 @@ async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_confi hass, mqtt_mock_entry_with_yaml_config, fan.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, True, "state-topic", "1", @@ -1681,7 +1681,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, fan.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, fan.DOMAIN, DEFAULT_CONFIG ) @@ -1693,7 +1693,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, fan.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_FAN_ATTRIBUTES_BLOCKED, ) @@ -1701,7 +1701,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, fan.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, fan.DOMAIN, DEFAULT_CONFIG ) @@ -1714,7 +1714,7 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, fan.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -1722,41 +1722,41 @@ async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, fan.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) 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, fan.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, caplog, fan.DOMAIN, DEFAULT_CONFIG ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique_id option only creates one fan per id.""" config = { - fan.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + fan.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, fan.DOMAIN, config @@ -1812,42 +1812,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 fan device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, fan.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, fan.DOMAIN, DEFAULT_CONFIG ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT fan device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, fan.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, fan.DOMAIN, DEFAULT_CONFIG ) 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, fan.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, fan.DOMAIN, DEFAULT_CONFIG ) 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, fan.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, fan.DOMAIN, DEFAULT_CONFIG ) 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, fan.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, fan.DOMAIN, DEFAULT_CONFIG ) 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, fan.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, fan.DOMAIN, DEFAULT_CONFIG ) @@ -1857,7 +1857,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, fan.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, fan.SERVICE_TURN_ON, ) @@ -1914,9 +1914,9 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = fan.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config = copy.deepcopy(DEFAULT_CONFIG) if topic == "preset_mode_command_topic": - config["preset_modes"] = ["auto", "eco"] + config[mqtt.DOMAIN][domain]["preset_modes"] = ["auto", "eco"] await help_test_publishing_with_custom_encoding( hass, @@ -1935,12 +1935,14 @@ 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 = fan.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = fan.DOMAIN @@ -1951,17 +1953,14 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = fan.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) - assert hass.states.get(f"{platform}.test") is not None + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = fan.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_humidifier.py b/tests/components/mqtt/test_humidifier.py index 0cc2be638bf..1e2d64b66cf 100644 --- a/tests/components/mqtt/test_humidifier.py +++ b/tests/components/mqtt/test_humidifier.py @@ -61,7 +61,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -750,7 +750,7 @@ async def test_encoding_subscribable_topics( attribute_value, ): """Test handling of incoming encoded payload.""" - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[humidifier.DOMAIN]) + config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][humidifier.DOMAIN]) config["modes"] = ["eco", "auto"] config[CONF_MODE_COMMAND_TOPIC] = "humidifier/some_mode_command_topic" await help_test_encoding_subscribable_topics( @@ -996,14 +996,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, humidifier.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG ) 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, humidifier.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG ) @@ -1013,7 +1013,7 @@ async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_conf hass, mqtt_mock_entry_with_yaml_config, humidifier.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, True, "state-topic", "1", @@ -1026,7 +1026,7 @@ async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_confi hass, mqtt_mock_entry_with_yaml_config, humidifier.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, True, "state-topic", "1", @@ -1038,7 +1038,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, humidifier.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG ) @@ -1050,7 +1050,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, humidifier.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_HUMIDIFIER_ATTRIBUTES_BLOCKED, ) @@ -1058,7 +1058,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, humidifier.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG ) @@ -1071,7 +1071,7 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, humidifier.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -1079,12 +1079,12 @@ async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, humidifier.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -1095,31 +1095,31 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, humidifier.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique_id option only creates one fan per id.""" config = { - humidifier.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "command_topic": "test_topic", - "target_humidity_command_topic": "humidity-command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "command_topic": "test_topic", - "target_humidity_command_topic": "humidity-command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + humidifier.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "command_topic": "test_topic", + "target_humidity_command_topic": "humidity-command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "command_topic": "test_topic", + "target_humidity_command_topic": "humidity-command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, humidifier.DOMAIN, config @@ -1191,42 +1191,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 fan device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG ) async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT fan device registry integration.""" await help_test_entity_device_info_with_identifier( - hass, mqtt_mock_entry_no_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG ) 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, humidifier.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG ) 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, humidifier.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG ) 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, humidifier.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG ) 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, humidifier.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, humidifier.DOMAIN, DEFAULT_CONFIG ) @@ -1236,7 +1236,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, humidifier.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, humidifier.SERVICE_TURN_ON, ) @@ -1286,9 +1286,9 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = humidifier.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config = copy.deepcopy(DEFAULT_CONFIG) if topic == "mode_command_topic": - config["modes"] = ["auto", "eco"] + config[mqtt.DOMAIN][domain]["modes"] = ["auto", "eco"] await help_test_publishing_with_custom_encoding( hass, @@ -1307,12 +1307,14 @@ 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 = humidifier.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = humidifier.DOMAIN @@ -1323,11 +1325,8 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = humidifier.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) - assert hass.states.get(f"{platform}.test") is not None + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") async def test_config_schema_validation(hass): @@ -1344,7 +1343,7 @@ async def test_config_schema_validation(hass): async def test_unload_config_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): """Test unloading the config entry.""" domain = humidifier.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index b794d9260ba..b76979cc990 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -1485,9 +1485,17 @@ async def test_setup_override_configuration(hass, caplog, tmp_path): @patch("homeassistant.components.mqtt.PLATFORMS", []) async def test_setup_manual_mqtt_with_platform_key(hass, caplog): """Test set up a manual MQTT item with a platform key.""" - config = {"platform": "mqtt", "name": "test", "command_topic": "test-topic"} + config = { + mqtt.DOMAIN: { + "light": { + "platform": "mqtt", + "name": "test", + "command_topic": "test-topic", + } + } + } with pytest.raises(AssertionError): - await help_test_setup_manual_entity_from_yaml(hass, "light", config) + await help_test_setup_manual_entity_from_yaml(hass, config) assert ( "Invalid config for [mqtt]: [platform] is an invalid option for [mqtt]" in caplog.text @@ -1497,9 +1505,9 @@ async def test_setup_manual_mqtt_with_platform_key(hass, caplog): @patch("homeassistant.components.mqtt.PLATFORMS", []) async def test_setup_manual_mqtt_with_invalid_config(hass, caplog): """Test set up a manual MQTT item with an invalid config.""" - config = {"name": "test"} + config = {mqtt.DOMAIN: {"light": {"name": "test"}}} with pytest.raises(AssertionError): - await help_test_setup_manual_entity_from_yaml(hass, "light", config) + await help_test_setup_manual_entity_from_yaml(hass, config) assert ( "Invalid config for [mqtt]: required key not provided @ data['mqtt']['light'][0]['command_topic']." " Got None. (See ?, line ?)" in caplog.text @@ -1509,8 +1517,8 @@ async def test_setup_manual_mqtt_with_invalid_config(hass, caplog): @patch("homeassistant.components.mqtt.PLATFORMS", []) async def test_setup_manual_mqtt_empty_platform(hass, caplog): """Test set up a manual MQTT platform without items.""" - config = [] - await help_test_setup_manual_entity_from_yaml(hass, "light", config) + config = {mqtt.DOMAIN: {"light": []}} + await help_test_setup_manual_entity_from_yaml(hass, config) assert "voluptuous.error.MultipleInvalid" not in caplog.text @@ -2797,7 +2805,11 @@ async def test_publish_or_subscribe_without_valid_config_entry(hass, caplog): @patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT]) async def test_reload_entry_with_new_config(hass, tmp_path): """Test reloading the config entry with a new yaml config.""" - config_old = [{"name": "test_old1", "command_topic": "test-topic_old"}] + # Test deprecated YAML configuration under the platform key + # Scheduled to be removed in HA core 2022.12 + config_old = { + "mqtt": {"light": [{"name": "test_old1", "command_topic": "test-topic_old"}]} + } config_yaml_new = { "mqtt": { "light": [{"name": "test_new_modern", "command_topic": "test-topic_new"}] @@ -2812,7 +2824,7 @@ async def test_reload_entry_with_new_config(hass, tmp_path): } ], } - await help_test_setup_manual_entity_from_yaml(hass, "light", config_old) + await help_test_setup_manual_entity_from_yaml(hass, config_old) assert hass.states.get("light.test_old1") is not None await help_test_entry_reload_with_new_config(hass, tmp_path, config_yaml_new) @@ -2824,7 +2836,9 @@ async def test_reload_entry_with_new_config(hass, tmp_path): @patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT]) async def test_disabling_and_enabling_entry(hass, tmp_path, caplog): """Test disabling and enabling the config entry.""" - config_old = [{"name": "test_old1", "command_topic": "test-topic_old"}] + config_old = { + "mqtt": {"light": [{"name": "test_old1", "command_topic": "test-topic_old"}]} + } config_yaml_new = { "mqtt": { "light": [{"name": "test_new_modern", "command_topic": "test-topic_new"}] @@ -2839,7 +2853,7 @@ async def test_disabling_and_enabling_entry(hass, tmp_path, caplog): } ], } - await help_test_setup_manual_entity_from_yaml(hass, "light", config_old) + await help_test_setup_manual_entity_from_yaml(hass, config_old) assert hass.states.get("light.test_old1") is not None mqtt_config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] @@ -2929,7 +2943,9 @@ async def test_setup_manual_items_with_unique_ids( hass, tmp_path, caplog, config, unique ): """Test setup manual items is generating unique id's.""" - await help_test_setup_manual_entity_from_yaml(hass, "light", config) + await help_test_setup_manual_entity_from_yaml( + hass, {mqtt.DOMAIN: {"light": config}} + ) assert hass.states.get("light.test1") is not None assert (hass.states.get("light.test2") is not None) == unique diff --git a/tests/components/mqtt/test_legacy_vacuum.py b/tests/components/mqtt/test_legacy_vacuum.py index ffe9183fe4c..4b44807b7c9 100644 --- a/tests/components/mqtt/test_legacy_vacuum.py +++ b/tests/components/mqtt/test_legacy_vacuum.py @@ -58,7 +58,7 @@ from .test_common import ( help_test_setting_blocked_attribute_via_mqtt_json_message, help_test_setup_manual_entity_from_yaml, help_test_unique_id, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -95,8 +95,6 @@ DEFAULT_CONFIG_2 = {mqtt.DOMAIN: {vacuum.DOMAIN: {"name": "test"}}} # 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) @@ -108,7 +106,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, DEFAULT_CONFIG_LEGACY) + 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") @@ -616,28 +614,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) @@ -646,7 +644,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) @@ -658,7 +656,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, - DEFAULT_CONFIG_2_LEGACY, + DEFAULT_CONFIG_2, MQTT_LEGACY_VACUUM_ATTRIBUTES_BLOCKED, ) @@ -666,7 +664,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) @@ -679,20 +677,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, vacuum.DOMAIN, - DEFAULT_CONFIG_2_LEGACY, + DEFAULT_CONFIG_2, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, vacuum.DOMAIN, - DEFAULT_CONFIG_2_LEGACY, + DEFAULT_CONFIG_2, ) @@ -703,27 +701,27 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, vacuum.DOMAIN, - DEFAULT_CONFIG_2_LEGACY, + DEFAULT_CONFIG_2, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one vacuum per unique_id.""" config = { - vacuum.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + vacuum.DOMAIN: [ + { + "name": "Test 1", + "command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, config @@ -732,7 +730,7 @@ async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): async def test_discovery_removal_vacuum(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test removal of discovered vacuum.""" - data = json.dumps(DEFAULT_CONFIG_2_LEGACY[vacuum.DOMAIN]) + data = json.dumps(DEFAULT_CONFIG_2[mqtt.DOMAIN][vacuum.DOMAIN]) await help_test_discovery_removal( hass, mqtt_mock_entry_no_yaml_config, caplog, vacuum.DOMAIN, data ) @@ -778,41 +776,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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config): """Test MQTT subscriptions are managed when entity_id is updated.""" config = { - vacuum.DOMAIN: { - "platform": "mqtt", - "name": "test", - "battery_level_topic": "test-topic", - "battery_level_template": "{{ value_json.battery_level }}", - "command_topic": "command-topic", - "availability_topic": "avty-topic", + mqtt.DOMAIN: { + vacuum.DOMAIN: { + "name": "test", + "battery_level_topic": "test-topic", + "battery_level_template": "{{ value_json.battery_level }}", + "command_topic": "command-topic", + "availability_topic": "avty-topic", + } } } await help_test_entity_id_update_subscriptions( @@ -827,20 +826,21 @@ async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_co 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT debug info.""" config = { - vacuum.DOMAIN: { - "platform": "mqtt", - "name": "test", - "battery_level_topic": "state-topic", - "battery_level_template": "{{ value_json.battery_level }}", - "command_topic": "command-topic", - "payload_turn_on": "ON", + mqtt.DOMAIN: { + vacuum.DOMAIN: { + "name": "test", + "battery_level_topic": "state-topic", + "battery_level_template": "{{ value_json.battery_level }}", + "command_topic": "command-topic", + "payload_turn_on": "ON", + } } } await help_test_entity_debug_info_message( @@ -904,8 +904,8 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = vacuum.DOMAIN - config = deepcopy(DEFAULT_CONFIG_LEGACY[domain]) - config["supported_features"] = [ + config = deepcopy(DEFAULT_CONFIG) + config[mqtt.DOMAIN][domain]["supported_features"] = [ "turn_on", "turn_off", "clean_spot", @@ -930,12 +930,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = vacuum.DOMAIN @@ -975,7 +977,7 @@ async def test_encoding_subscribable_topics( ): """Test handling of incoming encoded payload.""" domain = vacuum.DOMAIN - config = deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain]) config[CONF_SUPPORTED_FEATURES] = [ "turn_on", "turn_off", @@ -1007,11 +1009,8 @@ 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_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 + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.mqtttest") # Test deprecated YAML configuration under the platform key diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index b34406f42bc..05f9d0e72f0 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -223,7 +223,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -2094,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) @@ -2124,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) @@ -2136,7 +2136,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_LIGHT_ATTRIBUTES_BLOCKED, ) @@ -2144,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) @@ -2157,20 +2157,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -2181,29 +2181,29 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one light per unique_id.""" config = { - light.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + light.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, config @@ -2744,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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) @@ -2789,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_LEGACY, + DEFAULT_CONFIG, light.SERVICE_TURN_ON, ) @@ -2914,11 +2914,11 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = light.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config = copy.deepcopy(DEFAULT_CONFIG) if topic == "effect_command_topic": - config["effect_list"] = ["random", "color_loop"] + config[mqtt.DOMAIN][domain]["effect_list"] = ["random", "color_loop"] elif topic == "white_command_topic": - config["rgb_command_topic"] = "some-cmd-topic" + config[mqtt.DOMAIN][domain]["rgb_command_topic"] = "some-cmd-topic" await help_test_publishing_with_custom_encoding( hass, @@ -2939,12 +2939,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = light.DOMAIN @@ -2993,7 +2995,7 @@ async def test_encoding_subscribable_topics( init_payload, ): """Test handling of incoming encoded payload.""" - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[light.DOMAIN]) + config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][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" @@ -3036,7 +3038,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_LEGACY[light.DOMAIN]) + config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][light.DOMAIN]) config[CONF_BRIGHTNESS_COMMAND_TOPIC] = "light/CONF_BRIGHTNESS_COMMAND_TOPIC" await help_test_encoding_subscribable_topics( @@ -3138,17 +3140,14 @@ 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_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 + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index af6daf6b7e4..f2835121b86 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -125,7 +125,7 @@ from .test_common import ( help_test_setting_blocked_attribute_via_mqtt_json_message, help_test_setup_manual_entity_from_yaml, help_test_unique_id, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -1854,28 +1854,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) @@ -1884,7 +1884,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) @@ -1896,7 +1896,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_LIGHT_ATTRIBUTES_BLOCKED, ) @@ -1904,7 +1904,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) @@ -1917,20 +1917,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -1941,31 +1941,31 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one light per unique_id.""" config = { - light.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "schema": "json", - "state_topic": "test-topic", - "command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "schema": "json", - "state_topic": "test-topic", - "command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + light.DOMAIN: [ + { + "name": "Test 1", + "schema": "json", + "state_topic": "test-topic", + "command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "schema": "json", + "state_topic": "test-topic", + "command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, config @@ -2057,7 +2057,7 @@ async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_ hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -2067,7 +2067,7 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_ hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -2077,7 +2077,7 @@ async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -2087,27 +2087,21 @@ async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) 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_LEGACY, + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY, + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) @@ -2117,7 +2111,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_LEGACY, + DEFAULT_CONFIG, light.SERVICE_TURN_ON, command_payload='{"state":"ON"}', state_payload='{"state":"ON"}', @@ -2182,9 +2176,9 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = light.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config = copy.deepcopy(DEFAULT_CONFIG) if topic == "effect_command_topic": - config["effect_list"] = ["random", "color_loop"] + config[mqtt.DOMAIN][domain]["effect_list"] = ["random", "color_loop"] await help_test_publishing_with_custom_encoding( hass, @@ -2205,12 +2199,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = light.DOMAIN @@ -2241,7 +2237,7 @@ async def test_encoding_subscribable_topics( init_payload, ): """Test handling of incoming encoded payload.""" - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[light.DOMAIN]) + config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][light.DOMAIN]) config["color_mode"] = True config["supported_color_modes"] = [ "color_temp", @@ -2269,11 +2265,8 @@ 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_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 + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") # Test deprecated YAML configuration under the platform key diff --git a/tests/components/mqtt/test_light_template.py b/tests/components/mqtt/test_light_template.py index 8ecdcc2c872..dd3f267b0d0 100644 --- a/tests/components/mqtt/test_light_template.py +++ b/tests/components/mqtt/test_light_template.py @@ -71,7 +71,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -846,28 +846,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) @@ -876,7 +876,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) @@ -888,7 +888,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_LIGHT_ATTRIBUTES_BLOCKED, ) @@ -896,7 +896,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) @@ -909,20 +909,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -933,33 +933,35 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, light.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one light per unique_id.""" config = { - light.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "schema": "template", - "state_topic": "test-topic", - "command_topic": "test_topic", - "command_on_template": "on,{{ transition }}", - "command_off_template": "off,{{ transition|d }}", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "schema": "template", - "state_topic": "test-topic", - "command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + light.DOMAIN: [ + { + "name": "Test 1", + "schema": "template", + "state_topic": "test-topic", + "command_topic": "test_topic", + "command_on_template": "on,{{ transition }}", + "command_off_template": "off,{{ transition|d }}", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "schema": "template", + "state_topic": "test-topic2", + "command_topic": "test_topic2", + "command_on_template": "on,{{ transition }}", + "command_off_template": "off,{{ transition|d }}", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, config @@ -1048,56 +1050,57 @@ 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG ) async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): """Test MQTT debug info.""" config = { - light.DOMAIN: { - "platform": "mqtt", - "schema": "template", - "name": "test", - "command_topic": "test-topic", - "command_on_template": "ON", - "command_off_template": "off,{{ transition|d }}", - "state_template": '{{ value.split(",")[0] }}', + mqtt.DOMAIN: { + light.DOMAIN: { + "schema": "template", + "name": "test", + "command_topic": "test-topic", + "command_on_template": "ON", + "command_off_template": "off,{{ transition|d }}", + "state_template": '{{ value.split(",")[0] }}', + } } } await help_test_entity_debug_info_message( @@ -1169,9 +1172,9 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = light.DOMAIN - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain]) + config = copy.deepcopy(DEFAULT_CONFIG) if topic == "effect_command_topic": - config["effect_list"] = ["random", "color_loop"] + config[mqtt.DOMAIN][domain]["effect_list"] = ["random", "color_loop"] await help_test_publishing_with_custom_encoding( hass, @@ -1192,12 +1195,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = light.DOMAIN @@ -1222,7 +1227,7 @@ async def test_encoding_subscribable_topics( init_payload, ): """Test handling of incoming encoded payload.""" - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[light.DOMAIN]) + config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][light.DOMAIN]) config["state_template"] = "{{ value }}" await help_test_encoding_subscribable_topics( hass, @@ -1241,17 +1246,14 @@ 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_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 + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_lock.py b/tests/components/mqtt/test_lock.py index de97de23a1e..0784065ddbe 100644 --- a/tests/components/mqtt/test_lock.py +++ b/tests/components/mqtt/test_lock.py @@ -49,7 +49,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -452,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) @@ -482,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) @@ -494,7 +494,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_LOCK_ATTRIBUTES_BLOCKED, ) @@ -502,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) @@ -515,7 +515,7 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, lock.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -523,41 +523,41 @@ async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, lock.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, caplog, lock.DOMAIN, DEFAULT_CONFIG ) 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: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "command_topic": "test_topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + lock.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "command_topic": "test_topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, config @@ -626,42 +626,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 lock device registry integration.""" await help_test_entity_device_info_with_connection( - hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG ) @@ -671,7 +671,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config): hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, SERVICE_LOCK, command_payload="LOCK", ) @@ -701,7 +701,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = lock.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_publishing_with_custom_encoding( hass, @@ -720,12 +720,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = lock.DOMAIN @@ -754,7 +756,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, lock.DOMAIN, - DEFAULT_CONFIG_LEGACY[lock.DOMAIN], + DEFAULT_CONFIG[mqtt.DOMAIN][lock.DOMAIN], topic, value, attribute, @@ -765,17 +767,14 @@ 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_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 + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_number.py b/tests/components/mqtt/test_number.py index 69b0473fb9d..65c8655472c 100644 --- a/tests/components/mqtt/test_number.py +++ b/tests/components/mqtt/test_number.py @@ -59,7 +59,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -446,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) @@ -476,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) @@ -488,7 +488,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_NUMBER_ATTRIBUTES_BLOCKED, ) @@ -496,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) @@ -509,20 +509,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, number.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, number.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -533,29 +533,29 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, number.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one number per unique_id.""" config = { - number.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "command_topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "command_topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + number.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "command_topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "command_topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, config @@ -564,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_LEGACY[number.DOMAIN]) + data = json.dumps(DEFAULT_CONFIG[mqtt.DOMAIN][number.DOMAIN]) await help_test_discovery_removal( hass, mqtt_mock_entry_no_yaml_config, caplog, number.DOMAIN, data ) @@ -624,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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG ) @@ -669,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_LEGACY, + DEFAULT_CONFIG, SERVICE_SET_VALUE, service_parameters={ATTR_VALUE: 45}, command_payload="45", @@ -882,7 +882,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = NUMBER_DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_publishing_with_custom_encoding( hass, @@ -901,12 +901,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = number.DOMAIN @@ -935,8 +937,8 @@ async def test_encoding_subscribable_topics( hass, mqtt_mock_entry_with_yaml_config, caplog, - "number", - DEFAULT_CONFIG_LEGACY["number"], + number.DOMAIN, + DEFAULT_CONFIG[mqtt.DOMAIN][number.DOMAIN], topic, value, attribute, @@ -947,17 +949,14 @@ 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_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 + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_scene.py b/tests/components/mqtt/test_scene.py index a1c1644cc37..122c32caa03 100644 --- a/tests/components/mqtt/test_scene.py +++ b/tests/components/mqtt/test_scene.py @@ -87,25 +87,26 @@ 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, scene.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, scene.DOMAIN, DEFAULT_CONFIG ) async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by default payload with defined topic.""" config = { - scene.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "payload_on": 1, + mqtt.DOMAIN: { + scene.DOMAIN: { + "name": "test", + "command_topic": "command-topic", + "payload_on": 1, + } } } @@ -123,11 +124,12 @@ async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_conf async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by custom payload with defined topic.""" config = { - scene.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "payload_on": 1, + mqtt.DOMAIN: { + scene.DOMAIN: { + "name": "test", + "command_topic": "command-topic", + "payload_on": 1, + } } } @@ -145,20 +147,20 @@ async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_confi async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one scene per unique_id.""" config = { - scene.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + scene.DOMAIN: [ + { + "name": "Test 1", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, scene.DOMAIN, config @@ -175,8 +177,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_LEGACY[scene.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[scene.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][scene.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][scene.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["payload_on"] = "ON" @@ -223,12 +225,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = scene.DOMAIN @@ -239,17 +243,14 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): async def test_setup_manual_entity_from_yaml(hass): """Test setup manual configured MQTT entity.""" platform = scene.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) - assert hass.states.get(f"{platform}.test") is not None + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_select.py b/tests/components/mqtt/test_select.py index c66638a18af..5b588304061 100644 --- a/tests/components/mqtt/test_select.py +++ b/tests/components/mqtt/test_select.py @@ -49,7 +49,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -323,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) @@ -353,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) @@ -365,7 +365,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_SELECT_ATTRIBUTES_BLOCKED, ) @@ -373,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) @@ -386,20 +386,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, select.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, select.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -410,31 +410,31 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, select.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one select per unique_id.""" config = { - select.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "command_topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - "options": ["milk", "beer"], - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "command_topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - "options": ["milk", "beer"], - }, - ] + mqtt.DOMAIN: { + select.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "command_topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + "options": ["milk", "beer"], + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "command_topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + "options": ["milk", "beer"], + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, config @@ -443,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_LEGACY[select.DOMAIN]) + data = json.dumps(DEFAULT_CONFIG[mqtt.DOMAIN][select.DOMAIN]) await help_test_discovery_removal( hass, mqtt_mock_entry_no_yaml_config, caplog, select.DOMAIN, data ) @@ -501,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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG ) @@ -546,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_LEGACY, + DEFAULT_CONFIG, select.SERVICE_SELECT_OPTION, service_parameters={ATTR_OPTION: "beer"}, command_payload="beer", @@ -635,8 +635,8 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = select.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] - config["options"] = ["milk", "beer"] + config = DEFAULT_CONFIG + config[mqtt.DOMAIN][domain]["options"] = ["milk", "beer"] await help_test_publishing_with_custom_encoding( hass, @@ -655,12 +655,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = select.DOMAIN @@ -685,13 +687,13 @@ async def test_encoding_subscribable_topics( attribute_value, ): """Test handling of incoming encoded payload.""" - config = copy.deepcopy(DEFAULT_CONFIG_LEGACY["select"]) + config = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][select.DOMAIN]) config["options"] = ["milk", "beer"] await help_test_encoding_subscribable_topics( hass, mqtt_mock_entry_with_yaml_config, caplog, - "select", + select.DOMAIN, config, topic, value, @@ -703,17 +705,14 @@ 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_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 + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_sensor.py b/tests/components/mqtt/test_sensor.py index c5cf377cf1a..6cfaa9678bb 100644 --- a/tests/components/mqtt/test_sensor.py +++ b/tests/components/mqtt/test_sensor.py @@ -59,7 +59,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -585,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) @@ -608,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) @@ -617,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) @@ -626,34 +626,31 @@ 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) -async def test_default_availability_list_single( - hass, mqtt_mock_entry_no_yaml_config, caplog -): +async def test_default_availability_list_single(hass, caplog): """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_LEGACY, + DEFAULT_CONFIG, ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) @@ -760,7 +757,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) @@ -772,7 +769,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message( hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, MQTT_SENSOR_ATTRIBUTES_BLOCKED, ) @@ -780,7 +777,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) @@ -793,20 +790,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -817,27 +814,27 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one sensor per unique_id.""" config = { - sensor.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + sensor.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, config @@ -951,42 +948,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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) @@ -1021,42 +1018,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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY, None + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG, 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) @@ -1064,7 +1061,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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG ) @@ -1101,12 +1098,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = sensor.DOMAIN @@ -1230,7 +1229,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, sensor.DOMAIN, - DEFAULT_CONFIG_LEGACY[sensor.DOMAIN], + DEFAULT_CONFIG[mqtt.DOMAIN][sensor.DOMAIN], topic, value, attribute, @@ -1242,17 +1241,14 @@ 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_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 + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_siren.py b/tests/components/mqtt/test_siren.py index fd91847f767..7c16f3f01a3 100644 --- a/tests/components/mqtt/test_siren.py +++ b/tests/components/mqtt/test_siren.py @@ -46,7 +46,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -457,27 +457,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, siren.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG ) async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by default payload with defined topic.""" config = { - 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, + } } } @@ -495,13 +496,14 @@ async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_conf async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by custom payload with defined topic.""" config = { - 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, + } } } @@ -558,7 +560,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG ) @@ -567,14 +569,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_LEGACY, {} + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG, {} ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG ) @@ -587,20 +589,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, siren.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, siren.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -611,29 +613,29 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, siren.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one siren per unique_id.""" config = { - siren.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + siren.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, config @@ -656,8 +658,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_LEGACY[siren.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[siren.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][siren.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][siren.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "siren/state1" @@ -693,8 +695,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_LEGACY[siren.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[siren.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][siren.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][siren.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "siren/state1" @@ -844,42 +846,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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG ) @@ -889,7 +891,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_LEGACY, + DEFAULT_CONFIG, siren.SERVICE_TURN_ON, command_payload='{"state":"ON"}', ) @@ -926,8 +928,8 @@ 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_LEGACY[domain]) - config[siren.ATTR_AVAILABLE_TONES] = ["siren", "xylophone"] + config = copy.deepcopy(DEFAULT_CONFIG) + config[mqtt.DOMAIN][domain][siren.ATTR_AVAILABLE_TONES] = ["siren", "xylophone"] await help_test_publishing_with_custom_encoding( hass, @@ -946,12 +948,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = siren.DOMAIN @@ -980,7 +984,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, siren.DOMAIN, - DEFAULT_CONFIG_LEGACY[siren.DOMAIN], + DEFAULT_CONFIG[mqtt.DOMAIN][siren.DOMAIN], topic, value, attribute, @@ -991,17 +995,14 @@ 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_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 + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) diff --git a/tests/components/mqtt/test_state_vacuum.py b/tests/components/mqtt/test_state_vacuum.py index 4c2fbf3a596..917c5fa43d8 100644 --- a/tests/components/mqtt/test_state_vacuum.py +++ b/tests/components/mqtt/test_state_vacuum.py @@ -61,7 +61,7 @@ from .test_common import ( help_test_setting_blocked_attribute_via_mqtt_json_message, help_test_setup_manual_entity_from_yaml, help_test_unique_id, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -375,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) @@ -405,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) @@ -417,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_LEGACY, + DEFAULT_CONFIG_2, MQTT_VACUUM_ATTRIBUTES_BLOCKED, ) @@ -425,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) @@ -438,7 +438,7 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, vacuum.DOMAIN, - DEFAULT_CONFIG_2_LEGACY, + DEFAULT_CONFIG_2, ) @@ -446,12 +446,12 @@ async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, vacuum.DOMAIN, - DEFAULT_CONFIG_2_LEGACY, + DEFAULT_CONFIG_2, ) @@ -462,29 +462,29 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, vacuum.DOMAIN, - DEFAULT_CONFIG_2_LEGACY, + DEFAULT_CONFIG_2, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one vacuum per unique_id.""" config = { - vacuum.DOMAIN: [ - { - "platform": "mqtt", - "schema": "state", - "name": "Test 1", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "schema": "state", - "name": "Test 2", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + vacuum.DOMAIN: [ + { + "schema": "state", + "name": "Test 1", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "schema": "state", + "name": "Test 2", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, config @@ -539,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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2 ) @@ -584,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_LEGACY, + DEFAULT_CONFIG_2, vacuum.SERVICE_START, command_payload="start", state_payload="{}", @@ -643,8 +643,8 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = vacuum.DOMAIN - config = deepcopy(DEFAULT_CONFIG_LEGACY[domain]) - config["supported_features"] = [ + config = deepcopy(DEFAULT_CONFIG) + config[mqtt.DOMAIN][domain]["supported_features"] = [ "battery", "clean_spot", "fan_speed", @@ -674,12 +674,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = vacuum.DOMAIN @@ -719,7 +721,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, vacuum.DOMAIN, - DEFAULT_CONFIG_LEGACY[vacuum.DOMAIN], + DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN], topic, value, attribute, @@ -731,11 +733,8 @@ 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_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 + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.mqtttest") # Test deprecated YAML configuration under the platform key diff --git a/tests/components/mqtt/test_switch.py b/tests/components/mqtt/test_switch.py index 87d919f41c5..72a09529242 100644 --- a/tests/components/mqtt/test_switch.py +++ b/tests/components/mqtt/test_switch.py @@ -43,7 +43,7 @@ from .test_common import ( help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_unload_config_entry_with_platform, - help_test_update_with_json_attrs_bad_JSON, + help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) @@ -227,27 +227,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, switch.DOMAIN, DEFAULT_CONFIG_LEGACY + hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG ) async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by default payload with defined topic.""" config = { - switch.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_on": 1, - "payload_off": 0, + mqtt.DOMAIN: { + switch.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_on": 1, + "payload_off": 0, + } } } @@ -265,13 +266,14 @@ async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_conf async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config): """Test availability by custom payload with defined topic.""" config = { - switch.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "state-topic", - "command_topic": "command-topic", - "payload_on": 1, - "payload_off": 0, + mqtt.DOMAIN: { + switch.DOMAIN: { + "name": "test", + "state_topic": "state-topic", + "command_topic": "command-topic", + "payload_on": 1, + "payload_off": 0, + } } } @@ -328,7 +330,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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG ) @@ -337,14 +339,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_LEGACY, {} + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG, {} ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG ) @@ -357,20 +359,20 @@ async def test_update_with_json_attrs_not_dict( mqtt_mock_entry_with_yaml_config, caplog, switch.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) -async def test_update_with_json_attrs_bad_JSON( +async def test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog ): """Test attributes get extracted from a JSON result.""" - await help_test_update_with_json_attrs_bad_JSON( + await help_test_update_with_json_attrs_bad_json( hass, mqtt_mock_entry_with_yaml_config, caplog, switch.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) @@ -381,29 +383,29 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo mqtt_mock_entry_no_yaml_config, caplog, switch.DOMAIN, - DEFAULT_CONFIG_LEGACY, + DEFAULT_CONFIG, ) async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config): """Test unique id option only creates one switch per unique_id.""" config = { - switch.DOMAIN: [ - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - { - "platform": "mqtt", - "name": "Test 2", - "state_topic": "test-topic", - "command_topic": "command-topic", - "unique_id": "TOTALLY_UNIQUE", - }, - ] + mqtt.DOMAIN: { + switch.DOMAIN: [ + { + "name": "Test 1", + "state_topic": "test-topic", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + { + "name": "Test 2", + "state_topic": "test-topic", + "command_topic": "command-topic", + "unique_id": "TOTALLY_UNIQUE", + }, + ] + } } await help_test_unique_id( hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, config @@ -426,8 +428,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_LEGACY[switch.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][switch.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][switch.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "switch/state1" @@ -463,8 +465,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_LEGACY[switch.DOMAIN]) - config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN]) + config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][switch.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][switch.DOMAIN]) config1["name"] = "Beer" config2["name"] = "Milk" config1["state_topic"] = "switch/state1" @@ -534,42 +536,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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG ) 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_LEGACY + hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG ) @@ -579,7 +581,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_LEGACY, + DEFAULT_CONFIG, switch.SERVICE_TURN_ON, ) @@ -615,7 +617,7 @@ async def test_publishing_with_custom_encoding( ): """Test publishing MQTT payload with different encoding.""" domain = switch.DOMAIN - config = DEFAULT_CONFIG_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_publishing_with_custom_encoding( hass, @@ -634,12 +636,14 @@ 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_reloadable( hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config ) +# Test deprecated YAML configuration under the platform key +# Scheduled to be removed in HA core 2022.12 async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): """Test reloading the MQTT platform with late entry setup.""" domain = switch.DOMAIN @@ -668,7 +672,7 @@ async def test_encoding_subscribable_topics( mqtt_mock_entry_with_yaml_config, caplog, switch.DOMAIN, - DEFAULT_CONFIG_LEGACY[switch.DOMAIN], + DEFAULT_CONFIG[mqtt.DOMAIN][switch.DOMAIN], topic, value, attribute, @@ -679,17 +683,14 @@ 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_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 + await help_test_setup_manual_entity_from_yaml(hass, DEFAULT_CONFIG) + assert hass.states.get(f"{platform}.test") 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_LEGACY[domain] + config = DEFAULT_CONFIG await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config )