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

Fix check_date service in workday #105241

Merged
merged 2 commits into from
Dec 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions homeassistant/components/workday/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,26 @@ def is_exclude(self, day: str, now: date) -> bool:

async def async_update(self) -> None:
"""Get date and look whether it is a holiday."""
self._attr_is_on = self.date_is_workday(dt_util.now())

async def check_date(self, check_date: date) -> ServiceResponse:
"""Service to check if date is workday or not."""
return {"workday": self.date_is_workday(check_date)}

def date_is_workday(self, check_date: date) -> bool:
"""Check if date is workday."""
# Default is no workday
self._attr_is_on = False
is_workday = False

# Get ISO day of the week (1 = Monday, 7 = Sunday)
adjusted_date = dt_util.now() + timedelta(days=self._days_offset)
adjusted_date = check_date + timedelta(days=self._days_offset)
day = adjusted_date.isoweekday() - 1
day_of_week = ALLOWED_DAYS[day]

if self.is_include(day_of_week, adjusted_date):
self._attr_is_on = True
is_workday = True

if self.is_exclude(day_of_week, adjusted_date):
self._attr_is_on = False
is_workday = 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}
return is_workday
12 changes: 12 additions & 0 deletions tests/components/workday/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,18 @@ async def test_check_date_service(
)
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,
Expand Down