Xiaomi Gateway Integration

Cube is now showing in the front end. Tomorrow I see what automations I can set up. Thank you!

Please report back as it looks like a really intersting gadget to integrate with HA

First of all great work! Beside a problem finding the right gateway it all works wonderful!

I have currently 3 gateways and a lot of sensors (even open/close on my mailbox) so it would be great to see multiple gateway support in the future!

If you have multiple gateways be sure to check the logs which gateway is found first and use that code. Would be nice if we could also specify the gateway IP. I’m loving HA, so maybe it is time to learn some Python and help out or develop some plugins of my own.

Two things I found so far were a random trigger of the motion sensor that also did not show up in the Mi Home App. And all cats we accounted for. :slight_smile:

And if I control the power plug in the Mi Home App the status is picked up in HA. But if I try to power on the plug in HA it will turn on in the HA, but it is actually still off and off course Mi Home shows off then also.

Any ideas or pointers on this?

Do you mean turning on the plug in HA doesn’t physically turn on the plug itself?

Yes, I have some logs in you are interested.

i think you’re more interested than i am… post that here so the community can help

1 Like

Haha, I guess you’re right… the logs look pretty much the same in both cases.

[Removed]

Ok, found the problem… it’s me! Never trust users, haha. :slight_smile:

Will report back to confirm.

is there a reason why you went with 3 gateways instead of 1 gateway + using the plugs as repeaters?
Multiple gateway support would be great though either way

did you see this the first time HA turned on? i had sensors that got stuck in on state (after HA died and I had to reboot) and i had to trigger it first before it went back to normal.

Ok, back to square one. I was trying to add IP gateway setting for those with multiple gateways, because it would randomly connect with different gateways. Then ran into a lot of errors thinking it was my code. Then reverted everything and kept keeping errors. Added some extra logging and it seems now my gateway is responding with the message of another gateway?

17-01-08 12:42:44 INFO (MainThread) [homeassistant.bootstrap] Setting up xiaomi
17-01-08 12:42:44 INFO (Thread-4) [custom_components.xiaomi] Discovering Xiaomi Gateways
17-01-08 12:42:44 INFO (Thread-4) [custom_components.xiaomi] Gateway found on IP 192.168.1.46
17-01-08 12:42:44 INFO (Thread-4) [custom_components.xiaomi] Creating Multicast Socket
17-01-08 12:42:44 INFO (Thread-4) [custom_components.xiaomi] Listening
17-01-08 12:42:44 INFO (Thread-4) [custom_components.xiaomi] Discovering Xiaomi Devices
17-01-08 12:42:44 ERROR (Thread-4) [custom_components.xiaomi] Response from 192.168.1.46 does not match return cmd
17-01-08 12:42:44 ERROR (Thread-4) [custom_components.xiaomi] b'{"cmd":"iam","port":"9898","sid":"f0b4299aaa86","model":"gateway","ip":"192.168.1.47"}'
17-01-08 12:42:44 ERROR (MainThread) [homeassistant.bootstrap] Error during setup of component xiaomi

Notice the connection to 192.168.1.46 and the received “iam” reply with IP 192.168.1.47 (!)

@rave: Could you explain this? Was I just lucky the first few times?

I reverted to the current github version. Going to try and reset everything and then only use one gateway to confirm my earlier problem I was trying to solve.

I bought a starter kit and then later another one because for that price I couldn’t get the sensors separately. But I bought a third kit because even with two the range wasn’t really great. Only learned that sockets can be repeaters later on.

Also I use the gateways as night lights so if you enter the room there is some light.

it doesn’t work with multiple gateway. Turn off the other gateways and it will work

Just did that and now it is working again! Guess I was really lucky the first time.

This was also related to multiple gateways. And an error on my side. The switch was actually connected to another gateway but showed up on the one I was connecting to so I did not question if it was maybe connected to another gateway.

Lesson learned: switches show up on other gateways and show status, but cannot be controlled.

So everything is working as expected again! (Time to turn on the other gateways :slight_smile: )

With the latest fix, a cube shows up on my HA frontend, but no luck getting actionable data from it.

- alias: cube actions
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d00011a0350
      action_type: flip90
  action:
    - service: tts.google_say
      entity_id: media_player.chatty_cathy
      data:
        message: 'Flip 90.'

Same automation I’m using to test my wireless switch, which is reporting click, double click, and hold, but the cube isn’t registering the 90 degree turn. I do see the 90 degree flip in the Mi app.

edit.–to clarify: flip90 is the only thing I’ve tried. I assume no cube actions are being registered.

After an enjoyable day of learning Python and fighting errors I think I have added multiple gateway support! :slight_smile:

It’s a really quick hack and you have to put the gateway IP addresses and keys in the config, but it works for me.

All the credit goes to rave for building this! Here is my small addition for those interested.

@rave: After I’ve tested it some more I can make a pull request if you like. Not sure about the effects of my code alterations because this is my fist adventure into Python. Be gentle. :slight_smile:

You can set multiple gateways by using their IP and key. Just leave the main key empty because it is not used.

xiaomi:
    key: ""
    gateways:
      - name: Living
        gateway: 192.168.1.110
        key: xxxxxxxxxxxxxxxx
      - name: Bedroom
        gateway: 192.168.1.112
        key: xxxxxxxxxxxxxxxx
      - name: Kitchen
        gateway: 192.168.1.114
        key: xxxxxxxxxxxxxxxx

Great job. It was my first time writing in python when i wrote this component too. I checked your code and there are several things needs to be considered. Such as mapping the token to the correct gateway and make sure the the device is using the right gateway to write data to the hub such as turning on and off plugs. The multicast socket listener needs to be extracted out of the gateway class so it doesn’t listen twice to the broadcast message.

The motion sensor takes 2 minutes to turn off in Home Assistant. However, it does seem to keep responding instantly to the Xiaomi Hub: I tested adding an automation in the Mi App to ring the doorbell whenever there is motion. It rings every time without delay!

Does anybody know how to increase the motion response resolution/delay in home assistant?

Besides, is it possible to detect and use the Xiaomi hub in HA if the hub has no internet connection? I noticed HA does not start up in that case.

Do you mean you start up HA when there’s no internet? What about if you already start up HA and you disconnect the internet, does it still work?

Also, it seems strange it xiaomi depends on internet connection. Do you have the log when you start up HA without internet?

you mean
xiaomi app ON instantly and OFF instantly

HA ON instantly OFF 2 minutes??

I think that is covered. Each hub gets its own instance with its own settings. Each hub instance also registers its own list of devices.

Devices already contain a reference to the hub I think. Will have to look into that. Currently it is working in my setup. Going to order some more plugs today, but it will take 4 weeks until they arrive. Maybe someone else can test the current setup. I do get all sensor data.

Yes, now everything is received 3 times and the commands are probably sent 3 times and only one has the correct token.