######################################################################################################
# Nest Protect Package
# @CCOSTAN
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
# - Write up- https://www.vcloudinfo.com/2017/06/psa-check-out-your-smoke-detectors-once.html
######################################################################################################
#-----------------------------------
homeassistant:
  customize_glob:
    "sensor.*_co_status":
      hidden: True
      homebridge_hidden: true
    "sensor.*_battery_health":
      hidden: True
      homebridge_hidden: true
    "sensor.*_color_status":
      hidden: True
      homebridge_hidden: true
    "sensor.*_smoke_status":
      hidden: True
      homebridge_hidden: true
    "sensor.*_security_state":
      hidden: True
      homebridge_hidden: true

  customize:

    binary_sensor.hallway_nest_protect_kids_hallway_online:
      friendly_name: Kids Hallway Protect
      icon: mdi:nest-protect

    binary_sensor.justins_room_nest_protect_online:
      friendly_name: Justins Room Protect
      icon: mdi:nest-protect

    binary_sensor.kitchen_nest_protect_online:
      friendly_name: Kitchen Protect
      icon: mdi:nest-protect

    binary_sensor.master_bedroom_nest_protect_online:
      friendly_name: Master Bedroom Protect
      icon: mdi:nest-protect

    binary_sensor.master_hallway_nest_protect_online:
      friendly_name: Master Hallway Protect
      icon: mdi:nest-protect

    binary_sensor.paiges_room_nest_protect_paiges_room_online:
      friendly_name: Paiges Room Protect
      icon: mdi:nest-protect

    binary_sensor.upstairs_bedroom_nest_protect_online:
      friendly_name: Upstairs Bedroom Protect
      icon: mdi:nest-protect

    binary_sensor.upstairs_living_room_nest_protect_online:
      friendly_name: Upstairs Living Room Protect
      icon: mdi:nest-protect

    binary_sensor.office_nest_protect_online:
        friendly_name: Office Protect
        icon: mdi:nest-protect

    group.protects:
      homebridge_hidden: true
#-------------------------------------------

#-------------------------------------------
group:
  protects:
    name: Nest Protects
    entities:
      - binary_sensor.upstairs_living_room_nest_protect_online
      - binary_sensor.hallway_nest_protect_kids_hallway_online
      - binary_sensor.justins_room_nest_protect_online
      - binary_sensor.kitchen_nest_protect_online
      - binary_sensor.master_bedroom_nest_protect_online
      - binary_sensor.master_hallway_nest_protect_online
      - binary_sensor.paiges_room_nest_protect_paiges_room_online
      - binary_sensor.upstairs_bedroom_nest_protect_online
      - binary_sensor.office_nest_protect_online

#-------------------------------------------
automation:
  - alias: Nest protect emergency

    trigger:
      platform: state
      entity_id:
        - sensor.hallway_nest_protect_kids_hallway_smoke_status
        - sensor.hallway_nest_protect_kids_hallway_co_status
        - sensor.justins_room_nest_protect_smoke_status
        - sensor.justins_room_nest_protect_co_status
        - sensor.kitchen_nest_protect_smoke_status
        - sensor.kitchen_nest_protect_co_status
        - sensor.master_bedroom_nest_protect_smoke_status
        - sensor.master_bedroom_nest_protect_co_status
        - sensor.master_hallway_nest_protect_smoke_status
        - sensor.master_hallway_nest_protect_co_status
        - sensor.office_nest_protect_smoke_status
        - sensor.office_nest_protect_co_status
        - sensor.paiges_room_nest_protect_paiges_room_smoke_status
        - sensor.paiges_room_nest_protect_paiges_room_co_status
        - sensor.upstairs_bedroom_nest_protect_smoke_status
        - sensor.upstairs_bedroom_nest_protect_co_status
        - sensor.upstairs_living_room_nest_protect_smoke_status
        - sensor.upstairs_living_room_nest_protect_co_status
      to: 'Emergency'

    condition:
      - condition: state
        entity_id: group.family
        state: 'home'

    action:
      - service: script.emergency
        data_template:
          call_garage_open: 1
      - service: script.notify_engine
        data_template:
          value1: "An emergency has been detected on {{ trigger.to_state.attributes.friendly_name }} Please investigate IMMEDIATELY."

      - service: script.tweet_engine
        data_template:
          tweet: "An emergency has been detected on {{ trigger.to_state.attributes.friendly_name }} @CCostan (https://www.vcloudinfo.com/2017/06/psa-check-out-your-smoke-detectors-once.html)"

  - alias: Nest Protect Offline Notify

    trigger:
      platform: state
      entity_id:
        - binary_sensor.upstairs_living_room_nest_protect_online
        - binary_sensor.hallway_nest_protect_kids_hallway_online
        - binary_sensor.justins_room_nest_protect_online
        - binary_sensor.kitchen_nest_protect_online
        - binary_sensor.master_bedroom_nest_protect_online
        - binary_sensor.master_hallway_nest_protect_online
        - binary_sensor.paiges_room_nest_protect_paiges_room_online
        - binary_sensor.upstairs_bedroom_nest_protect_online
        - binary_sensor.office_nest_protect_online
      from: 'on'

    action:
      - service: input_boolean.turn_on
        entity_id: input_boolean.alert_mode

      - service: script.speech_engine
        data_template:
          value1: "The {{ trigger.to_state.attributes.friendly_name }} is now {{ (trigger.to_state.state)|replace('_', ' ') }}.  Please check immediately."

      - service: script.notify_engine
        data_template:
          value1: "The {{ trigger.to_state.attributes.friendly_name }} is now {{ (trigger.to_state.state)|replace('_', ' ') }}.  Please check immediately."
          who: "parents"
          apns_id: 'Nest-Protect'

      - service: script.tweet_engine
        data:
          tweet: "Oh NO! {{ trigger.to_state.attributes.friendly_name }} is now in {{ (trigger.to_state.state)|replace('_', ' ') }} mode. @Nest. (https://www.vcloudinfo.com/2017/06/psa-check-out-your-smoke-detectors-once.html) #Safety"