Xiaomi Gateway Integration

I frequently have problems related to one device connecting to a network and other device connecting to another ssid.

I have a similar problem with the Hue app on my smartphone and the Hue Bridge.

I did not troubleshoot well, but to me it seems that if the 2 devices are on different WiFi network (even if the DCHP assigns same IP range) you experience problems if the 2 devices need to interact.

Glad it helped. Now you have to find a way that all connects to the same SSID

My bet is that your 2.4GHz and 5GHz are different interfaces. The gateway connects to the 2.4GHz network, but your HASS instance connects to the 5GHz network. Multicast messages don’t pass between the two interfaces.

Some ideas how to fix that - if you have the same network name (SSID) for both networks change one of them so you can differentiate which one is the 5GHz and which one is the 2.4GHz. Make sure your HASS is connected to the 2.4GHz one.

If you use wired connectivity for HASS then what you’re saying doesn’t make much sense and is probably related to your router’s internal way of handling Wi-Fi/Ethernet traffic and passing traffic from one interface to the other.

I’ll merge the code but fyi, I have already separated the main code into pyXiaomi and separating xiaomi device code a week ago with some optimization. I’ll probably need to rewrite some code based on the code you have committed. I’ll not be working on it for another week so feel free so put in more pull request but I’ll probably stop accepting when I start rewriting the component code

1 Like

Originally, the code for the button were the idea from the flic button code. And since the cube is essentially the same as xiaomi button, so they’re using almost the same code base. Sensor class should work too but but since these are stateless devices, do you think there’s an advantage of moving that over?

No, I am not sure that sensor is any better.
And since Flic smart button is a binary sensor, this should probably also be that.

1 Like

Hello

Having isseus with the Mi Gateway light not working. Here is the log. Any advice? Thanks

Here is what I have tried so far:

  • added sid to HASS config
  • changed the password on Mi Home app several times and updated HASS config
  • turned off/on developer mode on Mi Home app

17-01-31 12:39:14 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.4/asyncio/tasks.py”, line 233, in _step
result = coro.throw(exc)
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/core.py”, line 1052, in _event_to_service_call
yield from service_handler.func(service_call)
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/light/init.py”, line 242, in async_handle_light_service
yield from light.async_turn_on(**params)
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 “/var/opt/homeassistant/custom_components/light/xiaomi.py”, line 163, in turn_on
if self.xiaomi_hub.write_to_hub(self._sid, self._data_key, rgbhex):
File “/var/opt/homeassistant/custom_components/xiaomi.py”, line 301, in write_to_hub
key = self._get_key()
File “/var/opt/homeassistant/custom_components/xiaomi.py”, line 331, in _get_key
from Crypto.Cipher import AES
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/Crypto/Cipher/init.py”, line 78, in
from Crypto.Cipher._mode_ecb import _create_ecb_cipher
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/Crypto/Cipher/_mode_ecb.py”, line 29, in
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/Crypto/Util/_raw_api.py”, line 33, in
from Crypto.Util.py3compat import byte_string
ImportError: cannot import name ‘byte_string’

@asapkota
It seams that you are using an old version of the Xiaomi library. Could you upgrade to the latest one: https://github.com/lazcad/homeassistant

Updated to the latest files. Now the component is failing on HASS start.

17-01-31 13:00:18 ERROR (MainThread) [homeassistant.bootstrap] Error during setup of component xiaomi
Traceback (most recent call last):
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/bootstrap.py”, line 151, in _async_setup_component
None, component.setup, hass, 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 “/var/opt/homeassistant/custom_components/xiaomi.py”, line 55, in setup
comp.discoverGateways()
File “/var/opt/homeassistant/custom_components/xiaomi.py”, line 136, in discoverGateways
self.XIAOMI_GATEWAYS[resp[“ip”]] = XiaomiGateway(resp[“ip”], resp[“port”], resp[“sid”], gatewayKey, self._socket)
File “/var/opt/homeassistant/custom_components/xiaomi.py”, line 230, in init
if self._discover_devices():
File “/var/opt/homeassistant/custom_components/xiaomi.py”, line 239, in _discover_devices
self.update_key(resp[“token”])
File “/var/opt/homeassistant/custom_components/xiaomi.py”, line 331, in update_key
from Crypto.Cipher import AES
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/Crypto/Cipher/init.py”, line 78, in
from Crypto.Cipher._mode_ecb import _create_ecb_cipher
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/Crypto/Cipher/_mode_ecb.py”, line 29, in
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
File “/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/Crypto/Util/_raw_api.py”, line 33, in
from Crypto.Util.py3compat import byte_string
ImportError: cannot import name ‘byte_string’

Seams to be something wrong with your pycrypto lib. Try to reinstall the pycrypto library.

That did it. Thank you very much.

this pycrypto is giving many problems to lot of users … to integrate it in HA, maybe that helps?

Is it difficult to get power consumption from the plug?

I installed my second gateway today, but is not recognized in HASS. The first is recognized as usual, the second simply does not exist in HASS (no error in log!!).
What to do?

xiaomi:
  gateways: 
    - sid: F0:B4:.....
      key: s0adr7t03s3q.....
    - sid: 28:6C:.....
      key: zq8etr3gls7.....

I spoke too early, I do have errors, although not of the second gateway

17-01-31 20:54:02 ERROR (MainThread) [homeassistant.components.switch] Error while setting up platform xiaomi
Traceback (most recent call last):
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/helpers/entity_component.py", line 151, in _async_setup_platform
    entity_platform.add_entities, discovery_info
  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 "/home/hass/.homeassistant/custom_components/switch/xiaomi.py", line 19, in setup_platform
    for device in gateway.XIAOMI_DEVICES['switch']:
AttributeError: 'list' object has no attribute 'XIAOMI_DEVICES'
17-01-31 20:54:02 INFO (Thread-10) [googleapiclient.discovery] URL being requested: GET https://www.googleapis.com/calendar/v3/calendars/c.barbano%40adre.it/events?alt=json&maxResults=1&orderBy=startTime&singleEvents=true&timeMin=2017-01-31T00%3A00%3A00%2B01%3A00
17-01-31 20:54:02 INFO (Thread-5) [googleapiclient.discovery] URL being requested: GET https://www.googleapis.com/calendar/v3/calendars/%23contacts%40group.v.calendar.google.com/events?alt=json&maxResults=1&orderBy=startTime&singleEvents=true&timeMin=2017-01-31T00%3A00%3A00%2B01%3A00
17-01-31 20:54:02 ERROR (MainThread) [homeassistant.components.light] Error while setting up platform xiaomi
Traceback (most recent call last):
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/helpers/entity_component.py", line 151, in _async_setup_platform
    entity_platform.add_entities, discovery_info
  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 "/home/hass/.homeassistant/custom_components/light/xiaomi.py", line 23, in setup_platform
    for device in gateway.XIAOMI_DEVICES['light']:
AttributeError: 'list' object has no attribute 'XIAOMI_DEVICES'

Could you try with the latest version from https://github.com/lazcad/homeassistant ?

doing now. The switch component description is wrong (it says binary sensor, minor thing)

errors also on the first gateway.
This crypto thing, I hate it …

17-01-31 21:11:04 INFO (Thread-5) [custom_components.xiaomi] Xiaomi Gateway f0b429aa0aa8 found at IP 192.168.1.230
17-01-31 21:11:04 INFO (Thread-5) [custom_components.xiaomi] Discovering Xiaomi Devices
17-01-31 21:11:05 ERROR (MainThread) [homeassistant.bootstrap] Error during setup of component xiaomi
Traceback (most recent call last):
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/bootstrap.py", line 151, in _async_setup_component
    None, component.setup, hass, 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 "/home/hass/.homeassistant/custom_components/xiaomi.py", line 55, in setup
    comp.discoverGateways()
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 136, in discoverGateways
    self.XIAOMI_GATEWAYS[resp["ip"]] = XiaomiGateway(resp["ip"], resp["port"], resp["sid"], gatewayKey, self._socket)
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 230, in __init__
    if self._discover_devices():
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 239, in _discover_devices
    self.update_key(resp["token"])
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 333, in update_key
    encryptor = AES.new(self.GATEWAY_KEY, AES.MODE_CBC, IV=IV)
  File "/home/hass/.homeassistant/deps/Crypto/Cipher/AES.py", line 95, in new
    return AESCipher(key, *args, **kwargs)
  File "/home/hass/.homeassistant/deps/Crypto/Cipher/AES.py", line 59, in __init__
    blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
  File "/home/hass/.homeassistant/deps/Crypto/Cipher/blockalgo.py", line 141, in __init__
    self._cipher = factory.new(key, *args, **kwargs)
ValueError: Key cannot be the null string
17-01-31 21:11:05 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service=create, domain=persistent_notification, service_call_id=1978468048-1, service_data=notification_id=invalid_config, message=The following components and platforms could not be set up:
* [xiaomi](https://home-assistant.io/components/xiaomi/)
Please check your config, title=Invalid config>
17-01-31 21:11:05 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: 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-01-31T21:11:05.192173+01:00>, entity_id=persistent_notification.invalid_config, old_state=None>
17-01-31 21:11:05 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=1978468048-1>

and second gateway also non existant. Could be because second gateway is on other Wifi network?

But the Mi Home app sees both gateways

Could you try to replace components/xiaomi.py with this one:
https://github.com/Danielhiversen/homeassistant/blob/patch-3/components/xiaomi.py

Edit: just changed one more thing

17-01-31 21:26:03 INFO (MainThread) [homeassistant.bootstrap] Setting up xiaomi
17-01-31 21:26:03 INFO (Thread-11) [custom_components.xiaomi] Discovering Xiaomi Gateways
17-01-31 21:26:03 INFO (Thread-11) [custom_components.xiaomi] Xiaomi Gateway f0b429aa0aa8 found at IP 192.168.1.230
17-01-31 21:26:03 INFO (Thread-11) [custom_components.xiaomi] Discovering Xiaomi Devices
17-01-31 21:26:03 ERROR (MainThread) [homeassistant.bootstrap] Error during setup of component xiaomi
Traceback (most recent call last):
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/bootstrap.py", line 151, in _async_setup_component
    None, component.setup, hass, 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 "/home/hass/.homeassistant/custom_components/xiaomi.py", line 55, in setup
    comp.discoverGateways()
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 137, in discoverGateways
    self.XIAOMI_GATEWAYS[resp["ip"]] = XiaomiGateway(resp["ip"], resp["port"], resp["sid"], gatewayKey, self._socket)
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 231, in __init__
    if self._discover_devices():
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 240, in _discover_devices
    self.update_key(resp["token"])
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 334, in update_key
    encryptor = AES.new(self.GATEWAY_KEY, AES.MODE_CBC, IV=IV)
  File "/home/hass/.homeassistant/deps/Crypto/Cipher/AES.py", line 95, in new
    return AESCipher(key, *args, **kwargs)
  File "/home/hass/.homeassistant/deps/Crypto/Cipher/AES.py", line 59, in __init__
    blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
  File "/home/hass/.homeassistant/deps/Crypto/Cipher/blockalgo.py", line 141, in __init__
    self._cipher = factory.new(key, *args, **kwargs)
ValueError: Key cannot be the null string
17-01-31 21:26:03 INFO (MainThread) [homeassistant.loader] Loaded sensor.yr from homeassistant.components.sensor.yr
17-01-31 21:26:03 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service=create, service_data=notification_id=invalid_config, message=The following components and platforms could not be set up:
* [xiaomi](https://home-assistant.io/components/xiaomi/)
Please check your config, title=Invalid config, domain=persistent_notification, service_call_id=1978033808-1>
17-01-31 21:26:03 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=persistent_notification.invalid_config, 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-01-31T21:26:03.648002+01:00>, old_state=None>
1