init home-assistant

This commit is contained in:
2026-11-19 15:15:32 -05:00
parent 2e81d9d300
commit feec6ef9fb
19230 changed files with 3335526 additions and 0 deletions

View File

@@ -0,0 +1,337 @@
"""Tests the Home Assistant workday binary sensor."""
from __future__ import annotations
from typing import Any
from holidays import OPTIONAL
from homeassistant.components.workday.const import (
DEFAULT_EXCLUDES,
DEFAULT_NAME,
DEFAULT_OFFSET,
DEFAULT_WORKDAYS,
DOMAIN,
)
from homeassistant.config_entries import SOURCE_USER
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def init_integration(
hass: HomeAssistant,
config: dict[str, Any],
entry_id: str = "1",
source: str = SOURCE_USER,
) -> MockConfigEntry:
"""Set up the Scrape integration in Home Assistant."""
config_entry = MockConfigEntry(
domain=DOMAIN,
source=source,
data={},
options=config,
entry_id=entry_id,
)
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
return config_entry
TEST_CONFIG_NO_COUNTRY = {
"name": DEFAULT_NAME,
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_NO_COUNTRY_ADD_HOLIDAY = {
"name": DEFAULT_NAME,
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": ["2020-02-24"],
"remove_holidays": [],
}
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": [],
"language": "de",
}
TEST_CONFIG_NO_LANGUAGE_CONFIGURED = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
}
TEST_CONFIG_INCORRECT_COUNTRY = {
"name": DEFAULT_NAME,
"country": "ZZ",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
"language": "de",
}
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": [],
"language": "de",
}
TEST_CONFIG_NO_PROVINCE = {
"name": DEFAULT_NAME,
"country": "DE",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
"language": "de",
}
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": [],
"language": "en_US",
}
TEST_CONFIG_NO_STATE = {
"name": DEFAULT_NAME,
"country": "US",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
"language": "en_US",
}
TEST_CONFIG_INCLUDE_HOLIDAY = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": ["sat", "sun"],
"days_offset": DEFAULT_OFFSET,
"workdays": ["holiday"],
"add_holidays": [],
"remove_holidays": [],
"language": "de",
}
TEST_CONFIG_EXAMPLE_1 = {
"name": DEFAULT_NAME,
"country": "US",
"excludes": ["sat", "sun"],
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
"language": "en_US",
}
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": [],
"language": "de",
}
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"],
"language": "en_US",
}
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"],
"language": "en_US",
}
TEST_CONFIG_REMOVE_DATE = {
"name": DEFAULT_NAME,
"country": "US",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": ["2024-02-05", "2024-02-06"],
"language": "en_US",
}
TEST_CONFIG_TOMORROW = {
"name": DEFAULT_NAME,
"country": "DE",
"excludes": DEFAULT_EXCLUDES,
"days_offset": 1,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
"language": "de",
}
TEST_CONFIG_DAY_AFTER_TOMORROW = {
"name": DEFAULT_NAME,
"country": "DE",
"excludes": DEFAULT_EXCLUDES,
"days_offset": 2,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
"language": "de",
}
TEST_CONFIG_YESTERDAY = {
"name": DEFAULT_NAME,
"country": "DE",
"excludes": DEFAULT_EXCLUDES,
"days_offset": -1,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
"language": "de",
}
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"],
"language": "de",
}
TEST_CONFIG_INCORRECT_ADD_DATE_RANGE = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": ["2023-12-01", "2023-12-30,2023-12-32"],
"remove_holidays": [],
"language": "de",
}
TEST_CONFIG_INCORRECT_REMOVE_DATE_RANGE = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": ["2023-12-25", "2023-12-30,2023-12-32"],
"language": "de",
}
TEST_CONFIG_INCORRECT_ADD_DATE_RANGE_LEN = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": ["2023-12-01", "2023-12-29,2023-12-30,2023-12-31"],
"remove_holidays": [],
"language": "de",
}
TEST_CONFIG_INCORRECT_REMOVE_DATE_RANGE_LEN = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": ["2023-12-25", "2023-12-29,2023-12-30,2023-12-31"],
"language": "de",
}
TEST_CONFIG_ADD_REMOVE_DATE_RANGE = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": ["2022-12-01", "2022-12-05,2022-12-15"],
"remove_holidays": ["2022-12-04", "2022-12-24,2022-12-26"],
"language": "de",
}
TEST_LANGUAGE_CHANGE = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": ["2022-12-01", "2022-12-05,2022-12-15"],
"remove_holidays": ["2022-12-04", "2022-12-24,2022-12-26"],
"language": "en",
}
TEST_LANGUAGE_NO_CHANGE = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": ["2022-12-01", "2022-12-05,2022-12-15"],
"remove_holidays": ["2022-12-04", "2022-12-24,2022-12-26"],
"language": "de",
}
TEST_NO_OPTIONAL_CATEGORY = {
"name": DEFAULT_NAME,
"country": "CH",
"province": "FR",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
"language": "de",
}
TEST_OPTIONAL_CATEGORY = {
"name": DEFAULT_NAME,
"country": "CH",
"province": "FR",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": [],
"language": "de",
"category": [OPTIONAL],
}

View File

@@ -0,0 +1,15 @@
"""Fixtures for Workday integration tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, patch
import pytest
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Mock setting up a config entry."""
with patch(
"homeassistant.components.workday.async_setup_entry", return_value=True
) as mock_setup:
yield mock_setup

View File

@@ -0,0 +1,59 @@
# serializer version: 1
# name: test_only_repairs_for_current_next_year
dict({
tuple(
'workday',
'bad_date_holiday-1-2024_08_15',
): IssueRegistryItemSnapshot({
'active': True,
'breaks_in_ha_version': None,
'created': <ANY>,
'data': dict({
'country': 'DE',
'entry_id': '1',
'named_holiday': '2024-08-15',
}),
'dismissed_version': None,
'domain': 'workday',
'is_fixable': True,
'is_persistent': False,
'issue_domain': None,
'issue_id': 'bad_date_holiday-1-2024_08_15',
'learn_more_url': None,
'severity': <IssueSeverity.WARNING: 'warning'>,
'translation_key': 'bad_date_holiday',
'translation_placeholders': dict({
'country': 'DE',
'remove_holidays': '2024-08-15',
'title': 'Mock Title',
}),
}),
tuple(
'workday',
'bad_date_holiday-1-2025_08_15',
): IssueRegistryItemSnapshot({
'active': True,
'breaks_in_ha_version': None,
'created': <ANY>,
'data': dict({
'country': 'DE',
'entry_id': '1',
'named_holiday': '2025-08-15',
}),
'dismissed_version': None,
'domain': 'workday',
'is_fixable': True,
'is_persistent': False,
'issue_domain': None,
'issue_id': 'bad_date_holiday-1-2025_08_15',
'learn_more_url': None,
'severity': <IssueSeverity.WARNING: 'warning'>,
'translation_key': 'bad_date_holiday',
'translation_placeholders': dict({
'country': 'DE',
'remove_holidays': '2025-08-15',
'title': 'Mock Title',
}),
}),
})
# ---

View File

@@ -0,0 +1,48 @@
# serializer version: 1
# name: test_diagnostics
dict({
'config_entry': dict({
'data': dict({
}),
'disabled_by': None,
'discovery_keys': dict({
}),
'domain': 'workday',
'entry_id': '1',
'minor_version': 1,
'options': dict({
'add_holidays': list([
'2022-12-01',
'2022-12-05,2022-12-15',
]),
'country': 'DE',
'days_offset': 0,
'excludes': list([
'sat',
'sun',
'holiday',
]),
'language': 'de',
'name': 'Workday Sensor',
'province': 'BW',
'remove_holidays': list([
'2022-12-04',
'2022-12-24,2022-12-26',
]),
'workdays': list([
'mon',
'tue',
'wed',
'thu',
'fri',
]),
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'Mock Title',
'unique_id': None,
'version': 1,
}),
})
# ---

View File

@@ -0,0 +1,463 @@
"""Tests the Home Assistant workday binary sensor."""
from datetime import date, datetime, timedelta, timezone
from typing import Any
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.workday.binary_sensor import SERVICE_CHECK_DATE
from homeassistant.components.workday.const import (
DEFAULT_EXCLUDES,
DEFAULT_NAME,
DEFAULT_OFFSET,
DEFAULT_WORKDAYS,
DOMAIN,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from homeassistant.util.dt import UTC
from . import (
TEST_CONFIG_ADD_REMOVE_DATE_RANGE,
TEST_CONFIG_DAY_AFTER_TOMORROW,
TEST_CONFIG_EXAMPLE_1,
TEST_CONFIG_EXAMPLE_2,
TEST_CONFIG_INCLUDE_HOLIDAY,
TEST_CONFIG_INCORRECT_ADD_DATE_RANGE,
TEST_CONFIG_INCORRECT_ADD_DATE_RANGE_LEN,
TEST_CONFIG_INCORRECT_ADD_REMOVE,
TEST_CONFIG_INCORRECT_COUNTRY,
TEST_CONFIG_INCORRECT_PROVINCE,
TEST_CONFIG_INCORRECT_REMOVE_DATE_RANGE,
TEST_CONFIG_INCORRECT_REMOVE_DATE_RANGE_LEN,
TEST_CONFIG_NO_COUNTRY,
TEST_CONFIG_NO_COUNTRY_ADD_HOLIDAY,
TEST_CONFIG_NO_LANGUAGE_CONFIGURED,
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,
TEST_LANGUAGE_CHANGE,
TEST_LANGUAGE_NO_CHANGE,
TEST_NO_OPTIONAL_CATEGORY,
TEST_OPTIONAL_CATEGORY,
init_integration,
)
from tests.common import async_fire_time_changed
@pytest.mark.parametrize(
("config", "expected_state", "expected_state_weekend"),
[
(TEST_CONFIG_NO_COUNTRY, "on", "off"),
(TEST_CONFIG_WITH_PROVINCE, "off", "off"),
(TEST_CONFIG_NO_PROVINCE, "off", "off"),
(TEST_CONFIG_WITH_STATE, "on", "off"),
(TEST_CONFIG_NO_STATE, "on", "off"),
(TEST_CONFIG_EXAMPLE_1, "on", "off"),
(TEST_CONFIG_EXAMPLE_2, "off", "off"),
(TEST_CONFIG_TOMORROW, "off", "off"),
(TEST_CONFIG_DAY_AFTER_TOMORROW, "off", "off"),
(TEST_CONFIG_YESTERDAY, "on", "off"), # Friday was good Friday
(TEST_CONFIG_NO_LANGUAGE_CONFIGURED, "off", "off"),
],
)
async def test_setup(
hass: HomeAssistant,
config: dict[str, Any],
expected_state: str,
expected_state_weekend: str,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test setup from various configs."""
# Start on a Friday
await hass.config.async_set_time_zone("Europe/Paris")
freezer.move_to(datetime(2022, 4, 15, 0, tzinfo=timezone(timedelta(hours=1))))
await init_integration(hass, config)
state = hass.states.get("binary_sensor.workday_sensor")
assert state 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"],
}
freezer.tick(timedelta(days=1)) # Saturday
async_fire_time_changed(hass)
state = hass.states.get("binary_sensor.workday_sensor")
assert state is not None
assert state.state == expected_state_weekend
async def test_setup_with_invalid_province_from_yaml(hass: HomeAssistant) -> None:
"""Test setup invalid province with import."""
await async_setup_component(
hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "workday",
"country": "DE",
"province": "invalid",
}
},
)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert state is None
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 is not None
assert state.state == "on"
@pytest.mark.parametrize(
"config",
[
TEST_CONFIG_EXAMPLE_2,
TEST_CONFIG_NO_COUNTRY_ADD_HOLIDAY,
],
)
async def test_setup_add_holiday(
hass: HomeAssistant,
config: dict[str, Any],
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 is not None
assert state.state == "off"
@pytest.mark.parametrize(
"time_zone", ["Asia/Tokyo", "Europe/Berlin", "America/Chicago", "US/Hawaii"]
)
async def test_setup_no_country_weekend(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
time_zone: str,
) -> None:
"""Test setup shows weekend as non-workday with no country."""
await hass.config.async_set_time_zone(time_zone)
zone = await dt_util.async_get_time_zone(time_zone)
freezer.move_to(datetime(2020, 2, 22, 0, 1, 1, tzinfo=zone)) # Saturday
await init_integration(hass, TEST_CONFIG_NO_COUNTRY)
state = hass.states.get("binary_sensor.workday_sensor")
assert state is not None
assert state.state == "off"
freezer.move_to(datetime(2020, 2, 24, 23, 59, 59, tzinfo=zone)) # Monday
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert state is not None
assert state.state == "on"
@pytest.mark.parametrize(
"time_zone", ["Asia/Tokyo", "Europe/Berlin", "America/Chicago", "US/Hawaii"]
)
async def test_setup_no_country_weekday(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
time_zone: str,
) -> None:
"""Test setup shows a weekday as a workday with no country."""
await hass.config.async_set_time_zone(time_zone)
zone = await dt_util.async_get_time_zone(time_zone)
freezer.move_to(datetime(2020, 2, 21, 23, 59, 59, tzinfo=zone)) # Friday
await init_integration(hass, TEST_CONFIG_NO_COUNTRY)
state = hass.states.get("binary_sensor.workday_sensor")
assert state is not None
assert state.state == "on"
freezer.move_to(datetime(2020, 2, 22, 23, 59, 59, tzinfo=zone)) # Saturday
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert state is not None
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 is not None
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 is not None
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 is not None
assert state.state == "off"
async def test_setup_faulty_country(
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_COUNTRY)
state = hass.states.get("binary_sensor.workday_sensor")
assert state is None
assert "Selected country ZZ is not valid" in caplog.text
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 "Selected province ZZ for country DE is not valid" 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
async def test_setup_incorrect_add_holiday_ranges(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test setup with incorrect add/remove holiday ranges."""
freezer.move_to(datetime(2017, 1, 6, 12, tzinfo=UTC)) # Friday
await init_integration(hass, TEST_CONFIG_INCORRECT_ADD_DATE_RANGE)
await init_integration(hass, TEST_CONFIG_INCORRECT_ADD_DATE_RANGE_LEN, "2")
hass.states.get("binary_sensor.workday_sensor")
assert "Incorrect dates in date range: 2023-12-30,2023-12-32" in caplog.text
assert (
"Incorrect dates in date range: 2023-12-29,2023-12-30,2023-12-31" in caplog.text
)
async def test_setup_incorrect_remove_holiday_ranges(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test setup with incorrect add/remove holiday ranges."""
freezer.move_to(datetime(2017, 1, 6, 12, tzinfo=UTC)) # Friday
await init_integration(hass, TEST_CONFIG_INCORRECT_REMOVE_DATE_RANGE)
await init_integration(hass, TEST_CONFIG_INCORRECT_REMOVE_DATE_RANGE_LEN, "2")
hass.states.get("binary_sensor.workday_sensor")
assert "Incorrect dates in date range: 2023-12-30,2023-12-32" in caplog.text
assert (
"Incorrect dates in date range: 2023-12-29,2023-12-30,2023-12-31" in caplog.text
)
async def test_setup_date_range(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test setup with date range."""
freezer.move_to(
datetime(2022, 12, 26, 12, tzinfo=UTC)
) # Boxing Day should be working day
await init_integration(hass, TEST_CONFIG_ADD_REMOVE_DATE_RANGE)
state = hass.states.get("binary_sensor.workday_sensor")
assert state.state == "on"
async def test_check_date_service(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test check date service with response data."""
freezer.move_to(datetime(2017, 1, 6, 12, tzinfo=UTC)) # Friday
await init_integration(hass, TEST_CONFIG_WITH_PROVINCE)
hass.states.get("binary_sensor.workday_sensor")
response = await hass.services.async_call(
DOMAIN,
SERVICE_CHECK_DATE,
{
"entity_id": "binary_sensor.workday_sensor",
"check_date": date(2022, 12, 25), # Christmas Day
},
blocking=True,
return_response=True,
)
assert response == {"binary_sensor.workday_sensor": {"workday": False}}
response = await hass.services.async_call(
DOMAIN,
SERVICE_CHECK_DATE,
{
"entity_id": "binary_sensor.workday_sensor",
"check_date": date(2022, 12, 23), # Normal Friday
},
blocking=True,
return_response=True,
)
assert response == {"binary_sensor.workday_sensor": {"workday": True}}
response = await hass.services.async_call(
DOMAIN,
SERVICE_CHECK_DATE,
{
"entity_id": "binary_sensor.workday_sensor",
"check_date": date(2022, 12, 17), # Saturday (no workday)
},
blocking=True,
return_response=True,
)
assert response == {"binary_sensor.workday_sensor": {"workday": False}}
async def test_language_difference_english_language(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test handling difference in English language naming."""
await init_integration(hass, TEST_LANGUAGE_CHANGE)
assert "Changing language from en to en_US" in caplog.text
async def test_language_difference_no_change_other_language(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test skipping if no difference in language naming."""
await init_integration(hass, TEST_LANGUAGE_NO_CHANGE)
assert "Changing language from en to en_US" not in caplog.text
@pytest.mark.parametrize(
("config", "end_state"),
[(TEST_OPTIONAL_CATEGORY, "off"), (TEST_NO_OPTIONAL_CATEGORY, "on")],
)
async def test_optional_category(
hass: HomeAssistant,
config: dict[str, Any],
end_state: str,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test setup from various configs."""
# CH, subdiv FR has optional holiday Jan 2nd
freezer.move_to(datetime(2024, 1, 2, 12, tzinfo=UTC)) # Tuesday
await init_integration(hass, config)
state = hass.states.get("binary_sensor.workday_sensor")
assert state is not None
assert state.state == end_state
async def test_only_repairs_for_current_next_year(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
issue_registry: ir.IssueRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test only repairs are raised for current and next year."""
freezer.move_to(datetime(2024, 8, 15, 12, tzinfo=UTC))
remove_dates = [
# None of these dates are holidays
"2024-08-15", # Creates issue
"2025-08-15", # Creates issue
"2026-08-15", # No issue
]
config = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": remove_dates,
"language": "de",
}
await init_integration(hass, config)
assert len(issue_registry.issues) == 2
assert issue_registry.issues == snapshot

View File

@@ -0,0 +1,700 @@
"""Test the Workday config flow."""
from __future__ import annotations
from datetime import datetime
from freezegun.api import FrozenDateTimeFactory
from holidays import HALF_DAY, OPTIONAL
import pytest
from homeassistant import config_entries
from homeassistant.components.workday.const import (
CONF_ADD_HOLIDAYS,
CONF_CATEGORY,
CONF_EXCLUDES,
CONF_OFFSET,
CONF_REMOVE_HOLIDAYS,
CONF_WORKDAYS,
DEFAULT_EXCLUDES,
DEFAULT_OFFSET,
DEFAULT_WORKDAYS,
DOMAIN,
)
from homeassistant.const import CONF_COUNTRY, CONF_LANGUAGE, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.util.dt import UTC
from . import init_integration
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
async def test_form(hass: HomeAssistant) -> None:
"""Test we get the forms."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_NAME: "Workday Sensor",
CONF_COUNTRY: "DE",
},
)
await hass.async_block_till_done()
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: [],
CONF_REMOVE_HOLIDAYS: [],
CONF_LANGUAGE: "de",
},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
assert result3["title"] == "Workday Sensor"
assert result3["options"] == {
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"language": "de",
}
async def test_form_no_country(hass: HomeAssistant) -> None:
"""Test we get the forms correctly without a country."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_NAME: "Workday Sensor",
},
)
await hass.async_block_till_done()
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: [],
CONF_REMOVE_HOLIDAYS: [],
},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
assert result3["title"] == "Workday Sensor"
assert result3["options"] == {
"name": "Workday Sensor",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
}
async def test_form_no_subdivision(hass: HomeAssistant) -> None:
"""Test we get the forms correctly without subdivision."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_NAME: "Workday Sensor",
CONF_COUNTRY: "SE",
},
)
await hass.async_block_till_done()
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: [],
CONF_REMOVE_HOLIDAYS: [],
},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
assert result3["title"] == "Workday Sensor"
assert result3["options"] == {
"name": "Workday Sensor",
"country": "SE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"language": "sv",
}
async def test_options_form(hass: HomeAssistant) -> None:
"""Test we get the form in options."""
entry = await init_integration(
hass,
{
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"language": "de",
},
)
result = await hass.config_entries.options.async_init(entry.entry_id)
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": "BW",
"language": "de",
},
)
assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["data"] == {
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": "BW",
"language": "de",
}
async def test_form_incorrect_dates(hass: HomeAssistant) -> None:
"""Test errors in setup entry."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_NAME: "Workday Sensor",
CONF_COUNTRY: "DE",
},
)
await hass.async_block_till_done()
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-xx-12"],
CONF_REMOVE_HOLIDAYS: [],
CONF_LANGUAGE: "de",
},
)
await hass.async_block_till_done()
assert result3["errors"] == {"add_holidays": "add_holiday_error"}
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-12-12"],
CONF_REMOVE_HOLIDAYS: ["Does not exist"],
CONF_LANGUAGE: "de",
},
)
await hass.async_block_till_done()
assert result3["errors"] == {"remove_holidays": "remove_holiday_error"}
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-12-12"],
CONF_REMOVE_HOLIDAYS: ["Weihnachtstag"],
CONF_LANGUAGE: "de",
},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
assert result3["title"] == "Workday Sensor"
assert result3["options"] == {
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-12-12"],
"remove_holidays": ["Weihnachtstag"],
"language": "de",
}
async def test_options_form_incorrect_dates(hass: HomeAssistant) -> None:
"""Test errors in options."""
entry = await init_integration(
hass,
{
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"language": "de",
},
)
result = await hass.config_entries.options.async_init(entry.entry_id)
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-xx-12"],
"remove_holidays": [],
"province": "BW",
"language": "de",
},
)
assert result2["errors"] == {"add_holidays": "add_holiday_error"}
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-12-12"],
"remove_holidays": ["Does not exist"],
"province": "BW",
"language": "de",
},
)
assert result2["errors"] == {"remove_holidays": "remove_holiday_error"}
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-12-12"],
"remove_holidays": ["Weihnachtstag"],
"province": "BW",
"language": "de",
},
)
assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["data"] == {
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-12-12"],
"remove_holidays": ["Weihnachtstag"],
"province": "BW",
"language": "de",
}
async def test_options_form_abort_duplicate(hass: HomeAssistant) -> None:
"""Test errors in options for duplicates."""
await init_integration(
hass,
{
"name": "Workday Sensor",
"country": "CH",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": "FR",
"category": [OPTIONAL],
},
entry_id="1",
)
entry2 = await init_integration(
hass,
{
"name": "Workday Sensor2",
"country": "CH",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2023-03-28"],
"remove_holidays": [],
"province": "FR",
"category": [OPTIONAL],
},
entry_id="2",
)
result = await hass.config_entries.options.async_init(entry2.entry_id)
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0.0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": "FR",
"category": [OPTIONAL],
},
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "already_configured"}
async def test_form_incorrect_date_range(hass: HomeAssistant) -> None:
"""Test errors in setup entry."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_NAME: "Workday Sensor",
CONF_COUNTRY: "DE",
},
)
await hass.async_block_till_done()
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-12-12", "2022-12-30,2022-12-32"],
CONF_REMOVE_HOLIDAYS: [],
CONF_LANGUAGE: "de",
},
)
await hass.async_block_till_done()
assert result3["errors"] == {"add_holidays": "add_holiday_range_error"}
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-12-12"],
CONF_REMOVE_HOLIDAYS: ["2022-12-25", "2022-12-30,2022-12-32"],
CONF_LANGUAGE: "de",
},
)
await hass.async_block_till_done()
assert result3["errors"] == {"remove_holidays": "remove_holiday_range_error"}
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: ["2022-12-12", "2022-12-01,2022-12-10"],
CONF_REMOVE_HOLIDAYS: ["2022-12-25", "2022-12-30,2022-12-31"],
CONF_LANGUAGE: "de",
},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
assert result3["title"] == "Workday Sensor"
assert result3["options"] == {
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-12-12", "2022-12-01,2022-12-10"],
"remove_holidays": ["2022-12-25", "2022-12-30,2022-12-31"],
"language": "de",
}
async def test_options_form_incorrect_date_ranges(hass: HomeAssistant) -> None:
"""Test errors in options."""
entry = await init_integration(
hass,
{
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"language": "de",
},
)
result = await hass.config_entries.options.async_init(entry.entry_id)
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-12-30,2022-12-32"],
"remove_holidays": [],
"province": "BW",
"language": "de",
},
)
assert result2["errors"] == {"add_holidays": "add_holiday_range_error"}
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-12-30,2022-12-31"],
"remove_holidays": ["2022-13-25,2022-12-26"],
"province": "BW",
"language": "de",
},
)
assert result2["errors"] == {"remove_holidays": "remove_holiday_range_error"}
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-12-30,2022-12-31"],
"remove_holidays": ["2022-12-25,2022-12-26"],
"province": "BW",
"language": "de",
},
)
assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["data"] == {
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": ["2022-12-30,2022-12-31"],
"remove_holidays": ["2022-12-25,2022-12-26"],
"province": "BW",
"language": "de",
}
pytestmark = pytest.mark.usefixtures()
@pytest.mark.parametrize(
("language", "holiday"),
[
("de", "Weihnachtstag"),
("en_US", "Christmas"),
],
)
async def test_language(
hass: HomeAssistant, language: str, holiday: str, freezer: FrozenDateTimeFactory
) -> None:
"""Test we get the forms."""
freezer.move_to(datetime(2023, 12, 25, 12, tzinfo=UTC)) # Monday
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_NAME: "Workday Sensor",
CONF_COUNTRY: "DE",
},
)
await hass.async_block_till_done()
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: [],
CONF_REMOVE_HOLIDAYS: [holiday],
CONF_LANGUAGE: language,
},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
assert result3["title"] == "Workday Sensor"
assert result3["options"] == {
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [holiday],
"language": language,
}
state = hass.states.get("binary_sensor.workday_sensor")
assert state is not None
assert state.state == "on"
async def test_form_with_categories(hass: HomeAssistant) -> None:
"""Test optional categories."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_NAME: "Workday Sensor",
CONF_COUNTRY: "CH",
},
)
await hass.async_block_till_done()
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: [],
CONF_REMOVE_HOLIDAYS: [],
CONF_LANGUAGE: "de",
CONF_CATEGORY: [HALF_DAY],
},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
assert result3["title"] == "Workday Sensor"
assert result3["options"] == {
"name": "Workday Sensor",
"country": "CH",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"language": "de",
"category": ["half_day"],
}
async def test_options_form_removes_subdiv(hass: HomeAssistant) -> None:
"""Test we get the form in options when removing a configured subdivision."""
entry = await init_integration(
hass,
{
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"language": "de",
"province": "BW",
},
)
result = await hass.config_entries.options.async_init(entry.entry_id)
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"language": "de",
},
)
assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["data"] == {
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"language": "de",
}

View File

@@ -0,0 +1,28 @@
"""Test Workday diagnostics."""
from __future__ import annotations
from syrupy.assertion import SnapshotAssertion
from syrupy.filters import props
from homeassistant.core import HomeAssistant
from . import TEST_CONFIG_ADD_REMOVE_DATE_RANGE, init_integration
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator
async def test_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
snapshot: SnapshotAssertion,
) -> None:
"""Test generating diagnostics for a config entry."""
entry = await init_integration(hass, TEST_CONFIG_ADD_REMOVE_DATE_RANGE)
diag = await get_diagnostics_for_config_entry(hass, hass_client, entry)
assert diag == snapshot(
exclude=props("full_features", "created_at", "modified_at"),
)

View File

@@ -0,0 +1,52 @@
"""Test Workday component setup process."""
from __future__ import annotations
from datetime import datetime
from freezegun.api import FrozenDateTimeFactory
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.util.dt import UTC
from . import TEST_CONFIG_EXAMPLE_1, TEST_CONFIG_WITH_PROVINCE, init_integration
async def test_load_unload_entry(hass: HomeAssistant) -> None:
"""Test load and unload entry."""
entry = await init_integration(hass, TEST_CONFIG_EXAMPLE_1)
state = hass.states.get("binary_sensor.workday_sensor")
assert state
await hass.config_entries.async_remove(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert not state
async def test_update_options(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test options update and config entry is reloaded."""
freezer.move_to(datetime(2023, 4, 12, 12, tzinfo=UTC)) # Monday
entry = await init_integration(hass, TEST_CONFIG_WITH_PROVINCE)
assert entry.state is ConfigEntryState.LOADED
assert entry.update_listeners is not None
state = hass.states.get("binary_sensor.workday_sensor")
assert state.state == "on"
new_options = TEST_CONFIG_WITH_PROVINCE.copy()
new_options["add_holidays"] = ["2023-04-12"]
hass.config_entries.async_update_entry(entry, options=new_options)
await hass.async_block_till_done()
entry_check = hass.config_entries.async_get_entry("1")
assert entry_check.state is ConfigEntryState.LOADED
state = hass.states.get("binary_sensor.workday_sensor")
assert state.state == "off"

View File

@@ -0,0 +1,502 @@
"""Test repairs for unifiprotect."""
from __future__ import annotations
import pytest
from homeassistant.components.workday.const import CONF_REMOVE_HOLIDAYS, DOMAIN
from homeassistant.const import CONF_COUNTRY
from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component
from . import (
TEST_CONFIG_INCORRECT_COUNTRY,
TEST_CONFIG_INCORRECT_PROVINCE,
TEST_CONFIG_REMOVE_DATE,
TEST_CONFIG_REMOVE_NAMED,
init_integration,
)
from tests.common import ANY
from tests.components.repairs import process_repair_fix_flow, start_repair_fix_flow
from tests.typing import ClientSessionGenerator, WebSocketGenerator
async def test_bad_country(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test fixing bad country."""
assert await async_setup_component(hass, "repairs", {})
entry = await init_integration(hass, TEST_CONFIG_INCORRECT_COUNTRY)
state = hass.states.get("binary_sensor.workday_sensor")
assert not state
ws_client = await hass_ws_client(hass)
client = await hass_client()
await ws_client.send_json({"id": 1, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
assert len(msg["result"]["issues"]) > 0
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_country":
issue = i
assert issue is not None
data = await start_repair_fix_flow(client, DOMAIN, "bad_country")
flow_id = data["flow_id"]
assert data["description_placeholders"] == {"title": entry.title}
assert data["step_id"] == "country"
data = await process_repair_fix_flow(client, flow_id, json={"country": "DE"})
data = await process_repair_fix_flow(client, flow_id, json={"province": "HB"})
assert data["type"] == "create_entry"
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert state
await ws_client.send_json({"id": 2, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_country":
issue = i
assert not issue
async def test_bad_country_none(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test fixing bad country with no province."""
assert await async_setup_component(hass, "repairs", {})
entry = await init_integration(hass, TEST_CONFIG_INCORRECT_COUNTRY)
state = hass.states.get("binary_sensor.workday_sensor")
assert not state
ws_client = await hass_ws_client(hass)
client = await hass_client()
await ws_client.send_json({"id": 1, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
assert len(msg["result"]["issues"]) > 0
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_country":
issue = i
assert issue is not None
data = await start_repair_fix_flow(client, DOMAIN, "bad_country")
flow_id = data["flow_id"]
assert data["description_placeholders"] == {"title": entry.title}
assert data["step_id"] == "country"
data = await process_repair_fix_flow(client, flow_id, json={"country": "DE"})
data = await process_repair_fix_flow(client, flow_id, json={})
assert data["type"] == "create_entry"
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert state
await ws_client.send_json({"id": 2, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_country":
issue = i
assert not issue
async def test_bad_country_no_province(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test fixing bad country."""
assert await async_setup_component(hass, "repairs", {})
entry = await init_integration(hass, TEST_CONFIG_INCORRECT_COUNTRY)
state = hass.states.get("binary_sensor.workday_sensor")
assert not state
ws_client = await hass_ws_client(hass)
client = await hass_client()
await ws_client.send_json({"id": 1, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
assert len(msg["result"]["issues"]) > 0
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_country":
issue = i
assert issue is not None
data = await start_repair_fix_flow(client, DOMAIN, "bad_country")
flow_id = data["flow_id"]
assert data["description_placeholders"] == {"title": entry.title}
assert data["step_id"] == "country"
data = await process_repair_fix_flow(client, flow_id, json={"country": "SE"})
assert data["type"] == "create_entry"
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert state
await ws_client.send_json({"id": 2, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_country":
issue = i
assert not issue
async def test_bad_province(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test fixing bad province."""
assert await async_setup_component(hass, "repairs", {})
entry = await init_integration(hass, TEST_CONFIG_INCORRECT_PROVINCE)
state = hass.states.get("binary_sensor.workday_sensor")
assert not state
ws_client = await hass_ws_client(hass)
client = await hass_client()
await ws_client.send_json({"id": 1, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
assert len(msg["result"]["issues"]) > 0
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_province":
issue = i
assert issue is not None
data = await start_repair_fix_flow(client, DOMAIN, "bad_province")
flow_id = data["flow_id"]
assert data["description_placeholders"] == {
CONF_COUNTRY: "DE",
"title": entry.title,
}
assert data["step_id"] == "province"
data = await process_repair_fix_flow(client, flow_id, json={"province": "BW"})
assert data["type"] == "create_entry"
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert state
await ws_client.send_json({"id": 2, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_province":
issue = i
assert not issue
async def test_bad_province_none(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test fixing bad province selecting none."""
assert await async_setup_component(hass, "repairs", {})
entry = await init_integration(hass, TEST_CONFIG_INCORRECT_PROVINCE)
state = hass.states.get("binary_sensor.workday_sensor")
assert not state
ws_client = await hass_ws_client(hass)
client = await hass_client()
await ws_client.send_json({"id": 1, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
assert len(msg["result"]["issues"]) > 0
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_province":
issue = i
assert issue is not None
data = await start_repair_fix_flow(client, DOMAIN, "bad_province")
flow_id = data["flow_id"]
assert data["description_placeholders"] == {
CONF_COUNTRY: "DE",
"title": entry.title,
}
assert data["step_id"] == "province"
data = await process_repair_fix_flow(client, flow_id, json={})
assert data["type"] == "create_entry"
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert state
await ws_client.send_json({"id": 2, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_province":
issue = i
assert not issue
async def test_bad_named_holiday(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test fixing bad province selecting none."""
assert await async_setup_component(hass, "repairs", {})
entry = await init_integration(hass, TEST_CONFIG_REMOVE_NAMED)
state = hass.states.get("binary_sensor.workday_sensor")
assert state
issues = issue_registry.issues.keys()
for issue in issues:
if issue[0] == DOMAIN:
assert issue[1].startswith("bad_named")
ws_client = await hass_ws_client(hass)
client = await hass_client()
await ws_client.send_json({"id": 1, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
assert len(msg["result"]["issues"]) > 0
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_named_holiday-1-not_a_holiday":
issue = i
assert issue is not None
data = await start_repair_fix_flow(
client, DOMAIN, "bad_named_holiday-1-not_a_holiday"
)
flow_id = data["flow_id"]
assert data["description_placeholders"] == {
CONF_COUNTRY: "US",
CONF_REMOVE_HOLIDAYS: "Not a Holiday",
"title": entry.title,
}
assert data["step_id"] == "fix_remove_holiday"
data = await process_repair_fix_flow(
client, flow_id, json={"remove_holidays": ["Christmas", "Not exist 2"]}
)
assert data["errors"] == {
CONF_REMOVE_HOLIDAYS: "remove_holiday_error",
}
data = await process_repair_fix_flow(
client, flow_id, json={"remove_holidays": ["Christmas", "Thanksgiving"]}
)
assert data["type"] == "create_entry"
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert state
await ws_client.send_json({"id": 2, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_named_holiday-1-not_a_holiday":
issue = i
assert not issue
async def test_bad_date_holiday(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test fixing bad province selecting none."""
assert await async_setup_component(hass, "repairs", {})
entry = await init_integration(hass, TEST_CONFIG_REMOVE_DATE)
state = hass.states.get("binary_sensor.workday_sensor")
assert state
issues = issue_registry.issues.keys()
for issue in issues:
if issue[0] == DOMAIN:
assert issue[1].startswith("bad_date")
ws_client = await hass_ws_client(hass)
client = await hass_client()
await ws_client.send_json({"id": 1, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
assert len(msg["result"]["issues"]) > 0
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_date_holiday-1-2024_02_05":
issue = i
assert issue is not None
data = await start_repair_fix_flow(client, DOMAIN, "bad_date_holiday-1-2024_02_05")
flow_id = data["flow_id"]
assert data["description_placeholders"] == {
CONF_COUNTRY: "US",
CONF_REMOVE_HOLIDAYS: "2024-02-05",
"title": entry.title,
}
assert data["step_id"] == "fix_remove_holiday"
data = await process_repair_fix_flow(
client, flow_id, json={"remove_holidays": ["2024-02-06"]}
)
assert data["type"] == "create_entry"
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert state
await ws_client.send_json({"id": 2, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_date_holiday-1-2024_02_05":
issue = i
assert not issue
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_date_holiday-1-2024_02_06":
issue = i
assert issue
@pytest.mark.parametrize(
"ignore_translations",
["component.workday.issues.issue_1.title"],
)
async def test_other_fixable_issues(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test fixing bad province selecting none."""
assert await async_setup_component(hass, "repairs", {})
await init_integration(hass, TEST_CONFIG_INCORRECT_PROVINCE)
ws_client = await hass_ws_client(hass)
client = await hass_client()
await ws_client.send_json({"id": 1, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
issue = {
"breaks_in_ha_version": "2022.9.0dev0",
"domain": DOMAIN,
"issue_id": "issue_1",
"is_fixable": True,
"learn_more_url": "",
"severity": "error",
"translation_key": "issue_1",
}
ir.async_create_issue(
hass,
issue["domain"],
issue["issue_id"],
breaks_in_ha_version=issue["breaks_in_ha_version"],
is_fixable=issue["is_fixable"],
is_persistent=False,
learn_more_url=None,
severity=issue["severity"],
translation_key=issue["translation_key"],
)
await ws_client.send_json({"id": 2, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()
assert msg["success"]
results = msg["result"]["issues"]
assert {
"breaks_in_ha_version": "2022.9.0dev0",
"created": ANY,
"dismissed_version": None,
"domain": "workday",
"is_fixable": True,
"issue_domain": None,
"issue_id": "issue_1",
"learn_more_url": None,
"severity": "error",
"translation_key": "issue_1",
"translation_placeholders": None,
"ignored": False,
} in results
data = await start_repair_fix_flow(client, DOMAIN, "issue_1")
flow_id = data["flow_id"]
assert data["step_id"] == "confirm"
data = await process_repair_fix_flow(client, flow_id)
assert data["type"] == "create_entry"
await hass.async_block_till_done()