From 71a0e474cc4630ea8905e3c604d3c12717a17c1a Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 29 Jun 2021 11:16:57 +0200 Subject: [PATCH] Filter MQTT number JSON attributes (#52286) --- homeassistant/components/mqtt/number.py | 10 ++++++++++ tests/components/mqtt/test_number.py | 14 +++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/mqtt/number.py b/homeassistant/components/mqtt/number.py index f7253541b69..866f93fd674 100644 --- a/homeassistant/components/mqtt/number.py +++ b/homeassistant/components/mqtt/number.py @@ -40,6 +40,14 @@ CONF_STEP = "step" DEFAULT_NAME = "MQTT Number" DEFAULT_OPTIMISTIC = False +MQTT_NUMBER_ATTRIBUTES_BLOCKED = frozenset( + { + number.ATTR_MAX, + number.ATTR_MIN, + number.ATTR_STEP, + } +) + def validate_config(config): """Validate that the configuration is valid, throws if it isn't.""" @@ -92,6 +100,8 @@ async def _async_setup_entity( class MqttNumber(MqttEntity, NumberEntity, RestoreEntity): """representation of an MQTT number.""" + _attributes_extra_blocked = MQTT_NUMBER_ATTRIBUTES_BLOCKED + def __init__(self, hass, config, config_entry, discovery_data): """Initialize the MQTT Number.""" self._config = config diff --git a/tests/components/mqtt/test_number.py b/tests/components/mqtt/test_number.py index 8b62d1f9f33..37693340308 100644 --- a/tests/components/mqtt/test_number.py +++ b/tests/components/mqtt/test_number.py @@ -5,7 +5,11 @@ from unittest.mock import patch import pytest from homeassistant.components import number -from homeassistant.components.mqtt.number import CONF_MAX, CONF_MIN +from homeassistant.components.mqtt.number import ( + CONF_MAX, + CONF_MIN, + MQTT_NUMBER_ATTRIBUTES_BLOCKED, +) from homeassistant.components.number import ( ATTR_MAX, ATTR_MIN, @@ -37,6 +41,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, @@ -250,6 +255,13 @@ 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, number.DOMAIN, DEFAULT_CONFIG, MQTT_NUMBER_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(