Fix lingering timers in calendar tests (#90845)

This commit is contained in:
epenet 2023-04-06 02:46:05 +02:00 committed by GitHub
parent 4276ce96ea
commit 60692bcfdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,7 +8,8 @@ forward exercising the triggers.
"""
from __future__ import annotations
from collections.abc import Callable, Generator
from collections.abc import AsyncIterator, Callable, Generator
from contextlib import asynccontextmanager
import datetime
import logging
import secrets
@ -22,6 +23,7 @@ import pytest
from homeassistant.components import calendar
import homeassistant.components.automation as automation
from homeassistant.components.calendar.trigger import EVENT_END, EVENT_START
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
@ -151,7 +153,10 @@ async def setup_calendar(hass: HomeAssistant, fake_schedule: FakeSchedule) -> No
await hass.async_block_till_done()
async def create_automation(hass: HomeAssistant, event_type: str, offset=None) -> None:
@asynccontextmanager
async def create_automation(
hass: HomeAssistant, event_type: str, offset=None
) -> AsyncIterator[None]:
"""Register an automation."""
trigger_data = {
"platform": calendar.DOMAIN,
@ -165,6 +170,7 @@ async def create_automation(hass: HomeAssistant, event_type: str, offset=None) -
automation.DOMAIN,
{
automation.DOMAIN: {
"alias": event_type,
"trigger": trigger_data,
"action": TEST_AUTOMATION_ACTION,
"mode": "queued",
@ -173,6 +179,16 @@ async def create_automation(hass: HomeAssistant, event_type: str, offset=None) -
)
await hass.async_block_till_done()
yield
# Disable automation to cleanup lingering timers
await hass.services.async_call(
automation.DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: f"automation.{event_type}"},
blocking=True,
)
@pytest.fixture
def calls(hass: HomeAssistant) -> Callable[[], list[dict[str, Any]]]:
@ -205,12 +221,13 @@ async def test_event_start_trigger(
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
end=datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00"),
)
await create_automation(hass, EVENT_START)
async with create_automation(hass, EVENT_START):
assert len(calls()) == 0
await fake_schedule.fire_until(
datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00"),
)
assert calls() == [
{
"platform": "calendar",
@ -239,8 +256,7 @@ async def test_event_start_trigger_with_offset(
start=datetime.datetime.fromisoformat("2022-04-19 12:00:00+00:00"),
end=datetime.datetime.fromisoformat("2022-04-19 12:30:00+00:00"),
)
await create_automation(hass, EVENT_START, offset=offset_str)
async with create_automation(hass, EVENT_START, offset=offset_str):
# No calls yet
await fake_schedule.fire_until(
datetime.datetime.fromisoformat("2022-04-19 11:55:00+00:00") + offset_delta,
@ -270,8 +286,7 @@ async def test_event_end_trigger(
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
end=datetime.datetime.fromisoformat("2022-04-19 12:00:00+00:00"),
)
await create_automation(hass, EVENT_END)
async with create_automation(hass, EVENT_END):
# Event started, nothing should fire yet
await fake_schedule.fire_until(
datetime.datetime.fromisoformat("2022-04-19 11:10:00+00:00")
@ -310,8 +325,7 @@ async def test_event_end_trigger_with_offset(
start=datetime.datetime.fromisoformat("2022-04-19 12:00:00+00:00"),
end=datetime.datetime.fromisoformat("2022-04-19 12:30:00+00:00"),
)
await create_automation(hass, EVENT_END, offset=offset_str)
async with create_automation(hass, EVENT_END, offset=offset_str):
# No calls yet
await fake_schedule.fire_until(
datetime.datetime.fromisoformat("2022-04-19 12:05:00+00:00") + offset_delta,
@ -338,9 +352,7 @@ async def test_calendar_trigger_with_no_events(
) -> None:
"""Test a calendar trigger setup with no events."""
await create_automation(hass, EVENT_START)
await create_automation(hass, EVENT_END)
async with create_automation(hass, EVENT_START), create_automation(hass, EVENT_END):
# No calls, at arbitrary times
await fake_schedule.fire_until(
datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00")
@ -363,8 +375,7 @@ async def test_multiple_start_events(
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
end=datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00"),
)
await create_automation(hass, EVENT_START)
async with create_automation(hass, EVENT_START):
await fake_schedule.fire_until(
datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00")
)
@ -397,11 +408,11 @@ async def test_multiple_end_events(
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
end=datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00"),
)
await create_automation(hass, EVENT_END)
async with create_automation(hass, EVENT_END):
await fake_schedule.fire_until(
datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00")
)
assert calls() == [
{
"platform": "calendar",
@ -431,11 +442,11 @@ async def test_multiple_events_sharing_start_time(
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
end=datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00"),
)
await create_automation(hass, EVENT_START)
async with create_automation(hass, EVENT_START):
await fake_schedule.fire_until(
datetime.datetime.fromisoformat("2022-04-19 11:35:00+00:00")
)
assert calls() == [
{
"platform": "calendar",
@ -465,11 +476,11 @@ async def test_overlap_events(
start=datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00"),
end=datetime.datetime.fromisoformat("2022-04-19 11:45:00+00:00"),
)
await create_automation(hass, EVENT_START)
async with create_automation(hass, EVENT_START):
await fake_schedule.fire_until(
datetime.datetime.fromisoformat("2022-04-19 11:20:00+00:00")
)
assert calls() == [
{
"platform": "calendar",
@ -537,8 +548,7 @@ async def test_update_next_event(
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
end=datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00"),
)
await create_automation(hass, EVENT_START)
async with create_automation(hass, EVENT_START):
# No calls before event start
await fake_schedule.fire_until(
datetime.datetime.fromisoformat("2022-04-19 10:45:00+00:00")
@ -580,8 +590,7 @@ async def test_update_missed(
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
end=datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00"),
)
await create_automation(hass, EVENT_START)
async with create_automation(hass, EVENT_START):
# Events are refreshed at t+TEST_UPDATE_INTERVAL minutes. A new event is
# added, but the next update happens after the event is already over.
await fake_schedule.fire_until(
@ -670,7 +679,7 @@ async def test_event_payload(
) -> None:
"""Test the fields in the calendar event payload are set."""
fake_schedule.create_event(**create_data)
await create_automation(hass, EVENT_START)
async with create_automation(hass, EVENT_START):
assert len(calls()) == 0
await fake_schedule.fire_until(fire_time)
@ -697,7 +706,7 @@ async def test_trigger_timestamp_window_edge(
start=datetime.datetime.fromisoformat("2022-04-19 11:14:00+00:00"),
end=datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00"),
)
await create_automation(hass, EVENT_START)
async with create_automation(hass, EVENT_START):
assert len(calls()) == 0
await fake_schedule.fire_until(
@ -741,12 +750,13 @@ async def test_event_start_trigger_dst(
start=datetime.datetime(2023, 3, 12, 3, 30, tzinfo=tzinfo),
end=datetime.datetime(2023, 3, 12, 3, 45, tzinfo=tzinfo),
)
await create_automation(hass, EVENT_START)
async with create_automation(hass, EVENT_START):
assert len(calls()) == 0
await fake_schedule.fire_until(
datetime.datetime.fromisoformat("2023-03-12 05:00:00-08:00"),
)
assert calls() == [
{
"platform": "calendar",