party-lock: global ACL block all zigbee2mqtt/+/set
Global party lock toggled via party-lock/set ON/OFF. When locked, mosquitto ACL denies ALL zigbee2mqtt/+/set publishes. Add lock button to CYD status bar.
This commit is contained in:
@@ -13,43 +13,34 @@ MQTT_HOST = os.environ.get("MQTT_HOST", "mosquitto")
|
|||||||
MQTT_PORT = int(os.environ.get("MQTT_PORT", "1883"))
|
MQTT_PORT = int(os.environ.get("MQTT_PORT", "1883"))
|
||||||
ACL_PATH = "/mosquitto/config/acl.conf"
|
ACL_PATH = "/mosquitto/config/acl.conf"
|
||||||
|
|
||||||
PLUG_MAP = {
|
locked = False
|
||||||
"bamboo": "0xffffb40e06050af6",
|
|
||||||
"dj-booth": "0xffffb40e0603ef11",
|
|
||||||
}
|
|
||||||
|
|
||||||
locked_plugs = set()
|
|
||||||
|
|
||||||
def rebuild_acl():
|
def rebuild_acl():
|
||||||
lines = []
|
lines = []
|
||||||
for plug in sorted(locked_plugs):
|
if locked:
|
||||||
ieee = PLUG_MAP.get(plug)
|
lines.append("topic deny zigbee2mqtt/+/set")
|
||||||
if ieee:
|
|
||||||
lines.append(f"topic deny zigbee2mqtt/{ieee}/set")
|
|
||||||
lines.append("topic readwrite #")
|
lines.append("topic readwrite #")
|
||||||
with open(ACL_PATH, "w") as f:
|
with open(ACL_PATH, "w") as f:
|
||||||
f.write("\n".join(lines) + "\n")
|
f.write("\n".join(lines) + "\n")
|
||||||
os.system("docker kill -s HUP mosquitto 2>/dev/null || true")
|
os.system("docker kill -s HUP mosquitto 2>/dev/null || true")
|
||||||
|
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
topic = msg.topic
|
global locked
|
||||||
payload = msg.payload.decode().strip().upper()
|
payload = msg.payload.decode().strip().upper()
|
||||||
parts = topic.split("/")
|
if payload == "ON":
|
||||||
if len(parts) >= 3 and parts[0] == "party-lock":
|
locked = True
|
||||||
plug_name = parts[1]
|
elif payload == "OFF":
|
||||||
if plug_name in PLUG_MAP and payload in ("ON", "OFF"):
|
locked = False
|
||||||
if payload == "ON":
|
else:
|
||||||
locked_plugs.add(plug_name)
|
return
|
||||||
else:
|
rebuild_acl()
|
||||||
locked_plugs.discard(plug_name)
|
|
||||||
rebuild_acl()
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
rebuild_acl()
|
rebuild_acl()
|
||||||
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
||||||
client.on_message = on_message
|
client.on_message = on_message
|
||||||
client.connect(MQTT_HOST, MQTT_PORT, 60)
|
client.connect(MQTT_HOST, MQTT_PORT, 60)
|
||||||
client.subscribe("party-lock/+/set", qos=1)
|
client.subscribe("party-lock/set", qos=1)
|
||||||
client.loop_forever()
|
client.loop_forever()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user