Skip to content

Commit

Permalink
publish JSON messages on MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
jschlyter committed Jun 28, 2024
1 parent 49dd290 commit 0202159
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions chargeamps/chargeamps2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

mqtt_reconnect_interval = 30
mqtt_broker = "127.0.0.1"

mqtt_base_topic = "chargeamps"
mqtt_commands_topic = "chargeamps/commands"
mqtt_results_topic = "chargeamps/results"
mqtt_status_topic = "chargeamps/status"

mqtt_commands_queue = Queue(0)
mqtt_results_queue = Queue(0)
Expand All @@ -30,13 +30,32 @@ async def mqtt_results(
logger.debug("Waiting for results")
result = await mqtt_results_queue.get()
logger.debug("Got result: %s", result)
await mqtt_client.publish(topic=mqtt_results_topic, payload=result.encode())

await mqtt_client.publish(
topic=f"{mqtt_base_topic}/results", payload=result.encode()
)

chargeamps_client.process_message(result)

await mqtt_client.publish(
topic=mqtt_status_topic,
payload=chargeamps_client.state.model_dump_json().encode(),
topic=f"{mqtt_base_topic}/status",
payload=chargeamps_client.state.model_dump_json(
exclude={"connector_settings", "connector_status"}
).encode(),
)

for n, settings in enumerate(chargeamps_client.state.connector_settings):
await mqtt_client.publish(
topic=f"{mqtt_base_topic}/connector/{n}/settings",
payload=settings.model_dump_json().encode(),
)

for n, status in enumerate(chargeamps_client.state.connector_status):
await mqtt_client.publish(
topic=f"{mqtt_base_topic}/connector/{n}/status",
payload=status.model_dump_json().encode(),
)


async def mqtt_commands(
mqtt_client: aiomqtt.Client, chargeamps_client: ChargeAmpsLocalClient
Expand Down

0 comments on commit 0202159

Please sign in to comment.