Skip to content

Commit

Permalink
break: Task services, service attributes and events renamed to todo
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Dec 13, 2023
1 parent 0358f94 commit d9a2082
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 112 deletions.
18 changes: 9 additions & 9 deletions custom_components/o365/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EventResponse(Enum):


ATTR_ALL_DAY = "all_day"
ATTR_ALL_TASKS = "all_tasks"
ATTR_ALL_TODOS = "all_todos"
ATTR_ATTACHMENTS = "attachments"
ATTR_ATTRIBUTES = "attributes"
ATTR_ATTENDEES = "attendees"
Expand Down Expand Up @@ -42,7 +42,7 @@ class EventResponse(Enum):
ATTR_MEMBERS = "members"
ATTR_MESSAGE_IS_HTML = "message_is_html"
ATTR_OFFSET = "offset_reached"
ATTR_OVERDUE_TASKS = "overdue_tasks"
ATTR_OVERDUE_TODOS = "overdue_todos"
ATTR_PHOTOS = "photos"
ATTR_REMINDER = "reminder"
ATTR_RESPONSE = "response"
Expand All @@ -55,8 +55,8 @@ class EventResponse(Enum):
ATTR_STATE = "state"
ATTR_SUBJECT = "subject"
ATTR_SUMMARY = "summary"
ATTR_TASKS = "tasks"
ATTR_TASK_ID = "task_id"
ATTR_TODOS = "todos"
ATTR_TODO_ID = "todo_id"
ATTR_TOPIC = "topic"
ATTR_TYPE = "type"
ATTR_ZIP_ATTACHMENTS = "zip_attachments"
Expand Down Expand Up @@ -139,11 +139,11 @@ class EventResponse(Enum):
ENTITY_ID_FORMAT_TODO = "todo.{}"

EVENT_HA_EVENT = "ha_event"
EVENT_COMPLETED_TASK = "completed_task"
EVENT_DELETE_TASK = "delete_task"
EVENT_NEW_TASK = "new_task"
EVENT_UNCOMPLETED_TASK = "uncompleted_task"
EVENT_UPDATE_TASK = "update_task"
EVENT_COMPLETED_TODO = "completed_todo"
EVENT_DELETE_TODO = "delete_todo"
EVENT_NEW_TODO = "new_todok"
EVENT_UNCOMPLETED_TODO = "uncompleted_todo"
EVENT_UPDATE_TODO = "update_todo"

EVENT_CREATE_CALENDAR_EVENT = "create_calendar_event"
EVENT_MODIFY_CALENDAR_EVENT = "modify_calendar_event"
Expand Down
22 changes: 6 additions & 16 deletions custom_components/o365/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
ATTR_STATE,
ATTR_SUBJECT,
ATTR_SUMMARY,
ATTR_TASKS,
ATTR_TODOS,
ATTR_TOPIC,
CONF_ACCOUNT,
CONF_ACCOUNT_NAME,
Expand Down Expand Up @@ -162,12 +162,10 @@ async def _async_todo_entities(self, task_lists):
else:
name = tasklist.get(CONF_NAME)
try:
todo = (
await self.hass.async_add_executor_job( # pylint: disable=no-member
ft.partial(
tasks.get_folder,
folder_id=task_list_id,
)
todo = await self.hass.async_add_executor_job( # pylint: disable=no-member
ft.partial(
tasks.get_folder,
folder_id=task_list_id,
)
)
unique_id = f"{task_list_id}_{self._account_name}"
Expand Down Expand Up @@ -306,7 +304,7 @@ async def _async_todos_update(self, key):
if entity_key in self._data:
error = self._data[entity_key][ATTR_ERROR]
else:
self._data[entity_key] = {ATTR_TASKS: {}, ATTR_STATE: 0}
self._data[entity_key] = {ATTR_TODOS: {}, ATTR_STATE: 0}
error = False
data, error = await self._async_todos_update_query(key, error)
if not error:
Expand Down Expand Up @@ -498,14 +496,6 @@ def _build_entity_id(hass, entity_id_format, name):
)


# def _raise_event(hass, event_type, task_id, time_type, task_datetime):
# hass.bus.fire(
# f"{DOMAIN}_{event_type}",
# {ATTR_TASK_ID: task_id, time_type: task_datetime, EVENT_HA_EVENT: False},
# )
# _LOGGER.debug("%s - %s - %s", event_type, task_id, task_datetime)


async def _async_delete_redundant_sensors(ent_reg, unique_id):
if entity_id := ent_reg.async_get_entity_id("sensor", DOMAIN, unique_id):
ent_reg.async_remove(entity_id)
16 changes: 9 additions & 7 deletions custom_components/o365/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
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
from O365.calendar import (
AttendeeType, # pylint: disable=no-name-in-module
EventSensitivity, # pylint: disable=no-name-in-module
EventShowAs, # pylint: disable=no-name-in-module
)
from O365.mailbox import ExternalAudience # pylint: disable=no-name-in-module
from O365.utils import ImportanceLevel # pylint: disable=no-name-in-module

Expand Down Expand Up @@ -43,7 +45,7 @@
ATTR_SHOW_AS,
ATTR_START,
ATTR_SUBJECT,
ATTR_TASK_ID,
ATTR_TODO_ID,
ATTR_TYPE,
ATTR_ZIP_ATTACHMENTS,
ATTR_ZIP_NAME,
Expand Down Expand Up @@ -259,18 +261,18 @@
}

TASK_SERVICE_UPDATE_SCHEMA = {
vol.Required(ATTR_TASK_ID): cv.string,
vol.Required(ATTR_TODO_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,
}

TASK_SERVICE_DELETE_SCHEMA = {
vol.Required(ATTR_TASK_ID): cv.string,
vol.Required(ATTR_TODO_ID): cv.string,
}
TASK_SERVICE_COMPLETE_SCHEMA = {
vol.Required(ATTR_TASK_ID): cv.string,
vol.Required(ATTR_TODO_ID): cv.string,
vol.Required(ATTR_COMPLETED): bool,
}

Expand Down
6 changes: 3 additions & 3 deletions custom_components/o365/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ update_todo:
integration: o365
domain: todo
fields:
task_id:
todo_id:
name: ToDo ID
description: ID for the todo, can be found as an attribute on your todo
example: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Expand Down Expand Up @@ -345,7 +345,7 @@ delete_todo:
integration: o365
domain: todo
fields:
task_id:
todo_id:
name: ToDo ID
description: ID for the todo, can be found as an attribute on your todo
example: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Expand All @@ -363,7 +363,7 @@ complete_todo:
integration: o365
domain: todo
fields:
task_id:
todo_id:
name: Todo ID
description: ID for the todo, can be found as an attribute on your todo
example: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Expand Down
76 changes: 38 additions & 38 deletions custom_components/o365/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

from .classes.entity import O365Entity
from .const import (
ATTR_ALL_TASKS,
ATTR_ALL_TODOS,
ATTR_COMPLETED,
ATTR_CREATED,
ATTR_DATA,
ATTR_DESCRIPTION,
ATTR_DUE,
ATTR_OVERDUE_TASKS,
ATTR_OVERDUE_TODOS,
ATTR_REMINDER,
ATTR_SUBJECT,
ATTR_TASK_ID,
ATTR_TODO_ID,
CONF_ACCOUNT,
CONF_ACCOUNT_NAME,
CONF_COORDINATOR_SENSORS,
Expand All @@ -39,12 +39,12 @@
CONF_TRACK_NEW,
DATETIME_FORMAT,
DOMAIN,
EVENT_COMPLETED_TASK,
EVENT_DELETE_TASK,
EVENT_COMPLETED_TODO,
EVENT_DELETE_TODO,
EVENT_HA_EVENT,
EVENT_NEW_TASK,
EVENT_UNCOMPLETED_TASK,
EVENT_UPDATE_TASK,
EVENT_NEW_TODO,
EVENT_UNCOMPLETED_TODO,
EVENT_UPDATE_TODO,
PERM_MINIMUM_TASKS_WRITE,
PERM_TASKS_READWRITE,
TODO_TODO,
Expand Down Expand Up @@ -205,7 +205,7 @@ def _update_status(self, hass):
if task.completed and task.completed > self.task_last_completed:
_raise_event_external(
hass,
EVENT_COMPLETED_TASK,
EVENT_COMPLETED_TODO,
task.task_id,
ATTR_COMPLETED,
task.completed,
Expand All @@ -214,7 +214,7 @@ def _update_status(self, hass):
task_last_completed = task.completed
if task.created and task.created > self.task_last_created:
_raise_event_external(
hass, EVENT_NEW_TASK, task.task_id, ATTR_CREATED, task.created
hass, EVENT_NEW_TODO, task.task_id, ATTR_CREATED, task.created
)
if task.created > task_last_created:
task_last_created = task.created
Expand All @@ -229,7 +229,7 @@ def _update_extra_state_attributes(self, tasks):
all_tasks = []
overdue_tasks = []
for item in tasks:
task = {ATTR_SUBJECT: item.subject, ATTR_TASK_ID: item.task_id}
task = {ATTR_SUBJECT: item.subject, ATTR_TODO_ID: item.task_id}
if item.body:
task[ATTR_DESCRIPTION] = item.body
if self._show_completed:
Expand All @@ -244,7 +244,7 @@ def _update_extra_state_attributes(self, tasks):
if due < dt.utcnow().date():
overdue_task = {
ATTR_SUBJECT: item.subject,
ATTR_TASK_ID: item.task_id,
ATTR_TODO_ID: item.task_id,
ATTR_DUE: due,
}
if item.is_reminder_on:
Expand All @@ -256,9 +256,9 @@ def _update_extra_state_attributes(self, tasks):

all_tasks.append(task)

extra_attributes = {ATTR_ALL_TASKS: all_tasks}
extra_attributes = {ATTR_ALL_TODOS: all_tasks}
if overdue_tasks:
extra_attributes[ATTR_OVERDUE_TASKS] = overdue_tasks
extra_attributes[ATTR_OVERDUE_TODOS] = overdue_tasks
return extra_attributes

async def async_create_todo_item(self, item: TodoItem) -> None:
Expand All @@ -274,7 +274,7 @@ async def async_new_todo(self, subject, description=None, due=None, reminder=Non

new_task = self.todolist.new_task()
await self._async_save_task(new_task, subject, description, due, reminder)
self._raise_event(EVENT_NEW_TASK, new_task.task_id)
self._raise_event(EVENT_NEW_TODO, new_task.task_id)
self.task_last_created = new_task.created
await self.coordinator.async_refresh()
return True
Expand All @@ -284,7 +284,7 @@ async def async_update_todo_item(self, item: TodoItem) -> None:
task = await self.hass.async_add_executor_job(self.todolist.get_task, item.uid)
if item.summary and item.summary != task.subject:
await self.async_update_todo(
task_id=item.uid, subject=item.summary, task=task
todo_id=item.uid, subject=item.summary, task=task
)
if item.status:
completed = None
Expand All @@ -297,7 +297,7 @@ async def async_update_todo_item(self, item: TodoItem) -> None:

async def async_update_todo(
self,
task_id,
todo_id,
subject=None,
description=None,
due=None,
Expand All @@ -310,66 +310,66 @@ async def async_update_todo(

if not task:
task = await self.hass.async_add_executor_job(
self.todolist.get_task, task_id
self.todolist.get_task, todo_id
)
await self._async_save_task(task, subject, description, due, reminder)
self._raise_event(EVENT_UPDATE_TASK, task_id)
self._raise_event(EVENT_UPDATE_TODO, todo_id)
await self.coordinator.async_refresh()
return True

async def async_delete_todo_items(self, uids: list[str]) -> None:
"""Delete items from the To-do list."""
for task_id in uids:
await self.async_delete_todo(task_id)
for todo_id in uids:
await self.async_delete_todo(todo_id)

async def async_delete_todo(self, task_id):
async def async_delete_todo(self, todo_id):
"""Delete task for this task list."""
if not self._validate_task_permissions():
return False

task = await self.hass.async_add_executor_job(self.todolist.get_task, task_id)
task = await self.hass.async_add_executor_job(self.todolist.get_task, todo_id)
await self.hass.async_add_executor_job(task.delete)
self._raise_event(EVENT_DELETE_TASK, task_id)
self._raise_event(EVENT_DELETE_TODO, todo_id)
await self.coordinator.async_refresh()
return True

async def async_complete_todo(self, task_id, completed, task=None):
async def async_complete_todo(self, todo_id, completed, task=None):
"""Complete task for this task list."""
if not self._validate_task_permissions():
return False

if not task:
task = await self.hass.async_add_executor_job(
self.todolist.get_task, task_id
self.todolist.get_task, todo_id
)
if completed:
await self._async_complete_task(task, task_id)
await self._async_complete_task(task, todo_id)
else:
await self._async_uncomplete_task(task, task_id)
await self._async_uncomplete_task(task, todo_id)

await self.coordinator.async_refresh()
return True

async def _async_complete_task(self, task, task_id):
async def _async_complete_task(self, task, todo_id):
if task.completed:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="todo_completed",
)
task.mark_completed()
self.hass.async_add_executor_job(task.save)
self._raise_event(EVENT_COMPLETED_TASK, task_id)
self._raise_event(EVENT_COMPLETED_TODO, todo_id)
self.task_last_completed = dt.utcnow()

async def _async_uncomplete_task(self, task, task_id):
async def _async_uncomplete_task(self, task, todo_id):
if not task.completed:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="todo_not_completed",
)
task.mark_uncompleted()
self.hass.async_add_executor_job(task.save)
self._raise_event(EVENT_UNCOMPLETED_TASK, task_id)
self._raise_event(EVENT_UNCOMPLETED_TODO, todo_id)

async def _async_save_task(self, task, subject, description, due, reminder):
# sourcery skip: raise-from-previous-error
Expand Down Expand Up @@ -401,12 +401,12 @@ async def _async_save_task(self, task, subject, description, due, reminder):

await self.hass.async_add_executor_job(task.save)

def _raise_event(self, event_type, task_id):
def _raise_event(self, event_type, todo_id):
self.hass.bus.fire(
f"{DOMAIN}_{event_type}",
{ATTR_TASK_ID: task_id, EVENT_HA_EVENT: True},
{ATTR_TODO_ID: todo_id, EVENT_HA_EVENT: True},
)
_LOGGER.debug("%s - %s", event_type, task_id)
_LOGGER.debug("%s - %s", event_type, todo_id)

def _validate_task_permissions(self):
return self._validate_permissions(
Expand All @@ -415,12 +415,12 @@ def _validate_task_permissions(self):
)


def _raise_event_external(hass, event_type, task_id, time_type, task_datetime):
def _raise_event_external(hass, event_type, todo_id, time_type, task_datetime):
hass.bus.fire(
f"{DOMAIN}_{event_type}",
{ATTR_TASK_ID: task_id, time_type: task_datetime, EVENT_HA_EVENT: False},
{ATTR_TODO_ID: todo_id, time_type: task_datetime, EVENT_HA_EVENT: False},
)
_LOGGER.debug("%s - %s - %s", event_type, task_id, task_datetime)
_LOGGER.debug("%s - %s - %s", event_type, todo_id, task_datetime)


def build_todo_query(key, todo):
Expand Down
Loading

0 comments on commit d9a2082

Please sign in to comment.