Skip to content

Instantly share code, notes, and snippets.

@sam2332
Created April 30, 2024 14:58
Show Gist options
  • Save sam2332/6e8cd92470d5f9e032bca5152f1e9087 to your computer and use it in GitHub Desktop.
Save sam2332/6e8cd92470d5f9e032bca5152f1e9087 to your computer and use it in GitHub Desktop.
pyscript task reset system
import logging
def get_list(entity_id):
"""Retrieve the list of to-do items from a custom 'todo' integration."""
return hass.services.async_call("todo", "get_items",
{
"entity_id": entity_id,
},
blocking=True,
return_response=True
)
def set_status(entity_id, item, status):
"""Update the status of a to-do item in the custom 'todo' integration."""
hass.services.async_call("todo", "update_item",
{
"entity_id": entity_id,
"item": item,
"status": status
},
blocking=True
)
def reset_list(entity_id):
logging.info("Resetting tasks in '%s'",entity_id)
todolist = get_list(entity_id=entity_id)
# Ensure todolist is properly parsed and has items
if todolist and entity_id in todolist:
items = todolist[entity_id].get('items', [])
for item in items:
if item.get('status') == 'completed':
set_status(entity_id, item["summary"], 'needs_action')
logging.info("Task '%s' reset to 'needs_action'",item['summary'])
@service # Decorate as a service so it can be triggered manually or by other automations
@time_trigger("cron(0 0 * * 1)") # Run every Monday at 00:00
def reset_weekly_quests():
"""Reset all tasks to 'needs_action'."""
reset_list('todo.weekly_quests')
@service # Decorate as a service so it can be triggered manually or by other automations
@time_trigger("cron(0 0 * * 1-5)") # Run every weekday at 00:00
def reset_work_quests():
"""Reset all tasks to 'needs_action'."""
reset_list('todo.work_quests')
@service # Decorate as a service so it can be triggered manually or by other automations
@time_trigger("cron(0 0 * * * *)") # Run every day at 00:00
def reset_daily_quests():
"""Reset all tasks to 'needs_action'."""
reset_list('todo.daily_quests')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment