Xiaomi Gateway Integration

I wonder if polling introduce extra delay. How many motion sensors do you have? Try turn off polling and see if it improves. Change the motion sensor should poll property to false

    def should_poll(self):
        return False

thanks! Merged

17-01-15 03:55:57 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/hass/hass_venv/lib/python3.4/site-packages/homeassistant/core.py”, line 1049, in _event_to_service_call
yield from service_handler.func(service_call)
File “/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/light/init.py”, line 241, in async_handle_light_service
yield from light.async_turn_on(**params)
File “/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/helpers/entity.py”, line 353, in async_turn_on
None, ft.partial(self.turn_on, **kwargs))
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/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/light/xiaomi.py”, line 163, in turn_on
if self.xiaomi_hub.write_to_hub(self._sid, self._data_key, rgbhex):
File “/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/xiaomi.py”, line 290, in write_to_hub
key = self._get_key()
File “/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/xiaomi.py”, line 315, in _get_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-15 03:55:59 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/hass/hass_venv/lib/python3.4/site-packages/homeassistant/core.py”, line 1049, in _event_to_service_call
yield from service_handler.func(service_call)
File “/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/light/init.py”, line 241, in async_handle_light_service
yield from light.async_turn_on(**params)
File “/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/helpers/entity.py”, line 353, in async_turn_on
None, ft.partial(self.turn_on, **kwargs))
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/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/light/xiaomi.py”, line 163, in turn_on
if self.xiaomi_hub.write_to_hub(self._sid, self._data_key, rgbhex):
File “/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/xiaomi.py”, line 290, in write_to_hub
key = self._get_key()
File “/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/xiaomi.py”, line 315, in _get_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

After updating today I have this problem. My config is:

xiaomi:
    gateways:
        - sid:
          key: xxxxxx

Error:

INFO:custom_components.xiaomi:Discovering Xiaomi Devices
INFO:custom_components.xiaomi:Found 8 devices
ERROR:homeassistant.bootstrap:Error during setup of component xiaomi
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/site-packages/homeassistant/bootstrap.py", line 151, in _async_setup_component
    None, component.setup, hass, config)
  File "/usr/local/lib/python3.4/asyncio/futures.py", line 358, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/local/lib/python3.4/asyncio/tasks.py", line 297, in _wakeup
    future.result()
  File "/usr/local/lib/python3.4/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/local/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/ha/.homeassistant/custom_components/xiaomi.py", line 53, in setup
    comp.discoverGateways()
  File "/home/ha/.homeassistant/custom_components/xiaomi.py", line 128, in discoverGateways
    gateway = XiaomiGateway(resp["ip"], resp["port"], resp["sid"], gatewayKey, self._socket)
  File "/home/ha/.homeassistant/custom_components/xiaomi.py", line 225, in __init__
    self._discover_devices()
  File "/home/ha/.homeassistant/custom_components/xiaomi.py", line 245, in _discover_devices
    model = resp["model"]
KeyError: 'model'

I added some debugging and the problem seems to be that the gateway reports a sid that’s actually missing, so there’s no model returned.

INFO:custom_components.xiaomi:{'data': '{"error":"No device"}', 'cmd': 'read_ack', 'sid': '286c0785a7eb'}

Make sure you’re on the latest firmware

just updated my firmware and the py files.
Now getting this on startup
17-01-15 16:04:50 homeassistant.bootstrap: Invalid config for [xiaomi]: expected a dictionary for dictionary value @ data[‘xiaomi’]. Got [OrderedDict([(‘sid’, None), (‘key’, ‘d33rhpxxxxxxx’)])]. (See /home/marc/.homeassistant/configuration.yaml:95). Please check the docs at https://home-assistant.io/components/xiaomi/

Refreshed and added a new key as well.
Any ideas?

config looks like this:

xiaomi:

  • sid:
    key: d33rhpj7klltnh1a

Read the README and update your configuration.yaml. Or read 2 post above you

duh … missed the gateway bit … thanks :slight_smile:

I upgraded, but also sent a pull request to make things more robust (since a model may not always be returned).

The issue is with the old firmware. It doesn’t recognize gateway as a device. that’s why a model is not returned. For a valid device, a model will always returned. Instead checking if a model is there, i think it’s better to check for the invalid device response from the hub

For those who like to test out a performance improvement fix can download this PY from the staging branch. Right now, HA polls every motion sensors every 5 second or so. This may cause the gateway to be unresponsive because if you 20 sensors, HA will request 20 sensor updates at the same time from the gateway. This fix disabled auto polling and only turn on polling when a motion sensor is activated. If a motion sensor is deactivated, polling stops. Polling is configured to be 10 seconds. To be honest, changing it to 30 seconds or more should have the same results as well because Xiaomi motion sensor only deactivate itself after 60 seconds

https://github.com/lazcad/homeassistant/blob/staging/components/binary_sensor/xiaomi.py

@rave thanks for all your hard work on this!!

Has anyone confirmed the zigbee switches (plugs) work as repeaters?

I just added one to extend coverage for a temp monitor out the front of the house that went offline (most likely due to range) and it doesn’t seem to have helped.

Not sure any way of testing this either…

Can you try it while the plug is turned on? Also, i heard from ST forum that it will silently turned off itself when nothing is plugged, so if turning on doesn’t help, try plug something into the plug

1 Like

@rave Trying the master branch with the staging performance fix applied.

Now the sensor isn’t working anymore (not even on my laptop).

Exception comes every time: ERROR:homeassistant.core:Error doing job: Task exception was never retrieved on \homeassistant\components\binary_sensor\xiaomi.py", line 128, in async_poll_status

Strange enough the return True does happen, but it seems to stop processing the event.

if value == 'motion':
    if self._state == True:
        return False
    else:
        self._state = True
        self._hass.loop.create_task(self.async_poll_status())
        return True # This line

Edit:

Enabled some debug logging for asyncio, this is a more detailed message:
got Future <Future pending> attached to a different loop.

Btw I’m using 2 motion sensors, how many are you using?

I’m using 3 motion sensors.No performance issue though. That is an async method, means it will run in another thread. so it is normal that the rest of the code continue to execute even the polling code failed.

It is strange why a simple method like that failed on your machine but works on mine. Which python version you’re using? I’m on Python 2.7.9 and HA 0.35.3

Edit. So no event is being published? that is not normal

Maybe do a try catch inside async_poll_status and see if the exception is thrown inside that code

@rave I’m running python 3.5 and HA 0.35.3.

If running without a sleep(10) (but with self.update()) the issue seams the same.
The event doesn’t get pushed in this code:

for device in gateway.XIAOMI_HA_DEVICES[sid]:
    device.push_data(jdata)

Non-thread-safe operation invoked on an event loop other

Don’t have time for further debugging now, back in few hours.

for me the new version works very well… i’ve lot of sensors… wish i can see performance improvement…

thanks @rave for all your works… (ps i’ve donated you something for a beer) :wink:

I’m actually on 3.4.2

thanks! So you’re using the one from the staging branch and it works fine?