Fix check_date service in workday (#105241)
* Fix check_date service in workday * Add test
This commit is contained in:
parent
c6187ed914
commit
3a10ea1892
2 changed files with 25 additions and 8 deletions
|
@ -209,21 +209,26 @@ class IsWorkdaySensor(BinarySensorEntity):
|
||||||
|
|
||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Get date and look whether it is a holiday."""
|
"""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
|
# Default is no workday
|
||||||
self._attr_is_on = False
|
is_workday = False
|
||||||
|
|
||||||
# Get ISO day of the week (1 = Monday, 7 = Sunday)
|
# 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 = adjusted_date.isoweekday() - 1
|
||||||
day_of_week = ALLOWED_DAYS[day]
|
day_of_week = ALLOWED_DAYS[day]
|
||||||
|
|
||||||
if self.is_include(day_of_week, adjusted_date):
|
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):
|
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:
|
return is_workday
|
||||||
"""Check if date is workday or not."""
|
|
||||||
holiday_date = check_date in self._obj_holidays
|
|
||||||
return {"workday": not holiday_date}
|
|
||||||
|
|
|
@ -316,6 +316,18 @@ async def test_check_date_service(
|
||||||
)
|
)
|
||||||
assert response == {"binary_sensor.workday_sensor": {"workday": 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(
|
async def test_language_difference_english_language(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
|
Loading…
Add table
Reference in a new issue