mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Fix lingering timers in calendar tests (#90845)
This commit is contained in:
parent
4276ce96ea
commit
60692bcfdb
@ -8,7 +8,8 @@ forward exercising the triggers.
|
|||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable, Generator
|
from collections.abc import AsyncIterator, Callable, Generator
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
import secrets
|
import secrets
|
||||||
@ -22,6 +23,7 @@ import pytest
|
|||||||
from homeassistant.components import calendar
|
from homeassistant.components import calendar
|
||||||
import homeassistant.components.automation as automation
|
import homeassistant.components.automation as automation
|
||||||
from homeassistant.components.calendar.trigger import EVENT_END, EVENT_START
|
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.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
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()
|
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."""
|
"""Register an automation."""
|
||||||
trigger_data = {
|
trigger_data = {
|
||||||
"platform": calendar.DOMAIN,
|
"platform": calendar.DOMAIN,
|
||||||
@ -165,6 +170,7 @@ async def create_automation(hass: HomeAssistant, event_type: str, offset=None) -
|
|||||||
automation.DOMAIN,
|
automation.DOMAIN,
|
||||||
{
|
{
|
||||||
automation.DOMAIN: {
|
automation.DOMAIN: {
|
||||||
|
"alias": event_type,
|
||||||
"trigger": trigger_data,
|
"trigger": trigger_data,
|
||||||
"action": TEST_AUTOMATION_ACTION,
|
"action": TEST_AUTOMATION_ACTION,
|
||||||
"mode": "queued",
|
"mode": "queued",
|
||||||
@ -173,6 +179,16 @@ async def create_automation(hass: HomeAssistant, event_type: str, offset=None) -
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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
|
@pytest.fixture
|
||||||
def calls(hass: HomeAssistant) -> Callable[[], list[dict[str, Any]]]:
|
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"),
|
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 11:30: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
|
assert len(calls()) == 0
|
||||||
|
|
||||||
await fake_schedule.fire_until(
|
await fake_schedule.fire_until(
|
||||||
datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00"),
|
datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00"),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert calls() == [
|
assert calls() == [
|
||||||
{
|
{
|
||||||
"platform": "calendar",
|
"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"),
|
start=datetime.datetime.fromisoformat("2022-04-19 12:00:00+00:00"),
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 12:30: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
|
# No calls yet
|
||||||
await fake_schedule.fire_until(
|
await fake_schedule.fire_until(
|
||||||
datetime.datetime.fromisoformat("2022-04-19 11:55:00+00:00") + offset_delta,
|
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"),
|
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 12: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
|
# Event started, nothing should fire yet
|
||||||
await fake_schedule.fire_until(
|
await fake_schedule.fire_until(
|
||||||
datetime.datetime.fromisoformat("2022-04-19 11:10:00+00:00")
|
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"),
|
start=datetime.datetime.fromisoformat("2022-04-19 12:00:00+00:00"),
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 12:30: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
|
# No calls yet
|
||||||
await fake_schedule.fire_until(
|
await fake_schedule.fire_until(
|
||||||
datetime.datetime.fromisoformat("2022-04-19 12:05:00+00:00") + offset_delta,
|
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:
|
) -> None:
|
||||||
"""Test a calendar trigger setup with no events."""
|
"""Test a calendar trigger setup with no events."""
|
||||||
|
|
||||||
await create_automation(hass, EVENT_START)
|
async with create_automation(hass, EVENT_START), create_automation(hass, EVENT_END):
|
||||||
await create_automation(hass, EVENT_END)
|
|
||||||
|
|
||||||
# No calls, at arbitrary times
|
# No calls, at arbitrary times
|
||||||
await fake_schedule.fire_until(
|
await fake_schedule.fire_until(
|
||||||
datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00")
|
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"),
|
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 11:15: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(
|
await fake_schedule.fire_until(
|
||||||
datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00")
|
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"),
|
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 11:15: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(
|
await fake_schedule.fire_until(
|
||||||
datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00")
|
datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00")
|
||||||
)
|
)
|
||||||
|
|
||||||
assert calls() == [
|
assert calls() == [
|
||||||
{
|
{
|
||||||
"platform": "calendar",
|
"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"),
|
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 11:30: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(
|
await fake_schedule.fire_until(
|
||||||
datetime.datetime.fromisoformat("2022-04-19 11:35:00+00:00")
|
datetime.datetime.fromisoformat("2022-04-19 11:35:00+00:00")
|
||||||
)
|
)
|
||||||
|
|
||||||
assert calls() == [
|
assert calls() == [
|
||||||
{
|
{
|
||||||
"platform": "calendar",
|
"platform": "calendar",
|
||||||
@ -465,11 +476,11 @@ async def test_overlap_events(
|
|||||||
start=datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00"),
|
start=datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00"),
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 11:45: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(
|
await fake_schedule.fire_until(
|
||||||
datetime.datetime.fromisoformat("2022-04-19 11:20:00+00:00")
|
datetime.datetime.fromisoformat("2022-04-19 11:20:00+00:00")
|
||||||
)
|
)
|
||||||
|
|
||||||
assert calls() == [
|
assert calls() == [
|
||||||
{
|
{
|
||||||
"platform": "calendar",
|
"platform": "calendar",
|
||||||
@ -537,8 +548,7 @@ async def test_update_next_event(
|
|||||||
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
|
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 11:15: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
|
# No calls before event start
|
||||||
await fake_schedule.fire_until(
|
await fake_schedule.fire_until(
|
||||||
datetime.datetime.fromisoformat("2022-04-19 10:45:00+00:00")
|
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"),
|
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 11:30: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
|
# 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.
|
# added, but the next update happens after the event is already over.
|
||||||
await fake_schedule.fire_until(
|
await fake_schedule.fire_until(
|
||||||
@ -670,7 +679,7 @@ async def test_event_payload(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Test the fields in the calendar event payload are set."""
|
"""Test the fields in the calendar event payload are set."""
|
||||||
fake_schedule.create_event(**create_data)
|
fake_schedule.create_event(**create_data)
|
||||||
await create_automation(hass, EVENT_START)
|
async with create_automation(hass, EVENT_START):
|
||||||
assert len(calls()) == 0
|
assert len(calls()) == 0
|
||||||
|
|
||||||
await fake_schedule.fire_until(fire_time)
|
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"),
|
start=datetime.datetime.fromisoformat("2022-04-19 11:14:00+00:00"),
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 11:30: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
|
assert len(calls()) == 0
|
||||||
|
|
||||||
await fake_schedule.fire_until(
|
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),
|
start=datetime.datetime(2023, 3, 12, 3, 30, tzinfo=tzinfo),
|
||||||
end=datetime.datetime(2023, 3, 12, 3, 45, 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
|
assert len(calls()) == 0
|
||||||
|
|
||||||
await fake_schedule.fire_until(
|
await fake_schedule.fire_until(
|
||||||
datetime.datetime.fromisoformat("2023-03-12 05:00:00-08:00"),
|
datetime.datetime.fromisoformat("2023-03-12 05:00:00-08:00"),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert calls() == [
|
assert calls() == [
|
||||||
{
|
{
|
||||||
"platform": "calendar",
|
"platform": "calendar",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user