Help getting Xiaomi Gateway to work with HA

So I received my Xiaomi Gateway yesturday and connected it to MiApp, addedd thermometer and wall switch. Then Enabled LAN communication, and started to configure it in HA. And I’m stuck now. Watever I do I always end 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
2017-11-17 09:22:47 INFO (MainThread) [homeassistant.setup] Setup of domain xiaomi_aqara took 50.2 seconds.
2017-11-17 09:22:47 ERROR (MainThread) [homeassistant.setup] Setup failed for xiaomi_aqara: Component failed to initialize.
2017-11-17 09:22:47 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=persistent_notification, service=create, service_data=title=Invalid config, message=The following components and platforms could not b
e set up:

 - [xiaomi-aqara](https://home-assistant.io/components/xiaomi_aqara/)

Please check your config., notification_id=invalid_config, service_call_id=140187416971672-4>
2017-11-17 09:22:47 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=persistent_notification.invalid_config, old_state=None, new_state=<state persistent_notification.invalid_config=notifying; title=I
nvalid config, message=The following components and platforms could not be set up:

 - [xiaomi-aqara](https://home-assistant.io/components/xiaomi_aqara/)

Please check your config. @ 2017-11-17T09:22:47.585736+01:00>>

I tried to set all combination with mac: and without, with host: I get additional error:

2017-11-17 09:20:20 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

My configuration is for now:

xiaomi_aqara:
   discovery_retry: 10
   gateways:
     - key: de1j0j5224keprbk
       mac: 34:ce:00:8d:46:b3

my LAN card is supporting multicast:

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

My HA is installed on NetBSD box with pf firewall when I explicity allow multicast, but I don’t even filter anything on inside interface:

# Allow multicast
pass proto udp from any to 224.0.0.0/4 keep state
pass proto udp from 224.0.0.0/4 to any keep state

Xiaomi Gateway has udp/9898 port open (checked from HA host)

dom# nmap -sU -p 9898 10.1.78.213

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

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

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

My HA is 57.3 updated yesturday. I checked and reenabled LAN communication and recreated key couple of times. Even hardresetted whole device. FW of Gateway is 1.4.1_150.0143

So now I’m clueless of what wlese I can check.

Any help would be most appreciated

Try removing the colons from the MAC address in the config.

Same:

2017-11-17 10:38:05 ERROR (SyncWorker_0) [homeassistant.components.xiaomi_aqara] No gateway discovered
2017-11-17 10:38:05 INFO (MainThread) [homeassistant.setup] Setup of domain xiaomi_aqara took 50.2 seconds.
2017-11-17 10:38:05 ERROR (MainThread) [homeassistant.setup] Setup failed for xiaomi_aqara: Component failed to initialize.

with:

xiaomi_aqara:
  discovery_retry: 10
  gateways:
    - key: de1j0j5224keprbk
      mac: 34ce008d46b3

Is the key correct? Mine is only hex digits 0-F.

How did you get the Xiaomi key? Was it from the developer mode via the app? My keys are in capital letters. Also. I am not using mac but do have

host: 192.168.0.1 above my key
where the ip is my xiaomi gw ip address

so my configuration for xiaomi is

  gateways:
    - host: 192.168.0.1
      mac:
      key:  ABCDE345347

Checked this, but here is a screenshot to be sure.

so your key has changed again. did you update your configuration file to reflect this?

Tried both as ip of GW for Xiaomi is HA itslef - no luck

xiaomi_aqara:
  discovery_retry: 10
  gateways:
    - host: localhost
      mac:
      key: de1j0j5224keprbk

xiaomi_aqara:
  discovery_retry: 10
  gateways:
    - host: localhost
      mac:
      key: de1j0j5224keprbk

Oh, so sorry, with the screenshot, uploaded one from yesturday, correct one is:

do you know the ip address of the gateway? try using that instead of localhost

Ah yes, second attempt was with IP. To summarize: gateway (xiaomi device) is 10.1.78.213, gateway (default route for xiaomi) is 10.1.78.1. HA is installed on 10.1.78.1

xiaomi_aqara:
  discovery_retry: 10
  gateways:
    - host: localhost
      mac:
      key: de1j0j5224keprbk

xiaomi_aqara:
  discovery_retry: 10
  gateways:
    - host: 10.1.78.1
      mac:
      key: de1j0j5224keprbk

Something have changed with config like this:

xiaomi_aqara:
  discovery_retry: 10
  gateways:
    - host: 10.1.78.1
      mac: 34:ce:00:8d:46:b3
      key: de1j0j5224keprbk

Still no connection but other errors:

2017-11-17 11:07:37 ERROR (SyncWorker_0) [PyXiaomiGateway] Cannot connect to Gateway
2017-11-17 11:07:37 INFO (SyncWorker_0) [PyXiaomiGateway] Discovering Xiaomi Devices
2017-11-17 11:07:47 ERROR (SyncWorker_0) [PyXiaomiGateway] Cannot connect to Gateway
2017-11-17 11:07:47 INFO (SyncWorker_0) [PyXiaomiGateway] Discovering Xiaomi Devices
2017-11-17 11:07:57 ERROR (SyncWorker_0) [PyXiaomiGateway] Cannot connect to Gateway
2017-11-17 11:07:57 INFO (SyncWorker_0) [PyXiaomiGateway] Discovering Xiaomi Devices
2017-11-17 11:08:07 ERROR (SyncWorker_0) [PyXiaomiGateway] Cannot connect to Gateway
2017-11-17 11:08:12 INFO (SyncWorker_0) [PyXiaomiGateway] Gateway discovery finished in 5 seconds
2017-11-17 11:08:12 INFO (SyncWorker_0) [PyXiaomiGateway] Creating Multicast Socket
2017-11-17 11:08:12 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

thats no good :frowning:

can you try removing the discovery_retry line and also remove the host: and also remove the mac value? so just have

xiaomi_aqara:
  gateways:
    - mac:
      key: de1j0j5224keprbk

Still no luck, with this config I get:

2017-11-17 11:22:23 ERROR (SyncWorker_3) [homeassistant.components.xiaomi_aqara] No gateway discovered
2017-11-17 11:22:23 INFO (MainThread) [homeassistant.setup] Setup of domain xiaomi_aqara took 15.4 seconds.
2017-11-17 11:22:23 ERROR (MainThread) [homeassistant.setup] Setup failed for xiaomi_aqara: Component failed to initialize.
1 Like

So, with this config: (please ignore changed password, as I have done many restarts etc.)

 xiaomi_aqara:
   gateways:
     - mac: 34CE008D46B3
       key: zslh8govlznj9vrx
       host: 10.1.78.213

My HA is able to discover Gateway:

2017-11-17 12:55:23 INFO (SyncWorker_4) [homeassistant.components.xiaomi_aqara] Discovering Xiaomi Gateways (Try 1)
2017-11-17 12:55:23 INFO (SyncWorker_4) [PyXiaomiGateway] Xiaomi Gateway 34ce008d46b3 configured at IP 10.1.78.213:9898
2017-11-17 12:55:23 INFO (SyncWorker_4) [PyXiaomiGateway] Discovering Xiaomi Devices
2017-11-17 12:55:23 INFO (SyncWorker_4) [PyXiaomiGateway] Found 3 devices

However couple of seconds later in logs I’m getting:

2017-11-17 12:55:28 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

Which leads again to:
2017-11-17 13_03_30-Home Assistant

Any further ideas?

I had no end of trouble with this as well until I was suggested to try this:

xiaomi_aqara:
  discovery_retry: 10
  gateways:
    - mac:
      key: xxxxxuu9ybtxxxxx

Note the difference from the suggestion further up is the discovery retry.

Tried that also, after 10 tries Gateway is not discovered. Only works for me if I point HA to IP of Gateway with host: but then I’m getting this python errors that I don’t understand.

Looks to me like some problem with the IP address, I could not get it to wok if I specified an IP address. I am just guessing but ave you tried:

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

EDIT: Actually looking at the docs the first two arguments should be constants so it might be the mreq variable? So in short I dont know…sorry…

this as well is not working, only with complete information like mac, key and host it is discovered , then python error occurs… :confused:

What about firmware for the gateway. Have you updated that?