From 91b4f7d1d53f9614b9a9287696a3346fd2d91d15 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 29 Jun 2021 10:32:23 +0200 Subject: [PATCH] Filter MQTT alarm JSON attributes (#52278) --- .../components/mqtt/alarm_control_panel.py | 10 ++++++++++ tests/components/mqtt/test_alarm_control_panel.py | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/homeassistant/components/mqtt/alarm_control_panel.py b/homeassistant/components/mqtt/alarm_control_panel.py index 1e7ccf5bb4c..aa98a48dc10 100644 --- a/homeassistant/components/mqtt/alarm_control_panel.py +++ b/homeassistant/components/mqtt/alarm_control_panel.py @@ -55,6 +55,14 @@ CONF_PAYLOAD_ARM_NIGHT = "payload_arm_night" CONF_PAYLOAD_ARM_CUSTOM_BYPASS = "payload_arm_custom_bypass" CONF_COMMAND_TEMPLATE = "command_template" +MQTT_ALARM_ATTRIBUTES_BLOCKED = frozenset( + { + alarm.ATTR_CHANGED_BY, + alarm.ATTR_CODE_ARM_REQUIRED, + alarm.ATTR_CODE_FORMAT, + } +) + DEFAULT_COMMAND_TEMPLATE = "{{action}}" DEFAULT_ARM_NIGHT = "ARM_NIGHT" DEFAULT_ARM_AWAY = "ARM_AWAY" @@ -112,6 +120,8 @@ async def _async_setup_entity( class MqttAlarm(MqttEntity, alarm.AlarmControlPanelEntity): """Representation of a MQTT alarm status.""" + _attributes_extra_blocked = MQTT_ALARM_ATTRIBUTES_BLOCKED + def __init__(self, hass, config, config_entry, discovery_data): """Init the MQTT Alarm Control Panel.""" self._state = None diff --git a/tests/components/mqtt/test_alarm_control_panel.py b/tests/components/mqtt/test_alarm_control_panel.py index cfb59ba27a7..c9d06ef343e 100644 --- a/tests/components/mqtt/test_alarm_control_panel.py +++ b/tests/components/mqtt/test_alarm_control_panel.py @@ -6,6 +6,9 @@ from unittest.mock import patch import pytest from homeassistant.components import alarm_control_panel +from homeassistant.components.mqtt.alarm_control_panel import ( + MQTT_ALARM_ATTRIBUTES_BLOCKED, +) from homeassistant.const import ( STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_CUSTOM_BYPASS, @@ -39,6 +42,7 @@ from .test_common import ( help_test_entity_id_update_subscriptions, 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_unique_id, help_test_update_with_json_attrs_bad_JSON, help_test_update_with_json_attrs_not_dict, @@ -544,6 +548,17 @@ async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): ) +async def test_setting_blocked_attribute_via_mqtt_json_message(hass, mqtt_mock): + """Test the setting of attribute via MQTT with JSON payload.""" + await help_test_setting_blocked_attribute_via_mqtt_json_message( + hass, + mqtt_mock, + alarm_control_panel.DOMAIN, + DEFAULT_CONFIG, + MQTT_ALARM_ATTRIBUTES_BLOCKED, + ) + + async def test_setting_attribute_with_template(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" await help_test_setting_attribute_with_template(