###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   04/15/2017
#   @package        :   WatchDog
#   @description    :   Watchdog Automations
###############################################################################

###############################################################################
#                _                        _   _
#     /\        | |                      | | (_)
#    /  \  _   _| |_ ___  _ __ ___   __ _| |_ _  ___  _ __  ___
#   / /\ \| | | | __/ _ \| '_ ` _ \ / _` | __| |/ _ \| '_ \/ __|
#  / ____ \ |_| | || (_) | | | | | | (_| | |_| | (_) | | | \__ \
# /_/    \_\__,_|\__\___/|_| |_| |_|\__,_|\__|_|\___/|_| |_|___/
#
###############################################################################
automation:
  # Garage:
  # Working in Garage Mode Watch Dog - Turns off after 2 hours
  ###############################################################################
  - alias: Working In Garage WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - input_boolean.working_in_garage
        to: "on"
        for:
          hours: 2
    action:
      - service: script.notify_me
        data:
          message: "It has been two hours, are you still working in the garage? Changing the status to 'Not working'!"
      - service: input_boolean.turn_off
        entity_id: input_boolean.working_in_garage

  # Garage:
  # Keeps and eye on the garage doors... reminds me to close after 5 min
  ###############################################################################
  - alias: Two Car garage Door WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.two_car_garage_door_tilt_sensor_sensor
          - binary_sensor.single_car_garage_door_tilt_sensor_sensor
        to: "on"
        for:
          minutes: 5
    condition:
      - condition: state
        entity_id: input_boolean.working_in_garage
        state: "off"
      - condition: template
        value_template: "{{ states('input_boolean.garage_door_notifications') == 'on' }}"
    action:
      - service: script.notify_me
        data_template:
          message: >
            Your {{ trigger.to_state.attributes.friendly_name }} is OPEN for more than 5 minutes!
      - service: script.voice_notify
        data_template:
          message: >
            Attention! Your {{ trigger.to_state.attributes.friendly_name }} is open for more than 5 minutes.
      - service: scipt.led_notify
        data_template:
          message: >
            Your {{ trigger.to_state.attributes.friendly_name }} is OPEN for more than 5 minutes!

  # Outdoor Lights:
  # Keeps and eye on the patio and backyard lights...
  ###############################################################################
  - alias: Outdoor Light WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - switch.wemobackyardlightswitch
          - switch.frontyard_light
        to: "on"
        for:
          minutes: 5
    condition:
      - condition: state
        entity_id: sun.sun
        state: "above_horizon"
    action:
      - service: switch.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"
      - service: script.notify_me
        data_template:
          message: >
            {{ trigger.to_state.attributes.friendly_name }} is ON during the day time. Saving power by turning it off!

  # General Watch Dog automation:
  # Keeps and eye on the lights & switches, turns off after 10 PM if they are on.
  ###############################################################################
  - alias: Lights And Switches WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - switch.basement_left
          - switch.basement_right
          - switch.downstairs_fragrance
          - switch.front_room
          - switch.guest_bedroom
          - switch.kids_bed_accent
          - switch.kids_bedroom
          - switch.kitchen
          - switch.prayer_room
          - switch.upstairs_fragrance
          - switch.zwave_smart_switch_switch
          - light.hue_color_lamp_1
          - light.hue_color_lamp_2
          - light.hue_color_lamp_3
          - light.master_bedroom_1
          - light.master_bedroom_2
          - light.master_bedroom_3
        to: "on"
        for:
          minutes: 10
    condition:
      - condition: template
        value_template: "{{ now().hour |int > 22 }}"
    action:
      - service: homeassistant.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"
