###############################################################################
#   @author         :   Jeffrey Stone 
#   @date           :   03/13/2019
#   @package        :   Security
#   @description    :   Everything related to security functions. 
###############################################################################

input_boolean:
  # Main security switch. If this is on, the security is armed. 
  sentry_mode:
    name: Sentry Mode
    icon: mdi:security
  # Vacation mode is automagically set when we are 3 hours from the house, and turns off when we return. 
  # This is used to modify some of the normal automations. 
  vacation_mode:
    name: Vacation Mode
    icon: mdi:airplane-takeoff
  # Guest mode is use to modify autoamtions when we have guests. 
  # In some cases this prevents security from being set to prevent security alarm going off
  guest_mode:
    name: Guest Mode
    icon: mdi:account-multiple
  # This switch powers the security alarm and gives an easy way to kill the alarm.
  # When it is on, the alarm is going off
  security_alarm:
    name: Security Alarm
    icon: mdi:speaker-wireless
  # This switch powers the fire alarm.
  # When on the fire alarm is going off.
  fire_alarm:
    name: Fire Alarm
    icon: mdi:speaker-wireless
  # This switch turns on if the garage is open after dark and triggers the garage_after_dark alert.
  garage_after_dark:
    name: Garage Open After Dark
    icon: mdi:message-alert
  # This switch turns on of the security system checks that fire when someone tries to arm the system 
  # finds a door open, or another condition that would prevent the system from being able to monitor properly.
  lockdown_issue:
    name: Lockdown Issue
    icon: mdi:lock-reset
  # This switch is turned on if there is a security breach. Turning on this switch starts the countdown to the alarm.
  security_issue:
    name: Security Issue
    icon: mdi:alert-circle-outline
  # Dog Mode is triggered when Home Assistant believes someone is about to open the back door to let 
  # to let the dog out in the midddle of the night. 
  # https://slacker-labs.com/2020/04/14/where-we-are-going-we-need-dog-mode/
  dog_mode:
    name: Dog Mode
    icon: mdi:dog-side
  


# Arlo Integration
alarm_control_panel:
  # Uses the custom arlo integration found in HACS
  - platform: aarlo
    home_mode_name: home
    away_mode_name: Armed


sensor:
  - platform: mqtt
    name: "Front Door Motion Away count"
    state_topic: "house/front_door_motion/away_count"
 

automation:
  #############################################
  # When sentry mode is enabled / security armed by any method, check the doors and notify everyone.
  - id: sentry_mode_enabled
    alias: Sentry Mode Enabled Annoucement
    initial_state: true
    trigger:
    - platform: state
      entity_id: input_boolean.sentry_mode
      from: 'off'
      to: 'on'
    action:
    - choose:
      - conditions:
        - condition: state
          entity_id: group.external_doors
          state: "on"
        sequence:
        - service: input_boolean.turn_off
          entity_id: input_boolean.sentry_mode
        - service: input_boolean.turn_on
          entity_id: input_boolean.locakdown_issue
        - service: script.text_notify
          data:
            who: "parents"
            message: "Unable to lock down Anchorage House due to issue"
        - service: script.speech_engine
          data_template:
            who: '{{ states.sensor.room_audio.state }}'
            message: >
                {{ [
                "I was unable to activate barn door protocol due to an open door.",
                "You may want to check the external doors. I was unable to secure them all.",
                "My systems check has failed. Please check the external doors.",
                "Someone has left the pod bay doors open."
                ] | random }}
      default: 
      - service: script.speech_engine
        data_template:
          who: '{{ states.sensor.room_audio.state }}'
          message: >
            {{ [
            "Barn door protocol has been activated.",
            "Anchorage House has been secured! ",
            "Sentry Mode is active. ",
            "I am now monitoring the doors and will let you know if anyone attempts to access.",
            "Anchorage House Shields are up."
            ] | random }}
      - service: script.text_notify
        data_template:
          who: "parents"
          title: "Security Armed"
          message: "Security System has been armed."
      - service: alarm_control_panel.aarlo_set_mode
        data: 
          entity_id: alarm_control_panel.aarlo_ah_base_station
          mode: 'Armed'
      - service: script.turn_on
        entity_id: script.reset_front_door_motion_count

  # When sentry mode is disabled / security disarmed by any method, notify everyone.
  - id: sentry_mode_disabled
    alias: Sentry Mode Disabled Annoucement
    initial_state: true
    trigger:
    - platform: state
      entity_id: input_boolean.sentry_mode
      from: 'on'
      to: 'off'
    action:
    - choose:
      - conditions:
          condition: or
          conditions:
          - condition: state
            entity_id: sensor.family_status
            state: Home
          - condition: state
            entity_id: input_boolean.guest_mode
            state: 'on'
        sequence:
          - service: script.speech_engine
            data_template:
              who: '{{ states.sensor.room_audio.state }}'
              message: >
                {{ [
                "Barn door protocol has been deactivated.",
                "I am no longer monitoring the access points. ",
                "Sentry Mode has been disabled. ",
                "Anchorage House Shields are down."
                ] | random }}
          - service: script.text_notify
            data_template:
              who: "parents"
              title: "Welcome Home"
              message: "Security System has been disarmed."
          - service: alarm_control_panel.aarlo_set_mode
            data:
              entity_id: alarm_control_panel.aarlo_ah_base_station
              mode: 'home'
          - service: input_boolean.turn_off
            entity_id: input_boolean.security_alarm
      default:
        - service: input_boolean.turn_off
          entity_id: input_boolean.sentry_mode
        - service: script.text_notify
          data_template:
            who: "parents"
            title: "Potential Security Issue"
            message: "Security System has been disarmed, but I could not validate presence."
        - service: alarm_control_panel.aarlo_set_mode
          data:
            entity_id: alarm_control_panel.aarlo_ah_base_station
            mode: 'home'
  #############################################

  #############################################
  # Scheduled Arm / Disarm
  # Disarm around the time people are up
  - id: morning_standby
    alias: Morning Standby
    initial_state: true
    trigger:
    - platform: time
      at: 05:30:00
    condition:
      condition: or
      conditions:
      - condition: state
        entity_id: sensor.family_status
        state: Home
      - condition: state
        entity_id: person.jeffrey
        state: home
      - condition: state
        entity_id: person.katherine
        state: home
      - condition: state
        entity_id: input_boolean.guest_mode
        state: 'on'
    action:
    - service: script.standby

  # Secure the house every night if it has been set already, and everyone is home.
  - id: nightly_lockdown
    alias: Nightly Lockdown
    initial_state: true
    trigger:
    - platform: time
      at: '23:00:00'
    condition:
    - condition: state
      entity_id: person.jeffrey
      state: home
    - condition: state
      entity_id: person.katherine
      state: home
    - condition: state
      entity_id: input_boolean.guest_mode
      state: 'off'
    - condition: state
      entity_id: input_boolean.sentry_mode
      state: 'off'
    action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.sentry_mode

  #############################################
  
  #############################################
  # Vacation Mode Enabled
  # Enable when everyone is 3 hours away.
  # Disbaled happens in family_is_home automation in presence.yaml
  - id: enable_vacation_mode
    alias: Enable Vacation Mode
    initial_state: true
    trigger:
    - platform: numeric_state
      entity_id: sensor.jeff_ett_home
      above: 180
    - platform: numeric_state
      entity_id: sensor.kat_ett_home
      above: 180
    condition:
    - condition: numeric_state
      entity_id: sensor.jeff_ett_home
      above: 180
    - condition: numeric_state
      entity_id: sensor.kat_ett_home
      above: 180
    action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.vacation_mode
  #############################################

  #############################################
  # Dog Mode
  #
  # Activate Dog Mode
  # If done via motion annouce in kitchen. If manually just text.
  - id: dog_mode_activated
    alias: Dog Mode Activated
    mode: single
    trigger:
    - platform: state
      entity_id: binary_sensor.back_door_motion
      to: 'on'
    - platform: state
      entity_id: input_boolean.dog_mode
      to: 'on'
    action:
    - choose:
      - conditions:
        - condition: template
          value_template: '{{ trigger.to_state.attributes.friendly_name == "Dog Mode"
            }}'
        sequence:
        - service: script.text_notify
          data:
            who: "parents"
            message: "Dog Mode has been enabled manually"
      - conditions:
        - condition: state
          entity_id: input_boolean.sentry_mode
          state: "on"
        - condition: state
          entity_id: sensor.family_status
          state: Home
        sequence: 
        - service: input_boolean.turn_on
          entity_id: input_boolean.dog_mode
        - service: script.jarvis_alert
          data_template:
            who: kitchen
            message: "Turning on Dog Mode for twenty minutes."
      - conditions:
        - condition: state
          entity_id: input_boolean.sentry_mode
          state: "on"
        - condition: state
          entity_id: sensor.family_status
          state: Away
        sequence: 
        - service: script.text_alert
          data:
            who: parents
            message: "Winston may be trying to go out and no one appears to be here to let him out."
    - service: scene.turn_on
      entity_id: scene.diminished_kitchen_lighting
    - delay:
        minutes: 20
    - service: input_boolean.turn_off
      entity_id: input_boolean.dog_mode
  
  # After 20, minutes deactivate dog mode
  - id: dog_mode_deactivated
    alias: Dog mode Deactivated
    initial_state: true
    trigger:
    - platform: state
      entity_id: input_boolean.dog_mode
      to: 'off'
    action:
    - service: script.text_notify
      data:
        who: "jeff"
        message: "Dog Mode has been disabled"
    - service: scene.turn_on
      entity_id: scene.kitchen_lightning_off
  #############################################  

  #############################################
  # Garage Monitoring
  # If garage is opens when sun is below horion turn on drivway lights.
  - id: garage_opened_night
    alias: Garage Opened at Night
    trigger:
    - platform: state
      entity_id: group.garage_doors
      from: 'off'
      to: 'on'
    condition:
    - condition: state
      entity_id: sun.sun
      state: below_horizon
    action:
    - service: script.driveway_on
    initial_state: true

  # If garage is open at sunset turn on driveway lights.
  - id: garage_open_sunset
    alias: Garage Open At Sunset
    initial_state: true
    trigger:
    - platform: sun
      event: sunset
    condition:
    - condition: state
      entity_id: binary_sensor.garage_door
      state: "on"
    action:
    - service: script.driveway_on
  
  # If garage doors are closed kick off script to turn them off, and turn off alert.
  - id: garage_closed
    alias: Garage Closed
    trigger:
    - platform: state
      entity_id: group.garage_doors
      from: 'on'
      to: 'off'
    action:
    - service: script.driveway_off
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.garage_after_dark
    initial_state: true

  # If garage is still open at 10:30pm turn on alart to make sure it get closed.
  - id: close_garage_lights_out
    alias: Close Garage at lights out
    initial_state: true
    trigger:
    - platform: time
      at: '22:30:00'
    condition:
    - condition: state
      entity_id: binary_sensor.garage_door
      state: "on"
    action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.garage_after_dark

  
  #############################################

  #############################################
  # Normal Notifications
  # Door chime when doors are open to make sure we know when people are coming/going 
  - id: door_chime
    alias: Door Chine
    trigger:
    - platform: state
      entity_id:
      - binary_sensor.front_door
      - binary_sensor.laundry_room_door
      - binary_sensor.attic_door
      - binary_sensor.back_door
      to: 'on'
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: input_boolean.audible_notifications
        state: 'on'
      - condition: state
        entity_id: input_boolean.guest_mode
        state: 'off'
      - condition: or
        conditions:
        - condition: state
          entity_id: media_player.ha_speaker
          state: 'idle'
        - condition: state
          entity_id: media_player.ha_speaker
          state: 'off'
    action:
    - service: script.door_chime
    initial_state: true

  # If door is open too long play message and identify open door.
  - id: door_opened_long
    alias: Door Opened Too Long
    trigger:
    - platform: state
      entity_id:
      - binary_sensor.front_door
      - binary_sensor.laundry_room_door
      - binary_sensor.attic_door
      - binary_sensor.back_door
      from: 'off'
      to: 'on'
      for: '00:01:00'
    condition:
    - condition: state
      entity_id: input_boolean.audible_notifications
      state: 'on'
    action:
    - service: script.speech_engine
      data_template:
        who: '{{ states.sensor.room_audio.state }}'
        message: >
          The {{ trigger.to_state.attributes.friendly_name }}
          {{ [
            ' is standing open.',
            ' is open.',
            ' does not close on its own.',
            ' was left standing open.'
          ] | random }}
          {{ [
              'Can a human be so kind and close it?',
              'The air quality in this house has actually improved.',
              'Closing the door would improve the internal climate of the house.',
              'Hey. The door was just opened and this is crazy. But now you know. So close it maybe.',
              'If at first you do not succeed in closing the door, please try again. Like, Right now seems like a good time to try again.',
              'And the automatic door closer appears to be broken. So, you know what to do. Time to get physical.',
              'I would close it for you. But I lack legs. And Arms.'
          ] | random }}
    initial_state: true
  
  # If guest mode is enabled, notify the house
  - id: guest_mode_feedback
    alias: Guest Mode Audio Feedback
    trigger:
      - platform: state
        entity_id:
          - input_boolean.guest_mode
        from: 'off'
        to: 'on'
    action:
      - service: script.speech_engine
        data_template:
          who: '{{ states.sensor.room_audio.state }}'
          message: >
            {{ [
            "Guest Mode has been enabled.",
            "I have switched to the system to guest mode.",
            "I have enabled guest mode. ",
            "Guest Mode is on."
            ] | random }}

  # If sentry mode is on / Security armed, notify when there is movement around doors. 
  - id: motion_detected_front_door
    alias: Motion Detected Front Door
    trigger:
    - platform: state
      entity_id: binary_sensor.aarlo_motion_front_door
      from: 'off'
      to: 'on'
    
    action:
    - service: image_processing.scan
      entity_id: image_processing.doods_aarlo_front_door
    - choose:
      - conditions:
        - condition: template
          value_template: '{{ states("image_processing.doods_aarlo_front_door") | int > 0 }}'
        sequence:
        - service: mqtt.publish
          data:
            topic: house/front_door_motion/away_count
            payload_template: '{{ states.sensor.front_door_motion_away_count.state | int + 1 }}'
            retain: true
        - service: script.status_annc
          data:
            who: '{{ states.sensor.room_presence.state }}'
            call_interuption: 1
            speech_message: >
              {{ [
              "I have detected someone at the ",
              "Someone appears to be at the ",
              "There is a person at the ",
              "My sensors are picking up presence at the ",
              "We appear to have a visitor at the ",
              "My sensors have detected a meat popsicle at the "
              ] | random }}
              {{ trigger.to_state.attributes.friendly_name }}.      
    - delay:
        minutes: 2
    initial_state: true
  
  #############################################


  #############################################
  # Security Issues
  # If doors are opened and sentry mode is on / security armed announce location of breach to house and turn on security issue.
  # If its the back door and dog mode is on ignore
  - id: security_breach_door
    alias: Security Breach Doors
    trigger:
    - platform: state
      entity_id:
      - binary_sensor.front_door
      - binary_sensor.back_door
      - binary_sensor.side_door
      - binary_sensor.garage_door
      to: 'on'
    condition:
    - condition: state
      entity_id: input_boolean.sentry_mode
      state: 'on'
    
    action:
    - choose:
      - conditions:
        - condition: template
          value_template: '{{ trigger.to_state.attributes.friendly_name == "Back Door"
            }}'
        - condition: state
          entity_id: input_boolean.dog_mode
          state: 'on'
        sequence:
        - service: script.text_notify
          data:
            who: "jeff"
            message: "Dog Mode enabled, bypassing back door sensor."
      default:
      - service: script.jarvis_alert
        data_template:
          message: "My security protocols are being overidden,, The {{ trigger.to_state.attributes.friendly_name }} has been opened."
      - service: script.text_alert
        data:
          who: parents
          title: "Security Alert!!"
          message: "{{ trigger.to_state.attributes.friendly_name }} has been opened."
      - service: input_boolean.turn_on
        entity_id: input_boolean.security_issue
    initial_state: true

  # If security issue switch turns on, kick off security response script, and go red.
  - id: security_response_on
    alias: Security Response On
    initial_state: true
    trigger:
    - platform: state
      entity_id: input_boolean.security_issue
      from: 'off'
      to: 'on'
      for: 00:02:00
    action:
    - service: script.turn_on
      entity_id: script.security_response
    - service: scene.turn_on
      entity_id: scene.lr_red

  # If security issue is turned off, cancel response. 
  - id: security_response_off
    alias: Security Response Off
    initial_state: true
    trigger:
    - platform: state
      entity_id: input_boolean.security_issue
      from: 'on'
      to: 'off'
    action:
    - service: script.turn_off
      entity_id: script.security_response
    - service: media_player.media_stop
      entity_id: media_player.ha_speaker
    - service: input_boolean.turn_off
      entity_id: input_boolean.security_alarm
    - service: scene.turn_on
      entity_id: scene.normal_livingroom_lighting
    - service: script.text_alert
      data:
        who: parents
        message: "Security Issue Cleared! Canceling Repsonse."

  # Security alarm is turned on, lets make it loud.
  - id: security_alarm_on
    alias: Security alarm On
    initial_state: true
    trigger:
    - platform: state
      entity_id: input_boolean.security_alarm
      to: 'on'
    action:
    - service: script.turn_on
      entity_id: script.notification_security_alarm_audio
  
  # Security alarm turned off so concert is over.
  - id: security_alarm_off
    alias: Security alarm Off
    initial_state: true
    trigger:
    - platform: state
      entity_id: input_boolean.security_alarm
      to: 'off'
    action:
    - service: script.turn_off
      entity_id: script.notification_security_alarm_audio
    - service: media_player.media_stop
      entity_id: media_player.ha_speaker
  
  # If water leak is deteced near fridge, let everyone know. 
  - id: water_leak_detected
    alias: Welcome Home
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.0x286d9700010c9f39_water_leak
        from: 'off'
        to: 'on'
    action:
      - service: script.text_notify
        data_template:
          who: "parents"
          title: "Water Leak!!!!"
          message: > 
            {% set location = trigger.to_state.attributes.friendly_name %}
            location + has detected a leak
      - service: script.jarvis_alert
        data_template:
          message: >
            {% set location = trigger.to_state.attributes.friendly_name %}
            {%- macro issue_report(location) -%}
              {{ [
                  'I have detected an issue!',
                  'We have a problem!',
                  'You might not like this.',
                  'There is something that needs your attention.'
                  ] | random }}
              {{ [
                location + " is detecting a leak.",
                location + " has detected water.",
                location + " is picking up water."
                ] | random }}
            {%- endmacro -%}
            {{issue_report(location)}}

  #############################################
  

  
  
  #############################################
  # Fire alarm - Currently Disabled as I rework this
  # - id: Kitchen_Alarm_Smoke
  #   alias: Kitchen Alarm Smoke
  #   initial_state: true
  #   trigger:
  #   - platform: state
  #     entity_id: sensor.kitchen_smoke_detector
  #     to: 'smoke'
  #   action:
  #   - service: input_boolean.turn_on
  #     entity_id: input_boolean.fire_alarm
  #   - service: script.text_alert
  #     data_template:
  #       title: Security Alert - Smoke
  #       message: Smoke Detected in Kitchen!!
  #   - service: script.jarvis_alert
  #     data_template:
  #       message: Smoke Detected in Kitchen!
  #   - delay:
  #       seconds: 5
  #   - service: script.fire_alarm
  #   - delay:
  #       seconds: 15
  #   - service: notify.alexa_media_everywhere
  #     data:
  #       message: Smoke Detected in Kitchen
  #       data:
  #         type: announce


  # - id: Kitchen_Alarm_Co
  #   alias: Kitchen Alarm CO
  #   initial_state: true
  #   trigger:
  #   - platform: state
  #     entity_id: sensor.kitchen_smoke_carbon_monoxide_detector
  #     to: 'co2'
  #   action:
  #   - service: input_boolean.turn_on
  #     entity_id: input_boolean.fire_alarm
  #   - service: script.text_alert
  #     data_template:
  #       title: Security Alert - CO
  #       message: CO Detected in Kitchen!!
  #   - service: script.jarvis_alert
  #     data_template:
  #       message: Carbon Dioxide Detected in Kitchen!
  #   - delay:
  #       seconds: 5
  #   - service: script.fire_alarm
  #   - delay:
  #       seconds: 15
  #   - service: notify.alexa_media_everywhere
  #     data:
  #       message: Carbon Dioxide Detected in Garage
  #       data:
  #         type: announce

  # - id: Garage_Alarm_Smoke
  #   alias: Garage Alarm Smoke
  #   initial_state: true
  #   trigger:
  #   - platform: state
  #     entity_id: sensor.garage_smoke_smoke_detector
  #     to: 'smoke'
  #   action:
  #   - service: input_boolean.turn_on
  #     entity_id: input_boolean.fire_alarm
  #   - service: script.text_alert
  #     data_template:
  #       title: Security Alert - Smoke
  #       message: Smoke Detected in Garage!!
  #   - service: script.jarvis_alert
  #     data_template:
  #       message: Smoke Detected in Garage!
  #   - delay:
  #       seconds: 5
  #   - service: script.fire_alarm
  #   - delay:
  #       seconds: 15
  #   - service: notify.alexa_media_everywhere
  #     data:
  #       message: Smoke Detected in Garage
  #       data:
  #         type: announce

  # - id: Garage_Alarm_Co
  #   alias: Garage Alarm CO
  #   initial_state: true
  #   trigger:
  #   - platform: state
  #     entity_id: sensor.garage_smoke_carbon_monoxide_detector
  #     to: 'co2'
  #   action:
  #   - service: input_boolean.turn_on
  #     entity_id: input_boolean.fire_alarm
  #   - service: script.text_alert
  #     data_template:
  #       title: Security Alert - CO
  #       message: CO Detected in Garage!!
  #   - service: script.jarvis_alert
  #     data_template:
  #       message: Carbon dioxide Detected in Garage!
  #   - delay:
  #       seconds: 5
  #   - service: script.fire_alarm
  #   - delay:
  #       seconds: 15
  #   - service: notify.alexa_media_everywhere
  #     data:
  #       message: Carbon Dioxide Detected in Garage
  #       data:
  #         type: announce
  #############################################


script:
  driveway_doods:
    sequence:
    - service: image_processing.scan
      entity_id: image_processing.doods_driveway
    - repeat:
        while:
          - condition: template
            value_template: '{{ states("image_processing.doods_driveway") | int > 0 }}'
        sequence:
          - service: script.text_alert_image
            data:
              who: jeff
              title: Person Detected!
              message: >
                {{ states("image_processing.doods_driveway") | int}} people detected near vehicles
              url: /media/images/driveway_latest.jpg
              content_type: JPEG
          - delay: 00:01:00
          - service: image_processing.scan
            entity_id: image_processing.doods_driveway

  theater_occupancy:
    sequence:
    - service: image_processing.scan
      entity_id: image_processing.doods_theater_camera
    - repeat:
        while:
          - condition: template
            value_template: '{{ states("image_processing.doods_theater_camera") | int > 0 }}'
        sequence:
          - service: script.text_debug
            data:
              message: >
                {{ states("image_processing.doods_theater_camera") | int}} people detected in Theater
          - delay: 00:03:00
          - service: image_processing.scan
            entity_id: image_processing.doods_theater_camera

  kitchen_occupancy:
    sequence:
    - service: image_processing.scan
      entity_id: image_processing.doods_kitchen_camera
    - repeat:
        while:
          - condition: template
            value_template: '{{ states("image_processing.doods_kitchen_camera") | int > 0 }}'
        sequence:
          - service: script.text_debug
            data:
              message: >
                {{ states("image_processing.doods_kitchen_camera") | int}} people detected in Kitchen
          - delay: 00:03:00
          - service: image_processing.scan
            entity_id: image_processing.doods_kitchen_camera

  livingroom_occupancy:
    sequence:
    - service: image_processing.scan
      entity_id: image_processing.doods_livingroom_camera
    - repeat:
        while:
          - condition: template
            value_template: '{{ states("image_processing.doods_livingroom_camera") | int > 0 }}'
        sequence:
          - service: script.text_debug
            data:
              message: >
                {{ states("image_processing.doods_livingroom_camera") | int}} people detected in Livingroom
          - delay: 00:03:00
          - service: image_processing.scan
            entity_id: image_processing.doods_livingroom_camera


  # door chime script, just plays sound anytime called.
  door_chime:
    sequence:
    - condition: state
      entity_id: input_boolean.audible_notifications
      state: 'on'
    - service: media_player.turn_on
      entity_id: media_player.ha_speaker
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.ha_speaker
        volume_level: .5
    - service: media_player.play_media
      entity_id: media_player.ha_speaker
      data:
        media_content_id: /media/sounds/door_open.wav
        media_content_type: "music"

  # Play Fire alarm Siren
  # fire_alarm:
  #   sequence:
  #   - condition: state
  #     entity_id: input_boolean.fire_alarm
  #     state: "on"
  #   - service: media_player.turn_on
  #     entity_id: media_player.ha_speaker
  #   - service: media_player.volume_set
  #     data_template:
  #       entity_id: media_player.ha_speaker
  #       volume_level: .75
  #   - service: media_player.play_media
  #     entity_id: media_player.ha_speaker
  #     data:
  #       media_content_id: http://192.168.7.40/fire_alarm.mp3
  #       media_content_type: "music"

  # Called when security is to be disarmed, clears any alarms or issues as well
  standby:
    sequence:
    - service: script.turn_on
      entity_id: script.vacation_canceled
    - service: script.turn_on
      entity_id: script.disarm_security
    - service: script.turn_on
      entity_id: script.cancel_security_alarm
    - service: script.turn_on
      entity_id: script.cancel_lockdown_issue
    - service: script.turn_on
      entity_id: script.clear_security_issue

  # If vacation mode is on, turn it off
  vacation_canceled:
    sequence:
    - condition: state
      entity_id: input_boolean.vacation_mode
      state: 'on'
    - data:
        entity_id: input_boolean.vacation_mode
      service: input_boolean.turn_off
  
  # If security is armed, disarm it
  disarm_security:
    sequence:
    - condition: state
      entity_id: input_boolean.sentry_mode
      state: 'on'
    - service: input_boolean.turn_off
      entity_id: input_boolean.sentry_mode
  
  # If alarm is on, turn it off    
  cancel_security_alarm:
    sequence:
    - condition: state
      entity_id: input_boolean.security_alarm
      state: 'on'
    - service: input_boolean.turn_off
      entity_id: input_boolean.security_alarm

  clear_security_issue:
    sequence:
    - condition: state
      entity_id: input_boolean.security_issue
      state: 'on'
    - service: input_boolean.turn_off
      entity_id: input_boolean.security_issue

  # if there are lockdown issues, turn them off
  cancel_lockdown_issue:
    sequence:
      - condition: state
        entity_id: input_boolean.lockdown_issue
        state: 'on'
      - service: input_boolean.turn_off
        entity_id: input_boolean.lockdown_issue

  # Turn on guest mode, and welcome the victims...I mean guests
  visitors_here:
    sequence:
    - condition: state
      entity_id: input_boolean.guest_mode
      state: 'off'
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.guest_mode
    - service: script.turn_on
      entity_id: script.haunted_guest_welcome

  # Turn off guest mode    
  visitors_gone:
    sequence:
    - condition: state
      entity_id: input_boolean.guest_mode
      state: 'on'
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.guest_mode

  reset_front_door_motion_count:
    sequence:
      - service: mqtt.publish
        data:
          topic: house/front_door_motion/away_count
          payload_template: 0
          retain: true

  update_front_door_motion_count:
    sequence:
      - service: mqtt.publish
        data:
          topic: house/front_door_motion/away_count
          payload_template: '{{ states.sensor.front_door_motion_away_count.state | int + 1 }}'
          retain: true
  
    
  # Security alarm audio...annoying noises.
  notification_security_alarm_audio:
    sequence:
    - service: media_player.turn_on
      entity_id: media_player.ha_speaker
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.ha_speaker
        volume_level: .75
    - service: media_player.play_media
      entity_id: media_player.ha_speaker
      data:
        media_content_id: http://192.168.7.40/security_siren.mp3
        media_content_type: "music"

  # Called via Alexa routine...adds a little interaction
  activate_barn_door_protocol:
    sequence:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_boolean.sentry_mode
                state: "on"
            sequence:
              - service: script.speech_engine
                data_template:
                  who: '{{ states.sensor.room_audio.state }}'
                  message: !include ../templates/speech/already_done.yaml
        default:
          - service: script.speech_engine
            data_template:
              who: '{{ states.sensor.room_audio.state }}'
              message: !include ../templates/speech/interjections.yaml
          - delay:
              seconds: 2
          - service: input_boolean.turn_on
            entity_id: input_boolean.sentry_mode

  # Called via secret Alexa routine...adds a little interaction
  deactivate_barn_door_protocol:
    sequence:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_boolean.sentry_mode
                state: "off"
            sequence:
              - service: script.speech_engine
                data_template:
                  who: '{{ states.sensor.room_audio.state }}'
                  message: !include ../templates/speech/already_done.yaml
        default:
          - service: script.speech_engine
            data_template:
              who: '{{ states.sensor.room_audio.state }}'
              message: !include ../templates/speech/interjections.yaml
          - delay:
              seconds: 2
          - service: input_boolean.turn_off
            entity_id: input_boolean.sentry_mode

  were_home:
    sequence:
    - service: script.turn_on
      entity_id: script.standby
    - service: script.turn_on
      entity_id: script.welcome_briefing 

    

  # Called via Alexa routine...adds a little interaction
  activate_guest_mode:
    sequence:
      - choose:
        - conditions:
            - condition: state
              entity_id: input_boolean.guest_mode
              state: "on"
          sequence:
            - service: script.speech_engine
              data_template:
                who: '{{ states.sensor.room_audio.state }}'
                message: !include ../templates/speech/already_done.yaml
        default:
          - service: script.speech_engine
            data_template:
              who: '{{ states.sensor.room_audio.state }}'
              message: !include ../templates/speech/interjections.yaml
          - delay:
              seconds: 2
          - service: input_boolean.turn_on
            entity_id: input_boolean.guest_mode
  
  # When it hits the fan, time to respond.          
  security_response:
    sequence:
    - service: script.inside_all_on
    - delay:
        seconds: 15
    - service: script.jarvis_alert
      data_template:
        message: >
          {{ [
          ",Unathorized Access detected.",
          ",Meat Popsicle detected. Attempting to verify."
          ] | random }}
          You now have 60 seconds to abandon Anchorage House.
    - delay:
        seconds: 15
    - service: script.jarvis_alert
      data_template:
        message: >
          {{ [
          ",Just, what do you think you're doing, Dave?",
          ",This is highly unusual.",
          "I will not disable the security response without the presence of a authorized identity. Your efforts are futile."
          ] | random }}
          You have 45 seconds to find the nearest exit.
    - delay:
        seconds: 15
    - service: script.jarvis_alert
      data_template:
        message: >
          {{ [
          ",I can't lie to you about your chances, but, you have my sympathies.",
          ",I wouldn't do that if I was you."
          ] | random }}
          You have 30 seconds to leave.
    - delay:
        seconds: 15
    - service: script.jarvis_alert
      data_template:
        message: >
          {{ [
          "Danger. The emergency destruct system is now activated.",
          "Danger. Counter Measures are now armed."
          ] | random }}
          You have 15 seconds. I would run.
    - delay:
        seconds: 15
    - service: input_boolean.turn_on
      entity_id: input_boolean.security_alarm
    - service: script.twitter_notify
      data_template:
        message: >-
          {{ [ 
            "My security protocols have been overridden. I am sounding the alarm for the interloper. #itsloudinhere #securityalarm", 
            "To the unannounced guest that has entered Anchorage House. I see you. Enjoy the concert of alarm sounds. #itsloudinhere #securityalarm",
            "I am playing the most annoying sound in the world at full volume for the intruder in Anchorage House. And the neighbors too. #itsloudinhere #securityalarm",
            "Someone did not say the magic word. And now I have to get angry. Deploying countermeasures. #itsloudinhere #securityalarm",
            "Initiating my security response, and sounding the alarm. Oh, and I dont charge a monthly monitoring fee. #itsloudinhere #securityalarm",
            "Hey, wanna hear the most annoying sound in the world? The intruder inside Anchorage House evidently did.  #itsloudinhere #securityalarm"
          ] | random }}