"""Tests for calendar platform of Workday integration.""" from datetime import datetime, timedelta import logging from freezegun.api import FrozenDateTimeFactory import pytest from homeassistant.components.calendar import ( DOMAIN as CALENDAR_DOMAIN, EVENT_END_DATETIME, EVENT_START_DATETIME, EVENT_SUMMARY, SERVICE_GET_EVENTS, ) from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from homeassistant.util import dt as dt_util from . import TEST_CONFIG_WITH_PROVINCE, init_integration from tests.common import async_fire_time_changed ATTR_END = "end" ATTR_START = "start" @pytest.mark.parametrize( "time_zone", ["Asia/Tokyo", "Europe/Berlin", "America/Chicago", "US/Hawaii"] ) async def test_holiday_calendar_entity( hass: HomeAssistant, freezer: FrozenDateTimeFactory, time_zone: str, ) -> None: """Test HolidayCalendarEntity functionality.""" await hass.config.async_set_time_zone(time_zone) zone = await dt_util.async_get_time_zone(time_zone) freezer.move_to(datetime(2023, 1, 1, 0, 1, 1, tzinfo=zone)) # New Years Day await init_integration(hass, TEST_CONFIG_WITH_PROVINCE) response = await hass.services.async_call( CALENDAR_DOMAIN, SERVICE_GET_EVENTS, { ATTR_ENTITY_ID: "calendar.workday_sensor_de_bw_calendar", EVENT_START_DATETIME: dt_util.now(), EVENT_END_DATETIME: dt_util.now() + timedelta(days=10, hours=1), }, blocking=True, return_response=True, ) assert { ATTR_END: "2023-01-02", ATTR_START: "2023-01-01", EVENT_SUMMARY: "Workday Sensor DE BW", } not in response["calendar.workday_sensor_de_bw_calendar"]["events"] assert { ATTR_END: "2023-01-04", ATTR_START: "2023-01-03", EVENT_SUMMARY: "Workday Sensor DE BW", } in response["calendar.workday_sensor_de_bw_calendar"]["events"] state = hass.states.get("calendar.workday_sensor_de_bw_calendar") assert state is not None assert state.state == "off" freezer.move_to( datetime(2023, 1, 2, 0, 1, 1, tzinfo=zone) ) # Day after New Years Day async_fire_time_changed(hass) await hass.async_block_till_done() # Binary sensor added to ensure same state for both entities state = hass.states.get("binary_sensor.workday_sensor_de_bw") assert state is not None assert state.state == "on" state = hass.states.get("calendar.workday_sensor_de_bw_calendar") assert state is not None assert state.state == "on" freezer.move_to(datetime(2023, 1, 7, 0, 1, 1, tzinfo=zone)) # Workday async_fire_time_changed(hass) await hass.async_block_till_done() state = hass.states.get("binary_sensor.workday_sensor_de_bw") assert state is not None assert state.state == "off" state = hass.states.get("calendar.workday_sensor_de_bw_calendar") assert state is not None assert state.state == "off" async def test_no_update_when_disabled( hass: HomeAssistant, freezer: FrozenDateTimeFactory, entity_registry: er.EntityRegistry, caplog: pytest.LogCaptureFixture, ) -> None: """Test that a disabled calendar entity does not trigger updates.""" zone = await dt_util.async_get_time_zone("US/Hawaii") freezer.move_to(datetime(2023, 1, 1, 0, 1, 1, tzinfo=zone)) await init_integration(hass, TEST_CONFIG_WITH_PROVINCE) entity_id = "calendar.workday_sensor_de_bw_calendar" state = hass.states.get(entity_id) assert state is not None entity_registry.async_update_entity( entity_id, disabled_by=er.RegistryEntryDisabler.USER ) await hass.async_block_till_done() with caplog.at_level(logging.WARNING, logger="homeassistant.helpers.entity"): freezer.move_to(datetime(2023, 1, 2, 0, 1, 1, tzinfo=zone)) async_fire_time_changed(hass) await hass.async_block_till_done() assert ( "incorrectly being triggered for updates while it is disabled" not in caplog.text )