Use reverse order of poo record

This commit is contained in:
2024-09-09 14:56:23 +02:00
parent 2e9d88c8a8
commit f14b7bf104
3 changed files with 77 additions and 4 deletions

View File

@@ -35,15 +35,18 @@ class PooRecorder:
formatted_date = now.strftime("%Y-%m-%d")
formatted_time = now.strftime("%H:%M")
status.strip()
await self._notion.append_table_row_text(self._table_id, [formatted_date, formatted_time, status, latitude + "," + longitude])
await self._notion.append_table_row_text_after_header(
self._table_id,
[formatted_date, formatted_time, status, latitude + "," + longitude],
)
async def record(self, record_detail: RecordField) -> None:
webhook_id: str = Config.get_env("HOMEASSISTANT_POO_TRIGGER_ID")
self._publish_text(record_detail.status)
# self._publish_text(record_detail.status)
now = datetime.now(tz=datetime.now().astimezone().tzinfo)
self._publish_time(now)
# self._publish_time(now)
await self._note(now, record_detail.status, record_detail.latitude, record_detail.longitude)
await self._homeassistant.trigger_webhook(payload={"status": record_detail.status}, webhook_id=webhook_id)
# await self._homeassistant.trigger_webhook(payload={"status": record_detail.status}, webhook_id=webhook_id)
def _publish_text(self, new_text: str) -> None:
self._mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)

View File

@@ -0,0 +1,62 @@
import asyncio
from dataclasses import dataclass
from pathlib import Path
from src.config import Config
from src.util.notion import NotionAsync
Config.init()
notion = NotionAsync(token=Config.get_env("NOTION_TOKEN"))
current_file_path = Path(__file__).resolve()
current_dir = str(current_file_path.parent)
rows: list[str] = []
@dataclass
class Column:
id: str
date: str
time: str
status: str
location: str
async def reverse_rows() -> None:
header: dict = await notion.get_block_children(block_id=Config.get_env("POO_RECORD_NOTION_TABLE_ID"), page_size=1)
header_id = header["results"][0]["id"]
start_cursor = header_id
rows: list[Column] = []
while start_cursor is not None:
children: dict = await notion.get_block_children(block_id=Config.get_env("POO_RECORD_NOTION_TABLE_ID"), start_cursor=start_cursor)
for entry in children["results"]:
row = Column(
id=entry["id"],
date=entry["table_row"]["cells"][0][0]["plain_text"],
time=entry["table_row"]["cells"][1][0]["plain_text"],
status=entry["table_row"]["cells"][2][0]["plain_text"],
location=entry["table_row"]["cells"][3][0]["plain_text"],
)
rows.append(row)
start_cursor = children["next_cursor"]
rows = rows[1:]
for row in rows:
print("delete block", row.date, row.time)
await notion.delete_block(row.id)
await asyncio.sleep(1)
for row in rows:
print("add block", row.date, row.time)
await notion.append_table_row_text(
table_id=Config.get_env("POO_RECORD_NOTION_TABLE_ID"),
text_list=[row.date, row.time, row.status, row.location],
after=header_id,
)
await asyncio.sleep(1)
asyncio.run(reverse_rows())

View File

@@ -40,6 +40,9 @@ class NotionAsync:
page_size=page_size,
)
async def delete_block(self, block_id: str) -> None:
await self._client.blocks.delete(block_id=block_id)
async def block_is_table(self, block_id: str) -> bool:
block: dict = await self.get_block(block_id=block_id)
return block["type"] == "table"
@@ -48,6 +51,11 @@ class NotionAsync:
table = await self._client.blocks.retrieve(block_id=table_id)
return table["table"]["table_width"]
async def append_table_row_text_after_header(self, table_id: str, text_list: list[str]) -> None:
header: dict = await self.get_block_children(block_id=table_id, page_size=1)
header_id = header["results"][0]["id"]
await self.append_table_row_text(table_id=table_id, text_list=text_list, after=header_id)
async def append_table_row_text(self, table_id: str, text_list: list[str], after: str | None = None) -> None:
cells: list[RichText] = []
for content in text_list: