From c4893f96f60ab9f05215040c21526aa47ea0803f Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Mon, 23 May 2022 08:08:08 +0200 Subject: [PATCH] Move manual configuration of MQTT siren to the integration key (#72278) Add siren --- homeassistant/components/mqtt/__init__.py | 1 + homeassistant/components/mqtt/siren.py | 28 +++++++++++++++++++---- tests/components/mqtt/test_siren.py | 13 +++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 36c39e5b782..dff5e75fa23 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -200,6 +200,7 @@ PLATFORM_CONFIG_SCHEMA_BASE = vol.Schema( vol.Optional(Platform.HUMIDIFIER.value): cv.ensure_list, vol.Optional(Platform.LIGHT.value): cv.ensure_list, vol.Optional(Platform.LOCK.value): cv.ensure_list, + vol.Optional(Platform.SIREN.value): cv.ensure_list, vol.Optional(Platform.SWITCH.value): cv.ensure_list, vol.Optional(Platform.VACUUM.value): cv.ensure_list, vol.Optional(Platform.NUMBER.value): cv.ensure_list, diff --git a/homeassistant/components/mqtt/siren.py b/homeassistant/components/mqtt/siren.py index beed5b51e20..c3a41c3618e 100644 --- a/homeassistant/components/mqtt/siren.py +++ b/homeassistant/components/mqtt/siren.py @@ -1,6 +1,7 @@ """Support for MQTT sirens.""" from __future__ import annotations +import asyncio import copy import functools import json @@ -51,8 +52,10 @@ from .debug_info import log_messages from .mixins import ( MQTT_ENTITY_COMMON_SCHEMA, MqttEntity, + async_get_platform_config_from_yaml, async_setup_entry_helper, async_setup_platform_helper, + warn_for_legacy_schema, ) DEFAULT_NAME = "MQTT Siren" @@ -71,7 +74,7 @@ CONF_SUPPORT_VOLUME_SET = "support_volume_set" STATE = "state" -PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend( +PLATFORM_SCHEMA_MODERN = mqtt.MQTT_RW_SCHEMA.extend( { vol.Optional(CONF_AVAILABLE_TONES): cv.ensure_list, vol.Optional(CONF_COMMAND_TEMPLATE): cv.template, @@ -88,7 +91,13 @@ PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend( }, ).extend(MQTT_ENTITY_COMMON_SCHEMA.schema) -DISCOVERY_SCHEMA = vol.All(PLATFORM_SCHEMA.extend({}, extra=vol.REMOVE_EXTRA)) +# Configuring MQTT Sirens under the siren platform key is deprecated in HA Core 2022.6 +PLATFORM_SCHEMA = vol.All( + cv.PLATFORM_SCHEMA.extend(PLATFORM_SCHEMA_MODERN.schema), + warn_for_legacy_schema(siren.DOMAIN), +) + +DISCOVERY_SCHEMA = vol.All(PLATFORM_SCHEMA_MODERN.extend({}, extra=vol.REMOVE_EXTRA)) MQTT_SIREN_ATTRIBUTES_BLOCKED = frozenset( { @@ -116,7 +125,8 @@ async def async_setup_platform( async_add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: - """Set up MQTT siren through configuration.yaml.""" + """Set up MQTT sirens configured under the fan platform key (deprecated).""" + # Deprecated in HA Core 2022.6 await async_setup_platform_helper( hass, siren.DOMAIN, config, async_add_entities, _async_setup_entity ) @@ -127,7 +137,17 @@ async def async_setup_entry( config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: - """Set up MQTT siren dynamically through MQTT discovery.""" + """Set up MQTT siren through configuration.yaml and dynamically through MQTT discovery.""" + # load and initialize platform config from configuration.yaml + await asyncio.gather( + *( + _async_setup_entity(hass, async_add_entities, config, config_entry) + for config in await async_get_platform_config_from_yaml( + hass, siren.DOMAIN, PLATFORM_SCHEMA_MODERN + ) + ) + ) + # setup for discovery setup = functools.partial( _async_setup_entity, hass, async_add_entities, config_entry=config_entry ) diff --git a/tests/components/mqtt/test_siren.py b/tests/components/mqtt/test_siren.py index f39154badc9..197ed34b7e4 100644 --- a/tests/components/mqtt/test_siren.py +++ b/tests/components/mqtt/test_siren.py @@ -42,6 +42,7 @@ from .test_common import ( help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, 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_not_dict, @@ -895,3 +896,15 @@ async def test_encoding_subscribable_topics( attribute, attribute_value, ) + + +async def test_setup_manual_entity_from_yaml(hass, caplog, tmp_path): + """Test setup manual configured MQTT entity.""" + platform = siren.DOMAIN + config = copy.deepcopy(DEFAULT_CONFIG[platform]) + config["name"] = "test" + del config["platform"] + await help_test_setup_manual_entity_from_yaml( + hass, caplog, tmp_path, platform, config + ) + assert hass.states.get(f"{platform}.test") is not None