Add action task

This commit is contained in:
2024-08-19 12:05:46 +02:00
parent 298faa6523
commit 94ba832932
2 changed files with 23 additions and 4 deletions

View File

@@ -1,3 +1,8 @@
from __future__ import annotations
import ast
from datetime import datetime, timedelta, timezone
from pydantic import BaseModel
from src.cloud_util.ticktick import TickTick
@@ -21,11 +26,24 @@ class HomeAssistant:
def _process_ticktick_message(self, message: PublishMessage) -> dict[str, str]:
if message.action == "create_shopping_list":
return self._create_shopping_list(item=message.content)
return self._create_shopping_list(content=message.content)
if message.action == "create_action_task":
return self._create_action_task(content=message.content)
return {"Status": "Unknown action"}
def _create_shopping_list(self, item: str) -> dict[str, str]:
def _create_shopping_list(self, content: str) -> dict[str, str]:
project_id = Config.get_env("TICKTICK_SHOPPING_LIST")
task = TickTick.Task(projectId=project_id, title=item)
item: dict[str, str] = ast.literal_eval(content)
task = TickTick.Task(projectId=project_id, title=item["item"])
return self._ticktick.create_task(task=task)
def _create_action_task(self, content: str) -> dict[str, str]:
detail: dict[str, str] = ast.literal_eval(content)
project_id = Config.get_env("TICKTICK_HOME_TASK_LIST")
due_hour = detail["due_hour"]
due = datetime.now(tz=datetime.now().astimezone().tzinfo) + timedelta(hours=due_hour)
due = (due + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
due = due.astimezone(timezone.utc)
task = TickTick.Task(projectId=project_id, title=detail["action"], dueDate=TickTick.datetime_to_ticktick_format(due))
return self._ticktick.create_task(task=task)

View File

@@ -75,5 +75,6 @@ class TickTick:
requests.post(ticktick_task_creation_url, headers=header, json=asdict(task), timeout=10)
return {"title": task.title}
def datetime_to_ticktick_format(self, datetime: datetime) -> str:
@staticmethod
def datetime_to_ticktick_format(datetime: datetime) -> str:
return datetime.strftime("%Y-%m-%dT%H:%M:%S") + "+0000"