Skip to content

Commit

Permalink
Move manual configuration of MQTT alarm control panel to the integrat…
Browse files Browse the repository at this point in the history
…ion key (#72165)

Add alarm_control_panel
  • Loading branch information
jbouwh committed May 20, 2022
1 parent 4fb3a01 commit 654b095
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/mqtt/__init__.py
Expand Up @@ -190,6 +190,7 @@

PLATFORM_CONFIG_SCHEMA_BASE = vol.Schema(
{
vol.Optional(Platform.ALARM_CONTROL_PANEL.value): cv.ensure_list,
vol.Optional(Platform.FAN.value): cv.ensure_list,
vol.Optional(Platform.LIGHT.value): cv.ensure_list,
}
Expand Down
28 changes: 24 additions & 4 deletions homeassistant/components/mqtt/alarm_control_panel.py
@@ -1,6 +1,7 @@
"""This platform enables the possibility to control a MQTT alarm."""
from __future__ import annotations

import asyncio
import functools
import logging
import re
Expand Down Expand Up @@ -44,8 +45,10 @@
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,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -82,7 +85,7 @@
REMOTE_CODE = "REMOTE_CODE"
REMOTE_CODE_TEXT = "REMOTE_CODE_TEXT"

PLATFORM_SCHEMA = mqtt.MQTT_BASE_PLATFORM_SCHEMA.extend(
PLATFORM_SCHEMA_MODERN = mqtt.MQTT_BASE_SCHEMA.extend(
{
vol.Optional(CONF_CODE): cv.string,
vol.Optional(CONF_CODE_ARM_REQUIRED, default=True): cv.boolean,
Expand Down Expand Up @@ -110,7 +113,13 @@
}
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)

DISCOVERY_SCHEMA = PLATFORM_SCHEMA.extend({}, extra=vol.REMOVE_EXTRA)
# Configuring MQTT alarm control panels under the alarm_control_panel 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(alarm.DOMAIN),
)

DISCOVERY_SCHEMA = PLATFORM_SCHEMA_MODERN.extend({}, extra=vol.REMOVE_EXTRA)


async def async_setup_platform(
Expand All @@ -119,7 +128,8 @@ async def async_setup_platform(
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up MQTT alarm control panel through configuration.yaml."""
"""Set up MQTT alarm control panel configured under the alarm_control_panel key (deprecated)."""
# Deprecated in HA Core 2022.6
await async_setup_platform_helper(
hass, alarm.DOMAIN, config, async_add_entities, _async_setup_entity
)
Expand All @@ -130,7 +140,17 @@ async def async_setup_entry(
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up MQTT alarm control panel dynamically through MQTT discovery."""
"""Set up MQTT alarm control panel 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, alarm.DOMAIN, PLATFORM_SCHEMA_MODERN
)
)
)
# setup for discovery
setup = functools.partial(
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
)
Expand Down
13 changes: 13 additions & 0 deletions tests/components/mqtt/test_alarm_control_panel.py
Expand Up @@ -57,6 +57,7 @@
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,
Expand Down Expand Up @@ -848,3 +849,15 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
domain = alarm_control_panel.DOMAIN
config = DEFAULT_CONFIG[domain]
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 = alarm_control_panel.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

0 comments on commit 654b095

Please sign in to comment.