Rework and added tests for mqtt event (#100769)
Use write_state_on_attr_change and add tests
This commit is contained in:
parent
f83a597603
commit
002be37257
2 changed files with 74 additions and 3 deletions
|
@ -13,6 +13,7 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from .test_common import (
|
||||
help_custom_config,
|
||||
help_test_availability_when_connection_lost,
|
||||
help_test_availability_without_topic,
|
||||
help_test_custom_availability_payload,
|
||||
|
@ -42,6 +43,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,
|
||||
|
@ -668,3 +670,68 @@ async def test_entity_name(
|
|||
await help_test_entity_name(
|
||||
hass, mqtt_mock_entry, domain, config, expected_friendly_name, device_class
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"hass_config",
|
||||
[
|
||||
help_custom_config(
|
||||
event.DOMAIN,
|
||||
DEFAULT_CONFIG,
|
||||
(
|
||||
{
|
||||
"availability_topic": "availability-topic",
|
||||
"json_attributes_topic": "json-attributes-topic",
|
||||
},
|
||||
),
|
||||
)
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("topic", "payload1", "payload2"),
|
||||
[
|
||||
("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)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])
|
||||
async def test_skipped_async_ha_write_state2(
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||
) -> None:
|
||||
"""Test a write state command is only called when there is a valid event."""
|
||||
await mqtt_mock_entry()
|
||||
topic = "test-topic"
|
||||
payload1 = '{"event_type": "press"}'
|
||||
payload2 = '{"event_type": "unknown"}'
|
||||
with patch(
|
||||
"homeassistant.components.mqtt.mixins.MqttEntity.async_write_ha_state"
|
||||
) as mock_async_ha_write_state:
|
||||
assert len(mock_async_ha_write_state.mock_calls) == 0
|
||||
async_fire_mqtt_message(hass, topic, payload1)
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_async_ha_write_state.mock_calls) == 1
|
||||
|
||||
async_fire_mqtt_message(hass, topic, payload1)
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_async_ha_write_state.mock_calls) == 2
|
||||
|
||||
async_fire_mqtt_message(hass, topic, payload2)
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_async_ha_write_state.mock_calls) == 2
|
||||
|
||||
async_fire_mqtt_message(hass, topic, payload2)
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_async_ha_write_state.mock_calls) == 2
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue