Mi Home app alternative: Home Assistant running in Keenetic router

7 min read
If you don't like Mi Home dependency from Xiaomi cloud servers, if you want to write much more complex scenarios for your smart home and add any third party devices - here is the way for you!

Is it possible to expand the functionality of the Mi Home smart home (but keep everything already existing), connect to it third-party devices incompatible with Mi Home, write very complex scripts with fine tuning, and do not depend on external servers? Answer: yes.

Router Keenetic и Xiaomi Gateway 3
Here we will take a look at deploying the Home Assistant smart home system directly into the Keenetic Viva router and controlling an existing smart home via Xiaomi gateway 3, while fully keep the functionality of both the router and the gateway and without flashing them, and preserving all the functionality of the existing Mi Home.
Many years ago I start use the Mi Home app for my Xiaomi devices. The app is quite easy with setting up and comfortable with using it. But as time went on, the number of devices grew, smart home scenarios became more complex and tricky, and the app's shortcomings began to outweigh its simplicity. For example, I need a few scenarios, which will turn on 8 bulbs, but I don't want to join that bulbs into the group. It is possible to create an automation in Mi Home, which will turn them on, and then call it from all scenarios where needed. Handy? No! Because in that case such scenarios will work in Cloud mode, slowly and unreliably. Solution? In each scenario manually set turning on of each bulb and set it lighting mode.
Next case: beautiful Xiaomi Mi Motion Sensor 2 in its logs in Mi Home app shows exact ambient light level in Lux, while all what you can use in scenario - "Motion detected" or "Motion detected and ambient light is weak". Weak for what? It is absolutely mismatch with my feeling of "too dark" or "too bright", but I cannot control that.
One more example. Aqara has very useful "presence sensors" FP1 and FP2, which can detect what somebody in the room even if he stays still. Great? No! At this time there is no way to add such sensors into Mi Home app, they can be used via Aqara app.
Sooner or later, you'll have devices that are only visible if the "Mainland China" region was selected in the Mi Home app, and devices that only work if you select a European country in the app. The problem solution? Create 2 applications for 2 different regions, in which there will be unrelated groups of devices. Uncomfortable? Not a word!
And I can endless continue such list... But one day I finally got fed up with this situation and wanted to change something, but without breaking everything that is already got working in my Mi Home app, which is about a hundred "smart" devices.
After learning of different alternative smart home systems, I decide to use Home Assistant. In result, without of buying any additional devices I've got a powerful Smart home system, which can control all my Mi Home devices, and works seamlessly with Mi Home app, i.e. I can to move scenarios from Mi Home to Home Assistant without rush, or even keep some of them at exising Mi Home.
One of inconvenient things when you start use Home Assistant is the need for a separate, constantly running computer for this system. Yes, you can install it into an old laptop or phone, but then you will have to disconnect the battery and solve other possible problems associated with continuous operation for years. But surely there are many devices that are already working continuously, and which will be suitable for Home Assistant. In my case, the choice fell on the already long-working router Keenetic Viva, and it seems it was a very convenient solution, even with additional bonuses, and nothing needs to be broken or re-flashed there.
So, why the router Keenetic Viva so good for our purposes? It has 2 USB ports and it allows you to install third-party software packages OpenWRT to expand routers capabilities. As a first step we need to prepare USB drive, mostly following this instruction. It was pretty enough for me the old and slow 8Gb flash drive Kingston. I formatted it to EXT4 file system, then (I'm Linux Mint user) ran "gparted" and added a 1GB "linux swap" partition. This difference from the manual was critical, I couldn't understand for a long time why the installed Home assistant just hangs the router for 10 minutes and then crashes with "killed" message (Out of memory was in the router's logs). You can read more detailed about swap files here.
As the next step need to install Entware repo. The final step will installing of all the necessary libraries and Home Assistant itself. I've used existing guide from Keenetic community forum, by executing following commands via SSH:
echo "src/gz ha http://bin.entware.net/mipselsf-k3.4/test/HA" >> /opt/etc/opkg.conf
opkg update
opkg install gcc ldd make gawk sed patch diffutils coreutils-install libffi
opkg install homeassistant
/opt/etc/init.d/S99hass start

After successful start I've got Home Assistant start page appeared in my browser by local address http://192.168.1.1:8123. After initial setup I had to get my all Mi Home devices in the new Home Assistant. I'm using Xiaomi Gateway 3 in my smart home, it runs well and allows to connect Bluetooth and Zigbee devices. At this step was super helpful HA integration Xiaomi Gateway 3 made by AlexxIT.
Custom components, to which this integration refers, are installed via HACS. But it possible to use the second way, which is also described in Readme: unpack "xiaomi_gateway3" directory from there and copy it (in my case) into "/opt//var/lib/homeassistant/custom_components" directory of the router. Restart Home Assistant, and now we have new integration Xiaomi Gateway 3.
Unfortunately at this point, depending on firmware version of Xiaomi gateway, it can be not so easy, possibly you will have to obtain gateway key.In my case, I've used this guide to connect UART to the gateway. After soldering I've run "minicom" utility on my Linux and have copied this key from initial logs of the gateway when it loading. Possibly you will not need to get this key.
After the gateway was successfully added into Home Assistant, all my Xiaomi devices connected to that gateway in Mi Home appeared in the new app, and now just need to sort them and start using.
Home Assistant runs in Keenetic Viva pretty smooth and without any side effects for router itself or Mi Home app (which is utilize the same gateway in the same time). Additional bonus from using Keenetic router is they domain names service KeenDNS. We can go to router page, select "Network rules" -> "Domain name", add new domain, select "This internet center" as destination, 8123 port, and in result, totally for free, we getting external access to uor smart home even without public IP.
Home Assistant will work simultaneously with the existing Mi Home, but now you can add components to it regardless of which Mi Home region they are intended for, as well as add many devices that are not present in Mi Home at all, write scenarios for them of any complexity, and do not be afraid of future failures of Xiaomi servers.
If you want to ask or add something, please left comment below.