Xiaomi Gateway Integration

Posted a bug fix. Motion event is firing too often when there’s a lot of movement. There’s now 1 second delay in between motion event being fired.

The default state of the sensor should be off. At the start of the device initialization, the status is read from the gateway response. You can try to put a logger here to find out what’s the message being sent by gateway

class XiaomiDevice(Entity):
    """Representation a base Xiaomi device."""

    def __init__(self, device, name, xiaomi_hub):
        """Initialize the xiaomi device."""
        self._sid = device['sid']
        self._name = '{}_{}'.format(name, self._sid)
        _LOGGER.error(device['data']) #Logger here
        self.parse_data(device['data'])

Thank you for the awesome work!

With the switch I am experiencing the issue, that only “long press” is picked up by HA.
I can see all presses in the app itself.

when you say picked up, do you mean using the click event as automation doesn’t work?

FYI, click and double click will not show a status in HA. it will only send an event. Only holding the button will create a status and shown in HA

Thanks for clarifying.
In this case it is all working as intended.

I have the same problem. All sensors on state “ON” after the Hass restarted. It needs to trigger it manually to make it back to normal.

Thanks @robg 5watts isn’t to bad… still works out to be $8.80 per year here in Sydney… i’m conscious now my sonos speakers cost around $3-4 each p/y to run… 6x = $24 p/y, so trying to avoid extra devices that run 24/7…

The most important thing to confirm before buying it - if I use this new Mi router as a bridge (keeping my TP-link routing internet traffic) setup a unique SSID that the hub connects to will this stop all the issues that I am facing? or do I need to throw internet traffic through it too? (Kinda defeats the purpose of the top of the line TP link router I bought…)

Keep in mind that HA and the gateway needs to by on the same network. Also, why do you think it’s a router issue? Since those are Zigbee button and you can use them with manual sid entry, i think it might not be a router issue. Have you done further debugging?

Thanks. Will add this right away before I experience another outage again.

Only hass? I usually restart hass due to config changes but I never observed that behavior. It might be possible to reproduce this issue if I restart my rpi.

Yep - same network I will just bridge my TPlink router & the Mi router.

I dont have any other explanations on why it might not be working… @barryhampants seems to have the same issue with a TP link too.

What other debugging can I do to try and get to the bottom of the issue? Happy to try anything to avoid buying a new router (and I would very frustrated if I bought a new router and it didn’t solve the issue).

HASS just shows a “configuration” issue, but a power cycle of the hub brings in back into hass, then a HASS restart knocks it out… (I also have the issue of having both 2.4ghz and 5ghz networks on at once will stop it all together - which I really like as my phone/mac will select the best network for the situation e.g. 2.4ghz in the backyard and 5ghz in the lounge to get full speed)

Try put a logger to find if the sid is being sent by the gateway

    def _discover_devices(self):

        cmd = '{"cmd" : "get_id_list"}'
        resp = self._send_cmd(cmd, "get_id_list_ack")
        if resp is None or "token" not in resp or "data" not in resp:
            return False
        self.update_key(resp["token"])
        sids = json.loads(resp["data"])
        _LOGGER.error(resp) #log here

If it doesn’t work all together, which means there’s issue with multicast communication. Router issue? Not really sure but i think i might have a dual band router somewhere, i can give it a try

Thanks @rave I really appricate all the support here! Truely dedicated to the cause of getting this working for everyone love it!

I have to apologies - i’m a tech geek without formal (or informal) programming training, so you might need to walk me through were I change / add that code into the platform. This has been a great learning exercise for me!

Edit the file custom_components\xiaomi.py

Here’s the code from line 265. Change it from

    def _discover_devices(self):

        cmd = '{"cmd" : "get_id_list"}'
        resp = self._send_cmd(cmd, "get_id_list_ack")
        if resp is None or "token" not in resp or "data" not in resp:
            return False
        self.update_key(resp["token"])
        sids = json.loads(resp["data"])
        sids.append(self.sid)

to

    def _discover_devices(self):

        cmd = '{"cmd" : "get_id_list"}'
        resp = self._send_cmd(cmd, "get_id_list_ack")
        if resp is None or "token" not in resp or "data" not in resp:
            return False
        self.update_key(resp["token"])
        sids = json.loads(resp["data"])
        _LOGGER.error(resp) #log here
        sids.append(self.sid)
1 Like

My initial issue was with getting the hub through the setup wizard as detailed earlier in the thread, disabling 5Ghz allowed me to get it connected to my home network. Once past initial setup and connected I was able to turn the 5Ghz back on. I have experienced this same behaviour with other devices including Google Home and it is a known fault, I only found it by googling the first time. There are other brands with same issue but I only know my own - Tp-Link Archer series.

The second issue is if the router is restarted. Some of my devices have to be power cycled AFTER the router is up and running. If they boot before the router it’s like they are in witness protection - they are connected, they work, and the router knows they are there but it wont pass messages on from or to Hass. I can restart Hass as much as I like and hub will reconnect consistently, it is only when the router itself is rebooted that I lose communications.

I have hass installed on a Pi 3b using the AIO installer. Pi is connected via ethernet to Router. Hass is up to date, hub has latest firmware and router I have on older firmware as TP-Link started removing features in later versions and blocking downgrades which made me nervous. Only other factor I might mention is that like you @Stewface I am located in Australia so using metric system, km, celcius etc and Australian WiFi bands. I have made sure to run US compatible channels on the Wifi Networks as have issues with foreign sourced devices in the past.

Just a short question before i start debugging the issue the next days.

Did somebody solve already in the latest commits the GHOST Openings of Door Sensors?

Yesterday i created a firewall rule, to deny the xiaomi gateway internet access and all door sensors after each other 2 times this day changing the state to “OPEN”…

Nothing like this happened the last weeks since i had the issue in the very beginning.

It makes no sense, i know just want to verify if someobdy is already aware of the issue?

Thanks guys for the great implementation btw.!

Is the xiaomi plug extending the (xiaomi) zigbee network?

Was previously running HA on windows but finally got a ubuntu server 16.04 VM running so I can transition everything onto the one box. Took me forever to get HA up and running since my linux skills are horrible.

Firstly tried HA with a docker image. it all booted fine and the devices are recognised and reporting. However there is no history. When I tap on say the temp sensor I see the current temp, last reported time, and then just a flat line for history.
Thought it could be the HA version since docker is on 0.37.1 so tried installed 0.36.1 directly to the VM. Still same issue.

Using the latest version on github.

I have device history for other HA components such as presence (using tomato router) and the weather ‘yr Symbol’

Any ideas?

You have the same problem with just the door sensors?

I might have restarted the hass service yesterday as well, but i got night time suddently a telegram notification that all windows are open, this time i didn’t restart!

Just to make sure my setup:

pi3, multi interface setup (lan, wlan) the service runs on the wlan interface. To the same SSID connected like the xiaomi gateway and alexa.

yelight bulbs in the same ssid connected, but had before the same issue without the bulbs in the same wifi.

I don’t use the latest commit yet.

Xiaomi gateway: wifi only, internet denied.

I hope its not a setup specific issue.

I had the issue with hass 0.36.1 and now latest release 0.37.1

The Mi Home app had before no status in the history that any door sensor got triggered (when my xiaomi gateway had internet)

I would like to get rid of this issue to use the door sensors for my alarm configuration.