Avoid redundant calls to async_write_ha_state in MQTT climate & water_heater (#100696)

Limit state writes for mqtt climate & water_heater
This commit is contained in:
Jan Bouwhuis 2023-09-22 11:22:57 +02:00 committed by GitHub
parent 1041610a70
commit 384adb1c87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 125 additions and 14 deletions

View file

@ -58,6 +58,7 @@ from .test_common import (
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_skipped_async_ha_write_state,
help_test_unique_id,
help_test_unload_config_entry_with_platform,
help_test_update_with_json_attrs_bad_json,
@ -2555,3 +2556,60 @@ async def test_unload_entry(
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry, domain, config
)
@pytest.mark.parametrize(
"hass_config",
[
help_custom_config(
climate.DOMAIN,
DEFAULT_CONFIG,
(
{
"availability_topic": "availability-topic",
"json_attributes_topic": "json-attributes-topic",
"action_topic": "action-topic",
"fan_mode_state_topic": "fan-mode-state-topic",
"mode_state_topic": "mode-state-topic",
"current_humidity_topic": "current-humidity-topic",
"current_temperature_topic": "current-temperature-topic",
"preset_mode_state_topic": "preset-mode-state-topic",
"preset_modes": ["eco", "away"],
"swing_mode_state_topic": "swing-mode-state-topic",
"target_humidity_state_topic": "target-humidity-state-topic",
"temperature_high_state_topic": "temperature-high-state-topic",
"temperature_low_state_topic": "temperature-low-state-topic",
"temperature_state_topic": "temperature-state-topic",
},
),
)
],
)
@pytest.mark.parametrize(
("topic", "payload1", "payload2"),
[
("availability-topic", "online", "offline"),
("json-attributes-topic", '{"attr1": "val1"}', '{"attr1": "val2"}'),
("action-topic", "cooling", "heating"),
("fan-mode-state-topic", "low", "medium"),
("mode-state-topic", "cool", "heat"),
("current-humidity-topic", "45", "46"),
("current-temperature-topic", "18.0", "18.1"),
("preset-mode-state-topic", "eco", "away"),
("swing-mode-state-topic", "on", "off"),
("target-humidity-state-topic", "45", "50"),
("temperature-state-topic", "18", "19"),
("temperature-low-state-topic", "18", "19"),
("temperature-high-state-topic", "18", "19"),
],
)
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)