Workday cleanup (#90267)

* clean binary sensor

* fix const

* clean sensor

* Fix tests

* Clean up

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
G Johansson 2023-03-27 23:11:49 +02:00 committed by GitHub
parent 182af87f97
commit cb6d384dba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 332 additions and 354 deletions

View File

@ -92,27 +92,23 @@ def setup_platform(
sensor_name: str = config[CONF_NAME] sensor_name: str = config[CONF_NAME]
workdays: list[str] = config[CONF_WORKDAYS] workdays: list[str] = config[CONF_WORKDAYS]
year: int = (get_date(dt.now()) + timedelta(days=days_offset)).year year: int = (dt.now() + timedelta(days=days_offset)).year
obj_holidays: HolidayBase = getattr(holidays, country)(years=year) obj_holidays: HolidayBase = getattr(holidays, country)(years=year)
if province: if province:
if ( try:
hasattr(obj_holidays, "subdivisions")
and province in obj_holidays.subdivisions
):
obj_holidays = getattr(holidays, country)(subdiv=province, years=year) obj_holidays = getattr(holidays, country)(subdiv=province, years=year)
else: except NotImplementedError:
LOGGER.error("There is no subdivision %s in country %s", province, country) LOGGER.error("There is no subdivision %s in country %s", province, country)
return return
# Add custom holidays # Add custom holidays
try: try:
obj_holidays.append(add_holidays) obj_holidays.append(add_holidays)
except TypeError: except ValueError as error:
LOGGER.debug("No custom holidays or invalid holidays") LOGGER.error("Could not add custom holidays: %s", error)
# Remove holidays # Remove holidays
try:
for remove_holiday in remove_holidays: for remove_holiday in remove_holidays:
try: try:
# is this formatted as a date? # is this formatted as a date?
@ -128,8 +124,6 @@ def setup_platform(
LOGGER.debug("Removed %s by name '%s'", holiday, remove_holiday) LOGGER.debug("Removed %s by name '%s'", holiday, remove_holiday)
except KeyError as unmatched: except KeyError as unmatched:
LOGGER.warning("No holiday found matching %s", unmatched) LOGGER.warning("No holiday found matching %s", unmatched)
except TypeError:
LOGGER.debug("No holidays to remove or invalid holidays")
LOGGER.debug("Found the following holidays for your configuration:") LOGGER.debug("Found the following holidays for your configuration:")
for holiday_date, name in sorted(obj_holidays.items()): for holiday_date, name in sorted(obj_holidays.items()):
@ -143,19 +137,6 @@ def setup_platform(
) )
def day_to_string(day: int) -> str | None:
"""Convert day index 0 - 7 to string."""
try:
return ALLOWED_DAYS[day]
except IndexError:
return None
def get_date(input_date: date) -> date:
"""Return date. Needed for testing."""
return input_date
class IsWorkdaySensor(BinarySensorEntity): class IsWorkdaySensor(BinarySensorEntity):
"""Implementation of a Workday sensor.""" """Implementation of a Workday sensor."""
@ -203,12 +184,9 @@ class IsWorkdaySensor(BinarySensorEntity):
self._attr_is_on = False self._attr_is_on = False
# Get ISO day of the week (1 = Monday, 7 = Sunday) # Get ISO day of the week (1 = Monday, 7 = Sunday)
adjusted_date = get_date(dt.now()) + timedelta(days=self._days_offset) adjusted_date = dt.now() + timedelta(days=self._days_offset)
day = adjusted_date.isoweekday() - 1 day = adjusted_date.isoweekday() - 1
day_of_week = day_to_string(day) day_of_week = ALLOWED_DAYS[day]
if day_of_week is None:
return
if self.is_include(day_of_week, adjusted_date): if self.is_include(day_of_week, adjusted_date):
self._attr_is_on = True self._attr_is_on = True

View File

@ -5,7 +5,7 @@ import logging
from homeassistant.const import WEEKDAYS from homeassistant.const import WEEKDAYS
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__package__)
ALLOWED_DAYS = WEEKDAYS + ["holiday"] ALLOWED_DAYS = WEEKDAYS + ["holiday"]

View File

@ -1 +1,159 @@
"""Tests the Home Assistant workday binary sensor.""" """Tests the Home Assistant workday binary sensor."""
from __future__ import annotations
from typing import Any
from homeassistant.components.workday.const import (
DEFAULT_EXCLUDES,
DEFAULT_NAME,
DEFAULT_OFFSET,
DEFAULT_WORKDAYS,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
async def init_integration(
hass: HomeAssistant,
config: dict[str, Any],
) -> None:
"""Set up the Workday integration in Home Assistant."""
await async_setup_component(
hass, "binary_sensor", {"binary_sensor": {"platform": "workday", **config}}
)
await hass.async_block_till_done()
TEST_CONFIG_WITH_PROVINCE = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_INCORRECT_PROVINCE = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "ZZ",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_NO_PROVINCE = {
"name": DEFAULT_NAME,
"country": "DE",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_WITH_STATE = {
"name": DEFAULT_NAME,
"country": "US",
"province": "CA",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_NO_STATE = {
"name": DEFAULT_NAME,
"country": "US",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_INCLUDE_HOLIDAY = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": ["sat", "sun"],
"days_offset": DEFAULT_OFFSET,
"workdays": ["holiday"],
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_EXAMPLE_1 = {
"name": DEFAULT_NAME,
"country": "US",
"excludes": ["sat", "sun"],
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_EXAMPLE_2 = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": ["mon", "wed", "fri"],
"add_holidays": ["2020-02-24"],
"remove_holidays": [],
}
TEST_CONFIG_REMOVE_HOLIDAY = {
"name": DEFAULT_NAME,
"country": "US",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": ["2020-12-25", "2020-11-26"],
}
TEST_CONFIG_REMOVE_NAMED = {
"name": DEFAULT_NAME,
"country": "US",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": ["Not a Holiday", "Christmas", "Thanksgiving"],
}
TEST_CONFIG_TOMORROW = {
"name": DEFAULT_NAME,
"country": "DE",
"excludes": DEFAULT_EXCLUDES,
"days_offset": 1,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_DAY_AFTER_TOMORROW = {
"name": DEFAULT_NAME,
"country": "DE",
"excludes": DEFAULT_EXCLUDES,
"days_offset": 2,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_YESTERDAY = {
"name": DEFAULT_NAME,
"country": "DE",
"excludes": DEFAULT_EXCLUDES,
"days_offset": -1,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_INCORRECT_ADD_REMOVE = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": ["2023-12-32"],
"remove_holidays": ["2023-12-32"],
}

View File

@ -1,118 +1,37 @@
"""Tests the Home Assistant workday binary sensor.""" """Tests the Home Assistant workday binary sensor."""
from datetime import date from datetime import datetime
from unittest.mock import patch from typing import Any
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
import voluptuous as vol import voluptuous as vol
import homeassistant.components.workday.binary_sensor as binary_sensor from homeassistant.components.workday import binary_sensor
from homeassistant.setup import setup_component from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import UTC
from tests.common import assert_setup_component, get_test_home_assistant from . import (
TEST_CONFIG_DAY_AFTER_TOMORROW,
FUNCTION_PATH = "homeassistant.components.workday.binary_sensor.get_date" TEST_CONFIG_EXAMPLE_1,
TEST_CONFIG_EXAMPLE_2,
TEST_CONFIG_INCLUDE_HOLIDAY,
TEST_CONFIG_INCORRECT_ADD_REMOVE,
TEST_CONFIG_INCORRECT_PROVINCE,
TEST_CONFIG_NO_PROVINCE,
TEST_CONFIG_NO_STATE,
TEST_CONFIG_REMOVE_HOLIDAY,
TEST_CONFIG_REMOVE_NAMED,
TEST_CONFIG_TOMORROW,
TEST_CONFIG_WITH_PROVINCE,
TEST_CONFIG_WITH_STATE,
TEST_CONFIG_YESTERDAY,
init_integration,
)
class TestWorkdaySetup: async def test_valid_country_yaml() -> None:
"""Test class for workday sensor.""" """Test valid country from yaml."""
def setup_method(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
# Set valid default config for test
self.config_province = {
"binary_sensor": {"platform": "workday", "country": "DE", "province": "BW"}
}
self.config_noprovince = {
"binary_sensor": {"platform": "workday", "country": "DE"}
}
self.config_invalidprovince = {
"binary_sensor": {
"platform": "workday",
"country": "DE",
"province": "invalid",
}
}
self.config_state = {
"binary_sensor": {"platform": "workday", "country": "US", "province": "CA"}
}
self.config_nostate = {
"binary_sensor": {"platform": "workday", "country": "US"}
}
self.config_includeholiday = {
"binary_sensor": {
"platform": "workday",
"country": "DE",
"province": "BW",
"workdays": ["holiday"],
"excludes": ["sat", "sun"],
}
}
self.config_example1 = {
"binary_sensor": {
"platform": "workday",
"country": "US",
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"excludes": ["sat", "sun"],
}
}
self.config_example2 = {
"binary_sensor": {
"platform": "workday",
"country": "DE",
"province": "BW",
"workdays": ["mon", "wed", "fri"],
"excludes": ["sat", "sun", "holiday"],
"add_holidays": ["2020-02-24"],
}
}
self.config_remove_holidays = {
"binary_sensor": {
"platform": "workday",
"country": "US",
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"excludes": ["sat", "sun", "holiday"],
"remove_holidays": ["2020-12-25", "2020-11-26"],
}
}
self.config_remove_named_holidays = {
"binary_sensor": {
"platform": "workday",
"country": "US",
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"excludes": ["sat", "sun", "holiday"],
"remove_holidays": ["Not a Holiday", "Christmas", "Thanksgiving"],
}
}
self.config_tomorrow = {
"binary_sensor": {"platform": "workday", "country": "DE", "days_offset": 1}
}
self.config_day_after_tomorrow = {
"binary_sensor": {"platform": "workday", "country": "DE", "days_offset": 2}
}
self.config_yesterday = {
"binary_sensor": {"platform": "workday", "country": "DE", "days_offset": -1}
}
def teardown_method(self):
"""Stop everything that was started."""
self.hass.stop()
def test_valid_country(self):
"""Test topic name/filter validation."""
# Invalid UTF-8, must not contain U+D800 to U+DFFF # Invalid UTF-8, must not contain U+D800 to U+DFFF
with pytest.raises(vol.Invalid): with pytest.raises(vol.Invalid):
binary_sensor.valid_country("\ud800") binary_sensor.valid_country("\ud800")
@ -125,226 +44,149 @@ class TestWorkdaySetup:
with pytest.raises(vol.Invalid): with pytest.raises(vol.Invalid):
binary_sensor.valid_country("HomeAssistantLand") binary_sensor.valid_country("HomeAssistantLand")
# Valid country code validation must not raise an exception
for country in ("IM", "LI", "US"):
assert binary_sensor.valid_country(country) == country
def test_setup_component_province(self): @pytest.mark.parametrize(
"""Set up workday component.""" ("config", "expected_state"),
with assert_setup_component(1, "binary_sensor"): [
setup_component(self.hass, "binary_sensor", self.config_province) (TEST_CONFIG_WITH_PROVINCE, "off"),
self.hass.block_till_done() (TEST_CONFIG_NO_PROVINCE, "off"),
(TEST_CONFIG_WITH_STATE, "on"),
(TEST_CONFIG_NO_STATE, "on"),
(TEST_CONFIG_EXAMPLE_1, "on"),
(TEST_CONFIG_EXAMPLE_2, "off"),
(TEST_CONFIG_TOMORROW, "off"),
(TEST_CONFIG_DAY_AFTER_TOMORROW, "off"),
(TEST_CONFIG_YESTERDAY, "on"),
],
)
async def test_setup(
hass: HomeAssistant,
config: dict[str, Any],
expected_state: str,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test setup from various configs."""
freezer.move_to(datetime(2022, 4, 15, 12, tzinfo=UTC)) # Monday
await init_integration(hass, config)
entity = self.hass.states.get("binary_sensor.workday_sensor") state = hass.states.get("binary_sensor.workday_sensor")
assert entity is not None assert state.state == expected_state
assert state.attributes == {
"friendly_name": "Workday Sensor",
"workdays": config["workdays"],
"excludes": config["excludes"],
"days_offset": config["days_offset"],
}
# Freeze time to a workday - Mar 15th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 3, 15))
def test_workday_province(self, mock_date):
"""Test if workdays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_province)
self.hass.block_till_done()
self.hass.start() async def test_setup_with_invalid_province_from_yaml(hass: HomeAssistant) -> None:
"""Test setup invalid province with import."""
entity = self.hass.states.get("binary_sensor.workday_sensor") await async_setup_component(
assert entity.state == "on" hass,
"binary_sensor",
# Freeze time to a weekend - Mar 12th, 2017 {
@patch(FUNCTION_PATH, return_value=date(2017, 3, 12)) "binary_sensor": {
def test_weekend_province(self, mock_date): "platform": "workday",
"""Test if weekends are reported correctly.""" "country": "DE",
with assert_setup_component(1, "binary_sensor"): "province": "invalid",
setup_component(self.hass, "binary_sensor", self.config_province) }
self.hass.block_till_done() },
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "off"
# Freeze time to a public holiday in province BW - Jan 6th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 1, 6))
def test_public_holiday_province(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_province)
self.hass.block_till_done()
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "off"
def test_setup_component_noprovince(self):
"""Set up workday component."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_noprovince)
self.hass.block_till_done()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity is not None
# Freeze time to a public holiday in province BW - Jan 6th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 1, 6))
def test_public_holiday_noprovince(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_noprovince)
self.hass.block_till_done()
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on"
# Freeze time to a public holiday in state CA - Mar 31st, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 3, 31))
def test_public_holiday_state(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_state)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "off"
# Freeze time to a public holiday in state CA - Mar 31st, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 3, 31))
def test_public_holiday_nostate(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_nostate)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on"
def test_setup_component_invalidprovince(self):
"""Set up workday component."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_invalidprovince)
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity is None
# Freeze time to a public holiday in province BW - Jan 6th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 1, 6))
def test_public_holiday_includeholiday(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_includeholiday)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on"
# Freeze time to a saturday to test offset - Aug 5th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 8, 5))
def test_tomorrow(self, mock_date):
"""Test if tomorrow are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_tomorrow)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "off"
# Freeze time to a saturday to test offset - Aug 5th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 8, 5))
def test_day_after_tomorrow(self, mock_date):
"""Test if the day after tomorrow are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_day_after_tomorrow)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on"
# Freeze time to a saturday to test offset - Aug 5th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 8, 5))
def test_yesterday(self, mock_date):
"""Test if yesterday are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_yesterday)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on"
# Freeze time to a Presidents day to test Holiday on a Work day - Jan 20th, 2020
# Presidents day Feb 17th 2020 is mon.
@patch(FUNCTION_PATH, return_value=date(2020, 2, 17))
def test_config_example1_holiday(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_example1)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on"
# Freeze time to test tue - Feb 18th, 2020
@patch(FUNCTION_PATH, return_value=date(2020, 2, 18))
def test_config_example2_tue(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_example2)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "off"
# Freeze time to test mon, but added as holiday - Feb 24th, 2020
@patch(FUNCTION_PATH, return_value=date(2020, 2, 24))
def test_config_example2_add_holiday(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_example2)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "off"
def test_day_to_string(self):
"""Test if day_to_string is behaving correctly."""
assert binary_sensor.day_to_string(0) == "mon"
assert binary_sensor.day_to_string(1) == "tue"
assert binary_sensor.day_to_string(7) == "holiday"
assert binary_sensor.day_to_string(8) is None
# Freeze time to test Fri, but remove holiday - December 25, 2020
@patch(FUNCTION_PATH, return_value=date(2020, 12, 25))
def test_config_remove_holidays_xmas(self, mock_date):
"""Test if removed holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_remove_holidays)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on"
# Freeze time to test Fri, but remove holiday by name - Christmas
@patch(FUNCTION_PATH, return_value=date(2020, 12, 25))
def test_config_remove_named_holidays_xmas(self, mock_date):
"""Test if removed by name holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(
self.hass, "binary_sensor", self.config_remove_named_holidays
) )
await hass.async_block_till_done()
self.hass.start() state = hass.states.get("binary_sensor.workday_sensor")
assert state is None
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on" async def test_setup_with_working_holiday(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test setup from various configs."""
freezer.move_to(datetime(2017, 1, 6, 12, tzinfo=UTC)) # Friday
await init_integration(hass, TEST_CONFIG_INCLUDE_HOLIDAY)
state = hass.states.get("binary_sensor.workday_sensor")
assert state.state == "on"
async def test_setup_add_holiday(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test setup from various configs."""
freezer.move_to(datetime(2020, 2, 24, 12, tzinfo=UTC)) # Monday
await init_integration(hass, TEST_CONFIG_EXAMPLE_2)
state = hass.states.get("binary_sensor.workday_sensor")
assert state.state == "off"
async def test_setup_remove_holiday(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test setup from various configs."""
freezer.move_to(datetime(2020, 12, 25, 12, tzinfo=UTC)) # Friday
await init_integration(hass, TEST_CONFIG_REMOVE_HOLIDAY)
state = hass.states.get("binary_sensor.workday_sensor")
assert state.state == "on"
async def test_setup_remove_holiday_named(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test setup from various configs."""
freezer.move_to(datetime(2020, 12, 25, 12, tzinfo=UTC)) # Friday
await init_integration(hass, TEST_CONFIG_REMOVE_NAMED)
state = hass.states.get("binary_sensor.workday_sensor")
assert state.state == "on"
async def test_setup_day_after_tomorrow(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test setup from various configs."""
freezer.move_to(datetime(2022, 5, 27, 12, tzinfo=UTC)) # Friday
await init_integration(hass, TEST_CONFIG_DAY_AFTER_TOMORROW)
state = hass.states.get("binary_sensor.workday_sensor")
assert state.state == "off"
async def test_setup_faulty_province(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test setup with faulty province."""
freezer.move_to(datetime(2017, 1, 6, 12, tzinfo=UTC)) # Friday
await init_integration(hass, TEST_CONFIG_INCORRECT_PROVINCE)
state = hass.states.get("binary_sensor.workday_sensor")
assert state is None
assert "There is no subdivision" in caplog.text
async def test_setup_incorrect_add_remove(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test setup with incorrect add/remove custom holiday."""
freezer.move_to(datetime(2017, 1, 6, 12, tzinfo=UTC)) # Friday
await init_integration(hass, TEST_CONFIG_INCORRECT_ADD_REMOVE)
hass.states.get("binary_sensor.workday_sensor")
assert (
"Could not add custom holidays: Cannot parse date from string '2023-12-32'"
in caplog.text
)
assert "No holiday found matching '2023-12-32'" in caplog.text