Fix flaky folder_watcher event entity test (#183686)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Erwin Douna <e.douna@gmail.com>
This commit is contained in:
epenet
2026-09-30 09:14:38 +02:00
committed by GitHub
co-authored by Claude Opus 5.5 Erwin Douna
parent 4d06b7bae4
commit 9341a09dfd
+16 -3
View File
@@ -1,11 +1,12 @@
"""The event entity tests for Folder Watcher."""
import asyncio
from pathlib import Path
from time import sleep
from syrupy.assertion import SnapshotAssertion
from homeassistant.core import HomeAssistant
from homeassistant.const import EVENT_STATE_CHANGED
from homeassistant.core import Event, EventStateChangedData, HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.util import dt as dt_util
@@ -23,12 +24,24 @@ async def test_event_entity(
entry = load_int
await hass.async_block_till_done()
# Watchdog delivers events from its own threads, so wait for the final one.
moved = asyncio.Event()
@callback
def _async_state_changed(event: Event[EventStateChangedData]) -> None:
new_state = event.data["new_state"]
if new_state and new_state.attributes.get("event_type") == "moved":
moved.set()
hass.bus.async_listen(EVENT_STATE_CHANGED, _async_state_changed)
file = tmp_path.joinpath("hello.txt")
file.write_text("Hello, world!")
new_file = tmp_path.joinpath("hello2.txt")
file.rename(new_file)
await hass.async_add_executor_job(sleep, 0.1)
async with asyncio.timeout(10):
await moved.wait()
entity_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
assert entity_entries