#:########################################################################################:#
# Raspberry Pi CCTV Monitor Package                                                      #
#:########################################################################################:#
#
# TITLE:
#   Raspberry Pi CCTV Monitor
#
# FILE:
#   packages/raspberry_pi_monitor.yaml
#
# VERSION:
#   V1.1 2026-07-20
#
# VERSION HISTORY:
#   V1.1 2026-07-20
#     - Added structured documentation for MQTT, availability, and controls.
#
#   V1.0
#     - Initial monitoring and remote-control configuration.
#
# PURPOSE:
#   Monitors the cctvquad1 and cctvquad2 Raspberry Pi hosts and provides remote
#   reboot and shutdown controls through MQTT.
#
# EXTERNAL DEPENDENCY:
#   - Each Raspberry Pi requires an external process that publishes telemetry
#     and state, consumes command topics, and performs the requested actions.
#   - This package does not install or manage that external process.
#
# MQTT STATUS:
#   Topic pattern:
#     viewroad-status/rpis/<host>/<field>
#
#   Fields:
#     cpu-temp, cpu-use, ram-use, disk-use, uptime, last-seen, ipv4-address,
#     reboot, and shutdown.
#
#   The last-seen payload must be a timestamp that Home Assistant can parse with
#   as_timestamp().
#
# MQTT COMMANDS:
#   Topic pattern:
#     viewroad-commands/rpis/<host>/reboot/set
#     viewroad-commands/rpis/<host>/shutdown/set
#
#   Commands use the default MQTT switch ON/OFF payloads, QoS 1, and are not
#   retained. The external process must publish matching switch state feedback.
#
# AVAILABILITY:
#   - A host is considered available when its last-seen timestamp is valid and
#     no more than 180 seconds old.
#   - The template uses now(), so Home Assistant periodically re-evaluates age.
#
# GROUPS:
#   - group.pi_cctvquad1_on collects Quad1 telemetry and controls.
#   - group.pi_cctvquad2_on collects Quad2 telemetry and controls.
#
# SAFETY:
#   - Reboot and shutdown interrupt CCTV services and host availability.
#   - Shutdown may require physical access to power the host back on.
#   - Dashboard controls should require confirmation before sending commands.
#
#:########################################################################################:#

mqtt:
  sensor:
    - unique_id: cctvquad1_cpu_temp
      state_topic: "viewroad-status/rpis/cctvquad1/cpu-temp"
      name: "CCTVQuad1 CPU Temperature"
      unit_of_measurement: "°C"
      device_class: temperature
      state_class: measurement
      value_template: "{{ value | float(0) }}"
      icon: mdi:thermometer

    - unique_id: cctvquad1_cpu_use
      state_topic: "viewroad-status/rpis/cctvquad1/cpu-use"
      name: "CCTVQuad1 CPU Use"
      unit_of_measurement: "%"
      state_class: measurement
      value_template: "{{ value | float(0) }}"
      icon: mdi:cpu-32-bit

    - unique_id: cctvquad1_ram_use
      state_topic: "viewroad-status/rpis/cctvquad1/ram-use"
      name: "CCTVQuad1 RAM Use"
      unit_of_measurement: "%"
      state_class: measurement
      value_template: "{{ value | float(0) }}"
      icon: mdi:raspberry-pi

    - unique_id: cctvquad1_disk_use
      state_topic: "viewroad-status/rpis/cctvquad1/disk-use"
      name: "CCTVQuad1 Disk Use"
      unit_of_measurement: "%"
      state_class: measurement
      value_template: "{{ value | float(0) }}"
      icon: mdi:harddisk

    - unique_id: cctvquad1_uptime
      state_topic: "viewroad-status/rpis/cctvquad1/uptime"
      name: "CCTVQuad1 Uptime"
      icon: mdi:timer

    - unique_id: cctvquad1_last_seen
      state_topic: "viewroad-status/rpis/cctvquad1/last-seen"
      name: "CCTVQuad1 Last Seen"
      icon: mdi:calendar-clock

    - unique_id: cctvquad1_ipv4_address
      state_topic: "viewroad-status/rpis/cctvquad1/ipv4-address"
      name: "CCTVQuad1 IPv4 Address"
      icon: mdi:server-network

    - unique_id: cctvquad2_cpu_temp
      state_topic: "viewroad-status/rpis/cctvquad2/cpu-temp"
      name: "CCTVQuad2 CPU Temperature"
      unit_of_measurement: "°C"
      device_class: temperature
      state_class: measurement
      value_template: "{{ value | float(0) }}"
      icon: mdi:thermometer

    - unique_id: cctvquad2_cpu_use
      state_topic: "viewroad-status/rpis/cctvquad2/cpu-use"
      name: "CCTVQuad2 CPU Use"
      unit_of_measurement: "%"
      state_class: measurement
      value_template: "{{ value | float(0) }}"
      icon: mdi:cpu-32-bit

    - unique_id: cctvquad2_ram_use
      state_topic: "viewroad-status/rpis/cctvquad2/ram-use"
      name: "CCTVQuad2 RAM Use"
      unit_of_measurement: "%"
      state_class: measurement
      value_template: "{{ value | float(0) }}"
      icon: mdi:raspberry-pi

    - unique_id: cctvquad2_disk_use
      state_topic: "viewroad-status/rpis/cctvquad2/disk-use"
      name: "CCTVQuad2 Disk Use"
      unit_of_measurement: "%"
      state_class: measurement
      value_template: "{{ value | float(0) }}"
      icon: mdi:harddisk

    - unique_id: cctvquad2_uptime
      state_topic: "viewroad-status/rpis/cctvquad2/uptime"
      name: "CCTVQuad2 Uptime"
      icon: mdi:timer

    - unique_id: cctvquad2_last_seen
      state_topic: "viewroad-status/rpis/cctvquad2/last-seen"
      name: "CCTVQuad2 Last Seen"
      icon: mdi:calendar-clock

    - unique_id: cctvquad2_ipv4_address
      state_topic: "viewroad-status/rpis/cctvquad2/ipv4-address"
      name: "CCTVQuad2 IPv4 Address"
      icon: mdi:server-network

  switch:
    - unique_id: cctvquad1_reboot
      name: "CCTVQuad1 Reboot"
      state_topic: "viewroad-status/rpis/cctvquad1/reboot"
      command_topic: "viewroad-commands/rpis/cctvquad1/reboot/set"
      qos: 1
      optimistic: false
      retain: false
      icon: mdi:refresh

    - unique_id: cctvquad1_shutdown
      name: "CCTVQuad1 Shutdown"
      state_topic: "viewroad-status/rpis/cctvquad1/shutdown"
      command_topic: "viewroad-commands/rpis/cctvquad1/shutdown/set"
      qos: 1
      optimistic: false
      retain: false
      icon: mdi:close-network

    - unique_id: cctvquad2_reboot
      name: "CCTVQuad2 Reboot"
      state_topic: "viewroad-status/rpis/cctvquad2/reboot"
      command_topic: "viewroad-commands/rpis/cctvquad2/reboot/set"
      qos: 1
      optimistic: false
      retain: false
      icon: mdi:refresh

    - unique_id: cctvquad2_shutdown
      name: "CCTVQuad2 Shutdown"
      state_topic: "viewroad-status/rpis/cctvquad2/shutdown"
      command_topic: "viewroad-commands/rpis/cctvquad2/shutdown/set"
      qos: 1
      optimistic: false
      retain: false
      icon: mdi:close-network

template:
  - binary_sensor:
      - name: "CCTV Quad1 Available"
        unique_id: pi_cctvquad1_on
        device_class: connectivity
        state: >
          {% set now_ts = as_timestamp(now()) %}
          {% set last_ts = as_timestamp(states('sensor.cctvquad1_last_seen'), default=0) %}
          {% set age = now_ts - last_ts %}
          {{ last_ts > 0 and age >= 0 and age <= 180 }}

      - name: "CCTV Quad2 Available"
        unique_id: pi_cctvquad2_on
        device_class: connectivity
        state: >
          {% set now_ts = as_timestamp(now()) %}
          {% set last_ts = as_timestamp(states('sensor.cctvquad2_last_seen'), default=0) %}
          {% set age = now_ts - last_ts %}
          {{ last_ts > 0 and age >= 0 and age <= 180 }}

group:
  pi_cctvquad1_on:
    name: "RPi CCTV Quad1"
    #control: hidden
    entities:
      - sensor.cctvquad1_ipv4_address
      - sensor.cctvquad1_uptime
      - switch.cctvquad1_reboot
      - switch.cctvquad1_shutdown
      - sensor.cctvquad1_cpu_temperature
      - sensor.cctvquad1_cpu_use
      - sensor.cctvquad1_ram_use
      - sensor.cctvquad1_disk_use
      - sensor.cctvquad1_last_seen

  pi_cctvquad2_on:
    name: "RPi CCTV Quad2"
    #control: hidden
    entities:
      - sensor.cctvquad2_ipv4_address
      - sensor.cctvquad2_uptime
      - switch.cctvquad2_reboot
      - switch.cctvquad2_shutdown
      - sensor.cctvquad2_cpu_temperature
      - sensor.cctvquad2_cpu_use
      - sensor.cctvquad2_ram_use
      - sensor.cctvquad2_disk_use
      - sensor.cctvquad2_last_seen
