###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   09/20/2018
#   @package        :   xiaomi_magic_cubes
#   @description    :   Xiaomi Aqara Magic Cube Automations
###############################################################################

###############################################################################
#                      XIAOMI AQARA MAGIC CUBE AUTOMATIONS                     #
#       http://patorjk.com/software/taag/#p=display&h=2&v=2&f=Big&t=Test       #
################################################################################

automation:
  ################################################################################
  #  ______              _ _         _____
  # |  ____|            (_) |       |  __ \
  # | |__ __ _ _ __ ___  _| |_   _  | |__) |___   ___  _ __ ___
  # |  __/ _` | '_ ` _ \| | | | | | |  _  // _ \ / _ \| '_ ` _ \
  # | | | (_| | | | | | | | | |_| | | | \ \ (_) | (_) | | | | | |
  # |_|  \__,_|_| |_| |_|_|_|\__, | |_|  \_\___/ \___/|_| |_| |_|
  #                           __/ |
  #                          |___/
  ################################################################################

  # Flip 90: Toggle Family Room Lights
  - alias: Family Room Cube Event flip90
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d0002794cd6
        action_type: flip90
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service_template: light.toggle
        entity_id: light.hue_color_lamp_1, light.hue_color_lamp_2, light.hue_color_lamp_3

  # Flip 180: Lights ON Family Room Lights FULL brightness
  - alias: Family Room Cube Event flip180
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d0002794cd6
        action_type: flip180
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_on
        entity_id: light.hue_color_lamp_1, light.hue_color_lamp_2, light.hue_color_lamp_3
        data:
          rgb_color: [255, 251, 245]
          brightness: 254
          color_temp: 162

  # Move: Toggle Kitchen Light
  - alias: Family Room Cube Event Move
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d0002794cd6
        action_type: move
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.toggle
        entity_id: switch.kitchen_switch

  # Double Tap:  Turn OFF Downstairs Lights
  - alias: Family Room Cube Event Tap Twice
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d0002794cd6
        action_type: tap_twice
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_1, light.hue_color_lamp_2, light.hue_color_lamp_3
          rgb_color:
            - "{{ range(0,255) |random }}"
            - "{{ range(0,255) |random }}"
            - "{{ range(0,255) |random }}"

  # Shake: Toggle Do Not Disturb Mode
  - alias: Family Room Cube Event Shake
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d0002794cd6
        action_type: shake_air
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: script.toggle_do_not_disturb

  ################################################################################
  #   ______               _     _____
  # |  ____|             | |   |  __ \
  # | |__ _ __ ___  _ __ | |_  | |__) |___   ___  _ __ ___
  # |  __| '__/ _ \| '_ \| __| |  _  // _ \ / _ \| '_ ` _ \
  # | |  | | | (_) | | | | |_  | | \ \ (_) | (_) | | | | | |
  # |_|  |_|  \___/|_| |_|\__| |_|  \_\___/ \___/|_| |_| |_|

  # Flip 90: Toggle Front Room Lights
  - alias: Front Room Cube Event flip90
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d00027c0276
        action_type: flip90
    action:
      - service_template: switch.toggle
        entity_id: switch.front_room

  # Move: Toggle Office Room Light
  - alias: Front Room Cube Event Move
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d00027c0276
        action_type: move
    action:
      - service: switch.toggle
        entity_id: switch.office_room
      - service: switch.toggle
        entity_id: switch.left

  # Shake: Toggle Do Not Disturb Mode
  - alias: Front Room Cube Event Shake
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d00027c0276
        action_type: shake_air
    action:
      - service: script.toggle_do_not_disturb

  ###############################################################################
  #   _____      _       _ _         _       _____
  #  / ____|    (_)     (_) |       ( )     |  __ \
  # | (___  _ __ _ _ __  _| | ____ _|/ ___  | |__) |___   ___  _ __ ___
  #  \___ \| '__| | '_ \| | |/ / _` | / __| |  _  // _ \ / _ \| '_ ` _ \
  #  ____) | |  | | | | | |   < (_| | \__ \ | | \ \ (_) | (_) | | | | | |
  # |_____/|_|  |_|_| |_|_|_|\_\__,_| |___/ |_|  \_\___/ \___/|_| |_| |_|

  # Flip 90: Toggle Srinika's Room Lights
  - alias: Srinika Room Cube Event flip90
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d00027cf60d
        action_type: flip90
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service_template: switch.toggle
        entity_id: switch.srinika_bedroom

  # Shake: Toggle Do Not Disturb Mode
  - alias: Srinika Room Cube Event Shake
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d00027cf60d
        action_type: shake_air
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: script.toggle_do_not_disturb

  ##############################################################################
  #  _    _           _ _         _       _____
  # | |  | |         (_) |       ( )     |  __ \
  # | |__| | __ _ ___ _| | ____ _|/ ___  | |__) |___   ___  _ __ ___
  # |  __  |/ _` / __| | |/ / _` | / __| |  _  // _ \ / _ \| '_ ` _ \
  # | |  | | (_| \__ \ |   < (_| | \__ \ | | \ \ (_) | (_) | | | | | |
  # |_|  |_|\__,_|___/_|_|\_\__,_| |___/ |_|  \_\___/ \___/|_| |_| |_|

  # Flip 90: Toggle Hasika's Room Lights
  - alias: Hasika Room Cube Event flip90
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d00027ce15c
        action_type: flip90
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service_template: switch.toggle
        entity_id: switch.hasika_bedroom

  # Shake: Toggle Do Not Disturb Mode
  - alias: Hasika Room Cube Event Shake
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d00027ce15c
        action_type: shake_air
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: script.toggle_do_not_disturb

  ################################################################################
  #    _____ _             _         _____
  #  / ____| |           | |       |  __ \
  # | (___ | |_ _   _  __| |_   _  | |__) |___   ___  _ __ ___
  #  \___ \| __| | | |/ _` | | | | |  _  // _ \ / _ \| '_ ` _ \
  #  ____) | |_| |_| | (_| | |_| | | | \ \ (_) | (_) | | | | | |
  # |_____/ \__|\__,_|\__,_|\__, | |_|  \_\___/ \___/|_| |_| |_|
  #                          __/ |
  #                         |___/

  # Flip 90: Toggle Study Room Lights
  - alias: Study Room Cube Event Flip90
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d000276e3e8
        action_type: flip90
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.toggle
        entity_id: switch.study_room

  # Shake: Toggle Do Not Disturb Mode
  - alias: Study Room Cube Event Shake
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d000276e3e8
        action_type: shake_air
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: script.toggle_do_not_disturb

  ################################################################################
  #  __  __           _              ____           _
  # |  \/  |         | |            |  _ \         | |
  # | \  / | __ _ ___| |_ ___ _ __  | |_) | ___  __| |_ __ ___   ___  _ __ ___
  # | |\/| |/ _` / __| __/ _ \ '__| |  _ < / _ \/ _` | '__/ _ \ / _ \| '_ ` _ \
  # | |  | | (_| \__ \ ||  __/ |    | |_) |  __/ (_| | | | (_) | (_) | | | | | |
  # |_|  |_|\__,_|___/\__\___|_|    |____/ \___|\__,_|_|  \___/ \___/|_| |_| |_|

  # Flip 90: All Lights OFF (upstairs and downstairs)
  - alias: Master Bedroom Cube Event Flip90
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d000278eb6a
        action_type: flip90
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: homeassistant.turn_off
        data:
          entity_id: group.downstairs_lights
      - service: homeassistant.turn_off
        data:
          entity_id: group.upstairs_lights

  # Flip 180: Toggle Lights
  - alias: Master Bedroom Cube Event Flip180
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d000278eb6a
        action_type: flip180
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.toggle
        entity_id: light.master_bedroom_1
      - service: light.toggle
        entity_id: light.master_bedroom_2

  # Move: Turn ON Master Bedroom Lights
  - alias: Master Bedroom Cube Event Move
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d000278eb6a
        action_type: move
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_on
        entity_id: light.master_bedroom_1
      - service: light.turn_on
        entity_id: light.master_bedroom_2

  # Double Tap:  Turn OFF Downstairs Lights
  - alias: Master Bedroom Cube Event Tap Twice
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d000278eb6a
        action_type: tap_twice
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_on
        data:
          entity_id: light.master_bedroom_1
          rgb_color:
            - "{{ range(0,255) |random }}"
            - "{{ range(0,255) |random }}"
            - "{{ range(0,255) |random }}"
      - service: light.turn_on
        data:
          entity_id: light.master_bedroom_2
          rgb_color:
            - "{{ range(0,255) |random }}"
            - "{{ range(0,255) |random }}"
            - "{{ range(0,255) |random }}"

  # Shake: Toggle Do Not Disturb Mode
  - alias: Master Bedroom Cube Event Shake
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.cube_action
      event_data:
        entity_id: binary_sensor.cube_158d000278eb6a
        action_type: shake_air
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: script.toggle_do_not_disturb

  ################################################################################
  #   _____                   _          _ _
  #  |  __ \                 | |        | | |
  #  | |  | | ___   ___  _ __| |__   ___| | |
  #  | |  | |/ _ \ / _ \| '__| '_ \ / _ \ | |
  #  | |__| | (_) | (_) | |  | |_) |  __/ | |
  #  |_____/ \___/ \___/|_|  |_.__/ \___|_|_|
  #
  ################################################################################

  ################################################################################
  # Doorbell Automations -
  #     When we are home, play the door bell
  #     When we are not home, play dog sounds
  ################################################################################
  - alias: Doorbell Single Press
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.click
      event_data:
        entity_id: binary_sensor.wall_switch_158d00045622d9
        click_type: "single"
    action:
      - service_template: >
          {% if states('input_boolean.home_mode_away') == 'off' %}
            script.play_ringtone
          {% else %}
            script.play_dog_sounds
          {% endif %}
      - service: script.doorbell_camera_pics

  - alias: Doorbell Double Press
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.click
      event_data:
        entity_id: binary_sensor.wall_switch_158d00045622d9
        click_type: "double"
    action:
      - service_template: >
          {% if states('input_boolean.home_mode_away') == 'off' %}
            script.play_ringtone
          {% else %}
            script.play_dog_sounds
          {% endif %}
      - service: script.doorbell_camera_pics

  - alias: Doorbell Long Press
    initial_state: true
    trigger:
      platform: event
      event_type: xiaomi_aqara.click
      event_data:
        entity_id: binary_sensor.wall_switch_158d00045622d9
        click_type: "long"
    action:
      - service_template: >
          {% if states('input_boolean.home_mode_away') == 'off' %}
            script.play_fav_song
          {% else %}
            script.play_dog_sounds
          {% endif %}
      - service: script.doorbell_camera_pics

################################################################################
# Script for sending camera snapshots when doorbell is pressed
################################################################################
script:

  doorbell_camera_pics:
    sequence:
      - service: script.notify_family
        data:
          message: "ALERT! Someone is at the front door!"
          notify_options:
            - telegram
            - voice
            - tv
            - led
      - service: camera.snapshot
        data:
          entity_id: "camera.frontdoor_camera"
          filename:
            "{{ '/config/www/downloads/camera/frontdoor/frontdoor_' ~
            (state_attr('script.doorbell_camera_pics', 'last_triggered') ~ '').replace('-','_')
            .replace(' ', '_').replace(':','_').replace('.','_').replace('+','_') ~ '.jpg' }}"
      - service: camera.snapshot
        data:
          entity_id: "camera.driveway_camera"
          filename:
            "{{ '/config/www/downloads/camera/driveway/driveway_' ~
            (state_attr('script.doorbell_camera_pics', 'last_triggered') ~ '').replace('-','_')
            .replace(' ', '_').replace(':','_').replace('.','_').replace('+','_') ~ '.jpg' }}"
      - service: notify.telegram
        data:
          title: "Door Bell"
          message: "Someone rang door bell!"
          data:
            photo:
              - file: "{{ '/config/www/downloads/camera/frontdoor/frontdoor_' ~
                       (state_attr('script.doorbell_camera_pics', 'last_triggered') ~ '').replace('-','_')
                       .replace(' ', '_').replace(':','_').replace('.','_').replace('+','_') ~ '.jpg' }}"
                caption: "Someone rang door bell!"
              - file: "{{ '/config/www/downloads/camera/driveway/driveway_' ~
                (state_attr('script.doorbell_camera_pics', 'last_triggered') ~ '').replace('-','_')
                .replace(' ', '_').replace(':','_').replace('.','_').replace('+','_') ~ '.jpg' }}"
                caption: "Someone rang door bell!"
      - service: script.notify_family
        data:
          message: Someone rang door bell!
          url: "{{ '/config/www/downloads/camera/frontdoor/frontdoor_' ~
                       (state_attr('script.doorbell_camera_pics', 'last_triggered') ~ '').replace('-','_')
                       .replace(' ', '_').replace(':','_').replace('.','_').replace('+','_') ~ '.jpg' }}"
          notify_options:
            - tv
            - led