Homeassistant安防功能与米家网关的联动自动化

一、创建安防实体,并配置相应属性。
二、在lovelace里添加安防面板。
三、创建延迟脚本和自动化,使虚拟的安防实体与米家网关进行联动。本人测试了多功能网关、空调伴侣升级版、多模网关,都可以联动,但由于多模网关没有内置音乐播放器,所以需要其他设备来播放警报和延时音乐。

alarm_control_panel:
  - platform: manual
    name: Home Alarm
    code: "1234"
    code_arm_required: false
    arming_time: 300
    delay_time: 120
    trigger_time: 600
    disarmed:
      trigger_time: 0
    armed_home:
      arming_time: 0
      delay_time: 0
    armed_away:
      arming_time: 20
      delay_time: 20
    armed_night:
      arming_time: 0
      delay_time: 0


script: 
  security_alarm_loop_1:
    sequence:
    - service: script.turn_off
      target:
        entity_id:
          - script.security_countdown_loop_1
          - script.security_countdown_loop_2
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: # 网关的mac地址
        ringtone_id: 2 
        ringtone_vol: 100
    - service: script.turn_off
      entity_id: script.security_alarm_loop_2
    - delay: "00:00:6"
    - service: script.turn_on
      entity_id: script.security_alarm_loop_2

  security_alarm_loop_2:
    sequence:
    - service: script.turn_off
      target:
        entity_id:
          - script.security_countdown_loop_1
          - script.security_countdown_loop_2
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: # 网关的mac地址
        ringtone_id: 1 
        ringtone_vol: 100
    - service: script.turn_off
      entity_id: script.security_alarm_loop_1
    - delay: "00:00:10"
    - service: script.turn_on
      entity_id: script.security_alarm_loop_1

  security_countdown_loop_1:
    sequence:
    - service: script.turn_off
      entity_id: script.security_countdown_loop_2
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: # 网关的mac地址
        ringtone_id: 29 
        ringtone_vol: 10
    - delay: "00:00:15"
    - service: script.turn_on
      entity_id: script.security_countdown_loop_2

  security_countdown_loop_2:
    sequence:
    - service: script.turn_off
      entity_id: script.security_countdown_loop_1
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: # 网关的mac地址
        ringtone_id: 29 
        ringtone_vol: 10
    - delay: "00:00:15"
    - service: script.turn_on
      entity_id: script.security_countdown_loop_1

  security_stop_loop_sounds:
    alias: Stop Security Sound Scripts on Gateway
    sequence:
    - service: script.turn_off
      target:
        entity_id:
          - script.security_countdown_loop_1
          - script.security_countdown_loop_2
          - script.security_alarm_loop_1
          - script.security_alarm_loop_2
    - service: xiaomi_aqara.stop_ringtone
      data:
        gw_mac: # 网关的mac地址

automation:
- alias: SECURITY Disarmed State
  trigger:
    - platform: state
      entity_id: alarm_control_panel.home_alarm
      to: "disarmed"
  action:
    # - service: media_player.volume_set
    #   data:
    #     volume_level: 0.6
    #   target:
    #     entity_id: media_player.nest_hub_max
    # - service: tts.google_translate_say
    #   entity_id: media_player.nest_hub_max
    #   data:
    #     message: 'Security System has been Disarmed'
    - service: script.security_stop_loop_sounds

- alias: SECURITY Arming State
  trigger:
    - platform: state
      entity_id: alarm_control_panel.home_alarm
      to: "arming"
  action:
    # - service: media_player.volume_set
    #   data:
    #     volume_level: 0.6
    #   target:
    #     entity_id: media_player.nest_hub_max
    # - service: tts.google_translate_say
    #   entity_id: media_player.nest_hub_max
    #   data:
    #     message: 'Security System is Arming, you have 5 minutes'
    - service: script.security_countdown_loop_1
    
- alias: SECURITY Armed_Away State
  trigger:
    - platform: state
      entity_id: alarm_control_panel.home_alarm
      to: "armed_away"
  action:
    - service: script.security_stop_loop_sounds
#     - service: media_player.volume_set
#       data:
#         volume_level: 0.6
#       target:
#         entity_id: media_player.nest_hub_max
#     - service: tts.google_translate_say
#       entity_id: media_player.nest_hub_max
#       data:
#         message: 'Security System is now armed'

- alias: SECURITY Armed_Home State
  trigger:
    - platform: state
      entity_id: alarm_control_panel.home_alarm
      to: "armed_home"
  action:
    - service: script.security_stop_loop_sounds

- alias: SECURITY Armed_Night State
  trigger:
    - platform: state
      entity_id: alarm_control_panel.home_alarm
      to: "armed_night"
  action:
    - service: script.security_stop_loop_sounds

- alias: SECURITY Triggered by the Sensors whilst Armed_Away
  trigger:
    # - platform: state
    #   entity_id: binary_sensor.door_window_sensor_158d0001b7xxxx #front door
    #   to: "on"
    # - platform: state
    #   entity_id: binary_sensor.door_window_sensor_158d0001b7xxxx #rear sliding door
    #   to: "on"
  condition:
    - condition: state
      entity_id: alarm_control_panel.home_alarm
      state: armed_away
  action:
    - service: alarm_control_panel.alarm_trigger
      target:
        entity_id: alarm_control_panel.home_alarm

- alias: SECURITY Triggered by the Sensors whilst Armed_Home
  trigger:
    # - platform: state
    #   entity_id: binary_sensor.door_window_sensor_158d0001b7xxxx #front door
    #   to: "on"
    # - platform: state
    #   entity_id: binary_sensor.door_window_sensor_158d0001b7xxxx #rear sliding door
    #   to: "on"
  condition:
    - condition: state
      entity_id: alarm_control_panel.home_alarm
      state: armed_home
  action:
    - service: alarm_control_panel.alarm_trigger
      target:
        entity_id: alarm_control_panel.home_alarm

- alias: SECURITY Triggered by the Sensors whilst Armed_Night
  trigger:
    # - platform: state
    #   entity_id: binary_sensor.door_window_sensor_158d0001b7xxxx #front door
    #   to: "on"
    # - platform: state
    #   entity_id: binary_sensor.door_window_sensor_158d0001b7xxxx #rear sliding door
    #   to: "on"
  condition:
    - condition: state
      entity_id: alarm_control_panel.home_alarm
      state: armed_night
  action:
    - service: alarm_control_panel.alarm_trigger
      target:
        entity_id: alarm_control_panel.home_alarm

- alias: SECURITY Pending State
  trigger:
    - platform: state
      entity_id: alarm_control_panel.home_alarm
      to: "pending"
  action:
    # - service: media_player.volume_set
    #   data:
    #     volume_level: 0.6
    #   target:
    #     entity_id: media_player.nest_hub_max
    # - service: tts.google_translate_say
    #   entity_id: media_player.nest_hub_max
    #   data:
    #     message: 'Security is armed, alarm countdown commencing'
    - service: script.security_countdown_loop_1

- alias: SECURITY Triggered State
  trigger:
    - platform: state
      entity_id: alarm_control_panel.home_alarm
      to: "triggered"
  action:
    - service: script.security_alarm_loop_1

谢谢楼主分享!zsbd

这个不错,谢谢分享

如果能补充详细的操作图文,更好。 :blush:

楼主可能不知道,差不多从0.104版本开始,HA官方自带的xiaomi_miio集成已经支持网关的警戒功能了,所以不再需要单独的手动警戒控制面板,更加不需要这些脚本和自动化了。

直接添加、自动维持状态同步。

感谢大神提示,我去看下文档研究一下