# Presence Detection Package

# -----------------------------------------------------------------------------
# Person Setup (handled by UI)
person:

# -----------------------------------------------------------------------------
# Person/Device Tracker Customization
homeassistant:
  customize:
    person.jpk:
      entity_picture: https://gravatar.com/avatar/e78e623948f3675cf1c51544f9bec928

    sensor.jp_presence_status:
      entity_picture: https://gravatar.com/avatar/e78e623948f3675cf1c51544f9bec928

    device_tracker.jp_p3a_gps:
      friendly_name: J.P. Pixel 3a
      icon: mdi:map-marker
    device_tracker.jp_p3a_ip:
      friendly_name: J.P. Pixel 3a
      icon: mdi:wifi
    device_tracker.jp_p3a_bt_entry:
      friendly_name: J.P. Pixel 3a
      source_type: bluetooth
      icon: mdi:bluetooth

    device_tracker.jp_gs8_ap:
      friendly_name: J.P. Galaxy S8
      icon: mdi:wifi

    device_tracker.jen_iphone_ip:
      friendly_name: Jen iPhone
      icon: mdi:wifi
    device_tracker.jen_iphone_bt_entry:
      friendly_name: Jen iPhone
      source_type: bluetooth
      icon: mdi:bluetooth

# -----------------------------------------------------------------------------
# Manual Setup Device Trackers
device_tracker:

  # Ping Platform
  - platform: ping
    hosts:
      jp_p3a_ip: !secret jp_p3a_ip
      jp_gs8_ip: !secret jp_gs8_ip
      jen_iphone_ip: !secret jen_iphone_ip

  # MQTT Platform (Bluetooth Presence Sensor)
  - platform: mqtt
    devices:
      jp_gs8_bt_entry: monitor/entry/jp_gs8/device_tracker
      jp_p3a_bt_entry: monitor/entry/jp_p3a/device_tracker
      jen_iphone_bt_entry: monitor/entry/jen_iphone/device_tracker 

# -----------------------------------------------------------------------------
# Proximity Sensors for Zones
proximity:
  home:
    ignored_zones:
      - !secret jp_work_name
      - !secret jen_work_name
    devices:
      - device_tracker.jen_iphone
      - device_tracker.jp_pixel3a
    tolerance: 50
    unit_of_measurement: km

  jp:
    ignored_zones:
      - !secret jp_work_name
    devices:
      - device_tracker.jp_pixel3a
    tolerance: 50
    unit_of_measurement: km

  jen:
    ignored_zones:
      - !secret jen_work_name
    devices:
      - device_tracker.jen_iphone
    tolerance: 50
    unit_of_measurement: km

# -----------------------------------------------------------------------------
# Extended Presence States
input_select:
  jp_presence_state:
    name: J.P.
    options:
      - Home
      - Just Arrived
      - Just Left
      - Away
      - Extended Away

  jen_presence_state:
    name: Jen
    options:
      - Home
      - Just Arrived
      - Just Left
      - Away
      - Extended Away

  house_presence_state:
    name: House Occupancy
    options:
      - Home
      - Just Arrived
      - Just Left
      - Away
      - Extended Away

# -----------------------------------------------------------------------------
# Extended Presence Sensors
binary_sensor:

  # Home / Not-Home Sensors
  - platform: template
    sensors:
      jp_presence:
        friendly_name: J.P.
        device_class: presence
        value_template: "{{ is_state('person.jpk', 'home') }}"

      jen_presence:
        friendly_name: Jen
        device_class: presence
        value_template: "{{ is_state('person.jen', 'home') }}" 

      house_presence:
        friendly_name: House
        device_class: presence
        value_template: >-
          {{    is_state('person.jpk', 'home') 
             or is_state('person.jen', 'home') }}

sensor:

  # Extended Presence Status
  - platform: template
    sensors:
      jp_presence_status:
        value_template: '{{ states.input_select.jp_presence_state.state }}'
        friendly_name_template: 'J.P. Presence Status'

  - platform: template
    sensors:
      jen_presence_status:
        value_template: '{{ states.input_select.jen_presence_state.state }}'
        friendly_name_template: 'Jen Presence Status'

  - platform: template
    sensors:
      house_presence_status:
        value_template: '{{ states.input_select.house_presence_state.state }}'
        friendly_name_template: 'House Presence Status'

# -----------------------------------------------------------------------------
# Automations for Presence Detection
automation:

  # Run Bluetooth Arrival Scan at Home Assistant startup
  - alias: Startup Arrival Scan
    trigger:
      - platform: homeassistant
        event: start
    mode: queued
    action:
      - service: mqtt.publish
        data: 
          topic: monitor/scan/arrive
          payload: scan

  # Run Bluetooth Departure Scan if a Ping Device Tracker goes Away
  - alias: Bluetooth Departure Scan
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: binary_sensor.front_door_sensor
      - platform: time_pattern
        minutes: "/10"
    mode: single
    action:
      - service: mqtt.publish
        data: 
          topic: monitor/scan/depart
          payload: scan

  # Handle Just Arrived/Just Left State Transitions
  - alias: Mark Person as Just Arrived
    trigger:
      - platform: state
        entity_id: person.jpk
        to: 'home'
      - platform: state
        entity_id: person.jen
        to: 'home'
      - platform: state
        entity_id: binary_sensor.house_presence
        to: 'on'
    mode: queued
    action:
      - service: input_select.select_option
        data_template: 
          entity_id: >
            {% if trigger.entity_id == 'person.jpk' %}
              input_select.jp_presence_state
            {% elif trigger.entity_id == 'person.jen' %}
              input_select.jen_presence_state
            {% elif trigger.entity_id == 'binary_sensor.house_presence' %}
              input_select.house_presence_state
            {% else %}
            {% endif %}
          option: >
            {% if trigger.entity_id == 'person.jpk' %}
              {% if states.input_select.jp_presence_state == 'Just Left' %}
                Home
              {% else %}
                Just Arrived
              {% endif %}
            {% elif trigger.entity_id == 'person.jen' %}
              {% if states.input_select.jen_presence_state == 'Just Left' %}
                Home
              {% else %}
                Just Arrived
              {% endif %}
            {% elif trigger.entity_id == 'binary_sensor.house_presence' %}
              {% if states.input_select.house_presence_state == 'Just Left' %}
                Home
              {% else %}
                Just Arrived
              {% endif %}
            {% else %}
            {% endif %}

  - alias: Mark Person as Just Left
    trigger:
      - platform: state
        entity_id: person.jpk
        from: 'home'
      - platform: state
        entity_id: person.jen
        from: 'home'
      - platform: state
        entity_id: binary_sensor.house_presence
        from: 'on'
    mode: queued
    action:
      - service: input_select.select_option
        data_template: 
          entity_id: >
            {% if trigger.entity_id == 'person.jpk' %}
              input_select.jp_presence_state
            {% elif trigger.entity_id == 'person.jen' %}
              input_select.jen_presence_state
            {% elif trigger.entity_id == 'binary_sensor.house_presence' %}
              input_select.house_presence_state
            {% else %}
            {% endif %}
          option: 'Just Left'

  # Handle Timed State Changes to Home/Away/Extended Away
  - alias: Mark Person as Home
    trigger:
      - platform: state
        entity_id: input_select.jp_presence_state
        to: 'Just Arrived'
        for: 
          minutes: 5
      - platform: state
        entity_id: input_select.jen_presence_state
        to: 'Just Arrived'
        for: 
          minutes: 5
      - platform: state
        entity_id: input_select.house_presence_state
        to: 'Just Arrived'
        for: 
          minutes: 5
    mode: queued
    action:
      - service: input_select.select_option
        data_template: 
          entity_id: '{{ trigger.entity_id }}'
          option: 'Home'

  - alias: Mark Person as Away
    trigger:
      - platform: state
        entity_id: input_select.jp_presence_state
        to: 'Just Left'
        for: 
          minutes: 5
      - platform: state
        entity_id: input_select.jen_presence_state
        to: 'Just Left'
        for: 
          minutes: 5
      - platform: state
        entity_id: input_select.house_presence_state
        to: 'Just Left'
        for: 
          minutes: 5
    mode: queued
    action:
      - service: input_select.select_option
        data_template: 
          entity_id: '{{ trigger.entity_id }}'
          option: 'Away'

  - alias: Mark Person as Extended Away
    trigger:
      - platform: state
        entity_id: input_select.jp_presence_state
        to: 'Away'
        for: 
          hours: 24
      - platform: state
        entity_id: input_select.jen_presence_state
        to: 'Away'
        for: 
          hours: 24
      - platform: state
        entity_id: input_select.house_presence_state
        to: 'Away'
        for: 
          hours: 24
    mode: queued
    action:
      - service: input_select.select_option
        data_template: 
          entity_id: '{{ trigger.entity_id }}'
          option: 'Extended Away'
