Xiaomi gateway not discovered

Yes, of course:

xiaomi_aqara:
  gateways:
   - host: x.x.x.x
     mac: xxxxxxxxxxxx
     key: xxxxxxxxxxxxxxxxxx
1 Like

@goprojojo have you resolved this issue? I have the same one (
in HA logs I can see this:

2017-10-11 22:02:28 INFO (MainThread) [homeassistant.components.discovery] Found new service: xiaomi_gw {'host': '192.168.0.88', 'port': 54321, 'hostname': 'lumi-gateway-v3_miio56564922.local.', 'properties': {'epoch': '1', 'mac': '34ce00853f5b'}, 'mac_address': '34ce00853f5b'}
2017-10-11 22:02:28 INFO (MainThread) [homeassistant.core] Bus:Handling <Event platform_discovered[L]: service=xiaomi_gw, discovered=host=192.168.0.88, port=54321, hostname=lumi-gateway-v3_miio56564922.local., properties=epoch=1, mac=34ce00853f5b, mac_address=34ce00853f5b>

but it always appears after unsuccessful discovering attempts. It doesn’t matter whether there is the xiaomi_aquara section or not, whether there is discovery_retry=3 or =10.

At the same time, this gateway can successfully work with Domoticz :disappointed:

More info if it is important:

C:\Program Files (x86)\Nmap>nmap -sU -p 9898 192.168.0.88

Starting Nmap 7.60 ( https://nmap.org ) at 2017-10-11 22:17
Nmap scan report for 192.168.0.88
Host is up (0.0011s latency).

PORT STATE SERVICE
9898/udp open monkeycom
MAC Address: 34:CE:00:85:3F:5B (Xiaomi Electronics,co.)

Nmap done: 1 IP address (1 host up) scanned in 2.08 seconds

Please specify a host:

xiaomi_aqara:
  gateways:
   - host: x.x.x.x
     mac: xxxxxxxxxxxx
     key: xxxxxxxxxxxxxxxxxx

and disable the “discovery” component just for testing.

1 Like

@syssi It’s a magic :smiley:
I played with host earlier. But disabling the ‘discovery’ component really helped. Gateway is on HA now, even after re-enabling the ‘discovery’.

Thanks a lot!

Hi, Are there any updates/documentation on how to get the Xiaomi gateway working stable?

Very frustrating, I had it working, setup all my devices incl customisations… thenrebooted and now not able to discover the gateway, have tried multiple re-itterations of the config.yaml entry with no avail…

Can someone update the docs if there is a solution out there?

Thanks

I’m late to the party about problems with connecting Gateway to HA, but this thread looks promising, so maybe someone could help.

I’m running HA 57.3 on NetBSD 7.1.

After couple of tries, and ending with:

2017-11-17 09:22:42 INFO (SyncWorker_2) [homeassistant.components.xiaomi_aqara] Discovering Xiaomi Gateways (Try 10)
2017-11-17 09:22:47 INFO (SyncWorker_2) [PyXiaomiGateway] Gateway discovery finished in 5 seconds
2017-11-17 09:22:47 ERROR (SyncWorker_2) [homeassistant.components.xiaomi_aqara] No gateway discovered

I finally got Gateway discovered with this settings:

xiaomi_aqara:
   discovery_retry: 10
   gateways:
     - mac: 34ce008d46b3
       key: zslh8govlznj9vrx
       host: 10.1.78.213

Even in first try:

2017-11-18 20:04:17 INFO (MainThread) [homeassistant.setup] Setting up xiaomi_aqara
2017-11-18 20:04:17 INFO (SyncWorker_4) [homeassistant.components.xiaomi_aqara] Discovering Xiaomi Gateways (Try 1)
2017-11-18 20:04:17 INFO (SyncWorker_4) [PyXiaomiGateway] Xiaomi Gateway 34ce008d46b3 configured at IP 10.1.78.213:9898
2017-11-18 20:04:17 INFO (SyncWorker_4) [PyXiaomiGateway] Discovering Xiaomi Devices
2017-11-18 20:04:17 INFO (SyncWorker_4) [PyXiaomiGateway] Found 3 devices

However, after discovery, I have some weird errors in log:

2017-11-18 20:04:22 INFO (SyncWorker_4) [PyXiaomiGateway] Gateway discovery finished in 5 seconds
2017-11-18 20:04:22 INFO (SyncWorker_4) [PyXiaomiGateway] Creating Multicast Socket
2017-11-18 20:04:22 ERROR (MainThread) [homeassistant.setup] Error during setup of component xiaomi_aqara
Traceback (most recent call last):
  File "/usr/pkg/lib/python3.6/site-packages/homeassistant/setup.py", line 194, in _async_setup_component
    component.setup, hass, processed_config)
  File "/usr/pkg/lib/python3.6/asyncio/futures.py", line 332, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/pkg/lib/python3.6/asyncio/tasks.py", line 250, in _wakeup
    future.result()
  File "/usr/pkg/lib/python3.6/asyncio/futures.py", line 245, in result
    raise self._exception
  File "/usr/pkg/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/pkg/lib/python3.6/site-packages/homeassistant/components/xiaomi_aqara.py", line 122, in setup
    xiaomi.listen()
  File "/root/.homeassistant/deps/lib/python3.6/site-packages/PyXiaomiGateway/__init__.py", line 133, in listen
    self._mcastsocket = self._create_mcast_socket()
  File "/root/.homeassistant/deps/lib/python3.6/site-packages/PyXiaomiGateway/__init__.py", line 126, in _create_mcast_socket
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
OSError: [Errno 22] Invalid argument

For what I gathered in forum in past three days Gateway requires to use multicast, and for what I can tell I have this configured correctly:

Doing ngrep (from HA host) shows some “beacons” from Gateway

dom# ngrep -l -q -d re1 -i "" udp and port 9898
interface: re1 (10.1.78.0/255.255.255.0)
filter: (ip or ip6) and ( udp and port 9898 )

U 10.1.78.213:4321 -> 224.0.0.50:9898
  {"cmd":"heartbeat","model":"gateway","sid":"34ce008d46b3","short_id":"0","token":"F8rlsHrcGiX2y4Gw","data":"{\"i
  p\":\"10.1.78.213\"}"}

U 10.1.78.213:4321 -> 224.0.0.50:9898
  {"cmd":"heartbeat","model":"gateway","sid":"34ce008d46b3","short_id":"0","token":"4ApaWmpNoerolAye","data":"{\"i
  p\":\"10.1.78.213\"}"}

LAN interface supports multicast:
re1: flags=8b43<UP,BROADCAST,RUNNING,PROMISC,ALLMULTI,SIMPLEX>re1: flags=8b43<MULTICAST> mtu 1500

Port 9898/UDP on Gateway is open:
dom# nmap -sU -p9898 10.1.78.213

Starting Nmap 7.60 ( https://nmap.org ) at 2017-11-18 20:09 CET
Nmap scan report for 10.1.78.213
Host is up (-0.056s latency).

PORT     STATE SERVICE
9898/udp open  monkeycom
MAC Address: 34:CE:00:8D:46:B3 (Xiaomi Electronics,co.)

Firmware of my Gateway is 1.4.1_150.0143 (updated after discovering in MiApp) and has connected two other devices - thermometer and wall switch.

If one could help debug, what is going on, it would be wonderfull.

Sorry for doubling topics, because I previously started one, but here are more technical posts, and I believe this is more about code that configuration. Other topic with tries of getting Gateway to work is: Help getting Xiaomi Gateway to work with HA if You are interested.

Thank You in advance.

I assume the setsockopt call must be optimized for NetBSD.

I can happily test it if one is willing to propose changes, unfortunately I don’t know Python (yet) :slight_smile:

I don’t know NetBSD yet. :wink: I will have a look at the IP stack of NetBSD and try to provide a patch for testing.

Only similiar problem (constructing multicast socket?) I have found is here, maybe helpfull

https://mail-index.netbsd.org/tech-kern/2013/09/30/msg015614.html

1 Like

Just edit this file and adopt line 126 accordingly. I’m sure you will find a solution on your own. Please provide the fix if you are successful.

Ok, will do my best with this.

Well, I kind of solved it by changing:

mreq = struct.pack("4sl", socket.inet_aton(self.MULTICAST_ADDRESS), socket.INADDR_ANY)

to

mreq = struct.pack("=4sl", socket.inet_aton(self.MULTICAST_ADDRESS), socket.INADDR_ANY)

If You missed what is a change its = mark near the 4sl :wink:

For what I read here https://docs.python.org/2/library/struct.html#format-strings this means to use native (OS based?) “byte order”. SO I assumed native is better than non-native, and surprisingly it worked :smiley:

1 Like

Could you provide the output/return value of “platform.system()”? I would like to do something like this:

https://github.com/Danielhiversen/PyXiaomiGateway/blob/master/PyXiaomiGateway/init.py#L120

Sure:

>>> platform.system()
'NetBSD'

One more thing, although now Gateway is discovered with all connected devices, it seems to only query it only one time, for example since I got this working at 15:00, I have straing line in temperature until now. Also when I tried to turn on light in Gateway or in wall switch i got:

2017-11-20 21:20:46 ERROR (SyncWorker_0) [PyXiaomiGateway] Got error element in data {“error”:“Invalid key”}

Of course key from mi app is correct, I tried to regenerate it, same behavior.

What is even more odd, on Centos VM with test HA it is not working also with same behavior (invalid key), but in FreeBSD VM also with test HA - I can turn it off and on how many times I want.

Any suggestion to this?

1 Like

I also can’t seem to discover my gateway. It works on a Hassio install with the same config.

I’m running Home Assistant on Docker on a Synology NAS on VMare on Windows 10. Everything seems to work fine, except this. Been working on it for HOURS. Wtf am I missing?

Maybe this is a solution for you.
Generate key at iOS will not work at this moment. You get a wrong key.
Generate key at Android is the solution for me. I get the right key with lowercase.

Thanks, already had it working. It was a VPN issue on Synology that blocked it. Once it’s discovered I can turn the VPN back on.

Hey guys, I think I’m in the same situation.

I initially setup home assistant in a docker and the gateway (firmware 1.4.1_151.0143) was detected and showing data from the sensors. After a couple of hours it would show the sensors as unavailable. Restarting the home assistant docker would reconnect fine…for a couple more hours.
I’ve since started investigation in other areas - more so the network. I’ve been playing around with node-red and can see udp traffic, but not on port 9898.

Wireshark shows UDP traffic from my gateway to 224.0.0.251 on port 5353.
This traffic is just “cache flush” messages and contains no sensor data.
I believe that it’s meant to be broadcasting to 224.0.0.50 on udp port 9898 but I see no traffic to port 9898 at all.

nmap shows that 9898 is open
9898/udp open monkeycom

I would expect to be seeing traffic on port 9898 with sensor data.
I’ve tried a couple of different networks, and hard reset the gateway multiple time but I get the same result.

The gateway works through the mi home app on android fine.
More info:
I’m running my docker containers with the --net host command, so it has an address on the same network as the gateway. So it should have full access to the host’s network.

Further investigation:
I installed Multicast Tester on my android phone and can see all the right multicast traffic on 224.0.0.50 port 9898. Still unsure as to why wireshark cannot see it.
I also spun up another docker container that runs tcpdump (corfr/tcpdump) and that can also view the right traffic. This container also uses --net host.

Am I making the right assumptions here?
Any help would be appreciated.

thanks,

Cam