#:########################################################################################:#
# Package: Security Notifications
# File:    security_notifications.yaml
# Version: v1.0.0
# Date:    2026-07-25
#
# Purpose:
#   Sends security alerts through the View Road announcement router when doors
#   and access points open or close. Includes a startup gate to suppress false
#   alerts after HA or Zigbee coordinator restarts, and a garage-door reminder
#   loop for prolonged-open warnings.
#
# Channels used per sensor:
#   Kitchen Top Cupboard       → pushover_zorruno
#   Key Cupboard               → pushover_zorruno
#   Loft Hatch                 → pushover_zorruno, sony_tv_lounge
#   Underhouse Stealth Door    → pushover_zorruno
#   Garage Roller Door         → pushover_zorruno, sony_tv_lounge
#
# Startup gate:
#   Every automation checks:
#     1. HA core uptime > 120 s   (blocks false alerts after HA restart)
#     2. Previous state != unavailable/unknown  (blocks false alerts after
#        Zigbee coordinator restart – devices briefly go unavailable then
#        re-report their last state)
#
# Garage reminder:
#   When opened:      immediate alert + 1-hour timer started
#   Timer fires:      door still open → reminder + restart (max 24 times)
#   Door closed:      timer cancelled, reminder count reset
#
# Dependencies:
#   - packages/view_road_announcement_router.yaml  (script.view_road_announcement)
#
# Version History:
#   - v1.0.0 (2026-07-25): Created from Node-RED Security Alerts tab.
#:########################################################################################:#

###########################################################################################
# HELPERS
###########################################################################################

input_number:
  garage_door_reminder_count:
    name: Garage Door Reminder Count
    icon: mdi:counter
    min: 0
    max: 24
    step: 1
    mode: box

timer:
  garage_door_open_reminder:
    name: Garage Door Open Reminder
    duration: "01:00:00"
    icon: mdi:timer-outline

###########################################################################################
# STARTUP GATE – common condition template
###########################################################################################
#
# Every automation below includes these two conditions:
#
#   1. HA has been running > 120 s
#        "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
#
#   2. Entity was not unavailable/unknown before this transition
#        "{{ trigger.from_state is not none and trigger.from_state.state not in ['unavailable', 'unknown'] }}"
#
# These are inlined so the package is self-contained and each automation
# remains easy to read, disable, or override individually.
#
###########################################################################################

###########################################################################################
# KITCHEN TOP CUPBOARD
###########################################################################################

automation:
  - id: security_kitchen_cupboard_opened
    alias: "Security – Kitchen Top Cupboard Opened"
    description: "Notify via Pushover when the kitchen top cupboard is opened."
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.kitchen_top_cupboard_x04rs_contact
        from: "off"
        to: "on"
    conditions:
      - condition: template
        value_template: "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
      - condition: template
        value_template: "{{ trigger.from_state is not none and trigger.from_state.state not in ['unavailable', 'unknown'] }}"
    actions:
      - action: script.view_road_announcement
        data:
          channels: "pushover_zorruno"
          title: "Security Alert"
          short_announcement: "Kitchen cupboard opened"
          long_announcement: "The kitchen top cupboard has been opened."

  - id: security_kitchen_cupboard_closed
    alias: "Security – Kitchen Top Cupboard Closed"
    description: "Notify via Pushover when the kitchen top cupboard is closed."
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.kitchen_top_cupboard_x04rs_contact
        from: "on"
        to: "off"
    conditions:
      - condition: template
        value_template: "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
      - condition: template
        value_template: "{{ trigger.from_state is not none and trigger.from_state.state not in ['unavailable', 'unknown'] }}"
    actions:
      - action: script.view_road_announcement
        data:
          channels: "pushover_zorruno"
          title: "Security Alert"
          short_announcement: "Kitchen cupboard closed"
          long_announcement: "The kitchen top cupboard is now closed."

  ###########################################################################################
  # KEY CUPBOARD
  ###########################################################################################

  - id: security_key_cupboard_opened
    alias: "Security – Key Cupboard Opened"
    description: "Notify via Pushover when the key cupboard is opened."
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.key_cupboard_zrs06
        from: "off"
        to: "on"
    conditions:
      - condition: template
        value_template: "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
      - condition: template
        value_template: "{{ trigger.from_state is not none and trigger.from_state.state not in ['unavailable', 'unknown'] }}"
    actions:
      - action: script.view_road_announcement
        data:
          channels: "pushover_zorruno"
          title: "Security Alert"
          short_announcement: "Key cupboard opened"
          long_announcement: "The key cupboard has been opened."

  - id: security_key_cupboard_closed
    alias: "Security – Key Cupboard Closed"
    description: "Notify via Pushover when the key cupboard is closed."
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.key_cupboard_zrs06
        from: "on"
        to: "off"
    conditions:
      - condition: template
        value_template: "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
      - condition: template
        value_template: "{{ trigger.from_state is not none and trigger.from_state.state not in ['unavailable', 'unknown'] }}"
    actions:
      - action: script.view_road_announcement
        data:
          channels: "pushover_zorruno"
          title: "Security Alert"
          short_announcement: "Key cupboard closed"
          long_announcement: "The key cupboard is now closed."

  ###########################################################################################
  # LOFT HATCH
  ###########################################################################################

  - id: security_loft_hatch_opened
    alias: "Security – Loft Hatch Opened"
    description: "Notify via Pushover and Sony TV when the loft hatch is opened."
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.upstairs_loft_hatch_x02rs_contact
        from: "off"
        to: "on"
    conditions:
      - condition: template
        value_template: "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
      - condition: template
        value_template: "{{ trigger.from_state is not none and trigger.from_state.state not in ['unavailable', 'unknown'] }}"
    actions:
      - action: script.view_road_announcement
        data:
          channels: "pushover_zorruno, sony_tv_lounge"
          title: "Security Alert"
          short_announcement: "Loft hatch opened"
          long_announcement: "The upstairs loft hatch has been opened."

  - id: security_loft_hatch_closed
    alias: "Security – Loft Hatch Closed"
    description: "Notify via Pushover and Sony TV when the loft hatch is closed."
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.upstairs_loft_hatch_x02rs_contact
        from: "on"
        to: "off"
    conditions:
      - condition: template
        value_template: "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
      - condition: template
        value_template: "{{ trigger.from_state is not none and trigger.from_state.state not in ['unavailable', 'unknown'] }}"
    actions:
      - action: script.view_road_announcement
        data:
          channels: "pushover_zorruno, sony_tv_lounge"
          title: "Security Alert"
          short_announcement: "Loft hatch closed"
          long_announcement: "The upstairs loft hatch is now closed."

  ###########################################################################################
  # UNDERHOUSE STEALTH DOOR
  ###########################################################################################

  - id: security_underhouse_stealth_door_opened
    alias: "Security – Underhouse Stealth Door Opened"
    description: "Notify via Pushover when the underhouse stealth door is opened."
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.underhouse_stealth_door_x07rs_contact
        from: "off"
        to: "on"
    conditions:
      - condition: template
        value_template: "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
      - condition: template
        value_template: "{{ trigger.from_state is not none and trigger.from_state.state not in ['unavailable', 'unknown'] }}"
    actions:
      - action: script.view_road_announcement
        data:
          channels: "pushover_zorruno"
          title: "Security Alert"
          short_announcement: "Stealth door opened"
          long_announcement: "The underhouse stealth door has been opened."

  - id: security_underhouse_stealth_door_closed
    alias: "Security – Underhouse Stealth Door Closed"
    description: "Notify via Pushover when the underhouse stealth door is closed."
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.underhouse_stealth_door_x07rs_contact
        from: "on"
        to: "off"
    conditions:
      - condition: template
        value_template: "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
      - condition: template
        value_template: "{{ trigger.from_state is not none and trigger.from_state.state not in ['unavailable', 'unknown'] }}"
    actions:
      - action: script.view_road_announcement
        data:
          channels: "pushover_zorruno"
          title: "Security Alert"
          short_announcement: "Stealth door closed"
          long_announcement: "The underhouse stealth door is now closed."

  ###########################################################################################
  # GARAGE ROLLER DOOR
  ###########################################################################################

  - id: security_garage_door_opened
    alias: "Security – Garage Roller Door Opened"
    description: >
      Notify via Pushover and Sony TV when the garage roller door opens.
      Also starts the 1-hour reminder timer so a follow-up alert is sent
      if the door stays open.
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.garage_roller_door_x18rs_contact
        from: "off"
        to: "on"
    conditions:
      - condition: template
        value_template: "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
      - condition: template
        value_template: "{{ trigger.from_state is not none and trigger.from_state.state not in ['unavailable', 'unknown'] }}"
    actions:
      - action: script.view_road_announcement
        data:
          channels: "pushover_zorruno, sony_tv_lounge"
          title: "Security Alert"
          short_announcement: "Garage door opened"
          long_announcement: "The garage roller door has been opened."
      - action: input_number.set_value
        data:
          entity_id: input_number.garage_door_reminder_count
          value: 0
      - action: timer.start
        data:
          entity_id: timer.garage_door_open_reminder
          duration: "01:00:00"

  - id: security_garage_door_closed
    alias: "Security – Garage Roller Door Closed"
    description: >
      Notify via Pushover and Sony TV when the garage roller door closes.
      Cancels the reminder timer and resets the reminder counter.
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.garage_roller_door_x18rs_contact
        from: "on"
        to: "off"
    conditions:
      - condition: template
        value_template: "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
      - condition: template
        value_template: "{{ trigger.from_state is not none and trigger.from_state.state not in ['unavailable', 'unknown'] }}"
    actions:
      - action: timer.cancel
        data:
          entity_id: timer.garage_door_open_reminder
      - action: input_number.set_value
        data:
          entity_id: input_number.garage_door_reminder_count
          value: 0
      - action: script.view_road_announcement
        data:
          channels: "pushover_zorruno, sony_tv_lounge"
          title: "Security Alert"
          short_announcement: "Garage door closed"
          long_announcement: "The garage roller door is now closed."

  - id: security_garage_door_reminder
    alias: "Security – Garage Roller Door Reminder"
    description: >
      Fires when the reminder timer finishes. If the door is still open and
      the reminder has fired fewer than 24 times, send another reminder and
      restart the timer.
    mode: single
    triggers:
      - trigger: event
        event_type: timer.finished
        event_data:
          entity_id: timer.garage_door_open_reminder
    conditions:
      - condition: template
        value_template: "{{ states('sensor.home_assistant_core_uptime') | int(0) > 120 }}"
      - condition: state
        entity_id: binary_sensor.garage_roller_door_x18rs_contact
        state: "on"
      - condition: template
        value_template: "{{ states('input_number.garage_door_reminder_count') | int(0) < 24 }}"
    actions:
      - action: input_number.increment
        data:
          entity_id: input_number.garage_door_reminder_count
      - action: script.view_road_announcement
        data:
          channels: "pushover_zorruno, sony_tv_lounge"
          title: "Security Alert"
          short_announcement: "Garage door still open"
          long_announcement: "A reminder, the garage roller door is still open."
      - action: timer.start
        data:
          entity_id: timer.garage_door_open_reminder
          duration: "01:00:00"
