Fix workday sensor to honor timezone (#47927)

This commit is contained in:
schiermi 2021-03-17 15:32:18 +01:00 committed by GitHub
parent a1ff45cbf2
commit 6a24ec7a30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
"""Sensor to indicate whether the current day is a workday.""" """Sensor to indicate whether the current day is a workday."""
from datetime import datetime, timedelta from datetime import timedelta
import logging import logging
from typing import Any from typing import Any
@ -9,6 +9,7 @@ import voluptuous as vol
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
from homeassistant.const import CONF_NAME, WEEKDAYS from homeassistant.const import CONF_NAME, WEEKDAYS
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -77,7 +78,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
sensor_name = config[CONF_NAME] sensor_name = config[CONF_NAME]
workdays = config[CONF_WORKDAYS] workdays = config[CONF_WORKDAYS]
year = (get_date(datetime.today()) + timedelta(days=days_offset)).year year = (get_date(dt.now()) + timedelta(days=days_offset)).year
obj_holidays = getattr(holidays, country)(years=year) obj_holidays = getattr(holidays, country)(years=year)
if province: if province:
@ -185,7 +186,7 @@ class IsWorkdaySensor(BinarySensorEntity):
self._state = False self._state = False
# Get ISO day of the week (1 = Monday, 7 = Sunday) # Get ISO day of the week (1 = Monday, 7 = Sunday)
date = get_date(datetime.today()) + timedelta(days=self._days_offset) date = get_date(dt.now()) + timedelta(days=self._days_offset)
day = date.isoweekday() - 1 day = date.isoweekday() - 1
day_of_week = day_to_string(day) day_of_week = day_to_string(day)