Xiaomi Gateway Integration

same problem

Wondering if anyone can help me with an issue I’m having with the Xiaomi Arqara wireless wall switch. For the life of me, I can’t get it working since my first few tests.

I have a simple automation set up that whenever the switch is clicked, it should toggle the light on /off. HA registers that there is a wall switch, but doesn’t seem to be able to pick up when the switch is being clicked. I’ve tried manually triggering the event, which toggles the event.

I’m suspicious that it may be the gateway that’s the issue. It’s version 1.4.1_149.0143 with the LAN protocol enabled. Completely stumped to what the issue is.

You hit this firmware issue: Aqara Wireless Switch(Double,not in wall) LEFT button FAILURE · Issue #8935 · home-assistant/core · GitHub

Thanks - I was able to get it from an android device.

Here’s an example of the 32 vs 96 character tokens

4a447a41467058496746505541597033 - android token

16a935db276e19b1c7bef0739f2deb7d3aca43c5e26a3c6445351cb2fb8495ea0143db63ee66b0cdff9f69917680151e - ios token

1 Like

Funny discovery: The last 32 char chunk of the 96 char token is always (for all users) the same: 0143db63ee66b0cdff9f69917680151e

If you AES-ECB encrypt an empty string with an empty password the result is 0143db63ee66b0cdff9f69917680151e. :stuck_out_tongue:

2 Likes

Yeay! I managed to decrypt the 96 character token:

# AES-ECB decrypt a 96 character iOS token -> valid 32 character token

from Crypto.Cipher import AES
import binascii
keystring = '00000000000000000000000000000000'
iostoken = '16a935db276e19b1c7bef0739f2deb7d3aca43c5e26a3c6445351cb2fb8495ea0143db63ee66b0cdff9f69917680151e'
key = bytes.fromhex(keystring)
cipher = AES.new(key, AES.MODE_ECB)
token = cipher.decrypt(bytes.fromhex(iostoken[:64]))
print(token)
# Output: b'4a447a41467058496746505541597033'
8 Likes

Hi there,

Is that Python code? Does it need the pycrypto library?

Thanks

Yes. pycrypto is a dependency of python-mirobo. If you are using python-mirobo already just switch to your virtualenv, start the python interpreter and write down the 8 lines of code and change the value of “iostoken”.

I do not know what python I am using. I installed home-assistant on virtual env following the installation guide. Do I need to install pycrypto?

Just switch to your home-assistant user (sudo su - homeassistant), enter the virtualenv (source /srv/homeassistant/bin/activate), execute python (python) and paste the code.

2 Likes

Hi again,

I had to install the pycrypto library before the interpreter could run your code.
What is that “b” at the start of the output string?

Thanks a lot in advance.

The “b” just indicates a byte-string. The important piece (token) is between the quotation marks.

1 Like

Thanks a lot. Did everything but still home assistant cannot see my Aqara gateway. Will have to double check.

I’m sorry. This thread is a mix of various xiaomi products and questions. If you try to setup a xiaomi gateway you need a key and not a token!

The note is important for you: Xiaomi - Home Assistant

Please read the first paragraph of: Xiaomi - Home Assistant

And follow this tutorial: Xiaomi Gateway Integration - #1832 by pplucky

I have never ever had the ghost issue since first setting up my Xiaomi stuff. Home Assistant sends me alerts on my iPhone on mouvement when I’m away and I’ve never had a false alarm.

Hello!

im trying to get my Xiaomi GW to work with HA:

I can see it in HA but cannot control it. I have enabled Lan access and saved the password.

This is my config

xiaomi:
gateways:

  • mac: 00:90:7F:AA:2E:7B
    key: C0869E2REE51321R (This is not my real code, just an example)

I see this error in my log:

raise ValueError(“Incorrect AES key length (%d bytes)” % len(key))
ValueError: Incorrect AES key length (0 bytes)

Could that be something?

Try remove the “:” from mac address.
yesterday I got doorbell sound working and I did not have the ::::

There is something wrong with your key. Could you post the full call trace of your home-assistant.log and a proper formatted version of your configuration.yaml? The number of spaces matters. Is your key in upper-case? I hope not.

This is the log:

[homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.6/asyncio/tasks.py", line 181, in _step
    result = coro.throw(exc)
  File "/usr/lib/python3.6/site-packages/homeassistant/core.py", line 1025, in _event_to_service_call
    yield from service_handler.func(service_call)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/light/__init__.py", line 279, in async_handle_light_service
    yield from light.async_turn_on(**params)
  File "/usr/lib/python3.6/asyncio/futures.py", line 331, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.6/asyncio/tasks.py", line 244, in _wakeup
    future.result()
  File "/usr/lib/python3.6/asyncio/futures.py", line 244, in result
    raise self._exception
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/light/xiaomi.py", line 97, in turn_on
    if self._write_to_hub(self._sid, **{self._data_key: rgbhex}):
  File "/usr/lib/python3.6/site-packages/PyXiaomiGateway/__init__.py", line 258, in write_to_hub
    data['key'] = self._get_key()
  File "/usr/lib/python3.6/site-packages/PyXiaomiGateway/__init__.py", line 288, in _get_key
    encryptor = AES.new(self.key.encode(), AES.MODE_CBC, IV=init_vector)
  File "/usr/lib/python3.6/site-packages/Crypto/Cipher/AES.py", line 202, in new
    return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/Crypto/Cipher/__init__.py", line 55, in _create_cipher
    return modes[mode](factory, **kwargs)
  File "/usr/lib/python3.6/site-packages/Crypto/Cipher/_mode_cbc.py", line 232, in _create_cbc_cipher
    cipher_state = factory._create_base_cipher(kwargs)
  File "/usr/lib/python3.6/site-packages/Crypto/Cipher/AES.py", line 92, in _create_base_cipher
    raise ValueError("Incorrect AES key length (%d bytes)" % len(key))
ValueError: Incorrect AES key length (0 bytes)

My config:

xiaomi:
  gateways:
   - mac: 00907FAA2E7B
     key: c0665e2dee33421d

I can see the controller, and if i manualy turn on the light it turns on in HA. But if I turn it on i HA nothing happends.
Yes it was in upper-case, did change that now. Still dont work

@hijinx I did have : tried too change it now, dident matter.

Where did you find your key? The key must be alphanumeric and is case-sensitive. It looks like the key is just wrong. Please follow this tutorial.