Fix caching, improve test

This commit is contained in:
Erik
2025-06-19 12:45:40 +02:00
parent eb9adb7e12
commit 920677d317
2 changed files with 41 additions and 3 deletions

View File

@@ -594,7 +594,8 @@ async def async_get_all_descriptions(
_load_triggers_files, hass, integrations
)
# Add missing descriptions to the cache
# Make a copy of the old cache and add missing descriptions to it
new_descriptions_cache = descriptions_cache.copy()
for missing_trigger in missing_triggers:
domain = triggers[missing_trigger]
@@ -607,6 +608,7 @@ async def async_get_all_descriptions(
description = {"fields": yaml_description.get("fields", {})}
descriptions_cache[missing_trigger] = description
new_descriptions_cache[missing_trigger] = description
return descriptions_cache
hass.data[TRIGGER_DESCRIPTION_CACHE] = new_descriptions_cache
return new_descriptions_cache

View File

@@ -17,6 +17,9 @@ from homeassistant.components.websocket_api.auth import (
TYPE_AUTH_OK,
TYPE_AUTH_REQUIRED,
)
from homeassistant.components.websocket_api.commands import (
ALL_TRIGGER_DESCRIPTIONS_JSON_CACHE,
)
from homeassistant.components.websocket_api.const import FEATURE_COALESCE_MESSAGES, URL
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import SIGNAL_BOOTSTRAP_INTEGRATIONS
@@ -685,6 +688,8 @@ async def test_subscribe_triggers(
assert await async_setup_component(hass, "system_health", {})
await hass.async_block_till_done()
assert ALL_TRIGGER_DESCRIPTIONS_JSON_CACHE not in hass.data
await websocket_client.send_json_auto_id({"type": "trigger_platforms/subscribe"})
# Test start subscription with initial event
@@ -693,6 +698,8 @@ async def test_subscribe_triggers(
msg = await websocket_client.receive_json()
assert msg == {"event": {"sun": {"fields": {}}}, "id": 1, "type": "event"}
old_cache = hass.data[ALL_TRIGGER_DESCRIPTIONS_JSON_CACHE]
# Test we receive an event when a new platform is loaded
assert await async_setup_component(hass, "tag", {})
msg = await websocket_client.receive_json()
@@ -702,6 +709,35 @@ async def test_subscribe_triggers(
"type": "event",
}
# Initiate a second subscription to check the cache is updated because of the new
# trigger
await websocket_client.send_json_auto_id({"type": "trigger_platforms/subscribe"})
msg = await websocket_client.receive_json()
assert msg == {"id": 2, "result": None, "success": True, "type": "result"}
msg = await websocket_client.receive_json()
assert msg == {
"event": {"sun": {"fields": {}}, "tag": {"fields": {}}},
"id": 2,
"type": "event",
}
assert hass.data[ALL_TRIGGER_DESCRIPTIONS_JSON_CACHE] is not old_cache
# Initiate a third subscription to check the cache is not updated because no new
# trigger was added
old_cache = hass.data[ALL_TRIGGER_DESCRIPTIONS_JSON_CACHE]
await websocket_client.send_json_auto_id({"type": "trigger_platforms/subscribe"})
msg = await websocket_client.receive_json()
assert msg == {"id": 3, "result": None, "success": True, "type": "result"}
msg = await websocket_client.receive_json()
assert msg == {
"event": {"sun": {"fields": {}}, "tag": {"fields": {}}},
"id": 3,
"type": "event",
}
assert hass.data[ALL_TRIGGER_DESCRIPTIONS_JSON_CACHE] is old_cache
async def test_get_config(
hass: HomeAssistant, websocket_client: MockHAClientWebSocket