Skip to content

Commit

Permalink
Publish states on start
Browse files Browse the repository at this point in the history
  • Loading branch information
meowmeowahr committed Mar 4, 2024
1 parent 7a7be74 commit f59244f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions mqtt_animator.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,7 @@ def on_message(cli: mqtt_client.Client, __, msg):
print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")

if msg.topic == data_request_topic:
cli.publish(data_request_return_topic,
json.dumps({"state": animation_state.state,
"brightness": animation_state.brightness,
"animation": animation_state.effect,
"args": json.dumps(dataclasses.asdict(animation_args))
})
)
publish_state(cli)
elif msg.topic == state_topic:
animation_state.state = "ON" if msg.payload.decode() == "ON" else "OFF"
cli.publish(state_return_topic, "ON" if msg.payload.decode() == "ON" else "OFF")
Expand Down Expand Up @@ -176,6 +170,14 @@ def on_message(cli: mqtt_client.Client, __, msg):

animation_args = AnimationArgs(**json.loads(data))

def publish_state(cli):
cli.publish(data_request_return_topic,
json.dumps({"state": animation_state.state,
"brightness": animation_state.brightness,
"animation": animation_state.effect,
"args": json.dumps(dataclasses.asdict(animation_args))
})
)

if __name__ == "__main__":
# connect to mqtt server
Expand All @@ -190,6 +192,8 @@ def on_message(cli: mqtt_client.Client, __, msg):
client.subscribe(animation_topic)
client.subscribe(args_topic)

publish_state(client)

# run mqtt background tasks in thread
threading.Thread(
target=client.loop_forever, name="MQTT_Updater", daemon=True
Expand Down

0 comments on commit f59244f

Please sign in to comment.