RogerSelwyn / MS365-ToDo

Microsoft 365 To Do Integration for Home Assistant
MIT License
9 stars 0 forks source link

Add Functionality for Important and My Day tasks #34

Open mf244 opened 1 week ago

mf244 commented 1 week ago

Hello!

First of all, thank you for all your work on this! I love this integration, but it would be really great if it could pull a list of tasks into home assistant based off of importance or due date.

I have made a proof of concept (mostly ChatGPT) that uses the graph API to pull all tasks and return un-completed important or due today tasks. Please find attached the files that make it happen. You will need to add a credentials.json if you wish to run it yourself.

Pastebin Proof Of Concept main.py: https://pastebin.com/7qVELW5R Pastebin Proof Of Concept auth.py: https://pastebin.com/nckLwEYN

Here is ChatGPT's explanation of how to add these features (not to homeassistant, but i'm sure the code will fit in)

  1. Filter for Important Tasks The Microsoft Graph API provides an importance property for tasks, allowing us to filter tasks with importance: high. Here’s how to add this feature:

Code Addition for Important Tasks In your main function or where you handle task retrieval, add the following code to filter for tasks marked as "Important."

important_tasks = []

for task in tasks:
    # Check if task is marked as Important
    if task.get("importance") == "high":
        important_tasks.append(task)

# Output Important tasks
print("Important Tasks:")
for task in important_tasks:
    print(task["title"])

This snippet creates a list of important tasks by checking if importance is set to "high".

  1. Filter for Tasks Due Today We can use the dueDateTime property to check if a task is due today. This filter retrieves tasks with a due date matching today’s date.

Code Addition for Due Today Tasks First, get today’s date in the correct format. Then, filter tasks by comparing each task’s dueDateTime with today’s date.

from datetime import datetime

due_today_tasks = []
today = datetime.utcnow().date()  # Get today's date in UTC

for task in tasks:
    # Check if task is due today
    due_date_str = task.get("dueDateTime", {}).get("dateTime")
    if due_date_str:
        due_date = datetime.fromisoformat(due_date_str).date()
        if due_date == today:
            due_today_tasks.append(task)

# Output tasks due today
print("Due Today Tasks:")
for task in due_today_tasks:
    print(task["title"])

Summary of Changes Important Tasks: Added a filter based on the importance property to retrieve tasks marked as "Important." Due Today Tasks: Added a filter using dueDateTime to retrieve tasks due today.

I hope this can be added in to the mainline, as it would significantly improve my life. The proof of concept script attached successfully returns the correct tasks, and it just needs to be added as a list into home assistant.

Please let me know if you need anything from me (or ChatGPT). Thank you for all your hard work on this integration!

RogerSelwyn commented 1 week ago

Thanks for the request and suggested code. I suspect ChatGPT is not aware that the integration makes exclusive use of the python-o365 library to access the graph api - https://github.com/O365/python-o365. It might be worth acquainting yourself with that.

I would suspect that all the ToDo information you are looking to filter on is already present in the attributes of the existing todo entity, so you can write a filter within HA to extract what you want. That said I'm not sure if you can create a template todo entity.

At the moment that query pulls everything, whilst you should really only pull what is needed from the api. The library I linked supports the filtering that the graph API supports. So it would be useful to have an example that uses a filter for importance and due date directly against the graph API, rather than post processing it.

I'm buried in other stuff at the moment and away for a couple of weeks, so this won't get looked at until December some time.

mf244 commented 1 week ago

Thanks for the info!

I do not yet possess the skills to just merge anything in, but will definitely start learning how stuff works. I'll rtfm and whatnot and see what I can do.

No rush on anything on your end. It would just be nice to have eventually.

I'll see if I can bang some rocks together until something sparks

Thanks, Matt

RogerSelwyn commented 1 week ago

If you can create the example api queries with the filters that would help substantially just a case of googling examples and then trying queries in ms graph explorer. It’s only what I would have to do.