Fix logbook spinner never disappearing when all entities are filtered (#72816)

This commit is contained in:
J. Nick Koston 2022-06-01 00:33:46 -10:00 committed by Franck Nijhof
parent 2f3359f376
commit bf47d86d30
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 52 additions and 1 deletions

View File

@ -356,7 +356,7 @@ async def ws_event_stream(
)
await _async_wait_for_recorder_sync(hass)
if not subscriptions:
if msg_id not in connection.subscriptions:
# Unsubscribe happened while waiting for recorder
return
@ -388,6 +388,8 @@ async def ws_event_stream(
if not subscriptions:
# Unsubscribe happened while waiting for formatted events
# or there are no supported entities (all UOM or state class)
# or devices
return
live_stream.task = asyncio.create_task(

View File

@ -2089,3 +2089,52 @@ async def test_recorder_is_far_behind(hass, recorder_mock, hass_ws_client, caplo
assert msg["success"]
assert "Recorder is behind" in caplog.text
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
async def test_subscribe_all_entities_have_uom(hass, recorder_mock, hass_ws_client):
"""Test subscribe/unsubscribe logbook stream with entities that are always filtered."""
now = dt_util.utcnow()
await asyncio.gather(
*[
async_setup_component(hass, comp, {})
for comp in ("homeassistant", "logbook", "automation", "script")
]
)
await async_wait_recording_done(hass)
init_count = sum(hass.bus.async_listeners().values())
hass.states.async_set("sensor.uom", "1", {ATTR_UNIT_OF_MEASUREMENT: "any"})
hass.states.async_set("sensor.uom", "2", {ATTR_UNIT_OF_MEASUREMENT: "any"})
hass.states.async_set("sensor.uom", "3", {ATTR_UNIT_OF_MEASUREMENT: "any"})
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
await websocket_client.send_json(
{
"id": 7,
"type": "logbook/event_stream",
"start_time": now.isoformat(),
"entity_ids": ["sensor.uom"],
}
)
msg = await asyncio.wait_for(websocket_client.receive_json(), 2)
assert msg["id"] == 7
assert msg["type"] == TYPE_RESULT
assert msg["success"]
hass.states.async_set("sensor.uom", "1", {ATTR_UNIT_OF_MEASUREMENT: "any"})
hass.states.async_set("sensor.uom", "2", {ATTR_UNIT_OF_MEASUREMENT: "any"})
hass.states.async_set("sensor.uom", "3", {ATTR_UNIT_OF_MEASUREMENT: "any"})
msg = await asyncio.wait_for(websocket_client.receive_json(), 2)
assert msg["id"] == 7
assert msg["type"] == "event"
assert msg["event"]["events"] == []
await websocket_client.close()
await hass.async_block_till_done()
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count