1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Convert some tests to async and drop usage get_test_home_assistant (#64394)

* Fix some tests

* Update MS tests

* Convert last logbook tests to async
This commit is contained in:
Paulus Schoutsen
2022-01-18 19:37:17 -08:00
committed by GitHub
parent 394c6850a3
commit 08083f399e
7 changed files with 532 additions and 610 deletions

View File

@@ -35,37 +35,32 @@ from homeassistant.const import (
import homeassistant.core as ha
from homeassistant.helpers.entityfilter import CONF_ENTITY_GLOBS
from homeassistant.helpers.json import JSONEncoder
from homeassistant.setup import async_setup_component, setup_component
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from tests.common import get_test_home_assistant, init_recorder_component, mock_platform
from tests.common import (
async_capture_events,
async_init_recorder_component,
mock_platform,
)
from tests.components.recorder.common import trigger_db_commit
EMPTY_CONFIG = logbook.CONFIG_SCHEMA({logbook.DOMAIN: {}})
@pytest.fixture
def hass_():
async def hass_(hass):
"""Set up things to be run when tests are started."""
hass = get_test_home_assistant()
init_recorder_component(hass) # Force an in memory DB
with patch("homeassistant.components.http.start_http_server_and_save_config"):
assert setup_component(hass, logbook.DOMAIN, EMPTY_CONFIG)
yield hass
hass.stop()
await async_init_recorder_component(hass) # Force an in memory DB
assert await async_setup_component(hass, logbook.DOMAIN, EMPTY_CONFIG)
return hass
def test_service_call_create_logbook_entry(hass_):
async def test_service_call_create_logbook_entry(hass_):
"""Test if service call create log book entry."""
calls = []
calls = async_capture_events(hass_, logbook.EVENT_LOGBOOK_ENTRY)
@ha.callback
def event_listener(event):
"""Append on event."""
calls.append(event)
hass_.bus.listen(logbook.EVENT_LOGBOOK_ENTRY, event_listener)
hass_.services.call(
await hass_.services.async_call(
logbook.DOMAIN,
"log",
{
@@ -76,7 +71,7 @@ def test_service_call_create_logbook_entry(hass_):
},
True,
)
hass_.services.call(
await hass_.services.async_call(
logbook.DOMAIN,
"log",
{
@@ -88,9 +83,11 @@ 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.
trigger_db_commit(hass_)
hass_.block_till_done()
hass_.data[recorder.DATA_INSTANCE].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
)
events = list(
logbook._get_events(
@@ -116,24 +113,17 @@ def test_service_call_create_logbook_entry(hass_):
assert last_call.data.get(logbook.ATTR_DOMAIN) == "logbook"
def test_service_call_create_log_book_entry_no_message(hass_):
async def test_service_call_create_log_book_entry_no_message(hass_):
"""Test if service call create log book entry without message."""
calls = []
@ha.callback
def event_listener(event):
"""Append on event."""
calls.append(event)
hass_.bus.listen(logbook.EVENT_LOGBOOK_ENTRY, event_listener)
calls = async_capture_events(hass_, logbook.EVENT_LOGBOOK_ENTRY)
with pytest.raises(vol.Invalid):
hass_.services.call(logbook.DOMAIN, "log", {}, True)
await hass_.services.async_call(logbook.DOMAIN, "log", {}, True)
# 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.
hass_.block_till_done()
await hass_.async_block_till_done()
assert len(calls) == 0
@@ -310,7 +300,7 @@ def create_state_changed_event_from_old_new(
async def test_logbook_view(hass, hass_client):
"""Test the logbook view."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
client = await hass_client()
@@ -320,7 +310,7 @@ async def test_logbook_view(hass, hass_client):
async def test_logbook_view_period_entity(hass, hass_client):
"""Test the logbook view with period and entity."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -404,7 +394,7 @@ async def test_logbook_view_period_entity(hass, hass_client):
async def test_logbook_describe_event(hass, hass_client):
"""Test teaching logbook about a new event."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
def _describe(event):
"""Describe an event."""
@@ -471,7 +461,7 @@ async def test_exclude_described_event(hass, hass_client):
Mock(async_describe_events=async_describe_events),
)
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
assert await async_setup_component(
hass,
logbook.DOMAIN,
@@ -515,7 +505,7 @@ async def test_exclude_described_event(hass, hass_client):
async def test_logbook_view_end_time_entity(hass, hass_client):
"""Test the logbook view with end_time and entity."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -573,7 +563,7 @@ async def test_logbook_view_end_time_entity(hass, hass_client):
async def test_logbook_entity_filter_with_automations(hass, hass_client):
"""Test the logbook view with end_time and entity with automations and scripts."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await async_setup_component(hass, "automation", {})
await async_setup_component(hass, "script", {})
@@ -648,7 +638,7 @@ async def test_logbook_entity_filter_with_automations(hass, hass_client):
async def test_filter_continuous_sensor_values(hass, hass_client):
"""Test remove continuous sensor events from logbook."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -684,7 +674,7 @@ async def test_filter_continuous_sensor_values(hass, hass_client):
async def test_exclude_new_entities(hass, hass_client):
"""Test if events are excluded on first update."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -719,7 +709,7 @@ async def test_exclude_new_entities(hass, hass_client):
async def test_exclude_removed_entities(hass, hass_client):
"""Test if events are excluded on last update."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -761,7 +751,7 @@ async def test_exclude_removed_entities(hass, hass_client):
async def test_exclude_attribute_changes(hass, hass_client):
"""Test if events of attribute changes are filtered."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -799,7 +789,7 @@ async def test_exclude_attribute_changes(hass, hass_client):
async def test_logbook_entity_context_id(hass, hass_client):
"""Test the logbook view with end_time and entity with automations and scripts."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await async_setup_component(hass, "automation", {})
await async_setup_component(hass, "script", {})
@@ -951,7 +941,7 @@ async def test_logbook_entity_context_id(hass, hass_client):
async def test_logbook_entity_context_parent_id(hass, hass_client):
"""Test the logbook view links events via context parent_id."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await async_setup_component(hass, "automation", {})
await async_setup_component(hass, "script", {})
@@ -1132,7 +1122,7 @@ async def test_logbook_entity_context_parent_id(hass, hass_client):
async def test_logbook_context_from_template(hass, hass_client):
"""Test the logbook view with end_time and entity with automations and scripts."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
assert await async_setup_component(
hass,
@@ -1218,7 +1208,7 @@ async def test_logbook_context_from_template(hass, hass_client):
async def test_logbook_entity_matches_only(hass, hass_client):
"""Test the logbook view with a single entity and entity_matches_only."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
assert await async_setup_component(
hass,
@@ -1292,7 +1282,7 @@ async def test_logbook_entity_matches_only(hass, hass_client):
async def test_custom_log_entry_discoverable_via_entity_matches_only(hass, hass_client):
"""Test if a custom log entry is later discoverable via entity_matches_only."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -1331,7 +1321,7 @@ async def test_custom_log_entry_discoverable_via_entity_matches_only(hass, hass_
async def test_logbook_entity_matches_only_multiple(hass, hass_client):
"""Test the logbook view with a multiple entities and entity_matches_only."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
assert await async_setup_component(
hass,
@@ -1415,7 +1405,7 @@ async def test_logbook_entity_matches_only_multiple(hass, hass_client):
async def test_logbook_invalid_entity(hass, hass_client):
"""Test the logbook view with requesting an invalid entity."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await hass.async_block_till_done()
client = await hass_client()
@@ -1434,7 +1424,7 @@ async def test_logbook_invalid_entity(hass, hass_client):
async def test_icon_and_state(hass, hass_client):
"""Test to ensure state and custom icons are returned."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -1481,7 +1471,7 @@ async def test_exclude_events_domain(hass, hass_client):
logbook.DOMAIN: {CONF_EXCLUDE: {CONF_DOMAINS: ["switch", "alexa"]}},
}
)
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -1521,7 +1511,7 @@ async def test_exclude_events_domain_glob(hass, hass_client):
},
}
)
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -1561,7 +1551,7 @@ async def test_include_events_entity(hass, hass_client):
},
}
)
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -1594,7 +1584,7 @@ async def test_exclude_events_entity(hass, hass_client):
logbook.DOMAIN: {CONF_EXCLUDE: {CONF_ENTITIES: [entity_id]}},
}
)
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -1628,7 +1618,7 @@ async def test_include_events_domain(hass, hass_client):
},
}
)
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -1672,7 +1662,7 @@ async def test_include_events_domain_glob(hass, hass_client):
},
}
)
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -1724,7 +1714,7 @@ async def test_include_exclude_events(hass, hass_client):
},
}
)
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -1778,7 +1768,7 @@ async def test_include_exclude_events_with_glob_filters(hass, hass_client):
},
}
)
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -1821,7 +1811,7 @@ async def test_empty_config(hass, hass_client):
logbook.DOMAIN: {},
}
)
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
await async_setup_component(hass, "logbook", config)
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
@@ -1843,7 +1833,7 @@ async def test_empty_config(hass, hass_client):
async def test_context_filter(hass, hass_client):
"""Test we can filter by context."""
await hass.async_add_executor_job(init_recorder_component, hass)
await async_init_recorder_component(hass)
assert await async_setup_component(hass, "logbook", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)