Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Check date service for Workday #97280

Merged
merged 6 commits into from Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 25 additions & 2 deletions homeassistant/components/workday/binary_sensor.py
Expand Up @@ -2,19 +2,25 @@
from __future__ import annotations

from datetime import date, timedelta
from typing import Final

from holidays import (
HolidayBase,
__version__ as python_holidays_version,
country_holidays,
)
import voluptuous as vol

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, ServiceResponse, SupportsResponse
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.entity_platform import (
AddEntitiesCallback,
async_get_current_platform,
)
from homeassistant.util import dt as dt_util

from .const import (
Expand All @@ -30,6 +36,9 @@
LOGGER,
)

SERVICE_CHECK_DATE: Final = "check_date"
CHECK_DATE: Final = "check_date"


def validate_dates(holiday_list: list[str]) -> list[str]:
"""Validate and adds to list of dates to add or remove."""
Expand Down Expand Up @@ -109,6 +118,15 @@ async def async_setup_entry(
_holiday_string = holiday_date.strftime("%Y-%m-%d")
LOGGER.debug("%s %s", _holiday_string, name)

platform = async_get_current_platform()
platform.async_register_entity_service(
SERVICE_CHECK_DATE,
{vol.Required(CHECK_DATE): cv.date},
"check_date",
None,
SupportsResponse.ONLY,
)

async_add_entities(
[
IsWorkdaySensor(
Expand Down Expand Up @@ -192,3 +210,8 @@ async def async_update(self) -> None:

if self.is_exclude(day_of_week, adjusted_date):
self._attr_is_on = False

async def check_date(self, check_date: date) -> ServiceResponse:
"""Check if date is workday or not."""
holiday_date = check_date in self._obj_holidays
return {"workday": not holiday_date}
9 changes: 9 additions & 0 deletions homeassistant/components/workday/services.yaml
@@ -0,0 +1,9 @@
check_date:
target:
entity:
integration: workday
fields:
check_date:
example: "2022-12-25"
selector:
date:
12 changes: 12 additions & 0 deletions homeassistant/components/workday/strings.json
Expand Up @@ -146,5 +146,17 @@
}
}
}
},
"services": {
"check_date": {
"name": "Check date",
"description": "Check if date is workday.",
"fields": {
"check_date": {
"name": "Date",
"description": "Date to check if workday."
}
}
}
}
}
40 changes: 39 additions & 1 deletion tests/components/workday/test_binary_sensor.py
@@ -1,10 +1,12 @@
"""Tests the Home Assistant workday binary sensor."""
from datetime import datetime
from datetime import date, datetime
from typing import Any

from freezegun.api import FrozenDateTimeFactory
import pytest

from homeassistant.components.workday.binary_sensor import SERVICE_CHECK_DATE
from homeassistant.components.workday.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import UTC
Expand Down Expand Up @@ -273,3 +275,39 @@ async def test_setup_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}}