#
# ─── INTEGRATIONS ───────────────────────────────────────────────────────────────
#

# https://www.home-assistant.io/components/person
person:
  - name: Georgi
    id: !secret georgi_user_id
    user_id: !secret georgi_user_id
    device_trackers:
      - device_tracker.carbon

# https://www.home-assistant.io/integrations/device_tracker
device_tracker:
  - platform: tile
    username: !secret tile_username
    password: !secret tile_password

# https://www.home-assistant.io/components/zone
zone:
  - name: Home
    latitude: !secret home_latitude
    longitude: !secret home_longitude
    radius: 20
    icon: mdi:home
  - name: Work
    latitude: !secret work_latitude
    longitude: !secret work_longitude
    radius: 60
    icon: mdi:worker

#
# ─── ENTITIES ───────────────────────────────────────────────────────────────────
#

input_boolean:
  georgi_home:
    name: Georgi home
    icon: mdi:account

binary_sensor:
  - platform: template
    sensors:
      georgi_home:
        friendly_name: Georgi
        device_class: presence
        value_template: "{{ is_state('input_boolean.georgi_home', 'on') }}"
        icon_template: >
          {% if is_state('input_boolean.georgi_home', 'on') %} mdi:account
          {% else %} mdi:account-off-outline
          {% endif %}

      trisensor_motion:
        friendly_name: TriSensor motion
        device_class: motion
        value_template: "{{ is_state('sensor.trisensor_burglar', '8') }}"

      multisensor_motion:
        friendly_name: MultiSensor motion
        device_class: motion
        value_template: "{{ is_state('sensor.multisensor_6_burglar', '8') }}"


group:
  hallway_motion:
    name: Hallway Motion
    entities:
      - binary_sensor.hallway_motion_occupancy
      - binary_sensor.trisensor_motion

sensor:
  - platform: history_stats
    name: Quarantined percent
    entity_id: device_tracker.carbon
    state: "home"
    type: ratio
    duration:
      days: 7
    end: "{{ now() }}"

  - platform: history_stats
    name: Quarantine times left
    entity_id: device_tracker.carbon
    state: "home"
    type: count
    duration:
      days: 7
    end: "{{ now() }}"


automation:
  # When my state changes to "home", wait for 15 minutes for the door to be opened, and then mark me as home
  - alias: Presence · Arrival detection
    trigger:
      - platform: state
        entity_id: person.georgi
        to: "home"
    condition:
      condition: state
      entity_id: input_boolean.georgi_home
      state: "off"
    action:
      - service: notify.telegram
        data:
          title: "🏠 *Presence Module · Arrival Detection*"
          message: "Device marked as home. Waiting for door to open!"
      - wait_template: "{{ is_state('binary_sensor.front_door', 'on') }}"
        continue_on_timeout: false
        timeout: 00:15:00
      - service: input_boolean.turn_on
        entity_id: input_boolean.georgi_home
      - service: notify.telegram
        data:
          title: "🏠 *Presence Module · Arrival Detection*"
          message: "✅ Hey, I've marked you as home now!"

  - alias: Presence · Leaving home
    trigger:
      - platform: state
        entity_id: person.georgi
        from: "home"
        to: "not_home"
      - platform: state
        entity_id: input_boolean.georgi_home
        to: "off"
    action:
      - service: script.routine_leaving_home

  - alias: Presence · Coming home
    trigger:
      platform: state
      entity_id: binary_sensor.georgi_home
      to: "on"
    action:
      - service: script.routine_coming_home

  - alias: Presence · Leaving work
    trigger:
      platform: state
      entity_id: person.georgi
      from: "Work"
      to: "not_home"
    action:
      - service: notify.telegram
        data:
          title: "🏠 *Presence Module · Leaving Work*"
          message: "Hey, I see you're leaving work. Are you coming home?"
          data:
            inline_keyboard:
              - "Yes:/leaving_work_to_home, No:/do_nothing"

#
# ─── CUSTOMIZATION ──────────────────────────────────────────────────────────────
#

homeassistant:
  customize:
    device_tracker.carbon:
      icon: mdi:cellphone-iphone

    device_tracker.tile_p_6e3c33752f24f417254c039040a984bd:
      friendly_name: Georgi's iPhone Tile
      icon: mdi:cellphone-iphone

    device_tracker.tile_1bcb2b3021b9f96b:
      friendly_name: Keys Tile
      icon: mdi:key-variant

    automation.presence_coming_home:
      icon: mdi:home-account

    automation.presence_leaving_home:
      icon: mdi:home

    automation.presence_arrival_detecion:
      icon: mdi:account-plus-outline

    group.hallway_motion:
      icon: mdi:motion-sensor
