diff --git a/README.md b/README.md index 79b0b04..7bab74e 100644 --- a/README.md +++ b/README.md @@ -257,7 +257,7 @@ garbage_collection: etc... ``` -Configuration (I use style to allow line-break in the state) +Configuration (I use style (by card-mod plugin) to allow line-break in the state) ```yaml - type: glance style: diff --git a/custom_components/garbage_collection/manifest.json b/custom_components/garbage_collection/manifest.json index 1eba3f1..a317c6c 100644 --- a/custom_components/garbage_collection/manifest.json +++ b/custom_components/garbage_collection/manifest.json @@ -9,6 +9,7 @@ ], "requirements": [ "datetime", + "python-dateutil", "integrationhelper>=0.2.2", "holidays>=0.10.1", "typing", diff --git a/custom_components/garbage_collection/sensor.py b/custom_components/garbage_collection/sensor.py index 4eac7d2..e410a88 100644 --- a/custom_components/garbage_collection/sensor.py +++ b/custom_components/garbage_collection/sensor.py @@ -5,6 +5,7 @@ import logging import locale from datetime import datetime, date, timedelta +from dateutil.relativedelta import relativedelta from homeassistant.core import HomeAssistant, State from typing import List, Any @@ -73,7 +74,7 @@ def nth_week_date(n: int, date_of_month: date, collection_day: int) -> date: """Find weekday in the nth week of the month""" first_of_month = date(date_of_month.year, date_of_month.month, 1) month_starts_on = first_of_month.weekday() - return first_of_month + timedelta( + return first_of_month + relativedelta( days=collection_day - month_starts_on + (n - 1) * 7 ) @@ -85,11 +86,11 @@ def nth_weekday_date(n: int, date_of_month: date, collection_day: int) -> date: # 1st of the month is before the day of collection # (so 1st collection week the week when month starts) if collection_day >= month_starts_on: - return first_of_month + timedelta( + return first_of_month + relativedelta( days=collection_day - month_starts_on + (n - 1) * 7 ) else: # Next week - return first_of_month + timedelta( + return first_of_month + relativedelta( days=7 - month_starts_on + collection_day + (n - 1) * 7 ) @@ -273,7 +274,7 @@ def find_candidate_date(self, day1: date) -> date: offset = ( 7 * in_weeks - weekday + WEEKDAYS.index(self.__collection_days[0]) ) - return day1 + timedelta(days=offset) + return day1 + relativedelta(days=offset) elif self.__frequency == "every-n-days": if self.__first_date is None or self.__period is None: _LOGGER.error( @@ -285,7 +286,7 @@ def find_candidate_date(self, day1: date) -> date: if (day1 - self.__first_date).days % self.__period == 0: return day1 offset = self.__period - ((day1 - self.__first_date).days % self.__period) - return day1 + timedelta(days=offset) + return day1 + relativedelta(days=offset) elif self.__frequency == "monthly": # Monthly if self.__monthly_force_week_numbers: @@ -362,7 +363,7 @@ def __insert_include_date(self, day1: date, next_date: date) -> date: return next_date def __skip_holiday(self, day: date) -> date: - return day + timedelta(days=1) + return day + relativedelta(days=1) def get_next_date(self, day1: date) -> date: """Find the next date starting from day1.""" @@ -379,7 +380,7 @@ def get_next_date(self, day1: date) -> date: if next_date not in self.__exclude_dates: return next_date _LOGGER.debug("(%s) Skipping exclude_date %s", self.__name, next_date) - first_day = next_date + timedelta(days=1) + first_day = next_date + relativedelta(days=1) i += 1 _LOGGER.error("(%s) Cannot find any suitable date", self.__name) return None