From 4e1436f67f72293809590a70fc7c54c34aa01e87 Mon Sep 17 00:00:00 2001 From: Juggels Date: Fri, 16 Mar 2018 08:13:38 +0100 Subject: [PATCH 1/2] Allow use of date_string in service call --- .../components/calendar/services.yaml | 43 +++++++++++-------- homeassistant/components/calendar/todoist.py | 17 +++++++- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/calendar/services.yaml b/homeassistant/components/calendar/services.yaml index 61ff4345fbec30..2aeb8f22486eed 100644 --- a/homeassistant/components/calendar/services.yaml +++ b/homeassistant/components/calendar/services.yaml @@ -1,21 +1,26 @@ # Describes the format for available calendar services -todoist: - new_task: - description: Create a new task and add it to a project. - fields: - content: - description: The name of the task (Required). - example: Pick up the mail - project: - description: The name of the project this task should belong to. Defaults to Inbox (Optional). - example: Errands - labels: - description: Any labels that you want to apply to this task, separated by a comma (Optional). - example: Chores,Deliveries - priority: - description: The priority of this task, from 1 (normal) to 4 (urgent) (Optional). - example: 2 - due_date: - description: The day this task is due, in format YYYY-MM-DD (Optional). - example: "2018-04-01" +todoist_new_task: + description: Create a new task and add it to a project. + fields: + content: + description: The name of the task (Required). + example: Pick up the mail + project: + description: The name of the project this task should belong to. Defaults to Inbox (Optional). + example: Errands + labels: + description: Any labels that you want to apply to this task, separated by a comma (Optional). + example: Chores,Deliveries + priority: + description: The priority of this task, from 1 (normal) to 4 (urgent) (Optional). + example: 2 + date_string: + description: The day this task is due, in natural language (Optional). + example: "tomorrow" + date_lang: + description: The langurage of date_string (Optional). + example: "en" + due_date: + description: The day this task is due, in format YYYY-MM-DD (Optional). + example: "2018-04-01" diff --git a/homeassistant/components/calendar/todoist.py b/homeassistant/components/calendar/todoist.py index 02840c7d0ee803..4657eccf5a61a5 100644 --- a/homeassistant/components/calendar/todoist.py +++ b/homeassistant/components/calendar/todoist.py @@ -41,6 +41,10 @@ DESCRIPTION = 'description' # Calendar Platform: Used in the '_get_date()' method DATETIME = 'dateTime' +# Service Call: When is this task due (in natural language)? +DATE_STRING = 'date_string' +# Service Call: The language of DATE_STRING +DATE_LANG = 'date_lang' # Attribute: When is this task due? # Service Call: When is this task due? DUE_DATE = 'due_date' @@ -83,7 +87,9 @@ vol.Optional(PROJECT_NAME, default='inbox'): vol.All(cv.string, vol.Lower), vol.Optional(LABELS): cv.ensure_list_csv, vol.Optional(PRIORITY): vol.All(vol.Coerce(int), vol.Range(min=1, max=4)), - vol.Optional(DUE_DATE): cv.string, + vol.Optional(DATE_STRING): cv.string, + vol.Optional(DATE_LANG): vol.All(cv.string, vol.Length(min=2, max=2)), + vol.Optional(DUE_DATE): cv.string }) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ @@ -186,7 +192,16 @@ def handle_new_task(call): if PRIORITY in call.data: item.update(priority=call.data[PRIORITY]) + if DATE_STRING in call.data: + item.update(date_string=call.data[DATE_STRING]) + + if DATE_LANG in call.data: + item.update(date_lang=call.data[DATE_LANG]) + if DUE_DATE in call.data: + # Make sure only DUE_DATE is set + item.update(date_string='') + due_date = dt.parse_datetime(call.data[DUE_DATE]) if due_date is None: due = dt.parse_date(call.data[DUE_DATE]) From 4e06b70061a4c55c5acbb4205ff5583d0ffae4ef Mon Sep 17 00:00:00 2001 From: Juggels Date: Fri, 16 Mar 2018 19:57:46 +0100 Subject: [PATCH 2/2] Add stricter validation, fix descriptions --- .../components/calendar/services.yaml | 18 ++++++------ homeassistant/components/calendar/todoist.py | 29 ++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/calendar/services.yaml b/homeassistant/components/calendar/services.yaml index 2aeb8f22486eed..ebf0c7b1591ab0 100644 --- a/homeassistant/components/calendar/services.yaml +++ b/homeassistant/components/calendar/services.yaml @@ -4,23 +4,23 @@ todoist_new_task: description: Create a new task and add it to a project. fields: content: - description: The name of the task (Required). + description: The name of the task. example: Pick up the mail project: - description: The name of the project this task should belong to. Defaults to Inbox (Optional). + description: The name of the project this task should belong to. Defaults to Inbox. example: Errands labels: - description: Any labels that you want to apply to this task, separated by a comma (Optional). + description: Any labels that you want to apply to this task, separated by a comma. example: Chores,Deliveries priority: - description: The priority of this task, from 1 (normal) to 4 (urgent) (Optional). + description: The priority of this task, from 1 (normal) to 4 (urgent). example: 2 - date_string: - description: The day this task is due, in natural language (Optional). + due_date_string: + description: The day this task is due, in natural language. example: "tomorrow" - date_lang: - description: The langurage of date_string (Optional). + due_date_lang: + description: The language of due_date_string. example: "en" due_date: - description: The day this task is due, in format YYYY-MM-DD (Optional). + description: The day this task is due, in format YYYY-MM-DD. example: "2018-04-01" diff --git a/homeassistant/components/calendar/todoist.py b/homeassistant/components/calendar/todoist.py index 4657eccf5a61a5..b70e44456db822 100644 --- a/homeassistant/components/calendar/todoist.py +++ b/homeassistant/components/calendar/todoist.py @@ -42,9 +42,13 @@ # Calendar Platform: Used in the '_get_date()' method DATETIME = 'dateTime' # Service Call: When is this task due (in natural language)? -DATE_STRING = 'date_string' -# Service Call: The language of DATE_STRING -DATE_LANG = 'date_lang' +DUE_DATE_STRING = 'due_date_string' +# Service Call: The language of DUE_DATE_STRING +DUE_DATE_LANG = 'due_date_lang' +# Service Call: The available options of DUE_DATE_LANG +DUE_DATE_VALID_LANGS = ['en', 'da', 'pl', 'zh', 'ko', 'de', + 'pt', 'ja', 'it', 'fr', 'sv', 'ru', + 'es', 'nl'] # Attribute: When is this task due? # Service Call: When is this task due? DUE_DATE = 'due_date' @@ -87,9 +91,11 @@ vol.Optional(PROJECT_NAME, default='inbox'): vol.All(cv.string, vol.Lower), vol.Optional(LABELS): cv.ensure_list_csv, vol.Optional(PRIORITY): vol.All(vol.Coerce(int), vol.Range(min=1, max=4)), - vol.Optional(DATE_STRING): cv.string, - vol.Optional(DATE_LANG): vol.All(cv.string, vol.Length(min=2, max=2)), - vol.Optional(DUE_DATE): cv.string + + vol.Exclusive(DUE_DATE_STRING, 'due_date'): cv.string, + vol.Optional(DUE_DATE_LANG): + vol.All(cv.string, vol.In(DUE_DATE_VALID_LANGS)), + vol.Exclusive(DUE_DATE, 'due_date'): cv.string, }) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ @@ -192,16 +198,13 @@ def handle_new_task(call): if PRIORITY in call.data: item.update(priority=call.data[PRIORITY]) - if DATE_STRING in call.data: - item.update(date_string=call.data[DATE_STRING]) + if DUE_DATE_STRING in call.data: + item.update(date_string=call.data[DUE_DATE_STRING]) - if DATE_LANG in call.data: - item.update(date_lang=call.data[DATE_LANG]) + if DUE_DATE_LANG in call.data: + item.update(date_lang=call.data[DUE_DATE_LANG]) if DUE_DATE in call.data: - # Make sure only DUE_DATE is set - item.update(date_string='') - due_date = dt.parse_datetime(call.data[DUE_DATE]) if due_date is None: due = dt.parse_date(call.data[DUE_DATE])