# https://www.home-assistant.io/integrations/amcrest
amcrest:
  - host: !secret amcrest_ip
    username: !secret amcrest_username
    password: !secret amcrest_password
    resolution: low
    binary_sensors:
      - motion_detected
      - online

binary_sensor:
  - platform: template
    sensors:
      camera_state:
        friendly_name: Security camera
        device_class: safety
        value_template: >-
          {{ state_attr('camera.amcrest_camera', 'motion_recording') != 'on' }}

switch:
  - platform: template
    switches:
      camera:
        friendly_name: Security camera
        value_template: "{{ state_attr('camera.amcrest_camera', 'motion_recording') == 'on' }}"
        icon_template: >
          {% if state_attr('camera.amcrest_camera', 'motion_recording') == 'on' %} mdi:shield-home
          {% else %} mdi:shield-off
          {% endif %}
        turn_on:
          service: script.security_camera_on
        turn_off:
          service: script.security_camera_off

script:
  security_camera_on:
    alias: Security camera · Turn on
    sequence:
      - service: camera.turn_on
        entity_id: camera.amcrest_camera
      - service: amcrest.goto_preset
        entity_id: camera.amcrest_camera
        data:
          preset: 1
      - service: amcrest.enable_motion_recording
        entity_id: camera.amcrest_camera
      - service: amcrest.enable_recording
        entity_id: camera.amcrest_camera
      - service: amcrest.enable_audio
        entity_id: camera.amcrest_camera

  security_camera_off:
    alias: Security camera · Turn off
    sequence:
      - service: amcrest.goto_preset
        entity_id: camera.amcrest_camera
        data:
          preset: 3
      - service: amcrest.disable_motion_recording
        entity_id: camera.amcrest_camera
      - service: amcrest.disable_recording
        entity_id: camera.amcrest_camera
      - service: amcrest.disable_audio
        entity_id: camera.amcrest_camera
      - service: camera.turn_off
        entity_id: camera.amcrest_camera

  alarm_start:
    sequence:
      - service: media_player.play_media
        data:
          entity_id: media_player.master_bedroom
          media_content_id: "https://raw.githubusercontent.com/saeedsohi/siren/master/FireTrucksSirens.mp3"
          media_content_type: music
      - service: light.turn_on
        entity_id: light.bedside_light
        data:
          brightness_pct: 100
      - service: light.turn_on
        entity_id: light.bedside_light
        data:
          effect: Police

  alarm_stop:
    sequence:
      - service: media_player.media_stop
        entity_id: media_player.master_bedroom
      - service: light.turn_on
        entity_id: light.bedside_light
        data:
          brightness_pct: 60
          kelvin: 2400

# Hack - the switch.camera initial state is wrong (ON) when the server starts.
automation:
  - alias: Camera · Startup
    trigger:
      - platform: homeassistant
        event: start
    action:
      - delay: 00:00:10
      - service: switch.turn_off
        entity_id: switch.camera

  - alias: Security · Door opened while away
    trigger:
      - platform: state
        entity_id: binary_sensor.front_door_on_off
        to: "on"
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: person.georgi
          state: "not_home"
        - condition: state
          entity_id: input_boolean.guest_mode
          state: "off"
    action:
      - service: notify.telegram
        data:
          title: "🛡 *Security Module · Door opened while away*"
          message: "It seems like the front door was opened while you are away! Check the camera!"
