#:########################################################################################:#
# Package: BYD Charging Schedule At Home
# File:    byd_charging_schedule_athome.yaml
# Version: v1.0.1
# Date:    2026-06-19
#
# Purpose:
#   Enables the BYD Atto 3 charging schedule when the car arrives home.
#   Disables the schedule only after the car has been confirmed away for a
#   configurable delay, to avoid rapid toggling from uncertain presence detection.
#
# Behaviour:
#   - Confirmed arrival home:
#       Turns ON switch.atto_3_schedule_enabled,
#       but only if the car had previously been confirmed away.
#
#   - Confirmed away:
#       After away_confirm_minutes, turns OFF switch.atto_3_schedule_enabled.
#
#   - Manual control:
#       If the car is already home and the user or another HA automation turns
#       the schedule OFF manually, this package will not turn it back ON unless
#       the car is later confirmed away and then arrives home again.
#
# Notes:
#   - Home Assistant usually stores the device_tracker home state as "home",
#     even if the UI displays "At Home".
#   - Change away_confirm_minutes below if you want a longer/shorter debounce.
#:########################################################################################:#

input_boolean:
  byd_atto3_charging_schedule_confirmed_away:
    name: "BYD Atto 3 Charging Schedule - Confirmed Away"
    icon: mdi:car-off

automation:
  #:######################################################################################:#
  # ARRIVAL - Turn schedule ON only after a confirmed away period
  #:######################################################################################:#
  - id: byd_atto3_charging_schedule_arrival_enable
    alias: "BYD Atto 3 Charging Schedule - Arrival Enable"
    description: >
      Turns the BYD charging schedule ON when the car arrives home, but only if
      it was previously confirmed away. This prevents brief away/home glitches
      from undoing a manual OFF while the car is actually still at home.
    mode: single
    variables: &byd_charging_schedule_vars
      tracker_entity: device_tracker.byd_atto3_ibeacon_bur4c2a_bermuda_tracker
      schedule_switch_entity: switch.atto_3_schedule_enabled

      # HA normally stores "At Home" as "home".
      home_state: "home"

      # Adjustable YAML-only delay before the car is treated as truly away.
      away_confirm_minutes: 15

      # These states will not be treated as confirmed away.
      ignored_tracker_states:
        - "unknown"
        - "unavailable"

    triggers:
      - trigger: state
        entity_id: device_tracker.byd_atto3_ibeacon_bur4c2a_bermuda_tracker

    conditions:
      - condition: template
        value_template: >-
          {{ trigger.to_state is not none
             and trigger.from_state is not none
             and trigger.to_state.state == home_state
             and trigger.from_state.state != home_state }}

    actions:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_boolean.byd_atto3_charging_schedule_confirmed_away
                state: "on"
            sequence:
              - action: switch.turn_on
                target:
                  entity_id: "{{ schedule_switch_entity }}"

      - action: input_boolean.turn_off
        target:
          entity_id: input_boolean.byd_atto3_charging_schedule_confirmed_away

  #:######################################################################################:#
  # DEPARTURE - Turn schedule OFF only after confirmed away timeout
  #:######################################################################################:#
  - id: byd_atto3_charging_schedule_departure_disable_after_timeout
    alias: "BYD Atto 3 Charging Schedule - Departure Disable After Timeout"
    description: >
      When the car leaves home, waits for the configured away timeout. If the car
      is still away after that delay, turns the BYD charging schedule OFF and
      marks the car as confirmed away.
    mode: restart
    variables: *byd_charging_schedule_vars

    triggers:
      - trigger: state
        entity_id: device_tracker.byd_atto3_ibeacon_bur4c2a_bermuda_tracker

    conditions:
      - condition: template
        value_template: >-
          {% set new_state = trigger.to_state.state if trigger.to_state is not none else '' %}
          {{ new_state != home_state
             and new_state not in ignored_tracker_states }}

    actions:
      - delay:
          minutes: "{{ away_confirm_minutes | int(15) }}"

      - condition: template
        value_template: >-
          {% set current_state = states(tracker_entity) %}
          {{ current_state != home_state
             and current_state not in ignored_tracker_states }}

      - action: switch.turn_off
        target:
          entity_id: "{{ schedule_switch_entity }}"

      - action: input_boolean.turn_on
        target:
          entity_id: input_boolean.byd_atto3_charging_schedule_confirmed_away

  #:######################################################################################:#
  # STARTUP SAFETY - If HA restarts while the car is away, re-arm arrival logic
  #:######################################################################################:#
  - id: byd_atto3_charging_schedule_startup_away_check
    alias: "BYD Atto 3 Charging Schedule - Startup Away Check"
    description: >
      On Home Assistant startup, if the car is already away and remains away
      beyond the configured timeout, turn the schedule OFF and mark the car as
      confirmed away so that the next confirmed arrival can enable the schedule.
    mode: single
    variables: *byd_charging_schedule_vars

    triggers:
      - trigger: homeassistant
        event: start

    actions:
      - delay: "00:01:00"

      - condition: template
        value_template: >-
          {% set current_state = states(tracker_entity) %}
          {{ current_state != home_state
             and current_state not in ignored_tracker_states }}

      - delay:
          minutes: "{{ away_confirm_minutes | int(15) }}"

      - condition: template
        value_template: >-
          {% set current_state = states(tracker_entity) %}
          {{ current_state != home_state
             and current_state not in ignored_tracker_states }}

      - action: switch.turn_off
        target:
          entity_id: "{{ schedule_switch_entity }}"

      - action: input_boolean.turn_on
        target:
          entity_id: input_boolean.byd_atto3_charging_schedule_confirmed_away
