Add action task
This commit is contained in:
@@ -1,3 +1,8 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import ast
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from src.cloud_util.ticktick import TickTick
|
from src.cloud_util.ticktick import TickTick
|
||||||
@@ -21,11 +26,24 @@ class HomeAssistant:
|
|||||||
|
|
||||||
def _process_ticktick_message(self, message: PublishMessage) -> dict[str, str]:
|
def _process_ticktick_message(self, message: PublishMessage) -> dict[str, str]:
|
||||||
if message.action == "create_shopping_list":
|
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"}
|
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")
|
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)
|
return self._ticktick.create_task(task=task)
|
||||||
|
|||||||
@@ -75,5 +75,6 @@ class TickTick:
|
|||||||
requests.post(ticktick_task_creation_url, headers=header, json=asdict(task), timeout=10)
|
requests.post(ticktick_task_creation_url, headers=header, json=asdict(task), timeout=10)
|
||||||
return {"title": task.title}
|
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"
|
return datetime.strftime("%Y-%m-%dT%H:%M:%S") + "+0000"
|
||||||
|
|||||||
Reference in New Issue
Block a user