Xiaomi gateway not discovered

Hey again.

Thanks for further clarifying. This was my understanding but it is clear now.

Can Xiaomi force a GW update (what ports do I block, btw?) that kill local operation?

Is there a way I can rename the individual signee accessories?

Normally an upgrade must be triggered. If you block the ip address of the device it’s like a network without internet access. :wink:

I updated my firmware to the build 150 today. The error message is gone and the ring tone is working. May I suggest to add a note about the required firmware version in the docs?

There is a note already:

The gateway provides two services: xiaomi.play_ringtone and xiaomi.stop_ringtone. To play ringtones by
Home Assistant, the version of the gateway firmware must be 1.4.1_145 at least. A ringtone_id and
gw_mac must be supplied. The parameter ringtone_vol (percent) is optional. Allowed values of the ringtone_id are:

Arg, sorry! :slight_smile:

So, with Homeassistant 0.54 running in HASSIO and by specifying the host option in the configurations I am able to control my Xiaomi gateway and its connected sensors. Thank you very much for the excellent work.

2 Likes

I have the same Hassio 0.54 and still have the issue with ‘Unable to find component xiaomi’ Could you share your config file where you specify the host option?

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