PyXiaomiGateway Problem: {"error":"Invalid key"}

I moved the explanation into this thread for completeness:

I will try to explain the local network protocol of the gateway a bit: The communication between Gateway and Home Assistant uses UDP. A protocol without handshaking dialogues and error correction if a packet gets lost. At the discovery of the Gateway a command requests the first token. All further needed tokens are published via a heartbeat message (multicast (udp), port 9898). The latest published token is always needed to execute a command on the gateway. If Home Assistant just receives the first token and none of the heartbeats with new tokens you will receive the “Invalid key” error message and the control of devices will fail.

There are two possibilities now: You network / wireless connection is very bad. There is packet loss and Home Assistant doesn’t receive all needed heartbeat messages. Second possibility: Your network doesn’t transport multicast messages properly and your Home Assistant instance just receives the first token (received directly by UDP (no multicast)) and this token never gets updated because of the missing multicast traffic.

1 Like

thanks,

Happy not to cross post if you can tell me which post this problem should sit in? As far as I am concerned this is a Xiaomi gateway integration issue, that could also be related to firmware… but now looks like this specific issue has it’s own thread…

I am just trying to get this working and joining (already created) other posts where I think issues may be related so that I can get updates from all the threads in order to try and resolve…

Otherwise, thanks for your help, :+1: :slight_smile:

Seems the issue is with the IOS Mi Home App, it gives the incorrect key, with the android App it gives the correct key. Tooks me weeks to figure this out.

noooo…that can’t be…are you sure? Do I have to gat an android phone to double check!!!

I don’t believe this to be the case. I have enabled the gateway and picking up sensors from the gateway and even got an automation with the gateway light working when I first connected… Anyway, @jakob.tewes if it is the case, you don’t need a phone, you can use bluestacks or andy android emulator on pc/mac…

Yeah I have nox player running on my mac, I actually gave my friend an extra gateway, a motion sensor to mess around with hoping he would figure it out as I got so fed up. Hours late he messaged me It’s a bug in the IOS app. Generate the code on a Android and it worked. I’m able interact with my gateway lights/sound and wall switches :smile:

Only regret i have is, I wish I had read all the forums sooner. Xiaomi Gateway Integration

2 Likes

100% certain, PyXiaomiGateway Problem: {"error":"Invalid key"}

ok brilliant!!! awesome work… will give it a try… here is a virtual beer :beer:

OK Awesome, loaded up bluestack and redid the key and this is now working! You the man!

Do you confirm? A proper “key” retrieved by the android mihome app solved the issue for you?

well, in terms of changes to the environment, this was the only change that i carried out and now the {“error”:“Invalid key”} message no longer appears in the logs. Additionally, I am able to control the light on the gateway, so unless there were some other changes on the infratructure side that I was not aware of, I can confirm that this fix has resolved my issue.

same for me. that costed lots of time. we should definitely make sure, that this makes it to the module docs.

Doubleho7 you made my day!!! works like a charm now :slight_smile:

I have already updated the docs…

1 Like

Since I have the same invalid key issue I have disabled local lan and enabled again and got a new key using an android device this time (I have iOS devices) .fresh installation hassio AIO on a raspberry 1 but again the same error when I sent a command to my gateway :frowning: invalid key.any suggestions?

The iOS app generates invalid keys. Use Android via an emulator like bluestacks and generate a valid key…

Sorry but if I got this right, you are saying that an android sim on my Mac can only do the job correctly and not a native android device ? I used a huawei p9 lite to generate the key .

Thing is, that iOS generates invalid keys. Use the android app to generate the key. Should male no difference, if you’re using a realy device or an emulator :slight_smile:

Thank you Jacob, sadly , I did this and I got the same results ! I will generate another key and retry and let you know .

Confirmed. iOS app generates wrong key, it took me weeks to find out, :frowning:

Btw, I saw that from log

Discovered: {'host': '192.168.1.1x7', 'port': 54321, 'hostname': 'lumi-gateway-v3_miio97xxxx98.local.', 'properties': {'epoch': '1', 'mac': '34ce0088xxx7'}, 'mac_address': '34ce0088xxx7'}

I dont know why 54321 port?
PS: Im trying to run hass on docker but no luck without net=host. Someone told me that we need to config for multicast but not sure how