mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 09:47:52 +00:00
Fix due date calculation for future dailies in Habitica integration (#126403)
Calculate next due date for dailies with startdate in the future
This commit is contained in:
parent
ccec85f047
commit
36e6ab4af8
@ -3,7 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from typing import Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from homeassistant.components.automation import automations_with_entity
|
from homeassistant.components.automation import automations_with_entity
|
||||||
from homeassistant.components.script import scripts_with_entity
|
from homeassistant.components.script import scripts_with_entity
|
||||||
@ -14,25 +14,44 @@ from homeassistant.util import dt as dt_util
|
|||||||
def next_due_date(task: dict[str, Any], last_cron: str) -> datetime.date | None:
|
def next_due_date(task: dict[str, Any], last_cron: str) -> datetime.date | None:
|
||||||
"""Calculate due date for dailies and yesterdailies."""
|
"""Calculate due date for dailies and yesterdailies."""
|
||||||
|
|
||||||
|
today = to_date(last_cron)
|
||||||
|
startdate = to_date(task["startDate"])
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
assert today
|
||||||
|
assert startdate
|
||||||
|
|
||||||
if task["isDue"] and not task["completed"]:
|
if task["isDue"] and not task["completed"]:
|
||||||
return dt_util.as_local(datetime.datetime.fromisoformat(last_cron)).date()
|
return to_date(last_cron)
|
||||||
|
|
||||||
|
if startdate > today:
|
||||||
|
if task["frequency"] == "daily" or (
|
||||||
|
task["frequency"] in ("monthly", "yearly") and task["daysOfMonth"]
|
||||||
|
):
|
||||||
|
return startdate
|
||||||
|
|
||||||
|
if (
|
||||||
|
task["frequency"] in ("weekly", "monthly")
|
||||||
|
and (nextdue := to_date(task["nextDue"][0]))
|
||||||
|
and startdate > nextdue
|
||||||
|
):
|
||||||
|
return to_date(task["nextDue"][1])
|
||||||
|
|
||||||
|
return to_date(task["nextDue"][0])
|
||||||
|
|
||||||
|
|
||||||
|
def to_date(date: str) -> datetime.date | None:
|
||||||
|
"""Convert an iso date to a datetime.date object."""
|
||||||
try:
|
try:
|
||||||
return dt_util.as_local(
|
return dt_util.as_local(datetime.datetime.fromisoformat(date)).date()
|
||||||
datetime.datetime.fromisoformat(task["nextDue"][0])
|
|
||||||
).date()
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# sometimes nextDue dates are in this format instead of iso:
|
# sometimes nextDue dates are JavaScript datetime strings instead of iso:
|
||||||
# "Mon May 06 2024 00:00:00 GMT+0200"
|
# "Mon May 06 2024 00:00:00 GMT+0200"
|
||||||
try:
|
try:
|
||||||
return dt_util.as_local(
|
return dt_util.as_local(
|
||||||
datetime.datetime.strptime(
|
datetime.datetime.strptime(date, "%a %b %d %Y %H:%M:%S %Z%z")
|
||||||
task["nextDue"][0], "%a %b %d %Y %H:%M:%S %Z%z"
|
|
||||||
)
|
|
||||||
).date()
|
).date()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return None
|
return None
|
||||||
except IndexError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def entity_used_in(hass: HomeAssistant, entity_id: str) -> list[str]:
|
def entity_used_in(hass: HomeAssistant, entity_id: str) -> list[str]:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user