Xiaomi Gateway Integration

I think is the new one, will tell you when arrives

Hello.

Just got today a brand new Xiaomi Cube to play around. It looks quite nice and adding it to the gateway was a piece of cake.

In Home Assistant, is there any way to be able to understand which event is being triggered? Entity state seems to be always off (Binary sensor?).

Still then, if I activate debug mode for Xiaomi component, I can see different events being triggered as I try them:

2017-07-13 22:00:51 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {'model': 'cube', 'sid': '158d0001135dbf', 'data': '{"status":"shake_air"}', 'short_id': 16197, 'cmd': 'report'}
2017-07-13 22:00:51 DEBUG (Thread-9) [custom_components.xiaomi] PUSH >> <Entity Cube_158d0001135dbf: off>: {'status': 'shake_air'}
2017-07-13 22:02:24 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {'model': 'cube', 'sid': '158d0001135dbf', 'data': '{"status":"free_fall"}', 'short_id': 16197, 'cmd': 'report'}
2017-07-13 22:02:24 DEBUG (Thread-6) [custom_components.xiaomi] PUSH >> <Entity Cube_158d0001135dbf: off>: {'status': 'free_fall'}
2017-07-13 22:08:29 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {'model': 'cube', 'sid': '158d0001135dbf', 'data': '{"status":"alert"}', 'short_id': 16197, 'cmd': 'report'}
2017-07-13 22:08:29 DEBUG (Thread-5) [custom_components.xiaomi] PUSH >> <Entity Cube_158d0001135dbf: off>: {'status': 'alert'}
2017-07-13 22:08:47 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {'model': 'cube', 'sid': '158d0001135dbf', 'data': '{"status":"flip90"}', 'short_id': 16197, 'cmd': 'report'}
2017-07-13 22:08:47 DEBUG (Thread-12) [custom_components.xiaomi] PUSH >> <Entity Cube_158d0001135dbf: off>: {'status': 'flip90'}
2017-07-13 22:08:49 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {'model': 'cube', 'sid': '158d0001135dbf', 'data': '{"status":"flip180"}', 'short_id': 16197, 'cmd': 'report'}
2017-07-13 22:08:49 DEBUG (Thread-3) [custom_components.xiaomi] PUSH >> <Entity Cube_158d0001135dbf: off>: {'status': 'flip180'}
2017-07-13 22:11:50 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {'model': 'cube', 'sid': '158d0001135dbf', 'data': '{"status":"tap_twice"}', 'short_id': 16197, 'cmd': 'report'}
2017-07-13 22:11:50 DEBUG (Thread-10) [custom_components.xiaomi] PUSH >> <Entity Cube_158d0001135dbf: off>: {'status': 'tap_twice'}
2017-07-13 22:16:27 DEBUG (Thread-13) [custom_components.xiaomi] MCAST (report) << {'model': 'cube', 'sid': '158d0001135dbf', 'data': '{"rotate":"-34,500"}', 'short_id': 16197, 'cmd': 'report'}
2017-07-13 22:16:27 DEBUG (Thread-5) [custom_components.xiaomi] PUSH >> <Entity Cube_158d0001135dbf: off>: {'rotate': '-34,500'}`

I suppose this means that HA is properly capturing the cube motion events.
What I’d like would be to get this status information into HA frontend. Is that even possible using @Danielhiversen repo?

Thanks in advance.

I’ve uploaded a few sounds to my gateway to play around with the cube:


- alias: Cube flip90
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d00011a052e
      action_type: flip90
  action:
    - service: script.turn_on                                                                                                                                                                      
      entity_id: script.play_sound                                                                                                                                                                 
      data:                                                                                                                                                                                        
        variables:                                                                                                                                                                                 
          ringtone_vol: 8                                                                                                                                                                          
          ringtone_id: 10002                                                                                                                                                                       
                                                                                                                                                                                                   
- alias: Cube flip180                                                                                                                                                                              
  trigger:                                                                                                                                                                                         
    platform: event                                                                                                                                                                                
    event_type: cube_action                                                                                                                                                                        
    event_data:                                                                                                                                                                                    
      entity_id: binary_sensor.cube_158d00011a052e                                                                                                                                                 
      action_type: flip180                                                                                                                                                                         
  action:                                                                                                                                                                                          
    - service: script.turn_on                                                                                                                                                                      
      entity_id: script.play_sound                                                                                                                                                                 
      data:                                                                                                                                                                                        
        variables:                                                                                                                                                                                 
          ringtone_vol: 8                                                                                                                                                                          
          ringtone_id: 10003                                                                                                                                                                       
                                                                                                                                                                                                   
- alias: Cube move                                                                                                                                                                                 
  trigger:                                                                                                                                                                                         
    platform: event                                                                                                                                                                                
    event_type: cube_action                                                                                                                                                                        
    event_data:                                                                                                                                                                                    
      entity_id: binary_sensor.cube_158d00011a052e                                                                                                                                                 
      action_type: move                                                                                                                                                                            
  action:                                                                                                                                                                                          
    - service: script.turn_on                                                                                                                                                                      
      entity_id: script.play_sound                                                                                                                                                                 
      data:                                                                                                                                                                                        
        variables:                                                                                                                                                                                 
          ringtone_vol: 8                                                                                                                                                                          
          ringtone_id: 10009                                                                                                                                                                       
                                                                                                                                                                                                   
- alias: Cube tap_twice                                                                                                                                                                            
  trigger:                                                                                                                                                                                         
    platform: event                                                                                                                                                                                
    event_type: cube_action                                                                                                                                                                        
    event_data:
      entity_id: binary_sensor.cube_158d00011a052e
      action_type: tap_twice
  action:
    - service: script.turn_on
      entity_id: script.play_sound
      data:
        variables:
          ringtone_vol: 8
          ringtone_id: 10008

- alias: Cube shake_air
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d00011a052e
      action_type: shake_air
  action:
    - service: script.turn_on
      entity_id: script.play_sound
      data:
        variables:
          ringtone_vol: 8
          ringtone_id: 10007

- alias: Cube swing
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d00011a052e
      action_type: swing
  action:
    - service: script.turn_on
      entity_id: script.play_sound
      data:
        variables:
          ringtone_vol: 8
          ringtone_id: 10006

- alias: Cube alert
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d00011a052e
      action_type: alert
  action:
    - service: script.turn_on
      entity_id: script.play_sound
      data:
        variables:
          ringtone_vol: 8
          ringtone_id: 10005

- alias: Cube free_fall
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d00011a052e
      action_type: free_fall
  action:
    - service: script.turn_on
      entity_id: script.play_sound
      data:
        variables:
          ringtone_vol: 8
          ringtone_id: 10004
3 Likes

And you can dim your light. But the code is bogus because the brightness attribute is not available if my light is off. Because of this fact I have to turn on the light first before I can dim the light with the cube. May be somebody can improve it:

- alias: Set brightness by rotating cube
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d00011a052e
      action_type: rotate
  action:
    service: light.turn_on
    data_template:
      entity_id: light.couch
      brightness: >
        {% set state = states.light.couch.attributes.brightness|int + ((trigger.event.data.action_value|float)*1.25)|int -%}
        {%-  if state > 255 -%}
          {%- set state  = 255 -%}
        {%-  elif state < 0 -%}
          {%- set state  = 0 -%}
        {%- endif %}
        {{ state }}
2 Likes

Very cool… how do you upload sounds to the gateway?

Thanks,

Tom

1 Like

It’s a feature of the Mi Home app: Select the gateway from the list view of “My devices” → Switch to “devices” at the top navigation → select the gateway again → Ringtone settings → Doorbell ring tone → Add ringtone → Phone music list → All audio files of your android smartphone will be listed, select a audio file → Confirm with “Yes” in the top right corner.

I uploaded a bunch of short notification sounds which can be accessed by the high ringtone ids (> 100001).

5 Likes

@Danielhiversen just merged the new feature (curtain device support): https://github.com/Danielhiversen/homeassistant/pull/20

Is there any model with UE Plug? Thanks :smiley:

Looks like I need to find an Android device - no worky on my iPhone.

Thanks for the instructions!

IOS instructions below…

Mi Home app: Select the relevant gateway from the list view of “My devices” -> Switch to “Device” at the top navigation -> select the gateway again -> Ringtone settings -> Door bell tone -> Press to record (make sure you hold on till you finish recording) -> save -> input name and Confirm. I forgot if need to press upload button after that or not.

3 Likes

… and that worked perfectly. Thanks!

How did you determine the ringtone id? Are you limited to just the ringtones or can you play anything that is uploaded to the gateway?

Thanks!

If I understood it correctly, it should be all. Probably @syssi can help more as I have not used the sound in HA yet.

1 Like

You can upload any audio file to the gateway. Your first upload is the 100001. The second 100002 and so on. :slight_smile: In my case the files are short notification sounds and they stops playing on its own. Real ringtones (lower numbers) are endless loops and you have to call the stop service.

1 Like

My gateway ringtones stop after about 5 seconds. How can I have a loop with the normal gateway?

Oh, you are right. There is no loop.

  action:                                                                                                                                                                                          
    - service: script.turn_on                                                                                                                                                                      
      entity_id: script.play_sound                                                                                                                                                                 
      data:                                                                                                                                                                                        
        variables:                                                                                                                                                                                 
          ringtone_vol: 8                                                                                                                                                                          
          ringtone_id: 10003  

How do I get service: “script.turn_on” ?

Thanks (I’m such a noob!)

Oooh, no. I posted a incomplete piece of code. This is the missing part:



input_boolean:
  mute_gateway_sounds:
    name: Mute
    icon: mdi:music-off

script:
  play_sound:
    sequence:
      - condition: state
        entity_id: input_boolean.mute_gateway_sounds
        state: 'off'
      - service: xiaomi.play_ringtone
        data_template:
          gw_sid: !secret 
          ringtone_id: "{{ ringtone_id }}"
          ringtone_vol: "{{ ringtone_vol }}"

I use the script to mute the notification sounds by an input_boolean.

4 Likes

Yup - working now - very nice!. Thank you for your help with this stuff!

My parcel arrived and I was so happy and relieved when i opened it. It is the new motion sensor. :heart_eyes: Will try to set it up later when I am free.

2 Likes