Fix logbook tests (#80264)

* Fix logbook tests

* Correct tests

* Improve tests
pull/80256/head^2
Erik Montnemery 2022-10-13 17:34:45 +02:00 committed by GitHub
parent e1ac8acf87
commit e852c9b012
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 55 additions and 40 deletions

View File

@ -528,7 +528,6 @@ async def test_subscribe_unsubscribe_logbook_stream_excluded_entities(
},
)
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
hass.states.async_set("light.exc", STATE_ON)
hass.states.async_set("light.exc", STATE_OFF)
@ -544,6 +543,7 @@ async def test_subscribe_unsubscribe_logbook_stream_excluded_entities(
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{"id": 7, "type": "logbook/event_stream", "start_time": now.isoformat()}
)
@ -684,7 +684,7 @@ async def test_subscribe_unsubscribe_logbook_stream_excluded_entities(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -722,7 +722,6 @@ async def test_subscribe_unsubscribe_logbook_stream_included_entities(
},
)
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
for entity_id in test_entities:
hass.states.async_set(entity_id, STATE_ON)
@ -732,6 +731,7 @@ async def test_subscribe_unsubscribe_logbook_stream_included_entities(
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{"id": 7, "type": "logbook/event_stream", "start_time": now.isoformat()}
)
@ -892,7 +892,7 @@ async def test_subscribe_unsubscribe_logbook_stream_included_entities(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -926,7 +926,6 @@ async def test_logbook_stream_excluded_entities_inherits_filters_from_recorder(
},
)
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
hass.states.async_set("light.exc", STATE_ON)
hass.states.async_set("light.exc", STATE_OFF)
@ -943,6 +942,7 @@ async def test_logbook_stream_excluded_entities_inherits_filters_from_recorder(
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{"id": 7, "type": "logbook/event_stream", "start_time": now.isoformat()}
)
@ -1083,7 +1083,7 @@ async def test_logbook_stream_excluded_entities_inherits_filters_from_recorder(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -1100,7 +1100,6 @@ async def test_subscribe_unsubscribe_logbook_stream(
)
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
hass.states.async_set("binary_sensor.is_light", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_OFF)
@ -1109,6 +1108,7 @@ async def test_subscribe_unsubscribe_logbook_stream(
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{"id": 7, "type": "logbook/event_stream", "start_time": now.isoformat()}
)
@ -1386,7 +1386,7 @@ async def test_subscribe_unsubscribe_logbook_stream(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -1403,7 +1403,6 @@ async def test_subscribe_unsubscribe_logbook_stream_entities(
)
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
hass.states.async_set("light.small", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_OFF)
@ -1412,6 +1411,7 @@ async def test_subscribe_unsubscribe_logbook_stream_entities(
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{
"id": 7,
@ -1484,7 +1484,7 @@ async def test_subscribe_unsubscribe_logbook_stream_entities(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -1501,7 +1501,6 @@ async def test_subscribe_unsubscribe_logbook_stream_entities_with_end_time(
)
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
hass.states.async_set("light.small", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_OFF)
@ -1510,6 +1509,7 @@ async def test_subscribe_unsubscribe_logbook_stream_entities_with_end_time(
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{
"id": 7,
@ -1586,7 +1586,12 @@ async def test_subscribe_unsubscribe_logbook_stream_entities_with_end_time(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) <= init_count
listeners = hass.bus.async_listeners()
# The async_fire_time_changed above triggers unsubscribe from
# homeassistant_final_write, don't worry about those
init_listeners.pop("homeassistant_final_write")
listeners.pop("homeassistant_final_write")
assert listeners == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -1603,7 +1608,6 @@ async def test_subscribe_unsubscribe_logbook_stream_entities_past_only(
)
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
hass.states.async_set("light.small", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_OFF)
@ -1612,6 +1616,7 @@ async def test_subscribe_unsubscribe_logbook_stream_entities_past_only(
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{
"id": 7,
@ -1654,7 +1659,7 @@ async def test_subscribe_unsubscribe_logbook_stream_entities_past_only(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -1675,7 +1680,6 @@ async def test_subscribe_unsubscribe_logbook_stream_big_query(
)
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
four_days_ago = now - timedelta(days=4)
five_days_ago = now - timedelta(days=5)
@ -1699,6 +1703,7 @@ async def test_subscribe_unsubscribe_logbook_stream_big_query(
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{
"id": 7,
@ -1754,7 +1759,7 @@ async def test_subscribe_unsubscribe_logbook_stream_big_query(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -1774,10 +1779,10 @@ async def test_subscribe_unsubscribe_logbook_stream_device(
device2 = devices[1]
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{
"id": 7,
@ -1848,7 +1853,7 @@ async def test_subscribe_unsubscribe_logbook_stream_device(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
async def test_event_stream_bad_start_time(hass, hass_ws_client, recorder_mock):
@ -1886,10 +1891,10 @@ async def test_logbook_stream_match_multiple_entities(
hass.states.async_set(entity_id, STATE_ON)
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{
"id": 7,
@ -1963,7 +1968,7 @@ async def test_logbook_stream_match_multiple_entities(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
async def test_event_stream_bad_end_time(hass, hass_ws_client, recorder_mock):
@ -2017,7 +2022,6 @@ async def test_live_stream_with_one_second_commit_interval(
device = devices[0]
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
hass.bus.async_fire("mock_event", {"device_id": device.id, "message": "1"})
@ -2030,6 +2034,7 @@ async def test_live_stream_with_one_second_commit_interval(
hass.bus.async_fire("mock_event", {"device_id": device.id, "message": "3"})
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{
"id": 7,
@ -2086,7 +2091,7 @@ async def test_live_stream_with_one_second_commit_interval(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -2101,7 +2106,6 @@ async def test_subscribe_disconnected(hass, recorder_mock, hass_ws_client):
)
await async_wait_recording_done(hass)
init_count = sum(hass.bus.async_listeners().values())
hass.states.async_set("light.small", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_OFF)
@ -2109,6 +2113,9 @@ async def test_subscribe_disconnected(hass, recorder_mock, hass_ws_client):
await hass.async_block_till_done()
await async_wait_recording_done(hass)
# We will compare event subscriptions after closing the websocket connection,
# count the listeners before setting it up
init_listeners = hass.bus.async_listeners()
websocket_client = await hass_ws_client()
await websocket_client.send_json(
{
@ -2139,7 +2146,7 @@ async def test_subscribe_disconnected(hass, recorder_mock, hass_ws_client):
await hass.async_block_till_done()
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -2153,7 +2160,7 @@ async def test_stream_consumer_stop_processing(hass, recorder_mock, hass_ws_clie
]
)
await async_wait_recording_done(hass)
init_count = sum(hass.bus.async_listeners().values())
init_listeners = hass.bus.async_listeners()
hass.states.async_set("light.small", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_OFF)
@ -2162,7 +2169,7 @@ async def test_stream_consumer_stop_processing(hass, recorder_mock, hass_ws_clie
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
after_ws_created_count = sum(hass.bus.async_listeners().values())
after_ws_created_listeners = hass.bus.async_listeners()
with patch.object(websocket_api, "MAX_PENDING_LOGBOOK_EVENTS", 5), patch.object(
websocket_api, "_async_events_consumer"
@ -2182,7 +2189,7 @@ async def test_stream_consumer_stop_processing(hass, recorder_mock, hass_ws_clie
assert msg["type"] == TYPE_RESULT
assert msg["success"]
assert sum(hass.bus.async_listeners().values()) != init_count
assert hass.bus.async_listeners() != init_listeners
for _ in range(5):
hass.states.async_set("binary_sensor.is_light", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_OFF)
@ -2190,9 +2197,9 @@ async def test_stream_consumer_stop_processing(hass, recorder_mock, hass_ws_clie
# Check our listener got unsubscribed because
# the queue got full and the overload safety tripped
assert sum(hass.bus.async_listeners().values()) == after_ws_created_count
assert hass.bus.async_listeners() == after_ws_created_listeners
await websocket_client.close()
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -2294,7 +2301,9 @@ async def test_subscribe_all_entities_are_continuous(
hass.states.async_set("counter.any", state)
hass.states.async_set("proximity.any", state)
init_count = sum(hass.bus.async_listeners().values())
# We will compare event subscriptions after closing the websocket connection,
# count the listeners before setting it up
init_listeners = hass.bus.async_listeners()
_cycle_entities()
await async_wait_recording_done(hass)
@ -2323,7 +2332,7 @@ async def test_subscribe_all_entities_are_continuous(
await hass.async_block_till_done()
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -2348,7 +2357,9 @@ async def test_subscribe_all_entities_have_uom_multiple(
entity_id, state, {ATTR_UNIT_OF_MEASUREMENT: "any"}
)
init_count = sum(hass.bus.async_listeners().values())
# We will compare event subscriptions after closing the websocket connection,
# count the listeners before setting it up
init_listeners = hass.bus.async_listeners()
_cycle_entities()
await async_wait_recording_done(hass)
@ -2378,14 +2389,14 @@ async def test_subscribe_all_entities_have_uom_multiple(
await hass.async_block_till_done()
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
async def test_subscribe_entities_some_have_uom_multiple(
hass, recorder_mock, hass_ws_client
):
"""Test logbook stream with uom filtered entities and non-fitlered entities."""
"""Test logbook stream with uom filtered entities and non-filtered entities."""
now = dt_util.utcnow()
await asyncio.gather(
*[
@ -2407,7 +2418,9 @@ async def test_subscribe_entities_some_have_uom_multiple(
for state in (STATE_ON, STATE_OFF):
hass.states.async_set(entity_id, state)
init_count = sum(hass.bus.async_listeners().values())
# We will compare event subscriptions after closing the websocket connection,
# count the listeners before setting it up
init_listeners = hass.bus.async_listeners()
_cycle_entities()
await async_wait_recording_done(hass)
@ -2481,7 +2494,7 @@ async def test_subscribe_entities_some_have_uom_multiple(
await hass.async_block_till_done()
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -2498,7 +2511,6 @@ async def test_logbook_stream_ignores_forced_updates(
)
await hass.async_block_till_done()
init_count = sum(hass.bus.async_listeners().values())
hass.states.async_set("binary_sensor.is_light", STATE_ON)
hass.states.async_set("binary_sensor.is_light", STATE_OFF)
@ -2507,6 +2519,7 @@ async def test_logbook_stream_ignores_forced_updates(
await async_wait_recording_done(hass)
websocket_client = await hass_ws_client()
init_listeners = hass.bus.async_listeners()
await websocket_client.send_json(
{"id": 7, "type": "logbook/event_stream", "start_time": now.isoformat()}
)
@ -2595,7 +2608,7 @@ async def test_logbook_stream_ignores_forced_updates(
assert msg["success"]
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
@ -2628,7 +2641,9 @@ async def test_subscribe_all_entities_are_continuous_with_device(
hass.bus.async_fire("mock_event", {"device_id": device.id})
hass.bus.async_fire("mock_event", {"device_id": device2.id})
init_count = sum(hass.bus.async_listeners().values())
# We will compare event subscriptions after closing the websocket connection,
# count the listeners before setting it up
init_listeners = hass.bus.async_listeners()
_create_events()
await async_wait_recording_done(hass)
@ -2688,4 +2703,4 @@ async def test_subscribe_all_entities_are_continuous_with_device(
await hass.async_block_till_done()
# Check our listener got unsubscribed
assert sum(hass.bus.async_listeners().values()) == init_count
assert hass.bus.async_listeners() == init_listeners