Skip to content

Commit

Permalink
switch to relativedelta
Browse files Browse the repository at this point in the history
should close #36 and #34
  • Loading branch information
pinkywafer committed Apr 1, 2020
1 parent 0a1e979 commit 29db75e
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions custom_components/anniversaries/sensor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
""" sensor """

from homeassistant.helpers.entity import Entity
import logging
from dateutil.relativedelta import relativedelta
from datetime import datetime, date, timedelta

import logging
from homeassistant.helpers.entity import Entity
from homeassistant.core import HomeAssistant, State

from homeassistant.const import (
Expand Down Expand Up @@ -50,7 +52,7 @@ def __init__(self, hass, config):
try:
self._date = datetime.strptime(config.get(CONF_DATE), "%Y-%m-%d")
except:
self._date = datetime.strptime(str(date.today().year) + "-" + config.get(CONF_DATE), "%Y-%m-%d")
self._date = datetime.strptime("2020-" + config.get(CONF_DATE), "%Y-%m-%d")
self._unknown_year = True
self._icon_normal = config.get(CONF_ICON_NORMAL)
self._icon_today = config.get(CONF_ICON_TODAY)
Expand All @@ -65,7 +67,7 @@ def __init__(self, hass, config):
self._show_half_anniversary = config.get(CONF_HALF_ANNIVERSARY)
if self._show_half_anniversary:
self._half_days_remaining = 0
self._half_date = self._date - timedelta(days=183)
self._half_date = self._date + relativedelta(months=+6)

@property
def unique_id(self):
Expand Down Expand Up @@ -114,11 +116,11 @@ async def async_update(self):
nextDate = self._date.date()

if today > nextDate:
nextDate = date(today.year, self._date.month, self._date.day)
nextDate = self._date.date() + relativedelta(year=today.year)
if today == nextDate:
years = years + 1
if today > nextDate:
nextDate = date(today.year + 1, self._date.month, self._date.day)
nextDate = self._date.date() + relativedelta(year=today.year + 1)
years = years + 1

daysRemaining = (nextDate - today).days
Expand All @@ -141,8 +143,8 @@ async def async_update(self):
if self._show_half_anniversary:
nextHalfDate = self._half_date.date()
if today > nextHalfDate:
nextHalfDate = date(today.year, self._half_date.month, self._half_date.day)
nextHalfDate = self._half_date.date() + relativedelta(year = today.year)
if today > nextHalfDate:
nextHalfDate = date(today.year + 1, self._half_date.month, self._half_date.day)
nextHalfDate = self._half_date.date() + relativedelta(year = today.year + 1)
self._half_days_remaining = (nextHalfDate - today).days
self._half_date = datetime(nextHalfDate.year, nextHalfDate.month, nextHalfDate.day)

0 comments on commit 29db75e

Please sign in to comment.