Xiaomi Gateway Integration

It’s finally completed after a week of work. It currently works with all Xiaomi sensors, plugs and wall switches. Communication is both way. You can turn on and off a plug from HA or from Xiaomi Hub and state will sync. Credits to @fooxy who started the project!

Right now, it has to be installed as a component, not as a custom component. The reason is a couple of limitation of using custom component such as issue importing classes

UPDATE : Feb 6, 2017

  • Instant motion event trigger. Event will be triggered whenever motion is detected. It does not have a 60 second window. Event is instantly triggered when there’s a motion. Useful for sound notification.
    platform: event
    event_type: motion
        entity_id: binary_sensor.motion_sensor_158d000113702f
  • Easier to start using this component. Just put in config below and you’re ready to go. Just keep in mind that without a key, you won’t be able to control the gateway light, plug or switches. Useful if you only need to use the sensors.

UPDATE : Jan 19, 2017

  • Performance improvement. Disabled polling in motion sensor. Motion sensor now only poll itself every 10 seconds after motion is detected. Once motion is deactivated, polling stopped.
  • Added error check

UPDATE : Jan 14, 2017

  • Multiple gateway support (Remember to update Configuration.yaml for new configuration. Check README)
  • Support Gateway Light (RGB light now supported!) Make sure you’re on the latest gateway firmware
  • Support Cube rotate action
  • Motion sensor fix. Motion disabled after 1 minute (Polling enabled)
  • Task to update device state from broadcast now handled by HA directly. Should improve performance
  • Component will work without a valid key if no write action is required (16 characters fake key still required)
  • Error logged if Key is invalid when sending action to gateway
  • Added interface option for those who has issue with socket binding. thanks @kirichkov
  • Updated README. Non-supported device is added. Don’t ask again in the thread questions like ‘if things like radio is supported or not’

UPDATE : Jan 03, 2017

  • Added Cube

UPDATE : Dec 26, 2016 (Updated all .py files)

  • Can now be installed into custom_components folder
  • Fixed door sensor bug
  • Pycrypto will install automatically

UPDATE : Dec 25, 2016 (Updated all .py files)

  • Entity_Id format has changed. This is because templating does not work if the device id starts with a number
    binary_sensor.158d000xxxxxc2_switch to binary_sensor.switch_158d000xxxxxc2

  • Event data for switch has changed


    entity_id: binary_sensor.158d000xxxxxc2_switch
    click_type: single

  • Added battery but i’m not sure whether it works. I haven’t get been able to get the sensor to send any battery data


Thank you thank you thank you! I’ve been hoping to see Xiaomi support, just because I want to use their push buttons. Ordering a hub and 2 buttons right now to start testing!

Great! Let me know if it works well.

Definitely will do! I just ordered the gateway, a PIR sensor, and 3 buttons from banggood. Paid for expedited shipping, so hopefully it’ll be here within a week or two.

LOL just got my Xiomi hub and PIR sensors 4 hours ago ) Funny coincidence. Thank you!

First, copy all the files into the Home Assistant root location. Yes, it only works in the location below /srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components

Do you mean this is the only accepted path? Or /homeassistant/lib/python3.4/site-packages/homeassistant/components or wherever it is installed will also work?

1 Like

Yeah. Where the components files are installed. I’ll update the readme file

I ordered them in November…

Is this the correct item from Banggood for this component?


Woooh baby!!!

I’ve been waiting to start playing with my hub… I picked up one of each of the sensors and 3x switches!!

Soon as I get it working I’m getting sensors for all the doors and windows in the house!!!

I wonder the battery life on the motion sensors though… seems like in high traffic areas it could be a lot for a coin battery?? Thoughts?

I put one in the kitchen and it’s going off a few hundred times per day…

That’s a lot of traffic! But The coin battery is pretty big for the motion sensor, not the normal cr2132, so I’m sure it will last for a year or so. If i remember correctly, xiaomi advertise that their Zigbee sensor can run for up to 2 years.

1 Like

That’s the one

Ok installed as stated. pycrypto had to get manually. Thanks for including that in instruction.
Although it seems my switch and motion sensors appeared correctly and are working. Still got some errors (running the latest HA 0.35.3)

In my config.yaml

xiaomi :
  key: oxxxxxxxxxxxxxm

upon restarting got the following

16-12-25 09:59:26 homeassistant.core: Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
result = next(coro)
  File "/hass/lib/python3.4/site-packages/homeassistant/helpers/entity_component.py", line 122, in _async_setup_platform
    self.hass, self.config, self.domain, platform_type)
  File "/hass/lib/python3.4/site-packages/homeassistant/bootstrap.py", line 286, in async_prepare_setup_platform
    platform = loader.get_platform(domain, platform_name)
  File "/hass/lib/python3.4/site-packages/homeassistant/loader.py", line 101, in get_platform
    return get_component(PLATFORM_FORMAT.format(domain, platform))
  File "/hass/lib/python3.4/site-packages/homeassistant/loader.py", line 139, in get_component
    module = importlib.import_module(path)
  File "/hass/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1467, in exec_module
  File "<frozen importlib._bootstrap>", line 1572, in get_code
  File "<frozen importlib._bootstrap>", line 1532, in source_to_code
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/hass/lib/python3.4/site-packages/homeassistant/components/sensor/xiaomi.py", line 46
    self.current_value = int(value) / 100
IndentationError: expected an indented block
16-12-25 09:59:33 homeassistant.helpers.condition: Value cannot be processed as a number: unknown

One more thing is the xiaomi button.
Seems not working.
In my dev states list i see it as athe folllowing. State changes to on only if i hold the button

binary_sensor.1xxxxxxxxxxxxd_switch	off	friendly_name: 1xxxxxxxxxxxxd_Switch

Following your example put up an automation rule (put button_name without the binary_sensor in front). The rule never executes. In MiHome all the presses register correctly.

- alias: Monitor button
    platform: event
    event_type: click
      button_name: 1xxxxxxxxxxd_switch
      click_type: single
    service: notify.telegram
      title: "Button"
      message: "Pressed"

Seems like indentation issue when i copy the code to github. Let me do a quick check and update that

The state never change if you click or double click the button, it will only send an event. Hold does change the state but once you release the button, it will send a hold event as well.

If you run hass with the command line, did you see that the event is being sent when you click the button?

Good to know. Have never done that before - good for debugging.

Yes i can clearly see an event there

16-12-25 11:56:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event click[L]: button_name=1xxxxxxxxxxxxxxd_Switch, click_type=single>

The only difference i see - in dev states it’s switch and friendly name and event above it’s capitilized Switch.
And this change in rules as well made it work! So it should be Switch in automation. But can you check that for further versions?


I am sorry I know this is a stupid question, but could you tell me what and where can i find the wall switches you refer to on your github?

Thank you and I am sorry if this is a noob question

It’s called Aqara Zigbee Wall Switch. Try google it and see if it’s sold in your area. But it’s a wall switch made for China market, so you may want to check whether it fits into the

Rave, thank you very much for the support

Do you know if it supports multiple gateway and how do i get this running ? I put it in my config and it’s not working

Is it because I have the switch running on homebridge as well

Great job in getting it up and working with HA. I’m finding a way to get rid of mi home application to have more automation function

It currently does not support multiple gateway but that’s one of the goal that i have. Since i only have one gateway, i will need to get another soon in order to see how it works.

I don’t think it conflicts with homebridge. Can you restart HA and post the startup log? It should show what’s the error