Xiaomi Gateway Integration

Yeah, only Zigbee devices which is connected to the gateway is supported. I couldn’t find anything online about any open port or API to connect to the plug. If someone has any slight about it then i would be happy to implement it as well

1 Like

I can add the XiaoMi gateway into the HASS, but I can’t control the Gateway-Light through the hass-webpage. When I closed the Gateway-Light on the HASS webpage, after 2 or 3 seconds, the toggle “back to OFF” by itself.

Another stange thing: when I use the phone-app(xiaomi app) to change the light color, the changing showing on the webpage of HASS.

Hardware:Rpi3 + XiaoMi gateway(only)
Software: HASSbian 1.1+toybox
HassVersion: 0.39.1
HASS-configuration:
xiaomi:
gateways:
- sid:
key: cf44de72xxxxxxxx

log:

17-02-27 07:50:52 INFO (Thread-5) [custom_components.xiaomi] Expecting 1 gateways
17-02-27 07:50:52 INFO (Thread-5) [custom_components.xiaomi] Discovering Xiaomi Gateways (Try 1)
17-02-27 07:50:52 INFO (Thread-5) [custom_components.xiaomi] Xiaomi Gateway f0b429b3eae8 found at IP 192.168.1.188
17-02-27 07:50:52 INFO (Thread-5) [custom_components.xiaomi] Discovering Xiaomi Devices
17-02-27 07:50:52 DEBUG (Thread-5) [custom_components.xiaomi] >> b’{“cmd” : “get_id_list”}’
17-02-27 07:50:52 DEBUG (Thread-5) [custom_components.xiaomi] << {‘sid’: ‘f0b429b3eae8’, ‘data’: ‘[]’, ‘token’: ‘J31zPwX8dLD8QM7R’, ‘cmd’: ‘get_id_list_ack’}
17-02-27 07:50:52 INFO (Thread-5) [custom_components.xiaomi] Found 1 devices
17-02-27 07:50:52 DEBUG (Thread-5) [custom_components.xiaomi] >> b’{“cmd”:“read”,“sid”:“f0b429b3eae8”}’
17-02-27 07:50:52 DEBUG (Thread-5) [custom_components.xiaomi] << {‘short_id’: 0, ‘sid’: ‘f0b429b3eae8’, ‘model’: ‘gateway’, ‘data’: ‘{“rgb”:637468881}’, ‘cmd’: ‘read_ack’}
17-02-27 07:50:57 INFO (Thread-5) [custom_components.xiaomi] Gateway finding finished in 5 seconds
17-02-27 07:50:57 INFO (Thread-5) [custom_components.xiaomi] Creating Multicast Socket
17-02-27 07:50:57 INFO (Thread-5) [custom_components.xiaomi] Listening for broadcast
17-02-27 07:51:03 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {‘short_id’: 0, ‘sid’: ‘f0b429b3eae8’, ‘model’: ‘gateway’, ‘data’: ‘{“rgb”:637468728}’, ‘cmd’: ‘report’}
17-02-27 07:51:03 DEBUG (Thread-8) [custom_components.xiaomi] PUSH >> : {‘rgb’: 637468728}
17-02-27 07:51:12 DEBUG (Thread-1) [custom_components.xiaomi] >> b’{“sid”: “f0b429b3eae8”, “data”: {“key”: “926c4816cf976ee46a11c3eddab5908d”, “rgb”: 1593831908}, “cmd”: “write”}’
17-02-27 07:51:12 DEBUG (Thread-1) [custom_components.xiaomi] << {‘sid’: ‘f0b429b3eae8’, ‘data’: ‘{“error”:“Invalid key”}’, ‘cmd’: ‘write_ack’}
17-02-27 07:51:12 ERROR (Thread-1) [custom_components.xiaomi] Got error element in data {“error”:“Invalid key”}
17-02-27 07:51:14 DEBUG (Thread-9) [custom_components.xiaomi] >> b’{“sid”: “f0b429b3eae8”, “data”: {“key”: “926c4816cf976ee46a11c3eddab5908d”, “rgb”: 1577910147}, “cmd”: “write”}’
17-02-27 07:51:14 DEBUG (Thread-9) [custom_components.xiaomi] << {‘sid’: ‘f0b429b3eae8’, ‘data’: ‘{“error”:“Invalid key”}’, ‘cmd’: ‘write_ack’}
17-02-27 07:51:14 ERROR (Thread-9) [custom_components.xiaomi] Got error element in data {“error”:“Invalid key”}
17-02-27 07:51:15 DEBUG (Thread-3) [custom_components.xiaomi] >> b’{“sid”: “f0b429b3eae8”, “data”: {“key”: “926c4816cf976ee46a11c3eddab5908d”, “rgb”: 1577910147}, “cmd”: “write”}’
17-02-27 07:51:15 DEBUG (Thread-3) [custom_components.xiaomi] << {‘sid’: ‘f0b429b3eae8’, ‘data’: ‘{“error”:“Invalid key”}’, ‘cmd’: ‘write_ack’}
17-02-27 07:51:15 ERROR (Thread-3) [custom_components.xiaomi] Got error element in data {“error”:“Invalid key”}
17-02-27 07:51:15 DEBUG (Thread-11) [custom_components.xiaomi] >> b’{“sid”: “f0b429b3eae8”, “data”: {“key”: “926c4816cf976ee46a11c3eddab5908d”, “rgb”: 1579555071}, “cmd”: “write”}’
17-02-27 07:51:15 DEBUG (Thread-11) [custom_components.xiaomi] << {‘sid’: ‘f0b429b3eae8’, ‘data’: ‘{“error”:“Invalid key”}’, ‘cmd’: ‘write_ack’}
17-02-27 07:51:15 ERROR (Thread-11) [custom_components.xiaomi] Got error element in data {“error”:“Invalid key”}
17-02-27 07:51:16 DEBUG (Thread-6) [custom_components.xiaomi] >> b’{“sid”: “f0b429b3eae8”, “data”: {“key”: “121a6577116b2fa703e627a487bae06b”, “rgb”: 1592330751}, “cmd”: “write”}’
17-02-27 07:51:16 DEBUG (Thread-6) [custom_components.xiaomi] << {‘sid’: ‘f0b429b3eae8’, ‘data’: ‘{“error”:“Invalid key”}’, ‘cmd’: ‘write_ack’}
17-02-27 07:51:16 ERROR (Thread-6) [custom_components.xiaomi] Got error element in data {“error”:“Invalid key”}
17-02-27 07:51:17 DEBUG (Thread-5) [custom_components.xiaomi] >> b’{“sid”: “f0b429b3eae8”, “data”: {“key”: “121a6577116b2fa703e627a487bae06b”, “rgb”: 1592852700}, “cmd”: “write”}’
17-02-27 07:51:17 DEBUG (Thread-5) [custom_components.xiaomi] << {‘sid’: ‘f0b429b3eae8’, ‘data’: ‘{“error”:“Invalid key”}’, ‘cmd’: ‘write_ack’}
17-02-27 07:51:17 ERROR (Thread-5) [custom_components.xiaomi] Got error element in data {“error”:“Invalid key”}
17-02-27 07:51:17 DEBUG (Thread-7) [custom_components.xiaomi] >> b’{“sid”: “f0b429b3eae8”, “data”: {“key”: “121a6577116b2fa703e627a487bae06b”, “rgb”: 1593180298}, “cmd”: “write”}’
17-02-27 07:51:17 DEBUG (Thread-7) [custom_components.xiaomi] << {‘sid’: ‘f0b429b3eae8’, ‘data’: ‘{“error”:“Invalid key”}’, ‘cmd’: ‘write_ack’}
17-02-27 07:51:17 ERROR (Thread-7) [custom_components.xiaomi] Got error element in data {“error”:“Invalid key”}
17-02-27 07:51:18 DEBUG (Thread-4) [custom_components.xiaomi] >> b’{“sid”: “f0b429b3eae8”, “data”: {“key”: “121a6577116b2fa703e627a487bae06b”, “rgb”: 1581974271}, “cmd”: “write”}’
17-02-27 07:51:18 DEBUG (Thread-4) [custom_components.xiaomi] << {‘sid’: ‘f0b429b3eae8’, ‘data’: ‘{“error”:“Invalid key”}’, ‘cmd’: ‘write_ack’}
17-02-27 07:51:18 ERROR (Thread-4) [custom_components.xiaomi] Got error element in data {“error”:“Invalid key”}
17-02-27 07:51:44 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {‘short_id’: 0, ‘sid’: ‘f0b429b3eae8’, ‘model’: ‘gateway’, ‘data’: ‘{“rgb”:637534178}’, ‘cmd’: ‘report’}
17-02-27 07:51:44 DEBUG (Thread-1) [custom_components.xiaomi] PUSH >> : {‘rgb’: 637534178}
17-02-27 07:51:56 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {‘short_id’: 0, ‘sid’: ‘f0b429b3eae8’, ‘model’: ‘gateway’, ‘data’: ‘{“rgb”:1191182306}’, ‘cmd’: ‘report’}
17-02-27 07:51:56 DEBUG (Thread-9) [custom_components.xiaomi] PUSH >> : {‘rgb’: 1191182306}
17-02-27 07:52:01 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {‘short_id’: 0, ‘sid’: ‘f0b429b3eae8’, ‘model’: ‘gateway’, ‘data’: ‘{“rgb”:318767074}’, ‘cmd’: ‘report’}
17-02-27 07:52:01 DEBUG (Thread-11) [custom_components.xiaomi] PUSH >> : {‘rgb’: 318767074}
17-02-27 07:52:04 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {‘short_id’: 0, ‘sid’: ‘f0b429b3eae8’, ‘model’: ‘gateway’, ‘data’: ‘{“rgb”:1006632930}’, ‘cmd’: ‘report’}
17-02-27 07:52:04 DEBUG (Thread-6) [custom_components.xiaomi] PUSH >> : {‘rgb’: 1006632930}
17-02-27 07:52:07 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {‘short_id’: 0, ‘sid’: ‘f0b429b3eae8’, ‘model’: ‘gateway’, ‘data’: ‘{“rgb”:1577058274}’, ‘cmd’: ‘report’}
17-02-27 07:52:07 DEBUG (Thread-5) [custom_components.xiaomi] PUSH >> : {‘rgb’: 1577058274}
17-02-27 07:52:10 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {‘short_id’: 0, ‘sid’: ‘f0b429b3eae8’, ‘model’: ‘gateway’, ‘data’: ‘{“rgb”:251658210}’, ‘cmd’: ‘report’}
17-02-27 07:52:10 DEBUG (Thread-7) [custom_components.xiaomi] PUSH >> : {‘rgb’: 251658210}

The one where you merged my pull request. I had been using it without too much trouble for quite some time (save for having an error thrown on shutdown/restarts about OSError). I updated it to take advantage of resolving the 100ºC/0% Humidity value of the temperature sensor and then rolled it back after a few hours.

https://github.com/lazcad/homeassistant/commit/1051dbc95d7f87d0fac96129b889d066b91f8d54

Invalid key means your API key is wrong. Double check the key for typo.

On my second gateway I always receive this error, but on the Miapp is working everythin fine. And the IP is correct (static IP and checked)

17-02-27 09:38:31 ERROR (Thread-19) [custom_components.xiaomi] Unknown gateway ip 192.168.1.231

THANKS!

Bingo!That’s my fool…

Because I not have a Android phone,so I get the key from a iPhone xiaomi app before,that’s FAKE!

when I use a Android-simulator,and get the key with Android xiaomi app,everything is OK!

1 Like

@rave do you know if there is some tts support planed? would be awesome to use the gateway’s speaker for tts output. Additional the light sensor information would be great in hass, is the api supporting it already?

Hi,

I’m trying to use this logic: https://home-assistant.io/cookbook/turn_on_light_for_10_minutes_when_motion_detected/ with the movement sensors, but for some reason since polling was added those don’t seem to work anymore, after 10 minutes the light turns off and I have to wait a random time between 0-1 minute for the sensor to activate again and the light turns on again for 10 more minutes.
Anyone experiencing the same problem?

Thanks

I had same problem. And solved it like this. Turn on light whet there is movement and then wait for no movement. The timer resets every time there is movement and light stays on.

1 Like

Whatever that’s in the API is already supported by this component. Just need to wait until they release new firmware

1 Like

That message means the gateway has not been added initially into HA during startup. Try restart HA

You can also turn off polling by setting

poll_motion=false

Hi @rave ,

On the documentation:

poll_motion: True # True (Default) or False. If False, motion sensor will be deactivated in 2 minutes. Turn on polling will shorten the time to 1 minute

It’s a bit misleading. So, changing to false will use the Gateway broadcast and changing to true will use the HA pool every minute?
I still don’t really understand the logic from one to another.

Thanks

Yes yes, but it happens all the time

@anon35356645
Try to add discovery_retry: 5 to your config. (You can also try with a higher number)
You can also try to increase the timeout here: https://github.com/lazcad/homeassistant/blob/master/components/xiaomi.py#L135

Changing to False will use gateway broadcast which will only send a “no motion” message after 2 minutes without motion.

Changing to True means it will request motion status from the gateway every 10 seconds when a motion is detected until the gateway reply with a “no motion” message which is 60 seconds

2 Likes

Just fixed the bug. Please try the latest commit

2 Likes

Now if i turn on and off gateway light from mi home it turns it off in HA. And that is ok. But if i turn on light from HA i can’t turn it off from HA or mi home. It can only be turn off from developer tools and set state to off on gateway light. And then i must change brightness in mi home on gateway light to get it working again.

Yeah, there’s still some bug when i tested them again. Working on it now

Edit. Bug fixed again. Hopefully it works this time

Now i get this in log. ERROR (Thread-5) [custom_components.xiaomi] Cannot connect to Gateway, and again if i turn it off from ha can’t turn it on from mi home or wireless switch.