Xiaomi Gateway Integration

Yes, but you need to be able to configure your router to do it.
You wont see any firmware updates, and wont be able to use the app - e.g. to add new devices, without reenabling the gateway to see internet.

Just switched from custom component to 0.50.2 version of HASS. Now I am getting this error:

2017-08-02 00:58:05 ERROR (MainThread) [homeassistant.setup] Error during setup of component xiaomi
Traceback (most recent call last):
File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/setup.py", line 194, in 
_async_setup_component
component.setup, hass, processed_config)
File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
yield self  # This tells Task to wait for completion.
File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
value = future.result()
File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
raise self._exception
File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
result = self.fn(*self.args, **self.kwargs)
File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/xiaomi.py", line 
65, 
in setup
from PyXiaomiGateway import PyXiaomiGateway
File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/PyXiaomiGateway/__init__.py", line 7, in 
<module>
from Crypto.Cipher import AES
ImportError: cannot import name 'AES'

Pycrypto and pycryptodome are both installed

(homeassistant_venv) root@HASS ~# pip3 install pycrypto
Requirement already satisfied: pycrypto in /srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages

(homeassistant_venv) root@HASS ~# pip3 install pycryptodome
Requirement already satisfied: pycryptodome in /srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages

Here is the config

xiaomi:
 gateways:
   - mac:
     key: !secret xiaomi_key

Any help is greatly appreciated

Please reinstall PyCrypto and post the output of

pip install --ignore-installed PyCrypto==2.6.1

My hass installed on synology docker, and when I connect synology to VPN, xiaomi gateway cannot be discovered, while chromecast and wemo can be.
Can anyone tell me what to do?

One news: yeelight can be added to google home starting today, but unfortunately only if it is connected to singapore server.

Ahh that’s why.

But if connected to Singapore server will not work on HASS???

I have both Yeelight and Xiaomi gateway.

Maybe keep the gateway mainland China and Yeelight Singapore??

I seem to be having the same problem as asapkota, but I’m prototyping this on a windows system.
I also had issues getting pycrypto installed but setting a few variables fixed that.
Collecting PyCrypto==2.6.1 Using cached pycrypto-2.6.1.tar.gz Installing collected packages: PyCrypto Running setup.py install for PyCrypto ... done Successfully installed PyCrypto-2.6.1

But the component won’t load

2017-08-02 16:01:33 INFO (MainThread) [homeassistant.setup] Setting up xiaomi 
2017-08-02 16:01:33 ERROR (MainThread) [homeassistant.setup] Error during setup of component xiaomi Traceback (most recent call last): File "c:\users\username\appdata\local\programs\python\python36\lib\site-packages\homeassistant\setup.py", line 194, in _async_setup_component component.setup, hass, processed_config) File "c:\users\username\appdata\local\programs\python\python36\lib\asyncio\futures.py", line 332, in __iter__ yield self # This tells Task to wait for completion. File "c:\users\username\appdata\local\programs\python\python36\lib\asyncio\tasks.py", line 250, in _wakeup future.result() File "c:\users\username\appdata\local\programs\python\python36\lib\asyncio\futures.py", line 245, in result raise self._exception File "c:\users\username\appdata\local\programs\python\python36\lib\concurrent\futures\thread.py", line 55, in run result = self.fn(*self.args, **self.kwargs) File "c:\users\username\appdata\local\programs\python\python36\lib\site-packages\homeassistant\components\xiaomi.py", line 65, in setup from PyXiaomiGateway import PyXiaomiGateway File "c:\users\username\appdata\local\programs\python\python36\lib\site-packages\PyXiaomiGateway\__init__.py", line 7, in <module> from Crypto.Cipher import AES File "c:\users\username\appdata\local\programs\python\python36\lib\site-packages\Crypto\Cipher\__init__.py", line 78, in <module> from Crypto.Cipher._mode_ecb import _create_ecb_cipher File "c:\users\username\appdata\local\programs\python\python36\lib\site-packages\Crypto\Cipher\_mode_ecb.py", line 29, in <module> from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, File "c:\users\username\appdata\local\programs\python\python36\lib\site-packages\Crypto\Util\_raw_api.py", line 33, in <module> from Crypto.Util.py3compat import byte_string ImportError: cannot import name 'byte_string' 
2017-08-02 16:01:33 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 be set up: * [xiaomi](https://home-assistant.io/components/xiaomi/) Please check your config, notification_id=invalid_config, service_call_id=81863456-1> 

2017-08-02 16:01:33 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=The following components and platforms could not be set up: * [xiaomi](https://home-assistant.io/components/xiaomi/) Please check your config; title=Invalid config @ 2017-08-02T16:01:33.063552+10:00>>

It would work with hass but not xiaomi gateway.
If you dont use Mihome app for automation, and have Google home or Echo, maybe connect the yeelight to Singapore server.

Your HA installation uses two crypto libraries (pycryptodome & pycrypto) which interfere with each other. Do you know which component requires pycryptodome? Do you use wink? The issue will be fixed in the next release of HA:

Hi Syssi

Thank you for the response. I installed pycryptodome as an alternative to pycrypto while troubleshooting.

Removing pycryptodome is still giving me errors. But even more similar to asapkota.
“Crypto.Cipher import AES ImportError: cannot import name ‘AES’”

Is there anything you’d like me to try or should I wait for next release?

2017-08-02 18:33:22 ERROR (MainThread) [homeassistant.setup] Error during setup of component xiaomi Traceback (most recent call last): File "c:\users\username\appdata\local\programs\python\python36\lib\site-packages\homeassistant\setup.py", line 194, in _async_setup_component component.setup, hass, processed_config) File "c:\users\username\appdata\local\programs\python\python36\lib\asyncio\futures.py", line 332, in __iter__ yield self # This tells Task to wait for completion. File "c:\users\username\appdata\local\programs\python\python36\lib\asyncio\tasks.py", line 250, in _wakeup future.result() File "c:\users\username\appdata\local\programs\python\python36\lib\asyncio\futures.py", line 245, in result raise self._exception File "c:\users\username\appdata\local\programs\python\python36\lib\concurrent\futures\thread.py", line 55, in run result = self.fn(*self.args, **self.kwargs) File "c:\users\username\appdata\local\programs\python\python36\lib\site-packages\homeassistant\components\xiaomi.py", line 65, in setup from PyXiaomiGateway import PyXiaomiGateway File "c:\users\username\appdata\local\programs\python\python36\lib\site-packages\PyXiaomiGateway\__init__.py", line 7, in <module> from Crypto.Cipher import AES ImportError: cannot import name 'AES' 2017-08-02 18:33:22 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 be set up: * [xiaomi](https://home-assistant.io/components/xiaomi/) Please check your config, notification_id=invalid_config, service_call_id=80597064-1> 2017-08-02 18:33:22 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=The following components and platforms could not be set up: * [xiaomi](https://home-assistant.io/components/xiaomi/) Please check your config; title=Invalid config @ 2017-08-02T18:33:22.374575+10:00>>

This is better. :wink: What’s the content of

c:\users\username\appdata\local\programs\python\python36\lib\site-packages\Crypto\Cipher\

Please post a directory listing.

Ahh sorry syssi, before I saw your message I fiddled a bit more and I think I got it working - but it involved uninstalling the package.

I did a pip uninstall pycrypto which erased the content of:
c:\users\username\appdata\local\programs\python\python36\lib\site-packages\Crypto\Cipher\
And told me: Successfully uninstalled pycrypto-2.6.1
Then I re-applied the visual studio specific environment hacks I needed to get the thing installed again (for reference in case anyone else gets weird errors while installing)

set VCINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio 14.0\vc
set CL=-FI"%VCINSTALLDIR%\INCLUDE\stdint.h"

Then I used “easy_install pycrypto” - how/why this is different to pip3 install pycrypto - don’t know.
But it installed Best match: pycrypto 2.6.1 - which appeared to be the same package I uninstalled earlier.
This installed to: c:\users\username\appdata\local\programs\python\python36\lib\site-packages\pycrypto-2.6.1-py3.6-win-amd64.egg instead of the old location you referenced above “site-packages\Crypto”.

Good news. It starts the package, locates and recognizes the gateway, receives the 3 sensors.

2017-08-02 20:11:58 INFO (MainThread) [homeassistant.setup] Setting up xiaomi
2017-08-02 20:11:58 DEBUG (SyncWorker_1) [homeassistant.components.xiaomi] Expecting 1 gateways
2017-08-02 20:11:58 INFO (SyncWorker_1) [homeassistant.components.xiaomi] Discovering Xiaomi Gateways (Try 1)
2017-08-02 20:11:58 INFO (MainThread) [homeassistant.setup] Setting up conversation
2017-08-02 20:11:58 INFO (MainThread) [homeassistant.core] Bus:Handling &lt;Event service_registered[L]: domain=conversation, service=process&gt;
2017-08-02 20:11:58 INFO (MainThread) [homeassistant.setup] Setup of domain conversation took 0.0 seconds.
2017-08-02 20:11:58 INFO (MainThread) [homeassistant.core] Bus:Handling &lt;Event component_loaded[L]: component=conversation&gt;
2017-08-02 20:11:58 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.waqi
2017-08-02 20:11:58 INFO (SyncWorker_1) [PyXiaomiGateway] Xiaomi Gateway 34ce008144b4 found at IP 10.1.1.134
2017-08-02 20:11:58 INFO (SyncWorker_1) [PyXiaomiGateway] Discovering Xiaomi Devices
2017-08-02 20:11:58 DEBUG (SyncWorker_1) [PyXiaomiGateway] &gt;&gt; b'{"cmd" : "get_id_list"}'
2017-08-02 20:11:59 INFO (MainThread) [homeassistant.core] Bus:Handling &lt;Event service_registered[L]: domain=tts, service=google_say&gt;
2017-08-02 20:11:59 INFO (MainThread) [homeassistant.core] Bus:Handling &lt;Event service_registered[L]: domain=tts, service=clear_cache&gt;
2017-08-02 20:11:59 INFO (MainThread) [homeassistant.setup] Setup of domain tts took 0.5 seconds.
2017-08-02 20:11:59 INFO (MainThread) [homeassistant.core] Bus:Handling &lt;Event component_loaded[L]: component=tts&gt;
2017-08-02 20:11:59 DEBUG (SyncWorker_1) [PyXiaomiGateway] &lt;&lt; {'cmd': 'get_id_list_ack', 'sid': '34ce008144b4', 'token': 'LJr2tI4c7CLW5nrw', 'data': '["158d0001a2ec69","158d0001720797"]'}
2017-08-02 20:11:59 INFO (SyncWorker_1) [PyXiaomiGateway] Found 3 devices
2017-08-02 20:11:59 DEBUG (SyncWorker_1) [PyXiaomiGateway] &gt;&gt; b'{"cmd":"read","sid":"158d0001a2ec69"}'
2017-08-02 20:11:59 DEBUG (SyncWorker_1) [PyXiaomiGateway] &lt;&lt; {'cmd': 'read_ack', 'model': 'sensor_ht', 'sid': '158d0001a2ec69', 'short_id': 57206, 'data': '{"voltage":3035,"temperature":"2512","humidity":"4579"}'}
2017-08-02 20:11:59 DEBUG (SyncWorker_1) [PyXiaomiGateway] Registering device 158d0001a2ec69, sensor_ht as: sensor
2017-08-02 20:11:59 DEBUG (SyncWorker_1) [PyXiaomiGateway] &gt;&gt; b'{"cmd":"read","sid":"158d0001720797"}'
2017-08-02 20:11:59 DEBUG (SyncWorker_1) [PyXiaomiGateway] &lt;&lt; {'cmd': 'read_ack', 'model': 'weather.v1', 'sid': '158d0001720797', 'short_id': 7229, 'data': '{"voltage":3095,"temperature":"2466","humidity":"4656","pressure":"101701"}'}
2017-08-02 20:11:59 DEBUG (SyncWorker_1) [PyXiaomiGateway] Registering device 158d0001720797, weather.v1 as: sensor
2017-08-02 20:11:59 DEBUG (SyncWorker_1) [PyXiaomiGateway] &gt;&gt; b'{"cmd":"read","sid":"34ce008144b4"}'
2017-08-02 20:11:59 DEBUG (SyncWorker_1) [PyXiaomiGateway] &lt;&lt; {'cmd': 'read_ack', 'model': 'gateway', 'sid': '34ce008144b4', 'short_id': 0, 'data': '{"rgb":0,"illumination":576,"proto_version":"1.0.8"}'}
2017-08-02 20:11:59 DEBUG (SyncWorker_1) [PyXiaomiGateway] Registering device 34ce008144b4, gateway as: sensor
2017-08-02 20:11:59 DEBUG (SyncWorker_1) [PyXiaomiGateway] Registering device 34ce008144b4, gateway as: light

And then it dies creating sockets:

2017-08-02 20:12:04 INFO (SyncWorker_1) [PyXiaomiGateway] Gateway finding finished in 5 seconds
2017-08-02 20:12:04 INFO (SyncWorker_1) [PyXiaomiGateway] Creating Multicast Socket
2017-08-02 20:12:04 ERROR (MainThread) [homeassistant.setup] Error during setup of component xiaomi
Traceback (most recent call last):
  File "c:\users\username\user\appdata\local\programs\python\python36\lib\site-packages\homeassistant\setup.py", line 194, in _async_setup_component
    component.setup, hass, processed_config)
  File "c:\users\username\user\appdata\local\programs\python\python36\lib\asyncio\futures.py", line 332, in __iter__
    yield self  # This tells Task to wait for completion.
  File "c:\users\username\user\appdata\local\programs\python\python36\lib\asyncio\tasks.py", line 250, in _wakeup
    future.result()
  File "c:\users\username\user\appdata\local\programs\python\python36\lib\asyncio\futures.py", line 245, in result
    raise self._exception
  File "c:\users\username\user\appdata\local\programs\python\python36\lib\concurrent\futures\thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "c:\users\username\user\appdata\local\programs\python\python36\lib\site-packages\homeassistant\components\xiaomi.py", line 79, in setup
    hass.data[PY_XIAOMI_GATEWAY].listen()
  File "c:\users\username\user\appdata\local\programs\python\python36\lib\site-packages\PyXiaomiGateway\__init__.py", line 107, in listen
    self._mcastsocket = self._create_mcast_socket()
  File "c:\users\username\user\appdata\local\programs\python\python36\lib\site-packages\PyXiaomiGateway\__init__.py", line 97, in _create_mcast_socket
    sock.bind((self.MULTICAST_ADDRESS, self.MULTICAST_PORT))
OSError: [WinError 10049] The requested address is not valid in its context
2017-08-02 20:12:04 INFO (MainThread) [homeassistant.core] Bus:Handling &lt;Event call_service[L]: domain=persistent_notification, service=create, service_data=title=Invalid config, message=The following components and platforms could not be set up:
* [xiaomi](https://home-assistant.io/components/xiaomi/)
Please check your config, notification_id=invalid_config, service_call_id=80512728-1&gt;
2017-08-02 20:12:04 INFO (MainThread) [homeassistant.core] Bus:Handling &lt;Event state_changed[L]: entity_id=persistent_notification.invalid_config, old_state=None, new_state=&lt;state persistent_notification.invalid_config=The following components and platforms could not be set up:
* [xiaomi](https://home-assistant.io/components/xiaomi/)
Please check your config; title=Invalid config @ 2017-08-02T20:12:04.376294+10:00&gt;&gt;

Now I might have hit an actual bug.

@syssi That worked. Thank you very much.

This is a windows specific problem. cp. networking - Python - bind error on multicast bind on windows - Stack Overflow

May be it will help to extend your configuration by the “interface” parameter:

xiaomi:
  gateways:
    - mac: xxxxxxxxxxxx
      key: xxxxxxxxxxxxxxxx
      interface: <ipaddress>

About the “ipaddress” I’m unsure also. I don’t know windows that much… may be some windows user can help here?

Hello, I kept getting “{“error”:“Invalid key”}” in the logs.

I checked several times the key and seems to be OK, I regenerated it, copied the new value checking it several times and always the same.

I obtain the key opening the Mijia app in my Xiaomi Mi5 phone, entering on gateway, pressing on the three-dot menu, “About”, “Local area network communication protocol”, and there is there the key (I confirmed the agreement and the “local area communication protocol” is activated)

Any idea on what I making wrong?

I am using hassio 1.0 on a Raspberry Pi 3 model B with Home Assistant 0.50.2

I just picked up a v2 recently solely to replace my v1 that didn’t support HA, if you’d like I’ll take a look at them when I get home and let you know. AFAIrecall they were both completely identical.

Yup, that’s what I did! I lost access to the Yeelight in my MI Home app after switching back to the China server, but it’s controllable by HA and Google Home now.

You mean after switching to Singapore?

After switching back to China. I’ve initially set up all my automations on the China server including my Yeelights, so I switched temporarily to the Singapore server, reset and re-paired my lights, and switched back to China, so the lights now show as ‘offline’ in my app, but are still controllable via HA and Google Home.

So why you switched back to China, what for?