Move manual configuration of MQTT scene to the integration key (#72273)

Add scene

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Jan Bouwhuis 2022-05-23 09:04:03 +02:00 committed by GitHub
parent eb988f7792
commit 673f43fbec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View File

@ -200,6 +200,7 @@ PLATFORM_CONFIG_SCHEMA_BASE = vol.Schema(
vol.Optional(Platform.HUMIDIFIER.value): cv.ensure_list, vol.Optional(Platform.HUMIDIFIER.value): cv.ensure_list,
vol.Optional(Platform.LIGHT.value): cv.ensure_list, vol.Optional(Platform.LIGHT.value): cv.ensure_list,
vol.Optional(Platform.LOCK.value): cv.ensure_list, vol.Optional(Platform.LOCK.value): cv.ensure_list,
vol.Optional(Platform.SCENE.value): cv.ensure_list,
vol.Optional(Platform.SIREN.value): cv.ensure_list, vol.Optional(Platform.SIREN.value): cv.ensure_list,
vol.Optional(Platform.SWITCH.value): cv.ensure_list, vol.Optional(Platform.SWITCH.value): cv.ensure_list,
vol.Optional(Platform.VACUUM.value): cv.ensure_list, vol.Optional(Platform.VACUUM.value): cv.ensure_list,

View File

@ -1,6 +1,7 @@
"""Support for MQTT scenes.""" """Support for MQTT scenes."""
from __future__ import annotations from __future__ import annotations
import asyncio
import functools import functools
import voluptuous as vol import voluptuous as vol
@ -21,14 +22,16 @@ from .mixins import (
CONF_OBJECT_ID, CONF_OBJECT_ID,
MQTT_AVAILABILITY_SCHEMA, MQTT_AVAILABILITY_SCHEMA,
MqttEntity, MqttEntity,
async_get_platform_config_from_yaml,
async_setup_entry_helper, async_setup_entry_helper,
async_setup_platform_helper, async_setup_platform_helper,
warn_for_legacy_schema,
) )
DEFAULT_NAME = "MQTT Scene" DEFAULT_NAME = "MQTT Scene"
DEFAULT_RETAIN = False DEFAULT_RETAIN = False
PLATFORM_SCHEMA = mqtt.MQTT_BASE_PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA_MODERN = mqtt.MQTT_BASE_SCHEMA.extend(
{ {
vol.Required(CONF_COMMAND_TOPIC): mqtt.valid_publish_topic, vol.Required(CONF_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_ICON): cv.icon, vol.Optional(CONF_ICON): cv.icon,
@ -42,7 +45,13 @@ PLATFORM_SCHEMA = mqtt.MQTT_BASE_PLATFORM_SCHEMA.extend(
} }
).extend(MQTT_AVAILABILITY_SCHEMA.schema) ).extend(MQTT_AVAILABILITY_SCHEMA.schema)
DISCOVERY_SCHEMA = PLATFORM_SCHEMA.extend({}, extra=vol.REMOVE_EXTRA) # Configuring MQTT Scenes under the scene 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(scene.DOMAIN),
)
DISCOVERY_SCHEMA = PLATFORM_SCHEMA_MODERN.extend({}, extra=vol.REMOVE_EXTRA)
async def async_setup_platform( async def async_setup_platform(
@ -51,7 +60,8 @@ async def async_setup_platform(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up MQTT scene through configuration.yaml.""" """Set up MQTT scene configured under the scene platform key (deprecated)."""
# Deprecated in HA Core 2022.6
await async_setup_platform_helper( await async_setup_platform_helper(
hass, scene.DOMAIN, config, async_add_entities, _async_setup_entity hass, scene.DOMAIN, config, async_add_entities, _async_setup_entity
) )
@ -62,7 +72,17 @@ async def async_setup_entry(
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up MQTT scene dynamically through MQTT discovery.""" """Set up MQTT scene 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, scene.DOMAIN, PLATFORM_SCHEMA_MODERN
)
)
)
# setup for discovery
setup = functools.partial( setup = functools.partial(
_async_setup_entity, hass, async_add_entities, config_entry=config_entry _async_setup_entity, hass, async_add_entities, config_entry=config_entry
) )

View File

@ -20,6 +20,7 @@ from .test_common import (
help_test_discovery_update_unchanged, help_test_discovery_update_unchanged,
help_test_reloadable, help_test_reloadable,
help_test_reloadable_late, help_test_reloadable_late,
help_test_setup_manual_entity_from_yaml,
help_test_unique_id, help_test_unique_id,
) )
@ -191,3 +192,15 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
domain = scene.DOMAIN domain = scene.DOMAIN
config = DEFAULT_CONFIG[domain] config = DEFAULT_CONFIG[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
async def test_setup_manual_entity_from_yaml(hass, caplog, tmp_path):
"""Test setup manual configured MQTT entity."""
platform = scene.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