Hep to add Device Tracker: Xiaomi MiWifi Mini

Device Tracker: Xiaomi MiWifi Mini

Hi, I have one Xiaomi router, to get the list of devices in home, I can make a GET request to:

http :// CONF_HOST/cgi-bin/luci/;stok=CONF_STOK/api/misystem/devicelist

example:

http :// 192.168.10.1/cgi-bin/luci/;stok=fe61dd1f6ce1e36734a4f5f1b92b38cb/api/misystem/devicelist

you can help me to fork components/device_tracker/ubus.py or similar to use this json?

The JSON result is like this:

{
	"code": 0,
	"list": [{
		"authority": {
			"admin": 1,
			"lan": 0,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "device_list_mi.png",
		"ip": [{
			"active": 1,
			"downspeed": "35",
			"ip": "192.168.10.150",
			"online": "35033",
			"upspeed": "19"
		}],
		"isap": 0,
		"mac": "28:6C:07:10:C3:46",
		"name": "yeelink-light-color1_miio48230235",
		"oname": "yeelink-light-color1_miio48230235",
		"online": 1,
		"parent": "",
		"push": 0,
		"statistics": {
			"downspeed": "35",
			"online": "35033",
			"upspeed": "19"
		},
		"times": 0,
		"type": 1
	}, {
		"authority": {
			"admin": 1,
			"lan": 0,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "device_list_samsung.png",
		"ip": [{
			"active": 1,
			"downspeed": "0",
			"ip": "192.168.10.175",
			"online": "28728",
			"upspeed": "0"
		}],
		"isap": 0,
		"mac": "64:B8:53:AA:EB:95",
		"name": "android-2dbd236c77d2d5dd",
		"oname": "android-2dbd236c77d2d5dd",
		"online": 1,
		"parent": "",
		"push": 1,
		"statistics": {
			"downspeed": "0",
			"online": "28728",
			"upspeed": "0"
		},
		"times": 0,
		"type": 1
	}, {
		"authority": {
			"admin": 1,
			"lan": 0,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "device_list_mi.png",
		"ip": [{
			"active": 1,
			"downspeed": "0",
			"ip": "192.168.10.198",
			"online": "2074",
			"upspeed": "0"
		}],
		"isap": 0,
		"mac": "28:6C:07:11:D4:76",
		"name": "yeelink-light-color1_miio48238731",
		"oname": "yeelink-light-color1_miio48238731",
		"online": 1,
		"parent": "",
		"push": 0,
		"statistics": {
			"downspeed": "0",
			"online": "2074",
			"upspeed": "0"
		},
		"times": 0,
		"type": 1
	}, {
		"authority": {
			"admin": 1,
			"lan": 1,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "device_list_mi.png",
		"ip": [{
			"active": 1,
			"downspeed": "0",
			"ip": "192.168.10.210",
			"online": "2735",
			"upspeed": "0"
		}],
		"isap": 0,
		"mac": "74:23:44:EA:22:58",
		"name": "Redmi3-GustavoUNO",
		"oname": "Redmi3-GustavoUNO",
		"online": 1,
		"parent": "",
		"push": 0,
		"statistics": {
			"downspeed": "0",
			"online": "2735",
			"upspeed": "0"
		},
		"times": 0,
		"type": 1
	}, {
		"authority": {
			"admin": 1,
			"lan": 0,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "",
		"ip": [{
			"active": 1,
			"downspeed": "38",
			"ip": "192.168.10.101",
			"online": "35033",
			"upspeed": "81"
		}],
		"isap": 0,
		"mac": "28:D9:8A:05:35:CE",
		"name": "28:D9:8A:05:35:CE",
		"oname": "28:D9:8A:05:35:CE",
		"online": 1,
		"parent": "",
		"push": 0,
		"statistics": {
			"downspeed": "38",
			"online": "35033",
			"upspeed": "81"
		},
		"times": 0,
		"type": 1
	}, {
		"authority": {
			"admin": 1,
			"lan": 0,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "device_list_mi.png",
		"ip": [{
			"active": 1,
			"downspeed": "0",
			"ip": "192.168.10.173",
			"online": "35033",
			"upspeed": "0"
		}],
		"isap": 0,
		"mac": "28:6C:07:10:AA:5D",
		"name": "yeelink-light-color1_miio48227954",
		"oname": "yeelink-light-color1_miio48227954",
		"online": 1,
		"parent": "",
		"push": 0,
		"statistics": {
			"downspeed": "0",
			"online": "35033",
			"upspeed": "0"
		},
		"times": 0,
		"type": 1
	}, {
		"authority": {
			"admin": 1,
			"lan": 0,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "device_list_mi.png",
		"ip": [{
			"active": 1,
			"downspeed": "29",
			"ip": "192.168.10.105",
			"online": "35033",
			"upspeed": "0"
		}],
		"isap": 0,
		"mac": "28:6C:07:10:BE:E1",
		"name": "yeelink-light-color1_miio48233206",
		"oname": "yeelink-light-color1_miio48233206",
		"online": 1,
		"parent": "",
		"push": 0,
		"statistics": {
			"downspeed": "29",
			"online": "35033",
			"upspeed": "0"
		},
		"times": 0,
		"type": 1
	}, {
		"authority": {
			"admin": 1,
			"lan": 0,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "device_list_mi.png",
		"ip": [{
			"active": 1,
			"downspeed": "0",
			"ip": "192.168.10.124",
			"online": "35033",
			"upspeed": "0"
		}],
		"isap": 0,
		"mac": "F0:B4:29:B4:4D:32",
		"name": "lumi-gateway-v3_miio45844807",
		"oname": "lumi-gateway-v3_miio45844807",
		"online": 1,
		"parent": "",
		"push": 0,
		"statistics": {
			"downspeed": "0",
			"online": "35033",
			"upspeed": "0"
		},
		"times": 0,
		"type": 1
	}, {
		"authority": {
			"admin": 1,
			"lan": 0,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "",
		"ip": [{
			"active": 1,
			"downspeed": "0",
			"ip": "192.168.10.205",
			"online": "34817",
			"upspeed": "0"
		}],
		"isap": 0,
		"mac": "AC:63:BE:DE:F7:9F",
		"name": "kindle-8d5c5ea15",
		"oname": "kindle-8d5c5ea15",
		"online": 1,
		"parent": "",
		"push": 0,
		"statistics": {
			"downspeed": "0",
			"online": "34817",
			"upspeed": "0"
		},
		"times": 0,
		"type": 1
	}, {
		"authority": {
			"admin": 1,
			"lan": 0,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "",
		"ip": [{
			"active": 1,
			"downspeed": "25",
			"ip": "192.168.10.199",
			"online": "1119",
			"upspeed": "25"
		}],
		"isap": 0,
		"mac": "80:A5:89:81:A2:C3",
		"name": "Unknown",
		"oname": "Unknown",
		"online": 1,
		"parent": "",
		"push": 0,
		"statistics": {
			"downspeed": "25",
			"online": "1119",
			"upspeed": "25"
		},
		"times": 0,
		"type": 1
	}, {
		"authority": {
			"admin": 1,
			"lan": 0,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "",
		"ip": [{
			"active": 1,
			"downspeed": "0",
			"ip": "192.168.10.238",
			"online": "35033",
			"upspeed": "0"
		}],
		"isap": 0,
		"mac": "B8:27:EB:C3:53:C6",
		"name": "HA",
		"oname": "raspberrypi",
		"online": 1,
		"parent": "",
		"push": 0,
		"statistics": {
			"downspeed": "0",
			"online": "35033",
			"upspeed": "0"
		},
		"times": 0,
		"type": 0
	}, {
		"authority": {
			"admin": 1,
			"lan": 0,
			"pridisk": 0,
			"wan": 1
		},
		"icon": "",
		"ip": [{
			"active": 1,
			"downspeed": "306",
			"ip": "192.168.10.201",
			"online": "721",
			"upspeed": "243"
		}],
		"isap": 0,
		"mac": "94:65:9C:9F:A1:00",
		"name": "uno-xps",
		"oname": "uno-xps",
		"online": 1,
		"parent": "",
		"push": 1,
		"statistics": {
			"downspeed": "306",
			"online": "721",
			"upspeed": "243"
		},
		"times": 0,
		"type": 2
	}],
	"mac": "94:65:9C:9F:A1:00"
}

I’ll have a look next year, I also have a Xiaomi Mini router, just didn’t know about his json capability. His the token fixed or is it renewed?

Is Xiaomi Mini router using OpenWrt? If so, maybe this works https://home-assistant.io/components/device_tracker.luci/

Not really, I think they customized an open source Firmware, but I’m not sure which it was.

OpenWrt installation is possible but the version is not stable yet (wifi signal loss are frequent), so it’s not a solution for now.

Device list can also be called by using username and password:
http://192.168.0.1/cgi-bin/luci/api/misystem/devicelist?username=admin&password=xxxxx

Token can be picked up by a call to
http://192.168.0.1/cgi-bin/luci/api/xqsystem/login?username=admin&password=xxxxx
The response JSON look like that:
{“url”:"/cgi-bin/luci/;stok=facad3fb0ebc2082e9e2380c1f9585e9/web/home",“token”:“facad3fb0ebc2082e9e2380c1f9585e9”,“code”:0}

It seems to use a modify version of luci, the url are a little different.

I created a first working device_tracker component for the Xiaomi Mi Router mini :grinning:

It’s a first draft and I’m sure there is room for improvement as this is the first time I write code in python!

https://github.com/RiRomain/home-assistant/commit/c32931d064d1f2b32e387662516a1735221bf206

https://github.com/home-assistant/home-assistant/pull/5283

Cool stuff, thanks! Will this work with Xiaomi router 3?

I suppose but I’m not sure…
Try to call this address with your user/pass (user is admin on the mini and cannot be changed):
http://192.168.0.1/cgi-bin/luci/api/xqsystem/login?username=admin&password=xxxxx

And then this one with your token (receive as response in the first link) in place of “YOUR_TOKEN”
http://192.168.0.1/cgi-bin/luci/;stok=YOUR_TOKEN/api/misystem/devicelist

Of course replace the ip with your router ip.

Yes, works perfectly fine! Can’t wait to have access to it. I’ve tried copy-past your xiaomi.py but it didn’t work. Couldn’t initialize or something. I’m also using the new Xiaomi gateway script that have the same name, do you think it’s getting conflicts? Maybe would be better if you could have a specific name like xiaomirouter.py?

Ah cool :smile: I didn’t find any api doc from xiaomi so I couldn’t be sure which would work or not.

For the conflict you must be right, their is way to include the package name when importing/instantiating the class but it will be easier to just rename it.

So the code was released today with home-assistant 0.36.0 :slight_smile:

I updated my main install (not the dev branch) and it worked without problem.
The documentation is also up:

I’m impressed at how easy it is to write custom component for HA, it was my first program in Python and took only few hours from start to release! :thumbsup:

1 Like

Hi @touliloup,

I’ve been having this errors:

17-02-19 21:44:54 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.4/asyncio/tasks.py”, line 237, in _step
result = next(coro)
File “/home/homeassistant/.homeassistant/custom_components/binary_sensor/xiaomi.py”, line 117, in _async_poll_status
self.xiaomi_hub.get_from_hub(self._sid)
File “/home/homeassistant/.homeassistant/custom_components/xiaomi.py”, line 344, in get_from_hub
resp = self._send_cmd(cmd, “read_ack”)
File “/home/homeassistant/.homeassistant/custom_components/xiaomi.py”, line 312, in _send_cmd
data, addr = self._socket.recvfrom(1024)
InterruptedError: [Errno 4] Interrupted system call

Do you have any idea what’s this about? It works though but from time to time I get those.

Thanks.

Regards,

You seem to have install some custom component for a xiaomi device. The trace doesn’t come from the device tracker if I see that right.

custom_components/binary_sensor/xiaomi.py

Maybe delete that file if it’s an early version of my device tracker implementation. The latest version is included in latest version of hass (>0.36.0)

Edit: it’s likely to come from xiaomi hub:
self.xiaomi_hub.get_from_hub

Omg, sorry my bad!
Anyway, do you have any idea why the known_devices file doesn’t populate anymore? Seems to be like that since v0.38

Does the tracking still work? I got a new device the 17.02 and it got registered in known_devices with version 0.38 without problem.
Could it be the custom component conflicting with the device tracker?

Are you still seeing the refresh info entry in log?
17-02-20 10:05:40 INFO (Thread-XX) [homeassistant.components.device_tracker.xiaomi] Refreshing device list

It’s working again since 0.38.4. Thanks :slight_smile:

I’m trying to make this work in my AX3600 configured as a relay.

However, when using this integration, no devices are shown with the integration at all.

When manually using this: using this URL http://192.168.x.x/cgi-bin/luci/api/misystem/devicelist?username=admin&password=XXXX
I only get a device (AA:AA:AA:AA:AA, fake MAC, of course), which is the one I’m currently using to connect to the router, despite having the router a lot of devices connected to it.
{“mac”:“AA:AA:AA:AA:AA”,“list”:[],“code”:0}

Same result with this: http://192.168.x.x/cgi-bin/luci/;stok=XXXXXXXX6bc74cc058749bf71f579516/api/misystem/devicelist

Am I doing anything wrong?

Exactly the same for Redmi AX6 running in AP (wired relay) mode. Seems that their firmware just doesn’t provide the connected devices list in this mode, both the android app and the web interface don’t show them either

1 Like