Add location in poo record
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
from src.cloud_util.homeassistant import HomeAssistant
|
||||
from src.cloud_util.mqtt import MQTT
|
||||
@@ -26,10 +25,6 @@ async def _lifespan(_app: FastAPI): # noqa: ANN202
|
||||
await mqtt.stop()
|
||||
|
||||
|
||||
class PooRecordField(BaseModel):
|
||||
status: str
|
||||
|
||||
|
||||
app = FastAPI(lifespan=_lifespan)
|
||||
|
||||
|
||||
@@ -45,8 +40,8 @@ async def homeassistant_publish(payload: HomeAssistant.Message) -> dict:
|
||||
|
||||
# Poo recorder
|
||||
@app.post("/poo/record")
|
||||
async def record(record_detail: PooRecordField) -> PooRecordField:
|
||||
await poo_recorder.record(record_detail.status)
|
||||
async def record(record_detail: PooRecorder.RecordField) -> PooRecorder.RecordField:
|
||||
await poo_recorder.record(record_detail)
|
||||
return record_detail
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from src.cloud_util.homeassistant import HomeAssistant
|
||||
from src.cloud_util.mqtt import MQTT
|
||||
from src.cloud_util.notion import NotionAsync
|
||||
@@ -15,6 +17,11 @@ class PooRecorder:
|
||||
ONLINE = "online"
|
||||
OFFLINE = "offline"
|
||||
|
||||
class RecordField(BaseModel):
|
||||
status: str
|
||||
latitude: str
|
||||
longitude: str
|
||||
|
||||
def __init__(self, mqtt: MQTT, notion: NotionAsync, homeassistant: HomeAssistant) -> None:
|
||||
print("Poo Recorder Initialization...")
|
||||
self._notion = notion
|
||||
@@ -24,18 +31,18 @@ class PooRecorder:
|
||||
self._mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
|
||||
self._homeassistant = homeassistant
|
||||
|
||||
async def _note(self, now: datetime, status: str) -> None:
|
||||
async def _note(self, now: datetime, status: str, latitude: str, longitude: str) -> None:
|
||||
formatted_date = now.strftime("%Y-%m-%d")
|
||||
formatted_time = now.strftime("%H:%M")
|
||||
await self._notion.append_table_row_text(self._table_id, [formatted_date, formatted_time, status])
|
||||
await self._notion.append_table_row_text(self._table_id, [formatted_date, formatted_time, status, latitude + "," + longitude])
|
||||
|
||||
async def record(self, status: str) -> None:
|
||||
async def record(self, record_detail: RecordField) -> None:
|
||||
webhook_id: str = Config.get_env("HOMEASSISTANT_POO_TRIGGER_ID")
|
||||
self._publish_text(status)
|
||||
self._publish_text(record_detail.status)
|
||||
now = datetime.now(tz=datetime.now().astimezone().tzinfo)
|
||||
self._publish_time(now)
|
||||
await self._note(now, status)
|
||||
await self._homeassistant.trigger_webhook(payload={"status": status}, webhook_id=webhook_id)
|
||||
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)
|
||||
|
||||
def _publish_text(self, new_text: str) -> None:
|
||||
self._mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True)
|
||||
|
||||
Reference in New Issue
Block a user