Use recorder fixtures and helpers in tests (#70773)

This commit is contained in:
Erik Montnemery 2022-04-26 18:08:00 +02:00 committed by GitHub
parent 24b090a038
commit 3016b5fbfc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 190 additions and 303 deletions

View file

@ -9,7 +9,7 @@ from unittest.mock import Mock, patch
import pytest
import voluptuous as vol
from homeassistant.components import logbook, recorder
from homeassistant.components import logbook
from homeassistant.components.alexa.smart_home import EVENT_ALEXA_SMART_HOME
from homeassistant.components.automation import EVENT_AUTOMATION_TRIGGERED
from homeassistant.components.recorder.models import process_timestamp_to_utc_isoformat
@ -40,9 +40,8 @@ import homeassistant.util.dt as dt_util
from tests.common import async_capture_events, mock_platform
from tests.components.recorder.common import (
async_trigger_db_commit,
async_recorder_block_till_done,
async_wait_recording_done,
trigger_db_commit,
)
EMPTY_CONFIG = logbook.CONFIG_SCHEMA({logbook.DOMAIN: {}})
@ -88,11 +87,7 @@ async def test_service_call_create_logbook_entry(hass_):
# Logbook entry service call results in firing an event.
# Our service call will unblock when the event listeners have been
# scheduled. This means that they may not have been processed yet.
await hass_.async_add_executor_job(trigger_db_commit, hass_)
await hass_.async_block_till_done()
await hass_.async_add_executor_job(
hass_.data[recorder.DATA_INSTANCE].block_till_done
)
await async_wait_recording_done(hass_)
events = list(
logbook._get_events(
@ -306,7 +301,7 @@ def create_state_changed_event_from_old_new(
async def test_logbook_view(hass, hass_client, recorder_mock):
"""Test the logbook view."""
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
client = await hass_client()
response = await client.get(f"/api/logbook/{dt_util.utcnow().isoformat()}")
assert response.status == HTTPStatus.OK
@ -315,7 +310,7 @@ async def test_logbook_view(hass, hass_client, recorder_mock):
async def test_logbook_view_period_entity(hass, hass_client, recorder_mock, set_utc):
"""Test the logbook view with period and entity."""
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
entity_id_test = "switch.test"
hass.states.async_set(entity_id_test, STATE_OFF)
@ -323,9 +318,7 @@ async def test_logbook_view_period_entity(hass, hass_client, recorder_mock, set_
entity_id_second = "switch.second"
hass.states.async_set(entity_id_second, STATE_OFF)
hass.states.async_set(entity_id_second, STATE_ON)
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -419,12 +412,7 @@ async def test_logbook_describe_event(hass, hass_client, recorder_mock):
return_value=dt_util.utcnow() - timedelta(seconds=5),
):
hass.bus.async_fire("some_event")
await hass.async_block_till_done()
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(
hass.data[recorder.DATA_INSTANCE].block_till_done
)
await async_wait_recording_done(hass)
client = await hass_client()
response = await client.get("/api/logbook")
@ -488,12 +476,7 @@ async def test_exclude_described_event(hass, hass_client, recorder_mock):
hass.bus.async_fire(
"some_event", {logbook.ATTR_NAME: name, logbook.ATTR_ENTITY_ID: entity_id3}
)
await hass.async_block_till_done()
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(
hass.data[recorder.DATA_INSTANCE].block_till_done
)
await async_wait_recording_done(hass)
client = await hass_client()
response = await client.get("/api/logbook")
@ -507,7 +490,7 @@ async def test_exclude_described_event(hass, hass_client, recorder_mock):
async def test_logbook_view_end_time_entity(hass, hass_client, recorder_mock):
"""Test the logbook view with end_time and entity."""
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
entity_id_test = "switch.test"
hass.states.async_set(entity_id_test, STATE_OFF)
@ -515,9 +498,7 @@ async def test_logbook_view_end_time_entity(hass, hass_client, recorder_mock):
entity_id_second = "switch.second"
hass.states.async_set(entity_id_second, STATE_OFF)
hass.states.async_set(entity_id_second, STATE_ON)
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -567,7 +548,7 @@ async def test_logbook_entity_filter_with_automations(hass, hass_client, recorde
await async_setup_component(hass, "automation", {})
await async_setup_component(hass, "script", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
entity_id_test = "alarm_control_panel.area_001"
hass.states.async_set(entity_id_test, STATE_OFF)
@ -586,9 +567,7 @@ async def test_logbook_entity_filter_with_automations(hass, hass_client, recorde
)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -653,7 +632,6 @@ async def test_logbook_entity_no_longer_in_state_machine(
entity_id_test, STATE_ON, {ATTR_FRIENDLY_NAME: "Alarm Control Panel"}
)
async_trigger_db_commit(hass)
await async_wait_recording_done(hass)
hass.states.async_remove(entity_id_test)
@ -679,7 +657,7 @@ async def test_filter_continuous_sensor_values(
):
"""Test remove continuous sensor events from logbook."""
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
entity_id_test = "switch.test"
hass.states.async_set(entity_id_test, STATE_OFF)
@ -691,9 +669,7 @@ async def test_filter_continuous_sensor_values(
hass.states.async_set(entity_id_third, STATE_OFF, {"unit_of_measurement": "foo"})
hass.states.async_set(entity_id_third, STATE_ON, {"unit_of_measurement": "foo"})
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -714,7 +690,7 @@ async def test_filter_continuous_sensor_values(
async def test_exclude_new_entities(hass, hass_client, recorder_mock, set_utc):
"""Test if events are excluded on first update."""
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
entity_id = "climate.bla"
entity_id2 = "climate.blu"
@ -724,9 +700,7 @@ async def test_exclude_new_entities(hass, hass_client, recorder_mock, set_utc):
hass.states.async_set(entity_id2, STATE_OFF)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -748,7 +722,7 @@ async def test_exclude_new_entities(hass, hass_client, recorder_mock, set_utc):
async def test_exclude_removed_entities(hass, hass_client, recorder_mock, set_utc):
"""Test if events are excluded on last update."""
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
entity_id = "climate.bla"
entity_id2 = "climate.blu"
@ -764,9 +738,7 @@ async def test_exclude_removed_entities(hass, hass_client, recorder_mock, set_ut
hass.states.async_remove(entity_id)
hass.states.async_remove(entity_id2)
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -789,7 +761,7 @@ async def test_exclude_removed_entities(hass, hass_client, recorder_mock, set_ut
async def test_exclude_attribute_changes(hass, hass_client, recorder_mock, set_utc):
"""Test if events of attribute changes are filtered."""
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
@ -802,9 +774,7 @@ async def test_exclude_attribute_changes(hass, hass_client, recorder_mock, set_u
await hass.async_block_till_done()
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -829,7 +799,7 @@ async def test_logbook_entity_context_id(hass, recorder_mock, hass_client):
await async_setup_component(hass, "automation", {})
await async_setup_component(hass, "script", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
context = ha.Context(
id="ac5bd62de45711eaaeb351041eec8dd9",
@ -913,11 +883,7 @@ async def test_logbook_entity_context_id(hass, recorder_mock, hass_client):
hass.states.async_set(
"light.switch", STATE_OFF, context=light_turn_off_service_context
)
await hass.async_block_till_done()
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -980,7 +946,7 @@ async def test_logbook_entity_context_parent_id(hass, hass_client, recorder_mock
await async_setup_component(hass, "automation", {})
await async_setup_component(hass, "script", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
context = ha.Context(
id="ac5bd62de45711eaaeb351041eec8dd9",
@ -1085,11 +1051,7 @@ async def test_logbook_entity_context_parent_id(hass, hass_client, recorder_mock
"alarm_control_panel.area_009",
missing_parent_context,
)
await hass.async_block_till_done()
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -1179,7 +1141,7 @@ async def test_logbook_context_from_template(hass, hass_client, recorder_mock):
}
},
)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
@ -1199,11 +1161,7 @@ async def test_logbook_context_from_template(hass, hass_client, recorder_mock):
hass.states.async_set(
"switch.test_state", STATE_ON, context=switch_turn_off_context
)
await hass.async_block_till_done()
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -1264,7 +1222,7 @@ async def test_logbook_entity_matches_only(hass, hass_client, recorder_mock):
}
},
)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
@ -1284,11 +1242,7 @@ async def test_logbook_entity_matches_only(hass, hass_client, recorder_mock):
hass.states.async_set(
"switch.test_state", STATE_ON, context=switch_turn_off_context
)
await hass.async_block_till_done()
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -1317,7 +1271,7 @@ async def test_custom_log_entry_discoverable_via_entity_matches_only(
):
"""Test if a custom log entry is later discoverable via entity_matches_only."""
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
logbook.async_log_entry(
hass,
@ -1326,10 +1280,7 @@ async def test_custom_log_entry_discoverable_via_entity_matches_only(
"switch",
"switch.test_switch",
)
await hass.async_block_till_done()
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -1377,7 +1328,7 @@ async def test_logbook_entity_matches_only_multiple(hass, hass_client, recorder_
}
},
)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
@ -1402,11 +1353,7 @@ async def test_logbook_entity_matches_only_multiple(hass, hass_client, recorder_
"switch.test_state", STATE_ON, context=switch_turn_off_context
)
hass.states.async_set("light.test_state", STATE_ON, context=switch_turn_off_context)
await hass.async_block_till_done()
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_wait_recording_done(hass)
client = await hass_client()
@ -1456,7 +1403,7 @@ async def test_logbook_invalid_entity(hass, hass_client, recorder_mock):
async def test_icon_and_state(hass, hass_client, recorder_mock):
"""Test to ensure state and custom icons are returned."""
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
@ -1475,7 +1422,7 @@ async def test_icon_and_state(hass, hass_client, recorder_mock):
)
hass.states.async_set("light.kitchen", STATE_OFF, {"icon": "mdi:chemical-weapon"})
await _async_commit_and_wait(hass)
await async_wait_recording_done(hass)
client = await hass_client()
response_json = await _async_fetch_logbook(client)
@ -1502,7 +1449,7 @@ async def test_exclude_events_domain(hass, hass_client, recorder_mock):
}
)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
@ -1511,7 +1458,7 @@ async def test_exclude_events_domain(hass, hass_client, recorder_mock):
hass.states.async_set(entity_id2, None)
hass.states.async_set(entity_id2, 20)
await _async_commit_and_wait(hass)
await async_wait_recording_done(hass)
client = await hass_client()
entries = await _async_fetch_logbook(client)
@ -1541,7 +1488,7 @@ async def test_exclude_events_domain_glob(hass, hass_client, recorder_mock):
}
)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
@ -1552,7 +1499,7 @@ async def test_exclude_events_domain_glob(hass, hass_client, recorder_mock):
hass.states.async_set(entity_id3, None)
hass.states.async_set(entity_id3, 30)
await _async_commit_and_wait(hass)
await async_wait_recording_done(hass)
client = await hass_client()
entries = await _async_fetch_logbook(client)
@ -1580,7 +1527,7 @@ async def test_include_events_entity(hass, hass_client, recorder_mock):
}
)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
@ -1589,7 +1536,7 @@ async def test_include_events_entity(hass, hass_client, recorder_mock):
hass.states.async_set(entity_id2, None)
hass.states.async_set(entity_id2, 20)
await _async_commit_and_wait(hass)
await async_wait_recording_done(hass)
client = await hass_client()
entries = await _async_fetch_logbook(client)
@ -1612,7 +1559,7 @@ async def test_exclude_events_entity(hass, hass_client, recorder_mock):
}
)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
@ -1621,7 +1568,7 @@ async def test_exclude_events_entity(hass, hass_client, recorder_mock):
hass.states.async_set(entity_id2, None)
hass.states.async_set(entity_id2, 20)
await _async_commit_and_wait(hass)
await async_wait_recording_done(hass)
client = await hass_client()
entries = await _async_fetch_logbook(client)
assert len(entries) == 2
@ -1645,7 +1592,7 @@ async def test_include_events_domain(hass, hass_client, recorder_mock):
}
)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
@ -1658,7 +1605,7 @@ async def test_include_events_domain(hass, hass_client, recorder_mock):
hass.states.async_set(entity_id2, None)
hass.states.async_set(entity_id2, 20)
await _async_commit_and_wait(hass)
await async_wait_recording_done(hass)
client = await hass_client()
entries = await _async_fetch_logbook(client)
@ -1688,7 +1635,7 @@ async def test_include_events_domain_glob(hass, hass_client, recorder_mock):
}
)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
@ -1703,7 +1650,7 @@ async def test_include_events_domain_glob(hass, hass_client, recorder_mock):
hass.states.async_set(entity_id3, None)
hass.states.async_set(entity_id3, 30)
await _async_commit_and_wait(hass)
await async_wait_recording_done(hass)
client = await hass_client()
entries = await _async_fetch_logbook(client)
@ -1739,7 +1686,7 @@ async def test_include_exclude_events(hass, hass_client, recorder_mock):
}
)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
@ -1754,7 +1701,7 @@ async def test_include_exclude_events(hass, hass_client, recorder_mock):
hass.states.async_set(entity_id4, None)
hass.states.async_set(entity_id4, 10)
await _async_commit_and_wait(hass)
await async_wait_recording_done(hass)
client = await hass_client()
entries = await _async_fetch_logbook(client)
@ -1794,7 +1741,7 @@ async def test_include_exclude_events_with_glob_filters(
}
)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
@ -1813,7 +1760,7 @@ async def test_include_exclude_events_with_glob_filters(
hass.states.async_set(entity_id6, None)
hass.states.async_set(entity_id6, 30)
await _async_commit_and_wait(hass)
await async_wait_recording_done(hass)
client = await hass_client()
entries = await _async_fetch_logbook(client)
@ -1836,14 +1783,14 @@ async def test_empty_config(hass, hass_client, recorder_mock):
}
)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
hass.states.async_set(entity_id, None)
hass.states.async_set(entity_id, 10)
await _async_commit_and_wait(hass)
await async_wait_recording_done(hass)
client = await hass_client()
entries = await _async_fetch_logbook(client)
@ -1857,7 +1804,7 @@ async def test_empty_config(hass, hass_client, recorder_mock):
async def test_context_filter(hass, hass_client, recorder_mock):
"""Test we can filter by context."""
assert await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await async_recorder_block_till_done(hass)
entity_id = "switch.blu"
context = ha.Context()
@ -1869,7 +1816,7 @@ async def test_context_filter(hass, hass_client, recorder_mock):
hass.states.async_set(entity_id, "off")
hass.states.async_set(entity_id, "unknown", context=context)
await _async_commit_and_wait(hass)
await async_wait_recording_done(hass)
client = await hass_client()
# Test results
@ -1903,14 +1850,6 @@ async def _async_fetch_logbook(client, params=None):
return await response.json()
async def _async_commit_and_wait(hass):
await hass.async_block_till_done()
await hass.async_add_executor_job(trigger_db_commit, hass)
await hass.async_block_till_done()
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
await hass.async_block_till_done()
def _assert_entry(
entry, when=None, name=None, message=None, domain=None, entity_id=None, state=None
):