From 10cd58e3dbcdcb34d12aa04e1fb4d224ea30891a Mon Sep 17 00:00:00 2001 From: 0xWallSecurity <48309682+WallSecurity@users.noreply.github.com> Date: Thu, 29 Feb 2024 08:56:27 +0100 Subject: [PATCH 1/4] trying to fix chores incorrectly becoming overdue at midnight --- custom_components/chore_helper/__init__.py | 2 +- custom_components/chore_helper/chore.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/custom_components/chore_helper/__init__.py b/custom_components/chore_helper/__init__.py index 875d1d6..05c90c2 100644 --- a/custom_components/chore_helper/__init__.py +++ b/custom_components/chore_helper/__init__.py @@ -102,7 +102,7 @@ OFFSET_DATE_SCHEMA = vol.Schema( { vol.Required(CONF_ENTITY_ID): vol.All(cv.ensure_list, [cv.string]), - vol.Required(const.CONF_DATE): cv.date, + vol.Optional(const.CONF_DATE): cv.date, vol.Required(const.CONF_OFFSET): vol.All( vol.Coerce(int), vol.Range(min=-31, max=31) ), diff --git a/custom_components/chore_helper/chore.py b/custom_components/chore_helper/chore.py index 8ae1d24..e490510 100644 --- a/custom_components/chore_helper/chore.py +++ b/custom_components/chore_helper/chore.py @@ -338,7 +338,7 @@ def move_to_range(self, day: date) -> date: def chore_schedule(self) -> Generator[date, None, None]: """Get dates within configured date range.""" - start_date: date = self._calculate_start_date() + start_date: date = self._calculate_start_date() # 2024-02-28 for _ in range(int(self._forecast_dates) + 1): try: next_due_date = self._find_candidate_date(start_date) @@ -480,8 +480,11 @@ async def async_update(self) -> None: def update_state(self) -> None: """Pick the first event from chore dates, update attributes.""" LOGGER.debug("(%s) Looking for next chore date", self._attr_name) + # 29.02.2024 00:07 self._last_updated = helpers.now() + # 29.02.2024 today = self._last_updated.date() + # 06.03.2024 self._next_due_date = self.get_next_due_date(self._calculate_start_date()) if self._next_due_date is not None: LOGGER.debug( @@ -580,10 +583,11 @@ def _calculate_start_date(self) -> date: def _calculate_schedule_start_date(self) -> date: """Calculate start date for scheduling offsets.""" - after = self._frequency[:6] == "after-" + # after = self._frequency[:6] == "after-" start_date = self._start_date - if after and self.last_completed is not None: + # if after and self.last_completed is not None: + if self.last_completed is not None: earliest_date = self._add_period_offset(self.last_completed.date()) if earliest_date > start_date: From ddd94141899e19f38c605daa7057976eb7bb5532 Mon Sep 17 00:00:00 2001 From: 0xWallSecurity <48309682+WallSecurity@users.noreply.github.com> Date: Thu, 29 Feb 2024 10:45:05 +0100 Subject: [PATCH 2/4] new due date fix, testing --- custom_components/chore_helper/chore.py | 16 +++++++++------- custom_components/chore_helper/chore_weekly.py | 16 ++++++++-------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/custom_components/chore_helper/chore.py b/custom_components/chore_helper/chore.py index e490510..46bec33 100644 --- a/custom_components/chore_helper/chore.py +++ b/custom_components/chore_helper/chore.py @@ -338,7 +338,7 @@ def move_to_range(self, day: date) -> date: def chore_schedule(self) -> Generator[date, None, None]: """Get dates within configured date range.""" - start_date: date = self._calculate_start_date() # 2024-02-28 + start_date: date = self._calculate_start_date() for _ in range(int(self._forecast_dates) + 1): try: next_due_date = self._find_candidate_date(start_date) @@ -480,11 +480,8 @@ async def async_update(self) -> None: def update_state(self) -> None: """Pick the first event from chore dates, update attributes.""" LOGGER.debug("(%s) Looking for next chore date", self._attr_name) - # 29.02.2024 00:07 self._last_updated = helpers.now() - # 29.02.2024 today = self._last_updated.date() - # 06.03.2024 self._next_due_date = self.get_next_due_date(self._calculate_start_date()) if self._next_due_date is not None: LOGGER.debug( @@ -553,6 +550,12 @@ def calculate_day1(self, day1: date, schedule_start_date: date) -> date: if schedule_start_date > day1: day1 = schedule_start_date today = helpers.now().date() + if ( + day1 == start_date + and self.last_completed is not None + and self.last_completed.date() == day1 + ): + day1 = day1 + relativedelta(days=1) if ( day1 == today and self.last_completed is not None @@ -583,11 +586,10 @@ def _calculate_start_date(self) -> date: def _calculate_schedule_start_date(self) -> date: """Calculate start date for scheduling offsets.""" - # after = self._frequency[:6] == "after-" + after = self._frequency[:6] == "after-" start_date = self._start_date - # if after and self.last_completed is not None: - if self.last_completed is not None: + if after and self.last_completed is not None: earliest_date = self._add_period_offset(self.last_completed.date()) if earliest_date > start_date: diff --git a/custom_components/chore_helper/chore_weekly.py b/custom_components/chore_helper/chore_weekly.py index af72336..904cbe2 100644 --- a/custom_components/chore_helper/chore_weekly.py +++ b/custom_components/chore_helper/chore_weekly.py @@ -30,19 +30,19 @@ def __init__(self, config_entry: ConfigEntry) -> None: def _add_period_offset(self, start_date: date) -> date: return start_date + relativedelta(weeks=self._period) - def _find_candidate_date(self, day1: date) -> date | None: + def _find_candidate_date(self, day1: date) -> date | None: # 2024-02-26 """Calculate possible date, for weekly frequency.""" - start_date = self._calculate_schedule_start_date() - start_week = start_date.isocalendar()[1] - day1 = self.calculate_day1(day1, start_date) - week = day1.isocalendar()[1] - weekday = day1.weekday() + start_date = self._calculate_schedule_start_date() # 2024-02-27 + start_week = start_date.isocalendar()[1] # 10 + day1 = self.calculate_day1(day1, start_date) # 27 + week = day1.isocalendar()[1] # 10 + weekday = day1.weekday() # 1 offset = -1 if (week - start_week) % self._period == 0: # Chore this week if self._chore_day is not None: - day_index = WEEKDAYS.index(self._chore_day) + day_index = WEEKDAYS.index(self._chore_day) # 6 if day_index >= weekday: # Chore still did not happen - offset = day_index - weekday + offset = day_index - weekday # 5 iterate_by_week = 7 - weekday + WEEKDAYS.index(self._chore_day) while offset == -1: # look in following weeks candidate = day1 + relativedelta(days=iterate_by_week) From 81e9f039174db0ab3f2f7295a648a8c6e677c5ab Mon Sep 17 00:00:00 2001 From: Wallexus <167881799+Wallexus@users.noreply.github.com> Date: Mon, 15 Jul 2024 05:09:17 +0200 Subject: [PATCH 3/4] Update chore_weekly.py Signed-off-by: Wallexus <167881799+Wallexus@users.noreply.github.com> --- custom_components/chore_helper/chore_weekly.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/custom_components/chore_helper/chore_weekly.py b/custom_components/chore_helper/chore_weekly.py index 904cbe2..21a8d7f 100644 --- a/custom_components/chore_helper/chore_weekly.py +++ b/custom_components/chore_helper/chore_weekly.py @@ -30,17 +30,17 @@ def __init__(self, config_entry: ConfigEntry) -> None: def _add_period_offset(self, start_date: date) -> date: return start_date + relativedelta(weeks=self._period) - def _find_candidate_date(self, day1: date) -> date | None: # 2024-02-26 + def _find_candidate_date(self, day1: date) -> date | None: """Calculate possible date, for weekly frequency.""" - start_date = self._calculate_schedule_start_date() # 2024-02-27 - start_week = start_date.isocalendar()[1] # 10 - day1 = self.calculate_day1(day1, start_date) # 27 - week = day1.isocalendar()[1] # 10 - weekday = day1.weekday() # 1 + start_date = self._calculate_schedule_start_date() + start_week = start_date.isocalendar()[1] + day1 = self.calculate_day1(day1, start_date) + week = day1.isocalendar()[1] + weekday = day1.weekday() offset = -1 if (week - start_week) % self._period == 0: # Chore this week if self._chore_day is not None: - day_index = WEEKDAYS.index(self._chore_day) # 6 + day_index = WEEKDAYS.index(self._chore_day) if day_index >= weekday: # Chore still did not happen offset = day_index - weekday # 5 iterate_by_week = 7 - weekday + WEEKDAYS.index(self._chore_day) From 4edaa30a595999f88e0de068f4352f04eed9d8cd Mon Sep 17 00:00:00 2001 From: Wallexus <167881799+Wallexus@users.noreply.github.com> Date: Mon, 15 Jul 2024 05:09:43 +0200 Subject: [PATCH 4/4] Update chore_weekly.py Signed-off-by: Wallexus <167881799+Wallexus@users.noreply.github.com> --- custom_components/chore_helper/chore_weekly.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/chore_helper/chore_weekly.py b/custom_components/chore_helper/chore_weekly.py index 21a8d7f..af72336 100644 --- a/custom_components/chore_helper/chore_weekly.py +++ b/custom_components/chore_helper/chore_weekly.py @@ -42,7 +42,7 @@ def _find_candidate_date(self, day1: date) -> date | None: if self._chore_day is not None: day_index = WEEKDAYS.index(self._chore_day) if day_index >= weekday: # Chore still did not happen - offset = day_index - weekday # 5 + offset = day_index - weekday iterate_by_week = 7 - weekday + WEEKDAYS.index(self._chore_day) while offset == -1: # look in following weeks candidate = day1 + relativedelta(days=iterate_by_week)