Add notion table
This commit is contained in:
15
.vscode/launch.json
vendored
Normal file
15
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Python Debugger: Current File",
|
||||||
|
"type": "debugpy",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${file}",
|
||||||
|
"console": "integratedTerminal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
33
recorder/notion_handle.py
Normal file
33
recorder/notion_handle.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from notion_client import AsyncClient as Client
|
||||||
|
|
||||||
|
|
||||||
|
class NotionClient:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
load_dotenv()
|
||||||
|
self._notion = Client(auth=os.environ["NOTION_TOKEN"])
|
||||||
|
self._page_id = "3cf594afd0754497ba0a93b94912b897"
|
||||||
|
self._table_id = "9828b56c53de46c794673fe1d01ad522"
|
||||||
|
|
||||||
|
async def note(self, now: datetime, status: str) -> None:
|
||||||
|
formatted_date = now.strftime("%Y-%m-%d")
|
||||||
|
formatted_time = now.strftime("%H:%M")
|
||||||
|
await self._notion.blocks.children.append(
|
||||||
|
block_id=self._table_id,
|
||||||
|
children=[
|
||||||
|
{
|
||||||
|
"object": "block",
|
||||||
|
"type": "table_row",
|
||||||
|
"table_row": {
|
||||||
|
"cells": [
|
||||||
|
[{"type": "text", "text": {"content": formatted_date}}],
|
||||||
|
[{"type": "text", "text": {"content": formatted_time}}],
|
||||||
|
[{"type": "text", "text": {"content": status}}],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
@@ -2,9 +2,12 @@ from datetime import datetime
|
|||||||
|
|
||||||
from fastapi_mqtt import FastMQTT, MQTTConfig
|
from fastapi_mqtt import FastMQTT, MQTTConfig
|
||||||
|
|
||||||
|
from recorder.notion_handle import NotionClient
|
||||||
|
|
||||||
|
|
||||||
class PooRecorder:
|
class PooRecorder:
|
||||||
mqtt_config = MQTTConfig(username="mqtt", password="mqtt", reconnect_retries=-1) # noqa: S106
|
mqtt_config = MQTTConfig(username="mqtt", password="mqtt", reconnect_retries=-1) # noqa: S106
|
||||||
|
notion = NotionClient()
|
||||||
mqtt = FastMQTT(config=mqtt_config, client_id="poo_recorder")
|
mqtt = FastMQTT(config=mqtt_config, client_id="poo_recorder")
|
||||||
CONFIG_TOPIC = "homeassistant/text/poo_recorder/config"
|
CONFIG_TOPIC = "homeassistant/text/poo_recorder/config"
|
||||||
AVAILABILITY_TOPIC = "studiotj/poo_recorder/status"
|
AVAILABILITY_TOPIC = "studiotj/poo_recorder/status"
|
||||||
@@ -29,6 +32,7 @@ class PooRecorder:
|
|||||||
PooRecorder.publish_text(status)
|
PooRecorder.publish_text(status)
|
||||||
now = datetime.now(tz=datetime.now().astimezone().tzinfo)
|
now = datetime.now(tz=datetime.now().astimezone().tzinfo)
|
||||||
PooRecorder.publish_time(now)
|
PooRecorder.publish_time(now)
|
||||||
|
await PooRecorder.notion.note(now, status)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@mqtt.on_connect()
|
@mqtt.on_connect()
|
||||||
|
|||||||
Reference in New Issue
Block a user