Skip to content

Commit

Permalink
Add services for update/delete task
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Dec 11, 2022
1 parent 87d8774 commit 7f55096
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 16 deletions.
50 changes: 40 additions & 10 deletions custom_components/o365/classes/taskssensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ATTR_OVERDUE_TASKS,
ATTR_REMINDER,
ATTR_SUBJECT,
ATTR_TASK_ID,
ATTR_TASKS,
CONF_CONFIG_TYPE,
PERM_MINIMUM_TASKS_WRITE,
Expand Down Expand Up @@ -41,14 +42,18 @@ def extra_state_attributes(self):
all_tasks = []
overdue_tasks = []
for item in self.coordinator.data[self.entity_id][ATTR_TASKS]:
task = {ATTR_SUBJECT: item.subject}
task = {ATTR_SUBJECT: item.subject, ATTR_TASK_ID: item.task_id}
if item.body:
task[ATTR_DESCRIPTION] = item.body
if item.due:
due = item.due.date()
task[ATTR_DUE] = due
if due < dt.utcnow().date():
overdue_task = {ATTR_SUBJECT: item.subject, ATTR_DUE: due}
overdue_task = {
ATTR_SUBJECT: item.subject,
ATTR_TASK_ID: item.task_id,
ATTR_DUE: due,
}
if item.is_reminder_on:
overdue_task[ATTR_REMINDER] = item.reminder
overdue_tasks.append(overdue_task)
Expand All @@ -66,27 +71,52 @@ def extra_state_attributes(self):
def new_task(self, subject, description=None, due=None, reminder=None):
"""Create a new task for this task list."""
if not self._validate_permissions():
return
return False

new_task = self.todo.new_task()
self._save_task(new_task, subject, description, due, reminder)
return True

def update_task(
self, task_id, subject=None, description=None, due=None, reminder=None
):
"""Update a task for this task list."""
if not self._validate_permissions():
return False

task = self.todo.get_task(task_id)
self._save_task(task, subject, description, due, reminder)
return True

def delete_task(self, task_id):
"""Delete task for this task list."""
if not self._validate_permissions():
return False

task = self.todo.get_task(task_id)
task.delete()
return True

def _save_task(self, task, subject, description, due, reminder):
# sourcery skip: raise-from-previous-error
new_task = self.todo.new_task(subject=subject)
if subject:
task.subject = subject
if description:
new_task.body = description
task.body = description
if due:
try:
if len(due) > 10:
new_task.due = dt.parse_datetime(due).date()
task.due = dt.parse_datetime(due).date()
else:
new_task.due = dt.parse_date(due)
task.due = dt.parse_date(due)
except ValueError:
error = f"Due date {due} is not in valid format YYYY-MM-DD"
raise vol.Invalid(error) # pylint: disable=raise-missing-from

if reminder:
new_task.reminder = reminder
task.reminder = reminder

new_task.save()
return True
task.save()

def _validate_permissions(self):
permissions = get_permissions(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/o365/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class EventResponse(Enum):
ATTR_DUE = "due"
ATTR_EMAIL = "email"
ATTR_END = "end"
ATTR_ENTITY_ID = "entity_id"
ATTR_ERROR = "error"
ATTR_EVENT_ID = "event_id"
ATTR_EXTERNALREPLY = "external_reply"
Expand All @@ -48,6 +47,7 @@ class EventResponse(Enum):
ATTR_SUBJECT = "subject"
ATTR_SUMMARY = "summary"
ATTR_TASKS = "tasks"
ATTR_TASK_ID = "task_id"
ATTR_TYPE = "type"
ATTR_ZIP_ATTACHMENTS = "zip_attachments"
ATTR_ZIP_NAME = "zip_name"
Expand Down
18 changes: 13 additions & 5 deletions custom_components/o365/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
ATTR_TITLE,
)
from homeassistant.const import CONF_ENABLED, CONF_NAME

from O365.calendar import AttendeeType # pylint: disable=no-name-in-module
from O365.calendar import EventSensitivity # pylint: disable=no-name-in-module
from O365.calendar import EventShowAs # pylint: disable=no-name-in-module
Expand All @@ -23,7 +22,6 @@
ATTR_DUE,
ATTR_EMAIL,
ATTR_END,
ATTR_ENTITY_ID,
ATTR_EVENT_ID,
ATTR_EXTERNALREPLY,
ATTR_INTERNALREPLY,
Expand All @@ -39,6 +37,7 @@
ATTR_SHOW_AS,
ATTR_START,
ATTR_SUBJECT,
ATTR_TASK_ID,
ATTR_TYPE,
ATTR_ZIP_ATTACHMENTS,
ATTR_ZIP_NAME,
Expand Down Expand Up @@ -176,7 +175,6 @@
)

CALENDAR_SERVICE_RESPOND_SCHEMA = {
vol.Required(ATTR_ENTITY_ID): cv.string,
vol.Required(ATTR_EVENT_ID): cv.string,
vol.Required(ATTR_RESPONSE, None): cv.enum(EventResponse),
vol.Optional(ATTR_SEND_RESPONSE, True): bool,
Expand Down Expand Up @@ -205,7 +203,6 @@


CALENDAR_SERVICE_MODIFY_SCHEMA = {
vol.Required(ATTR_ENTITY_ID): cv.string,
vol.Required(ATTR_EVENT_ID): cv.string,
vol.Optional(ATTR_START): cv.datetime,
vol.Optional(ATTR_END): cv.datetime,
Expand All @@ -221,7 +218,6 @@


CALENDAR_SERVICE_REMOVE_SCHEMA = {
vol.Required(ATTR_ENTITY_ID): cv.string,
vol.Required(ATTR_EVENT_ID): cv.string,
}

Expand Down Expand Up @@ -261,6 +257,18 @@
vol.Optional(ATTR_REMINDER): cv.datetime,
}

UPDATE_TASK_SCHEMA = {
vol.Required(ATTR_TASK_ID): cv.string,
vol.Optional(ATTR_SUBJECT): cv.string,
vol.Optional(ATTR_DESCRIPTION): cv.string,
vol.Optional(ATTR_DUE): cv.string,
vol.Optional(ATTR_REMINDER): cv.datetime,
}

DELETE_TASK_SCHEMA = {
vol.Required(ATTR_TASK_ID): cv.string,
}

AUTO_REPLY_ENABLE_SCHEMA = {
vol.Required(ATTR_START): cv.datetime,
vol.Required(ATTR_END): cv.datetime,
Expand Down
12 changes: 12 additions & 0 deletions custom_components/o365/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
from .schema import (
AUTO_REPLY_DISABLE_SCHEMA,
AUTO_REPLY_ENABLE_SCHEMA,
DELETE_TASK_SCHEMA,
NEW_TASK_SCHEMA,
TASK_LIST_SCHEMA,
UPDATE_TASK_SCHEMA,
)
from .utils import (
build_config_file_path,
Expand Down Expand Up @@ -428,6 +430,16 @@ async def _async_setup_task_services(hass, config):
NEW_TASK_SCHEMA,
"new_task",
)
platform.async_register_entity_service(
"update_task",
UPDATE_TASK_SCHEMA,
"update_task",
)
platform.async_register_entity_service(
"delete_task",
DELETE_TASK_SCHEMA,
"delete_task",
)


async def _async_setup_mailbox_services(hass, config):
Expand Down
39 changes: 39 additions & 0 deletions custom_components/o365/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,45 @@ new_task:
description: When a reminder is needed
example: "2023-01-01T12:00:00+0000"

update_task:
name: Update a task/ToDo
description: Update a new task/ToDo
target:
device:
integration: o365
entity:
integration: o365
domain: sensor
fields:
task_id:
description: ID for the task, can be found as an attribute on your task
example: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
subject:
description: The subject of the task
example: Pick up the mail
description:
description: Description of the task
example: Walk to the post box and collect the mail
due:
description: When the task is due by
example: "YYYY-MM-DD"
reminder:
description: When a reminder is needed
example: "2023-01-01T12:00:00+0000"

delete_task:
name: Delete a task/ToDo
description: Delete a new task/ToDo
target:
device:
integration: o365
entity:
integration: o365
domain: sensor
fields:
task_id:
description: ID for the task, can be found as an attribute on your task
example: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

auto_reply_enable:
name: Auto reply enable
Expand Down

0 comments on commit 7f55096

Please sign in to comment.