This is forked gist from Alienmario with some additions for newer version of MI Home app (8.7.708) and Android 10 (Rooted)
- Create a manual scene in Mi Home for the device you'd like to control
In my version of Mi Home the manual scene is called Batch Control.
- Use Http Toolkit to figure out the scene id.
- Install the app on PC and Android, follow the instructions - install CA certificate and connect
MI Home app did not trust automatically installed user certificate so I had to add system-wide certificate. To do so, you have to be rooted. Connect your phone to PC and open cmd. Run
adb shell
and thensu -c whoami
. Grant root access to shell from your phone. Run the same commands again, it should outputroot
now. It means adb sees that you are rooted. Open Http Toolkit on your PC and select "Android device via ADB" (it should be available now). System certificate is installed automatically at this point. On your phone Http Toolkit app you should see "System trust enabled". You can now disconnect your phone from PC. - Set to only intercept the Mi home app
To do so in Http Toolkit on your PC while intercepting add
api.io.mi.com
to the filter field. - While snooping, start the scene in Mi home, then you can disconnect http toolkit
In my version of Mi Home you have to run Batch Control action.
- Look for this request:
https://de.api.io.mi.com/app/scene/start
In my version the request differs. It looks like:
https://api.io.mi.com/app/appgateway/miot/appsceneservice/AppSceneService/NewRunScene
- The request body should look like this (actual values replaced with *)
data=*&rc4_hash__=*&signature=*&_nonce=*&ssecurity=*
In my version of Http Toolkit on PC there is block on the right called "Request body". You can find these values there.
- You will need the values for data, nonce and ssecurity. Run each one by https://www.urldecoder.org/.
I actually did not have to use https://www.urldecoder.org/ . Use encoder only if you get values in percent-encoded format (i.e. you see '%2F' instead of '/' or '%3D' instead of '=' in the string).
- Download this python script: https://gist.github.com/socram8888/4b8949023c8e8153970764d20c935785
- Run it like this:
python rc4mi.py --decipher -s <ssecurity> -n <nonce> -m <data>
Replace with your url-decoded values. - You should get a response like this:
CLEARTEXT: {"us_id":"1111111111","key":""}
In my case I got
CLEARTEXT: {"scene_id":"1111111111","scene_type":1,"home_id":"111111111111","owner_uid":1111111111}
Write down scene_id and owner_id. This is your scene id and account ID from MI Home respectively. - Your scene id is in us_id >(or scene_id in my case)
- When you have all scene id's you need, you may remove the certificates and VPN service the app installed in Android settings to regain security.
- Install the app on PC and Android, follow the instructions - install CA certificate and connect
- Get an automation app which can launch custom android activity. I prefer Automate which is free. Tasker can do the job as well.
- Add an App start / launch activity block
- Package:
com.xiaomi.smarthome
- Activity class:
com.xiaomi.smarthome.scene.activity.SmartHomeLauncherActivity
- Action:
com.xiaomi.smarthome.scene.smarthomelauncher
- Category:
android.intent.category.DEFAULT
- Extras:
{"extra_str_scene_id": "*1", "extra_scene_account": "*2"}
Replace *1 with your scene id retrieved earlier. Replace *2 with your account ID from Mi home.
- Package:
Typical use case for this is wifi connection based automation of your device. For example, I use it to turn on alarm on my Mi control hub when phone disconnects from my home wifi. You can use location based automation but I think it is more battery consuming than Wifi check.
You may want to add to the flow a small delay before running the app because connection might not switch to mobile fast enough to execute Mi Home automation.
Don't forget to disable battery optimizations and data saver (if you have it) for mi home and your automation app.
Credits: h3ku, socram8888 for the python script, Alienmario for original gist with instructions.