Skip to content

Commit

Permalink
#9 Fixed dates and response in services for creating tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Przemysław Szafrański authored and Hantick committed Jan 15, 2025
1 parent 6e1ab58 commit d85b55a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion custom_components/ticktick/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"ssdp": [],
"zeroconf": [],
"integration_type": "service",
"version": "2.0.3"
"version": "2.0.4"
}
25 changes: 23 additions & 2 deletions custom_components/ticktick/service_handlers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Service Hanlders for TickTick Integration."""

from collections.abc import Awaitable, Callable
from datetime import datetime
from typing import Any, TypeVar
from zoneinfo import ZoneInfo

from custom_components.ticktick.ticktick_api_python.models.task import Task

from homeassistant.core import ServiceCall
from homeassistant.util import dt as dt_util

from .const import PROJECT_ID, TASK_ID
from .ticktick_api_python.ticktick_api import TickTickAPIClient
Expand Down Expand Up @@ -54,9 +57,15 @@ async def handler(call: ServiceCall) -> dict[str, Any]:
args = {arg: call.data.get(arg) for arg in arg_names}
try:
response = None
if type:
if type == Task:
if "dueDate" in args and isinstance(args["dueDate"], str):
args["dueDate"] = _sanitize_date(args["dueDate"], args["timeZone"])
if "startDate" in args and isinstance(args["startDate"], str):
args["startDate"] = _sanitize_date(
args["startDate"], args["timeZone"]
)
instance = type(**args)
response = await client_method(instance)
response = await client_method(instance, returnAsJson=True)
else:
response = await client_method(**args, returnAsJson=True)

Expand All @@ -65,3 +74,15 @@ async def handler(call: ServiceCall) -> dict[str, Any]:
return {"error": str(e)}

return handler


def _sanitize_date(date: str, timeZone: str | None) -> datetime:
naive_dt = datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
if timeZone:
zone_info = ZoneInfo(timeZone)
else:
zone_info = dt_util.get_default_time_zone()

aware_dt = naive_dt.replace(tzinfo=zone_info)

return aware_dt.strftime("%Y-%m-%dT%H:%M:%S%z")
16 changes: 8 additions & 8 deletions custom_components/ticktick/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,32 @@ create_task:
description: "Description of the checklist."
selector:
text: null
is_all_day:
isAllDay:
name: Lasts all day
description: "Flag indicating Whether the task is all day."
default: false
selector:
boolean: null
start_date:
startDate:
name: Start Date
description: "Start date and time in 'yyyy-MM-dd'T'HH:mm:ssZ' format."
example: 2024-12-29T03:00:00+0000
selector:
datetime: {}
due_date:
dueDate:
name: Due Date
description: "Due date and time in 'yyyy-MM-dd'T'HH:mm:ssZ' format."
example: 2025-01-10T03:00:00+0000
selector:
datetime: {}

time_zone:
timeZone:
name: Time Zone
description: "The time zone in which the time is specified."
selector:
text: null

repeat_flag:
repeatFlag:
name: Repeat Flag
description: "Recurring rules of the task."
selector:
Expand All @@ -82,7 +82,7 @@ create_task:
selector:
number:
min: 0
sort_order:
sortOrder:
name: Sort Order
description: "The order of the task."
selector:
Expand Down Expand Up @@ -164,13 +164,13 @@ create_project:
example: #F18181
selector:
text: null
sort_order:
sortOrder:
name: Sort Order
description: "Sort order value of the project."
selector:
number:
min: 0
view_mode:
viewMode:
name: View Mode
description: "View mode of the project."
selector:
Expand Down
10 changes: 8 additions & 2 deletions custom_components/ticktick/ticktick_api_python/ticktick_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,22 @@ async def get_task(

return Task.from_dict(response)

async def create_task(self, task: Task) -> Task:
async def create_task(self, task: Task, returnAsJson: bool = False) -> Task:
"""Create a task."""
json = task.toJSON()
response = await self._post(CREATE_TASK, json)
if returnAsJson:
return response

return Task.from_dict(response)

async def update_task(self, task: Task) -> Task:
async def update_task(self, task: Task, returnAsJson: bool = False) -> Task:
"""Update a task."""
json = task.toJSON()
response = await self._post(UPDATE_TASK.format(taskId=task.id), json)
if returnAsJson:
return response

return Task.from_dict(response)

async def complete_task(self, projectId: str, taskId: str) -> str:
Expand Down

0 comments on commit d85b55a

Please sign in to comment.