Skip to content

Commit

Permalink
Merge pull request #99 from bruxy70/development
Browse files Browse the repository at this point in the history
timediff from dateutil instead of datetime
  • Loading branch information
bruxy70 authored Feb 24, 2020
2 parents 1285762 + 7f3971f commit 8ee07fe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions custom_components/garbage_collection/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
],
"requirements": [
"datetime",
"python-dateutil",
"integrationhelper>=0.2.2",
"holidays>=0.10.1",
"typing",
Expand Down
15 changes: 8 additions & 7 deletions custom_components/garbage_collection/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
)

Expand All @@ -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
)

Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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."""
Expand All @@ -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
Expand Down

0 comments on commit 8ee07fe

Please sign in to comment.