###############################################################################
#   @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
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    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.door_window_sensor_158d0004231f7b
          - binary_sensor.door_window_sensor_158d0004248d5b
        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' }}"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "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: script.led_message
        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.backyard_light
          - switch.frontyard_light
        to: "on"
        for:
          minutes: 5
    condition:
      - condition: state
        entity_id: sun.sun
        state: "above_horizon"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    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 10 Min
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - switch.basement_left
          - switch.basement_right
          - switch.front_room
          - switch.srinika_bedroom
          - switch.hasika_bed_accent
          - switch.hasika_bedroom
          - switch.kitchen_switch
          - switch.study_room
          - 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 }}"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: homeassistant.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"

  - alias: Guest Bathroom Lights WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - switch.guest_bathroom_lights
        to: "on"
        for:
          minutes: "{{ states('input_number.guest_bathroom_lights')|int }}"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: homeassistant.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"

  - alias: Master Bathroom Lights WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - light.master_bathroom_lights
        to: "on"
        for:
          minutes: "{{ states('input_number.master_bathroom_lights')|int }}"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: homeassistant.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"

  - alias: Garage Lights WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - switch.garage
        to: "on"
        for:
          minutes: "{{ states('input_number.garage_lights')|int }}"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: homeassistant.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"

  - alias: Garage Shop Lights WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - switch.garage_shop_lights
        to: "on"
        for:
          minutes: "{{ states('input_number.garage_shop_lights')|int }}"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: homeassistant.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"

  - alias: Guest Bathroom Exhaust WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - switch.guest_bathroom_exhaust
        to: "on"
        for:
          minutes: "{{ states('input_number.guest_bathroom_exhaust_timer_duration')|int }}"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: homeassistant.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"

  - alias: Master Bathroom Shower Exhaust WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - switch.master_bathroom_shower_exhaust
        to: "on"
        for:
          minutes: "{{ states('input_number.master_bathroom_shower_exhaust_timer_duration')|int }}"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: homeassistant.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"

  - alias: Master Bathroom Toilet Exhaust WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - switch.master_bathroom_toilet_exhaust
        to: "on"
        for:
          minutes: "{{ states('input_number.master_bathroom_toilet_exhaust_timer_duration')|int }}"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: homeassistant.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"


#############################################################################
# CLOSET LIGHTS WATCH DOG
#############################################################################

  - alias: Srinika Bedroom Closet WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - switch.srinika_bedroom_closet
        to: "on"
        for:
          minutes: "{{ states('input_number.srinika_bedroom_closet_lights')|int }}"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: homeassistant.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"

  - alias: Hasika Bedroom Closet WatchDog
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - switch.hasika_bedroom_closet
        to: "on"
        for:
          minutes: "{{ states('input_number.hasika_bedroom_closet_lights')|int }}"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: homeassistant.turn_off
        data_template:
          entity_id: "{{ trigger.entity_id }}"

  - alias: Notify Plex User Status
    initial_state: true
    trigger:
      # - platform: numeric_state
      #   entity_id: sensor.plex_mahasri_nas
      #   above: 0
      #   for:
      #     minutes: 2
      - platform: state
        entity_id:
          - media_player.plex_adi_bh_plex_for_roku_roku3
          - media_player.plex_adi_bh_plex_for_roku_rokuultra
          - media_player.plex_prade5_plex_for_roku_roku_express
        to: 'playing'
        for:
          minutes: 2
    action:
      - service: script.notify_me
        data_template:
          message: >-
            {%- set tag_map = {'adi.bh':'Adi', 'prade5':'Pradeep'} -%}
            {% for item in states.media_player if item.state == 'playing' and state_attr(item.entity_id, 'media_title') != None 
                                                              and state_attr(item.entity_id, 'username') != 'kalavala'
                                                              and tag_map[state_attr(item.entity_id, 'username')] |trim != '' %}

              {{ tag_map[state_attr(item.entity_id, 'username')] }} is watching {% if state_attr(item.entity_id, 'media_content_type') == "movie" -%}
                {{ "a movie, " + state_attr(item.entity_id, 'media_title') |title }}
              {% endif %}
              {%- if state_attr(item.entity_id, 'media_content_type') == "tvshow" %}
                TV Show - {{ state_attr(item.entity_id, 'media_series_title') }} 
                Season: {{ state_attr(item.entity_id, 'media_season') }}, Episode: {{ state_attr(item.entity_id, 'media_episode') }} ({{ state_attr(item.entity_id, 'media_title') }})
              {% endif %}
            {% endfor %}

  # - alias: Lights And Switches WatchDog 10 Min
  #   initial_state: true
  #   trigger:
  #     - platform: state
  #       entity_id:
  #         - switch.guest_bathroom_exhaust
  #         - switch.master_bathroom_toilet_exhaust
  #       to: "on"
  #       for:
  #         minutes: 10
  #   condition:
  #     - condition: state
  #       entity_id: input_boolean.light_automations
  #       state: "on"
  #   action:
  #     - service: homeassistant.turn_off
  #       data_template:
  #         entity_id: "{{ trigger.entity_id }}"

#
# Main master Bathoom has more CFM to vent out... keep it for 20 minutes
#
  # - alias: Lights And Switches WatchDog 20 Min
  #   initial_state: true
  #   trigger:
  #     - platform: state
  #       entity_id:
  #         - switch.master_bathroom_shower_exhaust
  #       to: "on"
  #       for:
  #         minutes: 20
  #   condition:
  #     - condition: state
  #       entity_id: input_boolean.light_automations
  #       state: "on"
  #   action:
  #     - service: homeassistant.turn_off
  #       data_template:
  #         entity_id: "{{ trigger.entity_id }}"

  # - alias: Lights And Switches WatchDog 30 Min
  #   initial_state: true
  #   trigger:
  #     - platform: state
  #       entity_id:
  #         - switch.guest_bathroom_lights
  #         - switch.master_bathroom_toilet_light
  #       to: "on"
  #       for:
  #         minutes: 30
  #   condition:
  #     - condition: state
  #       entity_id: input_boolean.light_automations
  #       state: "on"
  #   action:
  #     - service: homeassistant.turn_off
  #       data_template:
  #         entity_id: "{{ trigger.entity_id }}"

  # - alias: Lights And Switches WatchDog 45 Min
  #   initial_state: true
  #   trigger:
  #     - platform: state
  #       entity_id:
  #         - switch.garage_shop_lights
  #       to: "on"
  #       for:
  #         minutes: 45
  #   condition:
  #     - condition: state
  #       entity_id: input_boolean.light_automations
  #       state: "on"
  #   action:
  #     - service: homeassistant.turn_off
  #       data_template:
  #         entity_id: "{{ trigger.entity_id }}"