Drag out mqtt and make a new class

This commit is contained in:
2024-08-07 16:58:06 +02:00
parent bc365ffe7a
commit e1e6e0f7d7
3 changed files with 94 additions and 38 deletions
+15 -33
View File
@@ -1,14 +1,11 @@
from datetime import datetime
from fastapi_mqtt import FastMQTT, MQTTConfig
from src.cloud_util.mqtt import MQTT
from src.recorder.notion_handle import NotionClient
class PooRecorder:
mqtt_config = MQTTConfig(username="mqtt", password="mqtt", reconnect_retries=-1) # noqa: S106
notion = NotionClient()
mqtt = FastMQTT(config=mqtt_config, client_id="poo_recorder")
CONFIG_TOPIC = "homeassistant/text/poo_recorder/config"
AVAILABILITY_TOPIC = "studiotj/poo_recorder/status"
COMMAND_TOPIC = "studiotj/poo_recorder/update_text"
@@ -17,42 +14,27 @@ class PooRecorder:
ONLINE = "online"
OFFLINE = "offline"
def __init__(self) -> None:
print("Initialization.")
async def start(self) -> None:
print("Starting...")
await PooRecorder.mqtt.mqtt_startup()
async def stop(self) -> None:
print("Stopping...")
await PooRecorder.mqtt.mqtt_shutdown()
def __init__(self, mqtt: MQTT) -> None:
print("Poo Recorder Initialization...")
self._mqtt = mqtt
self._mqtt.publish(PooRecorder.CONFIG_TOPIC, PooRecorder.compose_config(), retain=True)
self._mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
async def record(self, status: str) -> None:
PooRecorder.publish_text(status)
self._publish_text(status)
now = datetime.now(tz=datetime.now().astimezone().tzinfo)
PooRecorder.publish_time(now)
self._publish_time(now)
await PooRecorder.notion.note(now, status)
@staticmethod
@mqtt.on_connect()
def on_connect(client, flags, rc, properties) -> None: # noqa: ANN001, ARG004
print("Connected")
config = PooRecorder.compose_config()
PooRecorder.mqtt.publish(PooRecorder.CONFIG_TOPIC, config, retain=True)
PooRecorder.mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
def _publish_text(self, new_text: str) -> None:
self._mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
self._mqtt.publish(PooRecorder.STATE_TOPIC, new_text, retain=True)
@staticmethod
def publish_text(new_text: str) -> None:
PooRecorder.mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
PooRecorder.mqtt.publish(PooRecorder.STATE_TOPIC, new_text, retain=True)
@staticmethod
def publish_time(time: datetime) -> None:
def _publish_time(self, time: datetime) -> None:
formatted_time = time.strftime("%a | %Y-%m-%d | %H:%M")
PooRecorder.mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
self._mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
json_string = {"last_poo": formatted_time}
PooRecorder.mqtt.publish(PooRecorder.JSON_TOPIC, json_string, retain=True)
self._mqtt.publish(PooRecorder.JSON_TOPIC, json_string, retain=True)
@staticmethod
def compose_config() -> dict:
@@ -60,7 +42,7 @@ class PooRecorder:
"device": {
"name": "Dog Poop Recorder",
"model": "poop-recorder-backend",
"sw_version": "1.2",
"sw_version": "1.3",
"identifiers": ["poo_recorder"],
"manufacturer": "Studio TJ",
},