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

################################################################################
#                     COMMON SCRIPTS USED IN THIS PACKAGE                     #
################################################################################
script:
  toggle_do_not_disturb:
    sequence:
      - service: input_boolean.toggle
        entity_id: input_boolean.do_not_disturb

################################################################################
#                      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
    action:
      - service_template: light.toggle
        entity_id: light.family_room

  # 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
    action:
      - service: light.turn_on
        entity_id: light.family_room
        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
    action:
      - service: switch.toggle
        entity_id: switch.kitchen

  # 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
    action:
      - service: light.turn_on
        data_template:
          entity_id: light.family_room
          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
    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.rf_switch_four

  # Flip 180: Toggle Office Room Accent Lights
  - 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_158d00027c0276
        action_type: flip180
    action:
      - service: switch.toggle
        entity_id: switch.zwave_smart_switch_switch

  # Move: Toggle Office Room 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_158d00027c0276
        action_type: move
    action:
      - service: switch.toggle
        entity_id: switch.office_room

  # 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_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
    action:
      - service_template: switch.toggle
        entity_id: switch.guest_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
    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
    action:
      - service_template: switch.toggle
        entity_id: switch.kids_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
    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
    action:
      - service: switch.toggle
        entity_id: switch.prayer_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
    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
    action:
      - service: homeassistant.turn_off
        entity_id: group.downstairs_lights
      - service: homeassistant.turn_off
        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
    action:
      - service: light.toggle
        entity_id: light.master_bedroom

  # 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
    action:
      - service: light.turn_on
        entity_id: light.master_bedroom

  # 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
    action:
      - service: light.turn_on
        data_template:
          entity_id: light.master_bedroom
          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
    action:
      - service: script.toggle_do_not_disturb
