From 5954ca2b1f21e2e32454a7033cb5fddb066cb579 Mon Sep 17 00:00:00 2001 From: Kai Bepperling Date: Fri, 21 Jan 2022 11:13:16 +0100 Subject: [PATCH] Add ToDoist Assignee option for new task service (#63918) Co-authored-by: Aaron Godfrey --- homeassistant/components/todoist/calendar.py | 18 ++++++ homeassistant/components/todoist/const.py | 6 ++ .../components/todoist/services.yaml | 62 ++++++++++--------- 3 files changed, 58 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/todoist/calendar.py b/homeassistant/components/todoist/calendar.py index fd6f1996a4d5..560ad3efe1f0 100644 --- a/homeassistant/components/todoist/calendar.py +++ b/homeassistant/components/todoist/calendar.py @@ -19,7 +19,9 @@ from homeassistant.util import dt from .const import ( ALL_DAY, ALL_TASKS, + ASSIGNEE, CHECKED, + COLLABORATORS, COMPLETED, CONF_EXTRA_PROJECTS, CONF_PROJECT_DUE_DATE, @@ -36,6 +38,7 @@ from .const import ( DUE_DATE_VALID_LANGS, DUE_TODAY, END, + FULL_NAME, ID, LABELS, NAME, @@ -60,6 +63,7 @@ NEW_TASK_SERVICE_SCHEMA = vol.Schema( vol.Required(CONTENT): cv.string, vol.Optional(PROJECT_NAME, default="inbox"): vol.All(cv.string, vol.Lower), vol.Optional(LABELS): cv.ensure_list_csv, + vol.Optional(ASSIGNEE): cv.string, vol.Optional(PRIORITY): vol.All(vol.Coerce(int), vol.Range(min=1, max=4)), vol.Exclusive(DUE_DATE_STRING, "due_date"): cv.string, vol.Optional(DUE_DATE_LANG): vol.All(cv.string, vol.In(DUE_DATE_VALID_LANGS)), @@ -112,6 +116,7 @@ def setup_platform( # Look up IDs based on (lowercase) names. project_id_lookup = {} label_id_lookup = {} + collaborator_id_lookup = {} api = TodoistAPI(token) api.sync() @@ -120,6 +125,7 @@ def setup_platform( # Grab all projects. projects = api.state[PROJECTS] + collaborators = api.state[COLLABORATORS] # Grab all labels labels = api.state[LABELS] @@ -137,6 +143,9 @@ def setup_platform( for label in labels: label_id_lookup[label[NAME].lower()] = label[ID] + for collaborator in collaborators: + collaborator_id_lookup[collaborator[FULL_NAME].lower()] = collaborator[ID] + # Check config for more projects. extra_projects = config[CONF_EXTRA_PROJECTS] for project in extra_projects: @@ -182,6 +191,15 @@ def setup_platform( label_ids = [label_id_lookup[label.lower()] for label in task_labels] item.update(labels=label_ids) + if ASSIGNEE in call.data: + task_assignee = call.data[ASSIGNEE].lower() + if task_assignee in collaborator_id_lookup: + item.update(responsible_uid=collaborator_id_lookup[task_assignee]) + else: + raise ValueError( + f"User is not part of the shared project. user: {task_assignee}" + ) + if PRIORITY in call.data: item.update(priority=call.data[PRIORITY]) diff --git a/homeassistant/components/todoist/const.py b/homeassistant/components/todoist/const.py index c83567981af6..26108128ca03 100644 --- a/homeassistant/components/todoist/const.py +++ b/homeassistant/components/todoist/const.py @@ -61,6 +61,8 @@ ID = "id" LABELS = "labels" # Todoist API: "Name" value NAME = "name" +# Todoist API: "Full Name" value +FULL_NAME = "full_name" # Attribute: Is this task overdue? OVERDUE = "overdue" # Attribute: What is this task's priority? @@ -79,6 +81,10 @@ START = "start" SUMMARY = "summary" # Todoist API: Fetch all Tasks TASKS = "items" +# Todoist API: "responsible" for a Task +ASSIGNEE = "assignee" +# Todoist API: Collaborators in shared projects +COLLABORATORS = "collaborators" DOMAIN = "todoist" diff --git a/homeassistant/components/todoist/services.yaml b/homeassistant/components/todoist/services.yaml index d0b680375f9c..3cab4d2bf673 100644 --- a/homeassistant/components/todoist/services.yaml +++ b/homeassistant/components/todoist/services.yaml @@ -22,6 +22,12 @@ new_task: example: Chores,Delivieries selector: text: + assignee: + name: Assignee + description: A members username of a shared project to assign this task to. + example: username + selector: + text: priority: name: Priority description: The priority of this task, from 1 (normal) to 4 (urgent). @@ -41,20 +47,20 @@ new_task: selector: select: options: - - 'da' - - 'de' - - 'en' - - 'es' - - 'fr' - - 'it' - - 'ja' - - 'ko' - - 'nl' - - 'pl' - - 'pt' - - 'ru' - - 'sv' - - 'zh' + - "da" + - "de" + - "en" + - "es" + - "fr" + - "it" + - "ja" + - "ko" + - "nl" + - "pl" + - "pt" + - "ru" + - "sv" + - "zh" due_date: name: Due date description: The time this task is due, in format YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS, in UTC timezone. @@ -73,20 +79,20 @@ new_task: selector: select: options: - - 'da' - - 'de' - - 'en' - - 'es' - - 'fr' - - 'it' - - 'ja' - - 'ko' - - 'nl' - - 'pl' - - 'pt' - - 'ru' - - 'sv' - - 'zh' + - "da" + - "de" + - "en" + - "es" + - "fr" + - "it" + - "ja" + - "ko" + - "nl" + - "pl" + - "pt" + - "ru" + - "sv" + - "zh" reminder_date: name: Reminder date description: When should user be reminded of this task, in format YYYY-MM-DDTHH:MM:SS, in UTC timezone.