###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   04/15/2017
#   @package        :   Zone
#   @description    :   All the zones are defined here
###############################################################################
homeassistant:
  customize:
    input_boolean.home_mode_away:
      friendly_name: Away Mode
    zone.home:
      friendly_name: Home
    group.zone_alerts:
      order: 40

input_boolean:
  home_mode_away:
    name: Away
    initial: "off"

proximity:
  home:
    devices: device_tracker.life360_suresh
    tolerance: 50
    unit_of_measurement: mi

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

automation:
  ###############################################################################
  # Zone based alerting
  ###############################################################################
  - alias: Alert When Someone Enters a Zone
    initial_state: true
    trigger:
      - platform: zone
        entity_id:
          - device_tracker.life360_hasika
          - device_tracker.life360_mallika
          - device_tracker.life360_srinika
          - device_tracker.life360_suresh
        zone: zone.home
        event: enter
    condition:
      - condition: template
        value_template: "{{ states('input_boolean.zone_alerts') == 'on' }}"
    action:
      - service: script.notify_me
        data_template:
          message: >
            {{ trigger.entity_id.split('.')[1].split('_')[1] | title}} just entered {{ trigger.zone.attributes.friendly_name }}!
      - service: script.voice_notify
        data_template:
          message: >
            {{ trigger.entity_id.split('.')[1].split('_')[1] | title}} just entered {{ trigger.zone.attributes.friendly_name }}!
          greeting: "no"

  - alias: Alert When Someone Leaves a Zone
    initial_state: true
    trigger:
      - platform: zone
        entity_id:
          - device_tracker.life360_hasika
          - device_tracker.life360_mallika
          - device_tracker.life360_srinika
          - device_tracker.life360_suresh
        zone: zone.home
        event: leave
    condition:
      - condition: template
        value_template: "{{ states('input_boolean.zone_alerts') == 'on' }}"
    action:
      - service: script.notify_me
        data_template:
          message: "{{ trigger.entity_id.split('.')[1].split('_')[1] | title}} just left {{ trigger.zone.attributes.friendly_name }}."
      - service: script.voice_notify
        data_template:
          message: >-
            {{ trigger.entity_id.split('.')[1].split('_')[1] | title }} just left {{ trigger.zone.attributes.friendly_name }}.
            {% if trigger.entity_id.split('.')[1].split('_')[1] == "suresh" and trigger.zone.attributes.friendly_name | lower == "office" %}
              and will be home in {{ (states('sensor.suresh2home') | int) + 5 }} minutes.
            {% endif %}
          greeting: "no"

  ###############################################################################
  # Welcome family members when they come home
  ###############################################################################
  - alias: Welcome Family
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - device_tracker.life360_mallika
          - device_tracker.life360_suresh
          - device_tracker.life360_srinika
          - device_tracker.life360_hasika
        from: "not_home"
        to: "home"
        for: "00:02:00"
    condition:
      - condition: state
        entity_id: input_boolean.zone_alerts
        state: "on"
    action:
      - service: script.notify_me
        data_template:
          message: >
            Welcome home, {{ trigger.entity_id.split('.')[1].split('_')[1] | title }}!
      - service: script.voice_notify
        data_template:
          message: >
            "Welcome home, {{ trigger.entity_id.split('.')[1].split('_')[1] | lower }}!"
          greeting: "no"
      - service: input_boolean.turn_off
        entity_id: input_boolean.home_mode_away
      - service: switch.turn_on
        entity_id: switch.chromecast_monitor

  ##############################################################################
  # Alert when every one is away
  ##############################################################################
  - alias: Poll Home Away Mode
    initial_state: true
    trigger:
      platform: time_pattern
      minutes: 00
      seconds: "/30"
    action:
      - service_template: >-
          {%- set suresh = states('device_tracker.life360_suresh') -%}
          {%- set mallika = states('device_tracker.life360_mallika') -%}
          {%- set srinika = states('device_tracker.life360_srinika') -%}
          {%- set hasika = states('device_tracker.life360_hasika') -%}
          {{ 'input_boolean.turn_on' if suresh != "home" and mallika != "home" and srinika != "home" and hasika != "home" else 'input_boolean.turn_off' }}
        entity_id: input_boolean.home_mode_away

  - alias: Home Mode Away ON
    initial_state: true
    trigger:
      platform: state
      entity_id: input_boolean.home_mode_away
      to: "on"
    action:
      - service: script.home_mode_away

  ###############################################################################
  # Proximity - Send an alert when someone is on their way home
  ###############################################################################
  - alias: Send a message as I get closer to home
    initial_state: true
    trigger:
      platform: numeric_state
      entity_id: proximity.home
      below: 5
      above: 1
    condition:
      - condition: template
        value_template: '{{ states.proximity.home.attributes.dir_of_travel == "towards" }}'
    action:
      - service: script.notify_me
        data_template:
          message: "Suresh is on his way home, he is within the 5 miles range. Should be home soon!"

####################################################################################
  - alias: Srinika Left Home
    initial_state: true
    trigger:
      - platform: state
        entity_id: device_tracker.life360_srinika
        from: "home"
        to: "not_home"
        for: "00:02:00"
    action:
      - service: switch.turn_off
        entity_id:
          - switch.srinika_bedroom
          - switch.srinika_bedroom_fan
          - switch.srinika_bedroom_fan_light
          - light.srinika_led_dimmer_switch
          - switch.srinika_bedroom_closet
          - switch.srinika_bedroom_accent_lights

####################################################################################
  - alias: Hasika Left Home
    initial_state: true
    trigger:
      - platform: state
        entity_id: device_tracker.life360_hasika
        from: "home"
        to: "not_home"
        for: "00:02:00"
    action:
      - service: switch.turn_off
        entity_id:
          - switch.hasika_bedroom
          - switch.hasika_bedroom_fan
          - switch.hasika_bedroom_fan_light
          - light.hasika_led_dimmer_switch
          - switch.hasika_bedroom_closet
          - switch.hasika_bed_accent
