Xiaomi Gateway Integration

I’m very sorry for asking this silly question but I still cannot figure out what the key is. I followed the steps here http://bbs.xiaomi.cn/t-13198850:

8 Write down the password for the fifth step and the MAC address that you noted in step 7, both of which will be used as the gateway name and password and will be configured into the config.json file at a later raspberry pie. Now, the gateway information to complete the steps to complete friends.

@Danielhiversen: There is a missing closing bracket in the binary_sensor/xiaomi.py file in line 87. This is causing an error and the binary_sensors don’t show up in HA.

Thanks, fixed now.

4 Likes

open the gateway app
press the 3 dots icon (top right)
press about
press local area network communication protocol
enable (top option)
the password is what you put for the key in your config.yaml
(should be 16 digit)

1 Like

Thanks for input. I’ll update code soon.

@Danielhiversen I use your fork and today when automation tried to turn on gateway light errors appear in log:

17-04-23 21:36:42 ERROR (Thread-7) [custom_components.xiaomi] Non matching response. Expecting read_ack, but got write_ack
17-04-23 21:36:42 ERROR (Thread-4) [custom_components.xiaomi] Non matching response. Expecting write_ack, but got read_ack
17-04-23 21:36:42 ERROR (Thread-7) [custom_components.xiaomi] No data in response from hub None
17-04-23 21:36:42 ERROR (Thread-4) [custom_components.xiaomi] No data in response from hub None

It’s first time when I see something like this.

Hi. Could you please replace file /custom_components/switch/xiaomi.py with following https://github.com/bastshoes/homeassistant/tree/master/components/switch and test plug again.

I would keep KWh as measurement of energy consumption, the bills are in KWh

I think this developer has crack code to add Philips hue and other zigbee make http://faire-ca-soi-meme.fr/hack/2017/04/24/hack-xiaomi-mi-smarthome-decouverte-de-clef/

1 Like

There were a few errors on something like inconsistent use of tab and space
I fixed those but now I get this:

Traceback (most recent call last):
  File "/usr/src/app/homeassistant/helpers/entity_component.py", line 155, in _async_setup_platform
    entity_platform.schedule_add_entities, discovery_info
  File "uvloop/future.pyx", line 230, in __iter__ (uvloop/loop.c:110600)
  File "uvloop/future.pyx", line 432, in uvloop.loop.BaseTask._fast_wakeup (uvloop/loop.c:113980)
  File "uvloop/future.pyx", line 101, in uvloop.loop.BaseFuture._result_impl (uvloop/loop.c:108900)
  File "/usr/local/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/switch/xiaomi.py", line 31, in setup_platform
    devices.append(XiaomiGenericSwitch(device, "Plug", 'status', gateway))
  File "/config/custom_components/switch/xiaomi.py", line 50, in __init__
    XiaomiDevice.__init__(self, device, name, xiaomi_hub)
  File "/config/custom_components/xiaomi.py", line 446, in __init__
    self.parse_data(device['data'])
  File "/config/custom_components/switch/xiaomi.py", line 102, in parse_data
    self._power_consumed = (int(data[POWER_CONSUMED])/1000)+' kWh'
TypeError: unsupported operand type(s) for +: 'float' and 'str'

Also I think I would prefer the number without the unit of measure in the attribute.
It is much easier to work with a number than to extract it from the string.
Make sure the unit is clear from the documentation.
Like you do with battery level as attribute. it does not have the % at the end.

But it is up to you.

I will probably change it into a template sensor with a Unit Of Measure attribute anyway so it can be plotted.
Strings won’t plot.

Whoa! He did crack the key. That means if you get a ZigBee stick you can use that instead! No more open/movement on power cuts.

1 Like

the article is of today …

I think the developer is trying to give us more access to the gateway like ikea tradfri

Yeah I see this as the most likely outcome.
Adding support for the xiaomi sensors in closed ecosystems (e.g. Hue) is less likely IMO

Ok. I’ve updated file. Please check.

I have the Kettle but you cant initiate a boil from the app or gateway.
Also the kettle connects via bluetooth and not zigbee.

I have not found a way to start a boil remotely yet.
If you find a way, I would be more than happy to try it

I have removed the tabs again, suggested an edit on github.
Now it loads, need to test some more.

Edit:
The OFF and IN-USE and LOAD POWER are very fast from on to off.
Still rather slow when switching on.

Edit2: not sure my github edit worked on your repo. Here is the file I cleaned up. https://github.com/Data-Monkey/HomeAutomation/blob/master/xiaomy.py

does anyone have an example of automation using the ringtone (alarm) function? I can’t figure it out what to put in the yaml.

also, can’t seem to be able to trigger action based on the motion sensor. I’ve spent 2 hours on it. Can anyone help?
I’ve tried using to: 'on' as below, also tried to: 'motion' and also tried state: 'on'

- alias: Turn on light on motion
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00011xxxx
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.gateway_light_f0b429cxxx

This motion trigger works for me:

trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_158d000113xxxx
    to: 'on'
condition:
  condition: and
  conditions:
  - condition: state
    entity_id: group.lamp
    state: 'off'
  - condition: state
    entity_id: media_player.samsung_tv_remote
    state: 'off'
  - condition: time
    after: '00:00:00'
  - condition: sun
    before: sunrise
action:
  - service: homeassistant.turn_on
    entity_id: scene.nightlight

Could it be that the entity ID of your motion sensor is missing a digit? Looks like it is if the xxxx’s replace actual characters.

Also to use the ringtone feature, you can do this (an example automation from my setup):

    trigger:
      platform: event
      event_type: flic_click
      event_data:
        button_name: flic_80e4da72d2bb
        click_type: single
    action:
      - service: switch.toggle
        entity_id: switch.plug_158d00012a9afb
      - service: xiaomi.play_ringtone
        data:
          ringtone_id: 20
          gw_sid: 286c0788xxxx
          ringtone_vol: 20
2 Likes