Config: Setting up the Xiaomi Mi Robot Vacuum in Home Assistant

I have a dog who sheds like crazy, and the only thing that keeps the dust bunnies from overrunning me is a robot vacuum. My aging Neato Botvac, after fulfilling several years of hard labor, ate it’s 3rd expensive replacement battery recently. This was obviously a great excuse to get a new one that works with Home Assistant.

I went with the Xiaomi Mi Robot.

After getting paired with the app, teaching the robot English, and sending it on it’s way around the house to do it’s thing, I realized that the process of getting it paired with Home Assistant was not going to be straightforward.

For anyone else who runs into trouble, here’s how I got it working.


GETTING DATA FROM iOS

I followed the instructions over at the Home Assistant documentation to extract the token from my iPhone. First, you better hope you didn’t forget your encryption password for your iPhone, because it is not the same as your iCloud password! Make an unencrypted backup of your phone, open it in iBackup Viewer, extract the mihome SQLite file, and open that in SQLite Browser. I made it this far without issue.

Now, the SQL command it asks you to execute didn’t work for me. No worries, this database only has one record and all the way at the end is my token.

Wait…. why is this a 96 character token? Home Assistant is asking for a 32 character one. WTF?

Apparently it’s encrypted within the database. Uh-oh. Thanks to this user on the Hass forums, we need to run a Python script. At a Linux terminal, make sure we have the libraries we need for the script:

sudo apt-get install python3-dev
sudo pip3 install pycrypto

Then copy this script into mirobot.py

from Crypto.Cipher import AES
import binascii
keystring = '00000000000000000000000000000000'
iostoken = '96charactertokenfromsqlite'
key = bytes.fromhex(keystring)
cipher = AES.new(key, AES.MODE_ECB)
token = cipher.decrypt(bytes.fromhex(iostoken[:64]))
print(token)

https://pastebin.com/HAS7G22S

Replacing the ’90charactertokenfromsqlite’ with your own.

From the terminal run:

python3 mirobot.py

You now have your unencrypted access token and can configure it for Home Assistant.

In the Home Assistant configuration.yaml, we can add a template sensor to extract it’s status as a separate entity:

sensors:
  - platform: template
    sensors:
      vacuum_status:
        friendly_name: Vacuum Status
        value_template: "{{ states.vacuum.robot.attributes.status }}"

https://pastebin.com/W43uCVBv

Leave a Reply

Your email address will not be published. Required fields are marked *