#
# ─── 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
  - name: Player 2
    id: !secret player_2_user_id

# 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
  player_2_home:
    name: Player 2 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 %}

      player_2_home:
        friendly_name: Player 2
        device_class: presence
        value_template: "{{ is_state('input_boolean.player_2_home', 'on') }}"
        icon_template: >
          {% if is_state('input_boolean.georgi_home', 'on') %} mdi:account
          {% else %} mdi:account-off-outline
          {% endif %}

      someone_home:
        friendly_name: Someone home
        device_class: presence
        value_template: >
          {{ is_state('input_boolean.georgi_home', 'on') or is_state('input_boolean.player_2_home', 'on') }}
        icon_template: >
          {% if is_state('input_boolean.georgi_home', 'on') or is_state('input_boolean.player_2_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') }}"

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:
  - alias: Presence · Georgi Arrival detection
    use_blueprint:
      path: arrival_detection.yaml
      input:
        person: person.georgi
        input_boolean: input_boolean.georgi_home

  - alias: Presence · Player 2 Arrival detection
    use_blueprint:
      path: arrival_detection.yaml
      input:
        person: person.player_2
        input_boolean: input_boolean.player_2

  - alias: Presence · Leaving home
    trigger:
      - platform: state
        entity_id: binary_sensor.someone_home
        to: "off"
    action:
      - service: script.routine_leaving_home

  - alias: Presence · Coming home
    trigger:
      platform: state
      entity_id: binary_sensor.someone_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

    group.hallway_motion:
      icon: mdi:motion-sensor
