Xiaomi Gateway Integration

What do you mean you can’t get it working with WiFi? What needs to be wired? Does the gateway have an ethernet port (I don’t have mine in hand yet), or are you referring to the device running homeassistant needing to be wired? IIRC from reading elsewhere, the Xiaomi Gateway uses broadcast messages, which your AP (or wireless router) likely isn’t forwarding. I want to say I read this on another Xiaomi-related post here on the hass community forums.

Also, ZigBee devices, like Z-Wave devices, act as repeaters when powered (not battery devices). However, the ZigBee protocol isn’t as universal as Z-Wave, in that each manufacturer can make slight differences that prevent ZigBee devices of another brand from working with a ZigBee Gateway. Again, this is just my understanding of ZigBee, which I’m fairly new too.

Sorry - doesn’t work on wifi from the HASS server while testing on my laptop (macbook retina) using osx. I need to be hardwired into the network to discover the hub.

I do all my testing currently on my macbook before moving it to the pi. (I had a pi1 and its slow as all hell while i’m waiting for my pi3 to ship)

The hub has not ethernet port so its wifi only (which scares me a little for a long term fitout) but for the price I am not complaining!

@rave have you (or anyone else) had any luck getting the battery status of the sensors working?

It would be awesome to see some example setups people have started using for these…

I’m thinking of setting up a few switches with the following setup -

Hallway -
single press - dim 30%, then turn off if no motion for 3min
double press - 100% turn off if no motion for 3min
Long press - stay on stop motion turn off (turn off with single press that will start the 30% for 3min)

What is the best way to post more than a single line of code in the forum? This is what I have got written thus far (i haven’t gotten to figuring out how to involve motion / delay yet… but I’m sure I will get there…)

1 Like

It seems like the gateway did not send any battery status at all. So right now, there’s no way to get the battery

Have a look at the last line, it states “taberror inconsistent use of tabs and spaces in indentation”.
Use a texteditor (like notepad++ or notepadbb) and show all characters.

I think you will see some tab char’s instead of spaces.
Remove the tab’s and use the space bar to align the code.

thanks, I managed to have it work. Unfortunately/fortunately now I don’t have an error (it works), but is intermittent, if/when I stop HA and reboot it, it will come up again … sometimes. That time I will post the log

Here is the log

17-01-01 11:48:17 homeassistant.bootstrap: Setting up xiaomi
17-01-01 11:48:17 custom_components.xiaomi: Discovering Xiaomi Gateways
17-01-01 11:48:17 custom_components.xiaomi: Gateway found on IP 192.168.1.230
17-01-01 11:48:17 custom_components.xiaomi: Creating Multicast Socket
17-01-01 11:48:17 custom_components.xiaomi: Listening
17-01-01 11:48:17 custom_components.xiaomi: Discovering Xiaomi Devices
17-01-01 11:48:17 custom_components.xiaomi: {'model': 'gateway', 'ip': '192.168.1.230', 'cmd': 'iam', 'port': '9898', 'sid': 'f0b429aa0aa8'}
17-01-01 11:48:17 custom_components.xiaomi: Response does not match return cmd
17-01-01 11:48:17 homeassistant.bootstrap: Error during setup of component xiaomi
Traceback (most recent call last):
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/bootstrap.py", line 151, in _async_setup_component
    None, component.setup, hass, 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/hass/.homeassistant/custom_components/xiaomi.py", line 44, in setup
    XIAOMI_HUB = XiaomiHub(key)
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 113, in __init__
    self._discover_devices()
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 119, in _discover_devices
    self.GATEWAY_TOKEN = resp["token"]
TypeError: 'NoneType' object is not subscriptable
17-01-01 11:48:17 homeassistant.loader: Loaded switch.broadlink from homeassistant.components.switch.broadlink
17-01-01 11:48:17 homeassistant.core: Bus:Handling <Event call_service[L]: domain=persistent_notification, service_data=message=The following components and platforms could not be set up:
* [xiaomi](https://home-assistant.io/components/xiaomi/)
Please check your config, title=Invalid config, notification_id=invalid_config, service=create, service_call_id=1977524272-1>
17-01-01 11:48:17 homeassistant.core: Bus:Handling <Event state_changed[L]: old_state=None, new_state=<state persistent_notification.invalid_config=The following components and platforms could not be set up:
* [xiaomi](https://home-assistant.io/components/xiaomi/)
Please check your config; title=Invalid config @ 2017-01-01T11:48:17.941517+01:00>, entity_id=persistent_notification.invalid_config>
17-01-01 11:48:17 homeassistant.core: Bus:Handling <Event service_executed[L]: service_call_id=1977524272-1>
17-01-01 11:48:18 homeassistant.bootstrap: Setting up switch
17-01-01 11:48:18 homeassistant.components.switch: Setting up switch.broadlink

You could be using an old gateway or old firmware that doesn’t support the API. The response should be a list of Zigbee devices connected to the hub. Not an I Am response like in the log.

My recommendations is to google around and make sure you have the latest hub and firmware and double check and go through the steps i provided and in the link.

Firmware is
1.4.1-142.0141

Mi home
3.10. 6

Hw is
MW300
Lumi gateway v3
Fw 0141.

I doubt is the hardware … becsause sometimes HA integration works perfectly, with no errors

Do you mean you can get it to work before? If that’s the case, make sure you’re not running any other Aqara components like the one from fooxy and the one from homebridge. They’re not compatible with this because we’re ultimately trying to achieve the same thing, to talk to the hub.

Finally got my second hub. The cube is coming tomorrow (i guess). Going to start playing with them soon

3 Likes

yes it does work. I don’t know how to make sure it works, it is intermittent.

I did not put the Aqara component

Do you have multiple hub?

No, one Xiaomi gateway only (I have a Hue Bridge)

unfortunately, i don’t have any advise on how to help you. I think what you’ll need is a way to consistently reproduce the error or try different setup until it works consistently. That means either turning off the Hue Bridge, unloading all the plugins or other things that you can think of

Thanks for your work rave.

Do you have any plans to add functions such as being able to play media over the gateway or change the radio stations? I’d love to be able to use mine for tts notifications or as an alarm clock.

Me too but there’s no public API for that so i doubt it will be controllable at all

@rave. I finally got it all working too. Currently running HA on my HP microserver with Win10 x64 Beer coming your way :smiley:

Some issues I had (mainly windows based) and hopefully this will help others:

using python 3.5 and NOT 3.6

the error: OSError: [WinError 10049] The requested address is not valid in its context. I had two NIC. I had to change
sock.bind((self.MULTICAST_ADDRESS, self.MULTICAST_PORT)) to
sock.bind((“MyIPAddress”, self.MULTICAST_PORT))
make sure you double quote

pyCyrpto. This is a pain since it won’t compile properly on windows. You need Visual studio build tools for it to work. My main PC had that so I installed using pip3 install and then copied the files across to my HA machine

Friendly names. too me 2 hours to get it working and I feel stupid. The key is not to add it anywhere in configuration.yaml but under home assistant: so it looks like

homeassistant:
  name: Home
  latitude:
  longitude: 
  elevation: 
  unit_system: metric
  time_zone: 
  customize:

Make sure its 2 spaces before the word customize. Use Notepad++. Better still, you can include a customize.yaml or even a directory. my code looks like this

homeassistant:
  name: Home
  latitude:
  longitude: 
  elevation: 
  unit_system: metric
  time_zone: 
  customize: !include_dir_merge_named customize

I have a directory called customize in the root and then xiaomi.yaml with all the customizations

inside xiaomi.yaml I have

#Door
binary_sensor.door_window_sensor_158d00xxxxab:
  friendly_name: D Balcony
binary_sensor.door_window_sensor_158d000xxxx0:
  friendly_name: D Bottom Garage

Note the spacing and also you need a space after friendly_name:

@rave, https://github.com/louisZL/lumi-gateway-local-api was updated a few days ago. A few new features include cube controller setup and also changing the light on the gateway inside https://github.com/louisZL/lumi-gateway-local-api/blob/master/网关.md
Hopefully you can work on the new template since currently the Door sensor and PIR are grouped together. Be nice to have the xiaomi app functionality of showing last triggered time. also a listview of historical activation states would be nice too.

@kaze. I have one PIR sensor that was stuck on the ‘on’ state. once I went past it to activate it, it went back to normal

Note with the Door sensor, if you very quickly close and open the sensor ie bring the magnets close and pull it away, it get’s stuck in the ‘closed’ state even though it is actually open. the xiaomi app reports it as closed too.

Glad you got it working. Great job noticing that update, I’ll take a look and hopefully can get it to work in HA soon

And thanks for the beer :grin:

you know what, i actually did try the code that he posted a while but unfortunately, the result is “device not found” after i send the RGB data.

{"cmd":"write_ack","sid":"f0b429cbxxxx","data":"{\"error\":\"No device\"}"}