zigbee2mqtt: add z2m-repair / z2m-rename recipes for rejoin

Add scripts/z2m_repair.py that:
  - Opens permit_join for N seconds (default 120)
  - Subscribes to bridge/event on the local MQTT broker
  - For each device_joined event, auto-renames the device back to its
    original friendly name (hardcoded IEEE->name mapping from the
    last known good state, overridable via --mapping).

After the zigbee2mqtt database was wiped, the user had to manually
pair each device via the web UI. With this recipe, devices rejoin
automatically when woken (network_key preserved), and the script
restores friendly names as they join.

Add justfile recipes:
  just z2m-repair [TIME=120]   - run the auto-repair script
  just z2m-rename IEEE NAME   - rename a single device by IEEE
This commit is contained in:
2026-07-01 01:25:38 -07:00
parent c2de5ba85b
commit fda24c18a2
2 changed files with 146 additions and 0 deletions
+19
View File
@@ -172,6 +172,25 @@ z2m-patch-bridge:
ssh {{RPI_HOST}} 'docker restart zigbee2mqtt'
@echo "Done. Verify with: just z2m-logs"
# Open permit_join + watch for device_joined events; auto-rename any
# rejoining device back to its original friendly name. Use after the
# zigbee2mqtt database has been wiped, to recover from a fresh state
# without manually re-pairing each device via the web UI.
#
# First put each device into pairing mode (ThirdReality smart plugs:
# hold the side button for ~5 seconds until the LED blinks rapidly).
# Then run this and press a device's button within the time window.
z2m-repair TIME="120":
uv run --with paho-mqtt python3 {{justfile_directory()}}/scripts/z2m_repair.py --time {{TIME}}
# Rename a zigbee2mqtt device by IEEE address. Use after a device
# rejoins with a fresh ieee (or for any rename).
z2m-rename IEEE NAME:
ssh {{RPI_HOST}} 'mosquitto_pub -h localhost \
-t "zigbee2mqtt/bridge/request/device/rename" \
-m "{\"from\":\"{{IEEE}}\",\"to\":\"{{NAME}}\",\"transaction\":\"rename-{{NAME}}\"}"'
@echo "Renamed {{IEEE}} -> {{NAME}}"
# Show paired zigbee devices and their friendly names
z2m-devices:
ssh {{RPI_HOST}} 'cat {{RPI_DIR}}/nr-flow-validator/infra/zigbee2mqtt/configuration.yaml' > /tmp/z2m.yaml