Basic working, no db
This commit is contained in:
3
main.py
3
main.py
@@ -8,7 +8,7 @@ recorder = PooRecorder()
|
|||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def _lifespan(_app: FastAPI):
|
async def _lifespan(_app: FastAPI): # noqa: ANN202
|
||||||
await recorder.start()
|
await recorder.start()
|
||||||
yield
|
yield
|
||||||
await recorder.stop()
|
await recorder.stop()
|
||||||
@@ -19,4 +19,5 @@ app = FastAPI(lifespan=_lifespan)
|
|||||||
|
|
||||||
@app.put("/record/s={status}")
|
@app.put("/record/s={status}")
|
||||||
async def record(status: str) -> dict:
|
async def record(status: str) -> dict:
|
||||||
|
await recorder.record(status)
|
||||||
return {"status": status}
|
return {"status": status}
|
||||||
|
|||||||
42
poo.py
42
poo.py
@@ -4,8 +4,15 @@ from fastapi_mqtt import FastMQTT, MQTTConfig
|
|||||||
|
|
||||||
|
|
||||||
class PooRecorder:
|
class PooRecorder:
|
||||||
mqtt_config = MQTTConfig(username="mqtt", password="mqtt")
|
mqtt_config = MQTTConfig(username="mqtt", password="mqtt") # noqa: S106
|
||||||
mqtt = FastMQTT(config=mqtt_config)
|
mqtt = FastMQTT(config=mqtt_config)
|
||||||
|
CONFIG_TOPIC = "homeassistant/text/poo_recorder/config"
|
||||||
|
AVAILABILITY_TOPIC = "studiotj/poo_recorder/status"
|
||||||
|
COMMAND_TOPIC = "studiotj/poo_recorder/update_text"
|
||||||
|
STATE_TOPIC = "studiotj/poo_recorder/text"
|
||||||
|
JSON_TOPIC = "studiotj/poo_recorder/attributes"
|
||||||
|
ONLINE = "online"
|
||||||
|
OFFLINE = "offline"
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
print("Initialization.")
|
print("Initialization.")
|
||||||
@@ -18,16 +25,30 @@ class PooRecorder:
|
|||||||
print("Stopping...")
|
print("Stopping...")
|
||||||
await PooRecorder.mqtt.mqtt_shutdown()
|
await PooRecorder.mqtt.mqtt_shutdown()
|
||||||
|
|
||||||
def record(self) -> None:
|
async def record(self, status: str) -> None:
|
||||||
print("Recording...", self._status)
|
PooRecorder.publish_text(status)
|
||||||
now = datetime.now(tz=datetime.now().astimezone().tzinfo)
|
now = datetime.now(tz=datetime.now().astimezone().tzinfo)
|
||||||
formatted_time = now.strftime("%a | %Y-%m-%d | %H:%M")
|
PooRecorder.publish_time(now)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
@mqtt.on_connect()
|
@mqtt.on_connect()
|
||||||
def on_connect(client, flags, rc, properties):
|
def on_connect(client, flags, rc, properties) -> None: # noqa: ANN001, ARG004
|
||||||
print("Connected")
|
print("Connected")
|
||||||
config = PooRecorder.compose_config()
|
config = PooRecorder.compose_config()
|
||||||
PooRecorder.mqtt.publish("homeassistant/text/poo_recorder/config", config)
|
PooRecorder.mqtt.publish(PooRecorder.CONFIG_TOPIC, config)
|
||||||
|
PooRecorder.publish_text("N/A")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def publish_text(new_text: str) -> None:
|
||||||
|
PooRecorder.mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE)
|
||||||
|
PooRecorder.mqtt.publish(PooRecorder.STATE_TOPIC, new_text)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def publish_time(time: datetime) -> None:
|
||||||
|
formatted_time = time.strftime("%a | %Y-%m-%d | %H:%M")
|
||||||
|
PooRecorder.mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE)
|
||||||
|
json_string = {"last_poo": formatted_time}
|
||||||
|
PooRecorder.mqtt.publish(PooRecorder.JSON_TOPIC, json_string)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def compose_config() -> dict:
|
def compose_config() -> dict:
|
||||||
@@ -40,12 +61,13 @@ class PooRecorder:
|
|||||||
"manufacturer": "Studio TJ",
|
"manufacturer": "Studio TJ",
|
||||||
},
|
},
|
||||||
"unique_id": "poo_recorder",
|
"unique_id": "poo_recorder",
|
||||||
"name": "Poop Recorder",
|
"name": "Poo Status",
|
||||||
"availability_topic": "studiotj/poo_recorder/status",
|
"availability_topic": PooRecorder.AVAILABILITY_TOPIC,
|
||||||
"availability_template": "{{ value_json.availability }}",
|
"availability_template": "{{ value_json.availability }}",
|
||||||
"json_attributes_topic": "studiotj/poo_recorder/attributes",
|
"json_attributes_topic": PooRecorder.JSON_TOPIC,
|
||||||
"min": 0,
|
"min": 0,
|
||||||
"max": 255,
|
"max": 255,
|
||||||
"mode": "text",
|
"mode": "text",
|
||||||
"command_topic": "studiotj/poo_recorder/command",
|
"command_topic": PooRecorder.COMMAND_TOPIC,
|
||||||
|
"state_topic": PooRecorder.STATE_TOPIC,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user