Xiaomi Gateway Invalid key size (0) for AES

So hello there…
I’ve finally managet to install HomeAssistant on OSMC and it seems that everything is working… or better, apparently everything is working.
So started HomeAssistant, it finds (with autodiscover) my Xiaomi Gateway and all my sensors.
I make the first try, just switch on the Gateway light, but I get this message:

2018-01-13 19:16:10 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
    result = coro.throw(exc)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/core.py", line 1031, in _event_to_service_call
    yield from service_handler.func(service_call)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/light/__init__.py", line 271, in async_handle_light_service
    yield from light.async_turn_on(**params)
  File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/light/xiaomi_aqara.py", line 98, in turn_on
    if self._write_to_hub(self._sid, **{self._data_key: rgbhex}):
  File "/srv/homeassistant/lib/python3.5/site-packages/PyXiaomiGateway/__init__.py", line 292, in write_to_hub
    data['key'] = self._get_key()
  File "/srv/homeassistant/lib/python3.5/site-packages/PyXiaomiGateway/__init__.py", line 322, in _get_key
    encryptor = Cipher(algorithms.AES(self.key.encode()), modes.CBC(init_vector),
  File "/srv/homeassistant/lib/python3.5/site-packages/cryptography/hazmat/primitives/ciphers/algorithms.py", line 32, in __init__
    self.key = _verify_key_size(self, key)
  File "/srv/homeassistant/lib/python3.5/site-packages/cryptography/hazmat/primitives/ciphers/algorithms.py", line 18, in _verify_key_size
    len(key) * 8, algorithm.name
ValueError: Invalid key size (0) for AES.

So hoping in a error of the discovery module, I’ve manually added the gateway, using the key got from MiHome app (android, not IOS, as I’ve read that IOS probably gives you a wrong key).
Same result…

So what’s the problem? Do I miss some libraries (it could be as I’m using OSMC and I could have missed something)?

Thanks in advance!

1 Like

Can you post your config?

Hi Daniel, thanks for your answer.
At the moment my configuration.yaml is the following

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: 41.9
  longitude: 12.4833
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 21
  # metric for Metric, imperial for Imperial
  unit_system: metric
  time_zone: Europe/Rome
  # Customization file
  customize: !include customize.yaml

# Show links to resources in log and frontend
introduction:

# Enables the frontend
frontend:

# Enables configuration UI
config:

http:
  # Secrets are defined in the file secrets.yaml
  # api_password: !secret http_password
  # Uncomment this if you are using SSL/TLS, running in Docker container, etc.
  # base_url: example.duckdns.org:8123

# Checks for available updates
# Note: This component will send some information about your system to
# the developers to assist with development of Home Assistant.
# For more information, please see:
# https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
updater:
  # Optional, allows Home Assistant developers to focus on popular components.
  # include_used_components: true

# Discover some devices automatically
discovery:

# Allows you to issue voice commands from the frontend in enabled browsers
conversation:

# Enables support for tracking state changes over time
history:

# View all events in a logbook
logbook:

# Enables a map showing the location of tracked devices
map:

# Track the sun
sun:

# Weather prediction
sensor:
  - platform: yr

# Text to speech
tts:
  - platform: google

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

#xiaomi_aqara:
#  gateways:
#    - mac: 98DED__D7C34
#      key: vgt5__r8ab7f0dpj

So you have disabled the xiaomi component?
Is that the actual mac and key?

Yes, I’ve tried both with the Xiaomi Component turned on manually (so disabling the discover component) and with the Xiaomi component turned off, with same result.
I’ve just plugged my zigbee switch (the one usable only with the Xiaomi Gateway): I can see the consumption power while using a hairdryer, but I can’t switch it on or off (same result of trying to switch on/off the gateway light, Invalid key size (0) for AES). As I said, I can also correctly see the temperature from my sensors…

Immagine

Yes sorry, just hid two chars for each…

Are you sure the mac address is correct?
Try to change the config to :

xiaomi_aqara:
 gateways:
   - mac:
     key: YOUR_KEY

With no mac address

Oh… now it is working!!!
My configuration.yaml is the following (commented discovery and uncommented xiaomi):

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: 41.9
  longitude: 12.4833
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 21
  # metric for Metric, imperial for Imperial
  unit_system: metric
  time_zone: Europe/Rome
  # Customization file
  customize: !include customize.yaml

# Show links to resources in log and frontend
introduction:

# Enables the frontend
frontend:

# Enables configuration UI
config:

http:
  # Secrets are defined in the file secrets.yaml
  # api_password: !secret http_password
  # Uncomment this if you are using SSL/TLS, running in Docker container, etc.
  # base_url: example.duckdns.org:8123

# Checks for available updates
# Note: This component will send some information about your system to
# the developers to assist with development of Home Assistant.
# For more information, please see:
# https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
updater:
  # Optional, allows Home Assistant developers to focus on popular components.
  # include_used_components: true

# Discover some devices automatically
#discovery:

# Allows you to issue voice commands from the frontend in enabled browsers
conversation:

# Enables support for tracking state changes over time
history:

# View all events in a logbook
logbook:

# Enables a map showing the location of tracked devices
map:

# Track the sun
sun:

# Weather prediction
sensor:
  - platform: yr

# Text to speech
tts:
  - platform: google

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

xiaomi_aqara:
  gateways:
    - mac: 
      key: vgt5__r8ab7f0dpj      

So what’s the problem with autodiscovery? (now I have to manually add my yeelight :sweat_smile: )

I found a bug in the xiaomi lib. Will be fixed soon: https://github.com/Danielhiversen/PyXiaomiGateway/pull/40

Ok thanks a lot!!
Once fixed, what to do for updating HA? As I said I’ve just installed HA on OSMC (not so easy by the way) and I still don’t know if an update would destroy my job :sweat_smile:

It will be part of HA 0.62, that will be released in two weeks.
But just use the config with a blank mac address

Ok thanks a lot again!!! Really really thanks!