hass-core/homeassistant/components/habitica/util.py
Mr. Bubbles 751935539a
Migrate Habitica Dailies and To-Do's to the todo platform (#116655)
* Add todo platform

* update for DataUpdateCoordinator

* set lastCron as dailies due date

* parse alternative duedate format

* fix tests

* send notification on item drop

* fix drop message

* update exception messages

* Simplified the update of user_fields by using set union

* move userFields to const

* Issue deprecation only if entity is acutally used

* Resolve issues

* user entity registry to get entity_id

* raise ServiceValidationError

* requested changes

* Move next_due_date helper function to util.py module

* some changes

* Move function to util.py
2024-07-07 17:50:27 +02:00

42 lines
1.4 KiB
Python

"""Utility functions for Habitica."""
from __future__ import annotations
import datetime
from typing import Any
from homeassistant.components.automation import automations_with_entity
from homeassistant.components.script import scripts_with_entity
from homeassistant.core import HomeAssistant
from homeassistant.util import dt as dt_util
def next_due_date(task: dict[str, Any], last_cron: str) -> datetime.date | None:
"""Calculate due date for dailies and yesterdailies."""
if task["isDue"] and not task["completed"]:
return dt_util.as_local(datetime.datetime.fromisoformat(last_cron)).date()
try:
return dt_util.as_local(
datetime.datetime.fromisoformat(task["nextDue"][0])
).date()
except ValueError:
# sometimes nextDue dates are in this format instead of iso:
# "Mon May 06 2024 00:00:00 GMT+0200"
try:
return dt_util.as_local(
datetime.datetime.strptime(
task["nextDue"][0], "%a %b %d %Y %H:%M:%S %Z%z"
)
).date()
except ValueError:
return None
except IndexError:
return None
def entity_used_in(hass: HomeAssistant, entity_id: str) -> list[str]:
"""Get list of related automations and scripts."""
used_in = automations_with_entity(hass, entity_id)
used_in += scripts_with_entity(hass, entity_id)
return used_in