Skip to content

Commit

Permalink
Brightness state mgmt
Browse files Browse the repository at this point in the history
  • Loading branch information
meowmeowahr committed Feb 21, 2024
1 parent 11f78fa commit b0ef970
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mqtt:
state_topic: "MQTTAnimator/state"
return_state_topic: "MQTTAnimator/rstate"
brightness_topic: "MQTTAnimator/brightness"
breturn_rightness_topic: "MQTTAnimator/rbrightness"
args_topic: "MQTTAnimator/args"
animation_topic: "MQTTAnimator/animation"
reconnection:
Expand Down
10 changes: 8 additions & 2 deletions mqtt_animator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@
args_topic: str = mqtt_topics.get("args_topic", "MQTTAnimator/args")
animation_topic: str = mqtt_topics.get("animation_topic", "MQTTAnimator/animation")

data_request_return_topic: str = mqtt_topics.get("return_data_request_topic", "MQTTAnimator/rdata_request")
data_request_return_topic: str = mqtt_topics.get("return_data_request_topic",
"MQTTAnimator/rdata_request")
state_return_topic: str = mqtt_topics.get("return_state_topic", "MQTTAnimator/rstate")
brightness_return_topic: str = mqtt_topics.get("return_brightness_topic",
"MQTTAnimator/rbrightness")

first_reconnect_delay: int = mqtt_reconnection.get("first_reconnect_delay", 1)
reconnect_rate: int = mqtt_reconnection.get("reconnect_rate", 2)
Expand Down Expand Up @@ -108,13 +111,16 @@ 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}))
cli.publish(data_request_return_topic,
json.dumps({"state": animation_state.state,
"brightness": animation_state.brightness}))
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")
elif msg.topic == brightness_topic:
if msg.payload.decode().isdigit():
animation_state.brightness = int(msg.payload.decode())
cli.publish(brightness_return_topic, int(msg.payload.decode()))
else:
logging.warning("Invalid brightness data: %s", msg.payload.decode())
elif msg.topic == animation_topic:
Expand Down

0 comments on commit b0ef970

Please sign in to comment.