Avoid redundant calls to async_write_ha_state in MQTT mqtt alarm_control_panel (#100691)

Limit state writes for mqtt alarm_control_panel
This commit is contained in:
Jan Bouwhuis 2023-09-22 11:22:09 +02:00 committed by GitHub
parent 1dadfcd52c
commit 1041610a70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 3 deletions

View file

@ -42,9 +42,14 @@ from .const import (
CONF_SUPPORTED_FEATURES, CONF_SUPPORTED_FEATURES,
) )
from .debug_info import log_messages from .debug_info import log_messages
from .mixins import MQTT_ENTITY_COMMON_SCHEMA, MqttEntity, async_setup_entry_helper from .mixins import (
MQTT_ENTITY_COMMON_SCHEMA,
MqttEntity,
async_setup_entry_helper,
write_state_on_attr_change,
)
from .models import MqttCommandTemplate, MqttValueTemplate, ReceiveMessage from .models import MqttCommandTemplate, MqttValueTemplate, ReceiveMessage
from .util import get_mqtt_data, valid_publish_topic, valid_subscribe_topic from .util import valid_publish_topic, valid_subscribe_topic
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -196,6 +201,7 @@ class MqttAlarm(MqttEntity, alarm.AlarmControlPanelEntity):
@callback @callback
@log_messages(self.hass, self.entity_id) @log_messages(self.hass, self.entity_id)
@write_state_on_attr_change(self, {"_attr_state"})
def message_received(msg: ReceiveMessage) -> None: def message_received(msg: ReceiveMessage) -> None:
"""Run when new MQTT message has been received.""" """Run when new MQTT message has been received."""
payload = self._value_template(msg.payload) payload = self._value_template(msg.payload)
@ -214,7 +220,6 @@ class MqttAlarm(MqttEntity, alarm.AlarmControlPanelEntity):
_LOGGER.warning("Received unexpected payload: %s", msg.payload) _LOGGER.warning("Received unexpected payload: %s", msg.payload)
return return
self._attr_state = str(payload) self._attr_state = str(payload)
get_mqtt_data(self.hass).state_write_requests.write_state_request(self)
self._sub_state = subscription.async_prepare_subscribe_topics( self._sub_state = subscription.async_prepare_subscribe_topics(
self.hass, self.hass,

View file

@ -62,6 +62,7 @@ from .test_common import (
help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_via_mqtt_json_message,
help_test_setting_attribute_with_template, help_test_setting_attribute_with_template,
help_test_setting_blocked_attribute_via_mqtt_json_message, help_test_setting_blocked_attribute_via_mqtt_json_message,
help_test_skipped_async_ha_write_state,
help_test_unique_id, help_test_unique_id,
help_test_unload_config_entry_with_platform, help_test_unload_config_entry_with_platform,
help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_bad_json,
@ -1232,3 +1233,39 @@ async def test_entity_name(
await help_test_entity_name( await help_test_entity_name(
hass, mqtt_mock_entry, domain, config, expected_friendly_name, device_class hass, mqtt_mock_entry, domain, config, expected_friendly_name, device_class
) )
@pytest.mark.parametrize(
"hass_config",
[
help_custom_config(
alarm_control_panel.DOMAIN,
DEFAULT_CONFIG,
(
{
"availability_topic": "availability-topic",
"json_attributes_topic": "json-attributes-topic",
"state_topic": "test-topic",
},
),
)
],
)
@pytest.mark.parametrize(
("topic", "payload1", "payload2"),
[
("test-topic", STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME),
("availability-topic", "online", "offline"),
("json-attributes-topic", '{"attr1": "val1"}', '{"attr1": "val2"}'),
],
)
async def test_skipped_async_ha_write_state(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
topic: str,
payload1: str,
payload2: str,
) -> None:
"""Test a write state command is only called when there is change."""
await mqtt_mock_entry()
await help_test_skipped_async_ha_write_state(hass, topic, payload1, payload2)