Make notion use config get env
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING, ClassVar
|
from typing import TYPE_CHECKING, ClassVar
|
||||||
|
|
||||||
from dotenv import dotenv_values, load_dotenv, set_key, unset_key
|
from dotenv import dotenv_values, set_key, unset_key
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
@@ -13,15 +12,11 @@ config_path = Path(__file__).parent.resolve()
|
|||||||
DOT_ENV_PATH = Path(config_path, ".env")
|
DOT_ENV_PATH = Path(config_path, ".env")
|
||||||
DOT_ENV_PATH.touch(mode=0o600, exist_ok=True)
|
DOT_ENV_PATH.touch(mode=0o600, exist_ok=True)
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
VERSION = "1.5"
|
|
||||||
NOTION_TOKEN = os.getenv("NOTION_TOKEN")
|
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
env_dict: ClassVar[OrderedDict[str, str]] = {}
|
env_dict: ClassVar[OrderedDict[str, str]] = {}
|
||||||
dot_env_path = DOT_ENV_PATH
|
dot_env_path = DOT_ENV_PATH
|
||||||
|
VERSION = "1.5"
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def init(dotenv_path: str = DOT_ENV_PATH) -> None:
|
def init(dotenv_path: str = DOT_ENV_PATH) -> None:
|
||||||
|
|||||||
@@ -4,8 +4,11 @@ from fastapi import FastAPI
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from src.cloud_util.mqtt import MQTT
|
from src.cloud_util.mqtt import MQTT
|
||||||
|
from src.config import Config
|
||||||
from src.recorder.poo import PooRecorder
|
from src.recorder.poo import PooRecorder
|
||||||
|
|
||||||
|
Config.init()
|
||||||
|
|
||||||
mqtt = MQTT()
|
mqtt = MQTT()
|
||||||
poo_recorder = PooRecorder(mqtt)
|
poo_recorder = PooRecorder(mqtt)
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ from datetime import datetime
|
|||||||
|
|
||||||
from notion_client import AsyncClient as Client
|
from notion_client import AsyncClient as Client
|
||||||
|
|
||||||
from src.config import NOTION_TOKEN
|
from src.config import Config
|
||||||
|
|
||||||
|
|
||||||
class NotionClient:
|
class NotionClient:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self._notion = Client(auth=NOTION_TOKEN)
|
self._notion = Client(auth=Config.get_env("NOTION_TOKEN"))
|
||||||
self._page_id = "3cf594afd0754497ba0a93b94912b897"
|
self._page_id = "3cf594afd0754497ba0a93b94912b897"
|
||||||
self._table_id = "9828b56c53de46c794673fe1d01ad522"
|
self._table_id = "9828b56c53de46c794673fe1d01ad522"
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from src.cloud_util.mqtt import MQTT
|
from src.cloud_util.mqtt import MQTT
|
||||||
|
from src.config import Config
|
||||||
from src.recorder.notion_handle import NotionClient
|
from src.recorder.notion_handle import NotionClient
|
||||||
|
|
||||||
|
|
||||||
class PooRecorder:
|
class PooRecorder:
|
||||||
notion = NotionClient()
|
|
||||||
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"
|
||||||
COMMAND_TOPIC = "studiotj/poo_recorder/update_text"
|
COMMAND_TOPIC = "studiotj/poo_recorder/update_text"
|
||||||
@@ -16,6 +16,7 @@ class PooRecorder:
|
|||||||
|
|
||||||
def __init__(self, mqtt: MQTT) -> None:
|
def __init__(self, mqtt: MQTT) -> None:
|
||||||
print("Poo Recorder Initialization...")
|
print("Poo Recorder Initialization...")
|
||||||
|
self._notion = NotionClient()
|
||||||
self._mqtt = mqtt
|
self._mqtt = mqtt
|
||||||
self._mqtt.publish(PooRecorder.CONFIG_TOPIC, PooRecorder.compose_config(), retain=True)
|
self._mqtt.publish(PooRecorder.CONFIG_TOPIC, PooRecorder.compose_config(), retain=True)
|
||||||
self._mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
|
self._mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
|
||||||
@@ -24,7 +25,7 @@ class PooRecorder:
|
|||||||
self._publish_text(status)
|
self._publish_text(status)
|
||||||
now = datetime.now(tz=datetime.now().astimezone().tzinfo)
|
now = datetime.now(tz=datetime.now().astimezone().tzinfo)
|
||||||
self._publish_time(now)
|
self._publish_time(now)
|
||||||
await PooRecorder.notion.note(now, status)
|
await self._notion.note(now, status)
|
||||||
|
|
||||||
def _publish_text(self, new_text: str) -> None:
|
def _publish_text(self, new_text: str) -> None:
|
||||||
self._mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
|
self._mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
|
||||||
@@ -42,7 +43,7 @@ class PooRecorder:
|
|||||||
"device": {
|
"device": {
|
||||||
"name": "Dog Poop Recorder",
|
"name": "Dog Poop Recorder",
|
||||||
"model": "poop-recorder-backend",
|
"model": "poop-recorder-backend",
|
||||||
"sw_version": "1.3",
|
"sw_version": Config.VERSION,
|
||||||
"identifiers": ["poo_recorder"],
|
"identifiers": ["poo_recorder"],
|
||||||
"manufacturer": "Studio TJ",
|
"manufacturer": "Studio TJ",
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user