Originally posted 2018-02-19 10:50:21.
So after my experiences with the Yeelights and Google Home, I wanted to add a couple of WiFi switches to my system. I already had one of the Belkin Wemo switches but Belkin has abandoned the Australian Market now and their switches are no longer available. Not only that, even if they were available, the price was really steep at around $60-$70 Aussie.
Looking around on eBay and also looking in the Home Assistant Community, I saw that some people were using the Xiaomi WiFi switches (pictured).
What I didn’t realise initially is that there are a few versions of this switch and these $13 ones were not supported out-of-the-box by Google Home/Assistant. I also didn’t check the reviews on eBay and it turns out a few people can’t get them to work at all.
One of the other issues is that all the instructions (same as for the Yeelights actually) are in Chinese and not a word of English to be found. Turns out there are a couple of tricks to be aware of.
First – just to be clear, I am using an iPhone and have not tried to use these with an android device. On the iPhone there are 2 apps you will need. When I first set the lights up, I’m not even sure which app I got first. I seem to remember getting the Yeelight App (Qingdao Yeelink Information Technology Co,. Ltd.) Current version 184.108.40.206. I think when I installed that app, I found I needed another app, Mi Home (Beijing Xiaomi Co., Ltd) Current version 4.7.1. The Mi Home app is totally in Chinese. As near as I remember, I had to use the Mi Home app to set up an account so I could use the devices. Like the Belkin Wemo, the Xiaomi switches and yeelights all have a vendor server that controls them. This means that even if you are on a 4G network or some other network outside your home that you can still control your devices. The Limitless LED lights I have also have this, and of course Google Assistant/Home communicates and is controlled by external servers.
It is the Mi Home application you need to use the switches. I am just going off my memory here now as I have my switches in a production environment and don’t want to reset them. With the globes, they are reset by switching them on then off (with a 2 second break between switching) five times. When they are reset and unconfigured, they then broadcast on an SSID that you connect to with your phone before actually setting the correct SSID and password in your internal network for them. The switches yellow flashing light (unconfigured) then changes to solid blue when connected and configured. There is a good video here showing this on an Android phone.
In the Mi Home app, you have to set the location for the external server that you want to use. This choice is important!
The location you set will control which Xiaomi devices you will see and can configure in the app. The location set will also determine whether or not the device can be controlled natively by the Google Assistant/Home. Remember, if you set Mainland China as the location, Google will/can not control it. For these switches however, the only location you can use is Mainland China – no other location has those switches as an option. Also the location you set in the app is for all devices in that app. You can chop and change locations in there so you can set it to Singapore and configure Yeelights and Mainland China to configure the Xiaomi switches.
Controlling Xiaomi in Home Assistant and Google Home
So anyway, I already had the 2 LED Xiaomi/Yeelights working and they are natively supported (Yeelight Actions) in Google Assistant/Google Home and I had previously added these to my Home Assistant as well. If I can control these switches in Home Assistant then I can control them with Google Assistant and bypass the China block in controlling them direct. There is an article on the home assistant site here that describes how to do this however it’s a little short on detail particularly regarding getting the ‘token’ you need to control the switches from your phone. They have fairly generic instructions for getting this access token here (specific to an Xiaomi Vacuum) and you need to read between the lines somewhat. So for the rest of this article I’m going to fill in the gaps and provide more detailed instructions and screenshots to make it clearer. Note these instructions are for using an iPhone to do this.
- Configure the switch with the Mi-Home app as per the above instructions.
- You will need to use iTunes to create an unencrypted backup of your iPhone. (no password)
- Download and Install iBackup Viewer, (free) open it, and open your backup.
My backup looks as above. Click on the backup to open it.
- Open the “Raw Data” module.
You need to switch list view to tree view.
- Navigate to
Open up the tree further by clicking on the arrow to the left and select Documents under AppDomain-com.xiaomi.mihome
- Search for a file that looks like this:
123456789_mihome.sqlite– note that
_mihome.sqliteis not the correct file.
Note in the above screen-grab, I have changed the column widths a bit so I can easily read the file names.
- Save this file to your filesystem by clicking on it so it changes colour as shown above and then selecting Export from the menu at the top and clicking on selected…
- Install DB Browser for SQLite.
- Open DB Browser and load the
.sqlitefile you saved from your backup.
- Click on the Browse Data Tab and make sure the Table is set to ZDEVICE as shown. If you scroll over to the right, you can see the ZNAME Column and also a column with the ZTOKEN which is what we need to configure the lights in Home Assistant.
- Click on the ZTOKEN for your device and copy the returned 96-digit hexadecimal string to your clipboard.
- Repeat for any extra switches you want to use as well (record the ZTOKENs in a text file for use in the next step.
Terminal(Mac or Linux) or a Ubuntu session in windows and execute this command:
echo '0: <YOUR HEXADECIMAL STRING>' | xxd -r -p | openssl enc -d -aes-128-ecb -nopad -nosalt -K 00000000000000000000000000000000Substituting in your 96 character string for <YOUR HEXADECIMAL STRING> of course
- Record the resulting string as your token. (Note the returned string will be 32 characters and has some gobbledegook after it as you can see.
- Make the following changes in your configuration.yaml file:
#Xiaomi Switches switch: - platform: xiaomi_miio name: Other Switch host: !secret other-switch-ip token: !secret other-switch-token - platform: xiaomi_miio name: Home Assistant Switch host: !secret home-assistant-ip token: !secret home-assistant-token
- Just a note… There were a few versions of Home Assistant, at least from 0.60.0-0.63.2 where this functionality was broken. 0.63.3 fixed this and is now working. Before someone points it out, I do have my raspberry Pi that is used for Home Assistant on one of these switches and obviously, if I turn it off, I can’t use Home Assistant to turn it back on – but it’s for control if I’m away from home and need to re-boot Home Assistant remotely and can’t log in – then I can power cycle it using the Mi Home App.
- If you have the switches domain in Home Assistant exposed to Google Assistant, all you then need to do is say “Hey Google. Sync my devices” and you will see your switches in the Google Assistant App and they can be controlled using your Google Home just like any other device. Cool!
Sometimes Xiaomi Switches reset themselves…
So just over the last few days, I was away from home and after a Home Assistant upgrade, I noticed the switches were no longer being detected. I also noticed that I could not see the switches in the Mi Home app on my iPhone. When I got home, I also saw that even though the Blue light was on on the switches that I still could not control them in the App and further that my router was not showing them as being connected.
Normally, the switches can be controlled by the Mi Home app even if you are on a public network as it is an encrypted connection over the public network to the Xiaomi mainland China server.
So it appeared there was nothing I could do except reset the switches. To do this, I turned the switch off (by pressing the button on the switch so the switch still had power but it turned off the power to the connected device – naturally I shutdown the 2 Raspberry Pi devices first). Then, the light goes off on the switch. You then press and hold the button on the switch and you will hear a ‘click’ and the light on the switch will start flashing with a yellow light. Then you switch the phone to the Mi Home app and press on the + button to add a new device, select the switch from the list of devices and it will ask you to confirm you see a flashing yellow light. Check the box and continue.
Next it will ask for your wireless network SSID and password. Enter these and continue whereupon it will tell you to switch to your wireless settings on your phone (or you can click on wifi setup). The switch has established an adhoc SSID and you can then set this as your wireless network – it does not require a password. When I did this yesterday, it didn’t detect the SSID of the switch immediately so I toggles WiFi off and on again and it came up. When you are connected to the SSID of the switch, go back to the Mi Home app and you should see the progress ‘circle’ going up to 100% as it configures the switch. You can then set the name and room for the switch.
Once the switch is talking to the Mi Home app again, you will need to follow the above steps from step 1 as the token has now unfortunately changed. So follow that procedure to get the new token and reconfigure Home Assistant and it’s all done.
I have no idea why it does this. I got the switches early January as best as I can remember and they ran till March. There are reports on the Home Assistant forum saying they ‘do this’. Anyway, if it happens to you, this is how you fix it. It probably only took me 10 minutes to run through this for 2 switches. First World problems!