Skip to content

Commit

Permalink
to_date is back
Browse files Browse the repository at this point in the history
  • Loading branch information
bruxy70 committed Feb 13, 2022
1 parent a654d07 commit 7be48ba
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions custom_components/garbage_collection/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import asyncio
import logging
from datetime import date, datetime, time, timedelta
from typing import List, Optional
from typing import Any, List, Optional

import homeassistant.util.dt as dt_util
from dateutil.parser import ParserError, parse
Expand Down Expand Up @@ -57,6 +57,20 @@ def nth_weekday_date(
)


def to_date(day: Any) -> date:
"""Convert datetime or text to date, if not already datetime.
Used for the first date for every_n_days (configured as text)
"""
if day is None:
raise ValueError
if isinstance(day, date):
return day
if isinstance(day, datetime):
return day.date()
return date.fromisoformat(day)


def parse_datetime(text: str) -> Optional[datetime]:
"""Parse text to datetime object."""
try:
Expand Down Expand Up @@ -114,7 +128,9 @@ def __init__(self, config_entry: ConfigEntry):
self._period = config.get(const.CONF_PERIOD)
self._first_week = config.get(const.CONF_FIRST_WEEK)
try:
self._first_date: Optional[date] = config.get(const.CONF_FIRST_DATE)
self._first_date: Optional[date] = to_date(
config.get(const.CONF_FIRST_DATE)
)
except ValueError:
self._first_date = None
self._collection_dates: List[date] = []
Expand Down

0 comments on commit 7be48ba

Please sign in to comment.