mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fix schedule during single weekday (#77543)
This commit is contained in:
parent
6ed095f000
commit
cac4015882
@ -271,7 +271,7 @@ class Schedule(Entity):
|
|||||||
# Find next event in the schedule, loop over each day (starting with
|
# Find next event in the schedule, loop over each day (starting with
|
||||||
# the current day) until the next event has been found.
|
# the current day) until the next event has been found.
|
||||||
next_event = None
|
next_event = None
|
||||||
for day in range(7):
|
for day in range(8): # 8 because we need to search same weekday next week
|
||||||
day_schedule = self._config.get(
|
day_schedule = self._config.get(
|
||||||
WEEKDAY_TO_CONF[(now.weekday() + day) % 7], []
|
WEEKDAY_TO_CONF[(now.weekday() + day) % 7], []
|
||||||
)
|
)
|
||||||
|
@ -6,7 +6,6 @@ from typing import Any
|
|||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from aiohttp import ClientWebSocketResponse
|
from aiohttp import ClientWebSocketResponse
|
||||||
from freezegun import freeze_time
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.schedule import STORAGE_VERSION, STORAGE_VERSION_MINOR
|
from homeassistant.components.schedule import STORAGE_VERSION, STORAGE_VERSION_MINOR
|
||||||
@ -177,6 +176,55 @@ async def test_invalid_schedules(
|
|||||||
assert error in caplog.text
|
assert error in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"schedule",
|
||||||
|
({CONF_FROM: "07:00:00", CONF_TO: "11:00:00"},),
|
||||||
|
)
|
||||||
|
async def test_events_one_day(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
schedule_setup: Callable[..., Coroutine[Any, Any, bool]],
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
schedule: list[dict[str, str]],
|
||||||
|
freezer,
|
||||||
|
) -> None:
|
||||||
|
"""Test events only during one day of the week."""
|
||||||
|
freezer.move_to("2022-08-30 13:20:00-07:00")
|
||||||
|
|
||||||
|
assert await schedule_setup(
|
||||||
|
config={
|
||||||
|
DOMAIN: {
|
||||||
|
"from_yaml": {
|
||||||
|
CONF_NAME: "from yaml",
|
||||||
|
CONF_ICON: "mdi:party-popper",
|
||||||
|
CONF_SUNDAY: schedule,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
items=[],
|
||||||
|
)
|
||||||
|
|
||||||
|
state = hass.states.get(f"{DOMAIN}.from_yaml")
|
||||||
|
assert state
|
||||||
|
assert state.state == STATE_OFF
|
||||||
|
assert state.attributes[ATTR_NEXT_EVENT].isoformat() == "2022-09-04T07:00:00-07:00"
|
||||||
|
|
||||||
|
freezer.move_to(state.attributes[ATTR_NEXT_EVENT])
|
||||||
|
async_fire_time_changed(hass)
|
||||||
|
|
||||||
|
state = hass.states.get(f"{DOMAIN}.from_yaml")
|
||||||
|
assert state
|
||||||
|
assert state.state == STATE_ON
|
||||||
|
assert state.attributes[ATTR_NEXT_EVENT].isoformat() == "2022-09-04T11:00:00-07:00"
|
||||||
|
|
||||||
|
freezer.move_to(state.attributes[ATTR_NEXT_EVENT])
|
||||||
|
async_fire_time_changed(hass)
|
||||||
|
|
||||||
|
state = hass.states.get(f"{DOMAIN}.from_yaml")
|
||||||
|
assert state
|
||||||
|
assert state.state == STATE_ON
|
||||||
|
assert state.attributes[ATTR_NEXT_EVENT].isoformat() == "2022-09-11T07:00:00-07:00"
|
||||||
|
|
||||||
|
|
||||||
async def test_setup_no_config(hass: HomeAssistant, hass_admin_user: MockUser) -> None:
|
async def test_setup_no_config(hass: HomeAssistant, hass_admin_user: MockUser) -> None:
|
||||||
"""Test component setup with no config."""
|
"""Test component setup with no config."""
|
||||||
count_start = len(hass.states.async_entity_ids())
|
count_start = len(hass.states.async_entity_ids())
|
||||||
@ -224,19 +272,19 @@ async def test_load(
|
|||||||
async def test_schedule_updates(
|
async def test_schedule_updates(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
schedule_setup: Callable[..., Coroutine[Any, Any, bool]],
|
schedule_setup: Callable[..., Coroutine[Any, Any, bool]],
|
||||||
|
freezer,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test the schedule updates when time changes."""
|
"""Test the schedule updates when time changes."""
|
||||||
with freeze_time("2022-08-10 20:10:00-07:00"):
|
freezer.move_to("2022-08-10 20:10:00-07:00")
|
||||||
assert await schedule_setup()
|
assert await schedule_setup()
|
||||||
|
|
||||||
state = hass.states.get(f"{DOMAIN}.from_storage")
|
state = hass.states.get(f"{DOMAIN}.from_storage")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == STATE_OFF
|
assert state.state == STATE_OFF
|
||||||
assert state.attributes[ATTR_NEXT_EVENT].isoformat() == "2022-08-12T17:00:00-07:00"
|
assert state.attributes[ATTR_NEXT_EVENT].isoformat() == "2022-08-12T17:00:00-07:00"
|
||||||
|
|
||||||
with freeze_time(state.attributes[ATTR_NEXT_EVENT]):
|
freezer.move_to(state.attributes[ATTR_NEXT_EVENT])
|
||||||
async_fire_time_changed(hass, state.attributes[ATTR_NEXT_EVENT])
|
async_fire_time_changed(hass)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
state = hass.states.get(f"{DOMAIN}.from_storage")
|
state = hass.states.get(f"{DOMAIN}.from_storage")
|
||||||
assert state
|
assert state
|
||||||
|
Loading…
x
Reference in New Issue
Block a user