Skip to content

Recover Hub 2 gateway key over LAN and during HA setup - #1781

Closed
oliver-mee wants to merge 6 commits into
AlexxIT:masterfrom
oliver-mee:feat/hub2-key-recovery-public
Closed

oliver-mee wants to merge 6 commits into
AlexxIT:masterfrom
oliver-mee:feat/hub2-key-recovery-public

Conversation

@oliver-mee

@oliver-mee oliver-mee commented Oct 13, 2026 •

Copy link
Copy Markdown

Hi! I found that the global/EU Xiaomi Smart Home Hub 2 (Multimode Gateway 2, lumi.gateway.mgl001) on stock 1.0.8_0013 can return its gateway key over the local network using its existing Mi Home token. This gives that build an alternative to opening the hub for UART when the key is missing (#1166).

The gateway key is a personalised 16-character value, separate from the 32-character token. Gateway3 uses it to authenticate system_command when enabling Telnet on newer Hub 2 firmware. It's stored in /data/miio/device.conf on the hub.

Recovery uses the token-authenticated async_device_feedback_log diagnostic request to upload that file to a temporary receiver. The request lists it in both fileList and filelist and supplies a fresh AES key and IV. The hub returns an encrypted tar archive; the receiver decrypts it in memory, checks the model and MAC against the hub, and reads the key. Upload size, source address and timeouts are checked. Gateway3's existing Telnet setup follows separately.

On one physical hub with this build, the recovered key worked with normal Gateway3 setup, local device control and automatic reconnection after a hub reboot. The hub was connected to the router by Ethernet; the recovery computer used Wi-Fi on the same local network. Recovery is enabled only for this exact model/build. A limited emulation of the older mgl03 on 1.5.7_0001 returned fixed logs instead of the requested file.

There are two ways to use it:

  • Home Assistant, recommended: select your gateway from Mi Cloud to fill its address/token, or enter those details manually. With Key empty, setup offers recovery, then asks you to confirm the usual Telnet/password setup and saves the key with the entry.
  • Python script: scripts/recover_gateway_key.py asks for the IP and token, then prints the key to paste into Gateway3.

Both use the same recovery code. The guide includes installation steps, troubleshooting and the protocol details. It also explains the firmware's conditional cleanup of its temporary diagnostic archive and the password/Telnet changes made by normal gateway setup.

129 tests pass on HA 2026.10.1 / Python 3.14, and 29 script/core tests pass on Python 3.10. These cover encrypted HTTP uploads, validation, cancellation, retries and HA progress with simulated gateway responses. The new HA screens and full physical setup through them still need testing. I'd welcome results from matching hubs and suggestions for other builds to investigate. Please include model, firmware and connection details, keeping tokens and recovered keys private. Feedback on the instructions and code placement is welcome too.


🤖 Generated with GPT-6.1 Sol
🧑‍💻 Ideated, directed and reviewed by a human, @oliver-mee

@oliver-mee oliver-mee changed the title Recover Hub 2 gateway key over LAN on tested stock firmware Recover Hub 2 gateway key over LAN and during HA setup Oct 13, 2026
@AlexxIT

AlexxIT commented Oct 14, 2026

Copy link
Copy Markdown
Owner

Thanks! Your PR is a really great job! But I rewrote it and fit it into a hundred lines - 5b667ba

Now, Telnet on the second gateway will open during the standard procedure for retrieving gateway information. This occurs either simply when retrieving information as part of a cloud integration, or during the process of adding a gateway to the integration.

The documentation still needs to be updated.

@AlexxIT AlexxIT closed this Oct 14, 2026
@AlexxIT

AlexxIT commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants