-
-
Notifications
You must be signed in to change notification settings - Fork 568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lack of callback mechanism in gateway makes cube and buttons integration impossible #699
Comments
Sounds good! Let's give it a try! A capture of incoming events/messages would be helpful, too. |
If I'm understanding it correctly, the idea would be basically to create a listener that acts like a miio device to the gateway? I don't think anyone has explored that possibility so far, but it is probably also a lot of work to pull this off. If you want to give it a go, feel free to do so :-) I have the cube myself, too, but instead of using the gateway, I'm using zigbee2mqtt which makes it all very simple. |
Everyone is aware that there is already an official HomeAssistant integration specifically for the xiaomi aqara gateway that uses the UDP protocol using pushes (instant updates of sub devices including the cube) |
If I understand correctly that this is a different protecol/way to subscribe to events that would be awesome and worth exploring. |
@starkillerOG
With xiaomi_miio for some gateway features and xiaomi_aqara for other we'll get two gateways in system, this will work but looks awkward and definitely not commodity solution ... Then I found fresh gateway support in xiaomi_miio component of hass I decided that xiomi_miio gateway should replace xiaomi_aqara and I'm trying to help with this move :) @rytilahti Ok good. I'll try to implement this approach |
Sounds good, very excited to test this new subscription method if you have some initial code working! |
@starkillerOG @rytilahti fake_device.py is implementation of miio server which:
How to run:
Output of fake_device.py looks as follows:
Here we can see:
|
But there are problems ....
this script contains token 79cf21b08fb051499389f23c113477a4, but this token differs from real device token which is 9bc7c7ce6291d3e443fd7708608b9892. I use token 9bc7c7ce6291d3e443fd7708608b9892 in fake_server for decryption and 79cf21b08fb051499389f23c113477a4 in script ... Any ideas are more then welcome :) |
That's pretty cool! 💯 I'm wondering if the system supports wildcards for events? I'm thinking about something like "subscribe all events on all devices" which could allow generic event support? Unfortunately I don't have ideas on that token issue, nor really any time to help with these efforts.. You may want to have a take a look at dustcloud (or at least the dummycloud impl https://github.com/dgiese/dustcloud/tree/master/dummycloud) if there's something relevant. |
@rytilahti
|
It seems PR is almost here please drop a look https://github.com/rytilahti/python-miio/compare/master...bskaplou:fake_device?expand=1 Meanwhile I'll capture remaining actions of cube and square button and add their support into scripts . Afterwards I'll beg for release :) |
Please feel free to create a PR already (you can mark it as WIP/draft if you want), it is much easier to comment on it. How do you plan this feature should be exposed? How would you propose to integrate this with the gateway support? |
Ammm ... I understood your question... No... Right now it works as follows:
Unfortunately right now I have no idea how to implement wildcards...
|
@rytilahti
|
is your gw still connected to xiaomi cloud? gw can refresh ip/token of wifi devices it runs automation on via cloud api (_sync.neighborDevInfo). i checked on my (newer) hub and it gets current token with send_data_frame. extra field is lumi encoded automation trigger. it's used (NOT human readable key field) for lan automations. they use this method for most hubs, only last one lumi.gateway.mgl03 dropped it. |
@rezmus Yes gateway is connected to the cloud. |
yeah it kinda use the same encryption as local miio but with cloud did/key credentials which you can get via shell (linux mi global hub or aqara). https://github.com/dgiese/dustcloud/wiki/Lumi-Aqara-Gateway-Root |
@rezmus, still trying to figure out a way to get the token that needs to be sent with "send_data_frame" which is diffrent from the token used for the rest of the communication. It sounds like you know a lot about those tokens, could you help me with devoloping a method to obtain that token? |
I did figure out that the "data_tkn" does not have to match, for me both "29576" or "52507" work just as fine. Just need to figure out how to get that "send_data_frame" token, the rest I know how to implement and get it in HomeAssistant. |
@xcray I see you have quite some experiance with the Xiaomi Miio protecol since you wrote a CSharp implementation. Do you have any ideas on how to obtain the token needed for "send_data_frame"? |
My C# implementation was based on this: https://github.com/OpenMiHome/mihome-binary-protocol/blob/master/doc/PROTOCOL.md, and from only a few devices it could obtain token directly. |
@starkillerOG if there is some hashing algo for tokens you need to reverse engineer it from hubs firmware. |
Alright, thanks for your info. On a side node, do you happen to know how to get any of the following properties:
|
i haven't used lumi.gateway.v3 for a long time so i'm not sure what it does return as device list. mgl03 returns such list
so you have sid/model. lumi.curtain.aq3 - Aqara Blind Controller custom name you give is part of xiaomi cloud api not related to device. |
just tryed the I can get devices using |
id - model map 0 - lumi.gateway |
@rezmus you are amazing, that is incredibly helpfull!!! How did you get that model map, where is that documented? |
firmware dump. |
@rezmus is that firmware dump available somewhere (on github) in readable text format? |
here is some older version https://github.com/dgiese/dustcloud-documentation/tree/master/lumi.gateway.v3 it's firmware so if you plan to do some reverse engineering you have to use ida/ghidra or similar. |
@rezmus I tried looking at the firmware dump you provided, but that goes a little above my head.... |
sorry, i took a look some time ago but without success. however i'm not really good at decompiled code analysis. |
Alright, do you happen to know someone who might be able to help with this? |
Maybe @dgiese has an idea about the gateway <-> subdevice intercomms? He has some deep knowledge on how xiaomi devices tick ;-) |
Any chance to add lumi.curtain.hagl08? Seems like it's the same as lumi.curtain.hagl05 |
I finished a PR for including this callback mechanism into python miio. If someone has any idea how to obtain the encrypted_token (besides packet capture/sniffing), help is more than welcome! |
I just found a way to get the "encrypted token":
It works for the tokens of both my 2 gateways. |
I'm trying to make xiaomi cube and xiaomi square button work.
Pub/Sub mechanism is necessary to make these devices work properly
While investigating the way to implement it I've found no subscription mechanism in python-miio ;(
I've also looked at nodejs miio implementation and found it uses developer-api https://github.com/aholstenson/miio/blob/master/lib/devices/gateway/developer-api.js
instead of miio protocol for event subscription. Which is akward and doesn't help to avoid soldering of gateway...
I've sniffed traffic between Mi Home app and gateway and found what when I create Mi Home automation involving cube as source and other wi-fi device in local network as destination Mi Home app sends to gateway a script as follows:
-> 192.168.2.17 data= {"id":2666,"method":"send_data_frame","params":{"cur":0,"data":"[[\"x.scene.2732711973\",[\"1.0\",1589956238,[\"0\",{\"src\":\"device\",\"key\":\"event.lumi.sensor_cube.v1.move\",\"did\":\"lumi.158d000103ec74\",\"model\":\"lumi.sensor_cube.v1\",\"token\":\"\",\"extra\":\"[1,18,2,85,[6,256],0,0]\",\"timespan\":[\"0 0 * * 0,1,2,3,4,5,6\",\"0 0 * * 0,1,2,3,4,5,6\"]}],[{\"id\":0,\"did\":\"120009025\",\"token\":\"XXXX\",\"ip\":\"192.168.2.94\",\"command\":\"chuangmi.plug.v3.set_usb_on\",\"value\":\"\",\"model\":\"chuangmi.plug.v3\",\"extra\":\"\"}]]]]","data_tkn":46567,"total":1,"type":"scene"}}
This message even includes token of destination device :) It makes possible extraction of any token if you know gateway token...
Anyway, my idea here is implement subscription as follows:
I'm new at miio hacking.... Can you tell me if you already tried this way of subscription or have some other way to implement pub/sub?
The text was updated successfully, but these errors were encountered: