# ADHDTasker — example Home Assistant automations
# Replace the placeholder entity_ids (media_player.living_room, light.living_room,
# tts.home_assistant_cloud) with your own. Needs adhdtasker-ha-config.yaml plus, in
# secrets.yaml:  adhdtasker_api_key  and  adhdtasker_webhook_id.

# 1) INBOUND — ping the family on school mornings.
- id: '1771500000001'
  alias: ADHDTasker School Morning Ping
  triggers:
    - trigger: time
      at: "07:00:00"
  conditions:
    - condition: time
      weekday: [mon, tue, wed, thu, fri]
  actions:
    - action: rest_command.adhdtasker
      data:
        payload: '{"action":"ping","title":"School morning","message":"Time to get ready for school!"}'
  mode: single

# 2) INBOUND — bedtime board briefing (uses sensor.adhdtasker).
- id: '1771500000002'
  alias: ADHDTasker Bedtime Briefing
  triggers:
    - trigger: time
      at: "19:30:00"
  conditions:
    - condition: numeric_state
      entity_id: sensor.adhdtasker
      above: 0
  actions:
    - action: tts.speak
      target:
        entity_id: tts.home_assistant_cloud      # your TTS engine
      data:
        media_player_entity_id: media_player.living_room   # your speaker
        message: >
          {{ states('sensor.adhdtasker') }} chores still need doing.
          {% set lb = state_attr('sensor.adhdtasker','leaderboard') %}
          {% if lb and lb[0].lifetime | int > 0 %} {{ lb[0].name }} is leading with {{ lb[0].lifetime }} points.{% endif %}
  mode: single

# 3) OUTBOUND — announce + colour-pulse a light on due / completed / reward.
#    Snapshots the light, flashes a colour for 3s, then restores it.
- id: '1771500000003'
  alias: ADHDTasker Announcements
  triggers:
    - trigger: webhook
      webhook_id: !secret adhdtasker_webhook_id
      allowed_methods: [POST]
      local_only: false
  conditions:
    # Drop this block if you did not set a shared secret; otherwise paste yours.
    - condition: template
      value_template: "{{ trigger.json.secret == 'YOUR_SHARED_SECRET' }}"
  variables:
    event: "{{ trigger.json.event }}"
    title: "{{ trigger.json.get('task', {}).get('title', 'a task') }}"
    who: "{{ trigger.json.get('profile', {}).get('name', 'Someone') }}"
    points: "{{ trigger.json.get('task', {}).get('points', 0) | int }}"
    colour: "{{ {'task.due': [255,152,0], 'task.completed': [67,160,71], 'reward.requested': [156,39,176]}.get(event) }}"
  actions:
    - choose:
        - conditions: "{{ event == 'task.due' }}"
          sequence:
            - action: tts.speak
              target: { entity_id: tts.home_assistant_cloud }
              data:
                media_player_entity_id: media_player.living_room
                message: "Heads up — {{ title }} is due."
        - conditions: "{{ event == 'task.completed' }}"
          sequence:
            - action: tts.speak
              target: { entity_id: tts.home_assistant_cloud }
              data:
                media_player_entity_id: media_player.living_room
                message: "Nice work {{ who }}! You finished {{ title }}{{ ' for ' ~ points ~ ' points' if points > 0 else '' }}."
    # Must render a real boolean — "{{ colour }}" renders the list itself, which HA
    # treats as False (the lights would never flash).
    - if: "{{ colour is not none and colour | count == 3 }}"
      then:
        - action: scene.create
          data: { scene_id: adhdtasker_restore, snapshot_entities: [light.living_room] }
        - action: light.turn_on
          target: { entity_id: light.living_room }
          data: { rgb_color: "{{ colour }}", brightness_pct: 100 }
        - delay: "00:00:03"
        - action: scene.turn_on
          target: { entity_id: scene.adhdtasker_restore }
  mode: queued
  max: 25
