Xiaomi Gateway Integration

try Xiaomi Gateway Integration it’s up to date and better explained!

It seems the new Aqara motion sensor lowered its price! http://www.gearbest.com/alarm-systems/pp_659226.html

Thank you!! I was missing the tapping of the screen!

1 Like

Wow everything working (almost!)! The only things not working are the events for the motion sensor and the door/window sensor. The state platform is working perfectly for these sensors.

Code is here:

- alias: Test 3A Motion Sensor Event NOT Working
  trigger:
    platform: event
    event_type: motion_action
    event_data:
      entity_id: binary_sensor.motion_sensor_158d00015ef4d3
      action_type: motion # motion / no_motion


- alias: Test 6 Door Sensor Event NOT Working
  trigger:
    platform: event
    event_type: door_window_action
    event_data:
      entity_id: binary_sensor.door_window_sensor_158d00018354bc
      action_type: open # open / close / no_close

Any ideas? Thanks to everyone for the great work on this - so cool!

Tom

Hi Syssi, I am getting closer… I can almost taste victory! The below is my most recent error logs after trying to implement the plug protocol.

I have the xiaomi folder in /hassbian/homeassistant/
I have teh xiaomiplug.py (renamed xiaomi from switch) in homeassistant/custom_components/ (and also in /switch).

My yaml is as follows:

switch:
  - platform: xiaomiplug
    host: 10.xx.xxx.xxx
    name: Lounge Lamp
    token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

I tried sudo pip3 install construct, which is now installed but I am still getting the below:

2017-07-12 01:07:39 ERROR (MainThread) [homeassistant.components.switch] Error while setting up platform xiaomiplug
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/entity_component.py", line 164, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=self.hass.loop)
  File "/usr/lib/python3.4/asyncio/tasks.py", line 372, in wait_for
    return fut.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/homeassistant/.homeassistant/custom_components/switch/xiaomiplug.py", line 17, in setup_platform
    import xiaomi
  File "/home/homeassistant/.homeassistant/xiaomi/__init__.py", line 2, in <module>
    from .protocol import Message, Utils
  File "/home/homeassistant/.homeassistant/xiaomi/protocol.py", line 7, in <module>
    from construct import (Struct, Bytes, Const, Int16ub, Int32ub, GreedyBytes,
ImportError: No module named 'construct'

Any more advice on where I may be going wrong?

Thanks for all the help!

This thread is about the support for the Xiaomi Gateway and Zigbee sensors. For the Wifi plug, I would suggest you post in the thread for that component so that it does not confuse folks in this already long thread. :sweat:

The thread for that should be “Xiaomi mi wifi plug & air purifierXiaomi mi wifi plug & air purifier.

@skull29 Apologies your quite right! If anyone has stumbled across this… I will post in that conversation and hopefully continue to get closer to a solution.

Hy, I was trying to follow this topic, but stupidly I thought that the AEON Gen5 (Zwave) would also work with the Xiaomi Sensores /ZigBee)… The most basic error :unamused: Now that I learned my lesson the hard way, I would like to have some help with the right gateway to use with HomeAssistant and my Xiaomi sensores.

Thanks :slight_smile:

It looks like the events are broken. The door/window sensor never calls bus.fire(). The motion sensor event is incomplete:

$ grep -riC3 bus.fire * 
components/binary_sensor/xiaomi.py-
components/binary_sensor/xiaomi.py-    @asyncio.coroutine
components/binary_sensor/xiaomi.py-    def _async_delay_fire_motion(self):
components/binary_sensor/xiaomi.py:        self._hass.bus.fire('motion', {
components/binary_sensor/xiaomi.py-            'entity_id': self.entity_id
components/binary_sensor/xiaomi.py-        })
components/binary_sensor/xiaomi.py-
--
components/binary_sensor/xiaomi.py-        elif value == 'double_click':
components/binary_sensor/xiaomi.py-            click_type = 'double'
components/binary_sensor/xiaomi.py-
components/binary_sensor/xiaomi.py:        self._hass.bus.fire('click', {
components/binary_sensor/xiaomi.py-            'entity_id': self.entity_id,
components/binary_sensor/xiaomi.py-            'click_type': click_type
components/binary_sensor/xiaomi.py-        })
--
components/binary_sensor/xiaomi.py-    def parse_data(self, data):
components/binary_sensor/xiaomi.py-        """Parse data sent by gateway"""
components/binary_sensor/xiaomi.py-        if self.STATUS in data:
components/binary_sensor/xiaomi.py:            self._hass.bus.fire('cube_action', {
components/binary_sensor/xiaomi.py-                'entity_id': self.entity_id,
components/binary_sensor/xiaomi.py-                'action_type': data[self.STATUS]
components/binary_sensor/xiaomi.py-            })
components/binary_sensor/xiaomi.py-
components/binary_sensor/xiaomi.py-        if self.ROTATE in data:
components/binary_sensor/xiaomi.py:            self._hass.bus.fire('cube_action', {
components/binary_sensor/xiaomi.py-                'entity_id': self.entity_id,
components/binary_sensor/xiaomi.py-                'action_type': self.ROTATE,
components/binary_sensor/xiaomi.py- 
$

The event_type is called “motion” instead of “motion_action” and no action_type is used?! Did I miss something?

This is the xiaomi gateway you are looking for: http://www.gearbest.com/living-appliances/pp_344667.html

Hi all. I believe I am stuck. I have followed all instructions, got the key from the mihome app copied the files to the custom_components dir I even manually installed pycrypto and still cannot get it to work. I get this error from the log file.

2017-07-12 18:15:19 ERROR (MainThread) [homeassistant.setup] Error during setup of component xiaomi
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/setup.py", line 190, in _async_setup_component
component.setup, hass, processed_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/homeassistant/.homeassistant/custom_components/xiaomi.py", line 79, in setup
PY_XIAOMI_GATEWAY.discover_gateways()
  File "/home/homeassistant/.homeassistant/custom_components/xiaomi.py", line 230, in discover_gateways
self.gateways[ip_add] = XiaomiGateway(ip_add, port, sid, gateway_key, self._socket)
  File "/home/homeassistant/.homeassistant/custom_components/xiaomi.py", line 325, in __init__
if self._discover_devices():
  File "/home/homeassistant/.homeassistant/custom_components/xiaomi.py", line 334, in _discover_devices
self.update_key(resp["token"])
  File "/home/homeassistant/.homeassistant/custom_components/xiaomi.py", line 462, in update_key
encryptor = AES.new(self.key.encode(), AES.MODE_CBC, IV=init_vector)
  File "/home/homeassistant/.homeassistant/deps/Crypto/Cipher/AES.py", line 95, in new
return AESCipher(key, *args, **kwargs)
  File "/home/homeassistant/.homeassistant/deps/Crypto/Cipher/AES.py", line 59, in __init__
blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
  File "/home/homeassistant/.homeassistant/deps/Crypto/Cipher/blockalgo.py", line 141, in __init__
self._cipher = factory.new(key, *args, **kwargs)
ValueError: Key cannot be the null string

my config.yaml looks like this

xiaomi:
 gateways:
  - sid: 192.168.188.36
    key: 6dq8re5kaxi97du5 

sorry for the noobness but I can not figure out what I am doing wrong.

Thanks in advance guys

The sid is not an IP address, it should be the mac address without semicolon.
Check the instruction above.

If you have only one gateway, you do not even need to put anything for the sid as below.

xiaomi:
  gateways:
    - sid:
      key: 6dq8re5kaxi97du5

I bought it, on the way

I’m having issues adding the gateway, added this to my configuration.yaml:
xiaomi:
gateways:
- sid:
key: xxxxxxxxxxxxx

and then when I check config, i get this:
Failed config General Errors: - Component not found: xiaomi - Setup failed for xiaomi: Component not found.

Any ideas?
2 yeelights i have appear on discovery without issue

did you put the xiaomi.py in custom_components in your HA folder?

No,

Is there a default xiaomi.py file somewhere?

Use this one https://github.com/Danielhiversen/homeassistant and place it in here
Download the components folder and place it inside your home automation folder
Then rename components to custom_components and then it should work fine.

Cheers matt. So it goes into /home/homeassistant/.homeassistant where the configuration.yaml etc is? And I obviously change "default_key=“xxxxxxxxx” to my device id?

Yep thats where you need to put the custom_components folder and inside that you’d have xiaomi.py and the other folders with sensors and switches etc…