###############################################################################
#  @author         :   Mahasri Kalavala
#  @date           :   12/27/2017
#  @package        :   Pi Admin Package
#  @description    :   Got way too many Raspberry Pi's and this package is to
#                      help me with managing them via Home Assistant.
#
#  How Does It Work?
#
#  Each Raspberry Pi runs a service (a python program) locally that listens for
#  commands via MQTT on a pre-defined topic. This package publishes commands
#  to MQTT requesting each Raspberry Pi to respond and or return data.
#  When the responses are received (on a differet topic), the sensors gets
#  updated automatically and are displayed.
#
################################################################################

homeassistant:
  customize:
    script.restart_all_raspberrypis:
      friendly_name: Restart All Raspberry Pis
    script.restart_all_snapclients:
      friendly_name: Restart All Snapserver Clients
    script.shutdown_all_raspberrypis:
      friendly_name: Shutdown All Raspberry Pis
    script.query_wifi_info:
      friendly_name: Query WiFi Info
    script.query_disk_info:
      friendly_name: Query Disk Info

input_select:
  raspberry_pis:
    name: All Raspberry Pis
    options:
      - Select One
      - pi_basement
      - pi_theater
      - pi_kitchen
      - pi_familyroom
      - pi_frontroom
      - pi_garage
      - pi_guest1
      - pi_guest2
      - pi_masterbedroom
    initial: Select One
    icon: mdi:pig

  rpi_commands:
    name: RPi Commands
    options:
      - Select One
      - Restart Server
      - Shutdown
      - Restart Snapcast
    initial: Select One
    icon: mdi:pig

sensor:
  ## WiFi Information related sensors
  - platform: mqtt
    state_topic: "/wifi/pi_basement"
    name: Basement Pi Signal Level
    value_template: '{{ value_json["Signal Level"]}}'
    unit_of_measurement: dBm
  - platform: mqtt
    state_topic: "/wifi/pi_basement"
    name: Basement Pi Link Quality
    value_template: '{{ value_json["Link Quality"]}}'

  - platform: mqtt
    state_topic: "/wifi/pi_theater"
    name: Theater Pi Signal Level
    value_template: '{{ value_json["Signal Level"]}}'
    unit_of_measurement: dBm
  - platform: mqtt
    state_topic: "/wifi/pi_theater"
    name: Theater Pi Link Quality
    value_template: '{{ value_json["Link Quality"]}}'

  - platform: mqtt
    state_topic: "/wifi/pi_kitchen"
    name: Kitchen Pi Signal Level
    value_template: '{{ value_json["Signal Level"]}}'
    unit_of_measurement: dBm
  - platform: mqtt
    state_topic: "/wifi/pi_kitchen"
    name: Kitchen Pi Link Quality
    value_template: '{{ value_json["Link Quality"]}}'

  - platform: mqtt
    state_topic: "/wifi/pi_familyroom"
    name: Family Room Pi Signal Level
    value_template: '{{ value_json["Signal Level"]}}'
    unit_of_measurement: dBm
  - platform: mqtt
    state_topic: "/wifi/pi_familyroom"
    name: Family Room Pi Link Quality
    value_template: '{{ value_json["Link Quality"]}}'

  - platform: mqtt
    state_topic: "/wifi/pi_frontroom"
    name: Front Room Pi Signal Level
    value_template: '{{ value_json["Signal Level"]}}'
    unit_of_measurement: dBm
  - platform: mqtt
    state_topic: "/wifi/pi_frontroom"
    name: Front Room Pi Link Quality
    value_template: '{{ value_json["Link Quality"]}}'

  - platform: mqtt
    state_topic: "/wifi/pi_garage"
    name: Garage Pi Signal Level
    value_template: '{{ value_json["Signal Level"]}}'
    unit_of_measurement: dBm
  - platform: mqtt
    state_topic: "/wifi/pi_garage"
    name: Garage Pi Link Quality
    value_template: '{{ value_json["Link Quality"]}}'

  - platform: mqtt
    state_topic: "/wifi/pi_guest1"
    name: Guest 1 Pi Signal Level
    value_template: '{{ value_json["Signal Level"]}}'
    unit_of_measurement: dBm
  - platform: mqtt
    state_topic: "/wifi/pi_guest1"
    name: Guest 1 Pi Link Quality
    value_template: '{{ value_json["Link Quality"]}}'

  - platform: mqtt
    state_topic: "/wifi/pi_guest2"
    name: Guest 2 Pi Signal Level
    value_template: '{{ value_json["Signal Level"]}}'
    unit_of_measurement: dBm
  - platform: mqtt
    state_topic: "/wifi/pi_guest2"
    name: Guest 2 Pi Link Quality
    value_template: '{{ value_json["Link Quality"]}}'

  - platform: mqtt
    state_topic: "/wifi/pi_masterbedroom"
    name: master Bedroom Pi Signal Level
    value_template: '{{ value_json["Signal Level"]}}'
    unit_of_measurement: dBm
  - platform: mqtt
    state_topic: "/wifi/pi_masterbedroom"
    name: Master Bedroom Pi Link Quality
    value_template: '{{ value_json["Link Quality"]}}'

  ## Disk Information related Sensors
  - platform: mqtt
    state_topic: "/disk/pi_basement"
    name: Basement Pi Available Disk
    value_template: '{{ value_json["Available"]}}'
  - platform: mqtt
    state_topic: "/disk/pi_basement"
    name: Basement Pi Disk Used
    value_template: '{{ value_json["% Used"]}}'

  - platform: mqtt
    state_topic: "/disk/pi_theater"
    name: Theater Pi Available Disk
    value_template: '{{ value_json["Available"]}}'
  - platform: mqtt
    state_topic: "/disk/pi_theater"
    name: Theater Pi Disk Used
    value_template: '{{ value_json["% Used"]}}'

  - platform: mqtt
    state_topic: "/disk/pi_kitchen"
    name: Kitchen Pi Available Disk
    value_template: '{{ value_json["Available"]}}'
  - platform: mqtt
    state_topic: "/disk/pi_kitchen"
    name: Kitchen Pi Disk Used
    value_template: '{{ value_json["% Used"]}}'

  - platform: mqtt
    state_topic: "/disk/pi_familyroom"
    name: Family Room Pi Available Disk
    value_template: '{{ value_json["Available"]}}'
  - platform: mqtt
    state_topic: "/disk/pi_familyroom"
    name: Family Room Pi Disk Used
    value_template: '{{ value_json["% Used"]}}'

  - platform: mqtt
    state_topic: "/disk/pi_frontroom"
    name: Front Room Pi Available Disk
    value_template: '{{ value_json["Available"]}}'
  - platform: mqtt
    state_topic: "/disk/pi_frontroom"
    name: Front Room Pi Disk Used
    value_template: '{{ value_json["% Used"]}}'

  - platform: mqtt
    state_topic: "/disk/pi_garage"
    name: Garage Pi Available Disk
    value_template: '{{ value_json["Available"]}}'
  - platform: mqtt
    state_topic: "/disk/pi_garage"
    name: Garage Pi Disk Used
    value_template: '{{ value_json["% Used"]}}'

  - platform: mqtt
    state_topic: "/disk/pi_guest1"
    name: Guest 1 Pi Available Disk
    value_template: '{{ value_json["Available"]}}'
  - platform: mqtt
    state_topic: "/disk/pi_guest1"
    name: Guest 1 Pi Disk Used
    value_template: '{{ value_json["% Used"]}}'

  - platform: mqtt
    state_topic: "/disk/pi_guest2"
    name: Guest 2 Pi Available Disk
    value_template: '{{ value_json["Available"]}}'
  - platform: mqtt
    state_topic: "/disk/pi_guest2"
    name: Guest 2 Pi Disk Used
    value_template: '{{ value_json["% Used"]}}'

  - platform: mqtt
    state_topic: "/disk/pi_masterbedroom"
    name: Master Bedroom Pi Available Disk
    value_template: '{{ value_json["Available"]}}'
  - platform: mqtt
    state_topic: "/disk/pi_masterbedroom"
    name: Master Bedroom Pi Disk Used
    value_template: '{{ value_json["% Used"]}}'

script:
  restart_all_raspberrypis:
    sequence:
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_basement"
          payload: CMD_REBOOT_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_theater"
          payload: CMD_REBOOT_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_kitchen"
          payload: CMD_REBOOT_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_familyroom"
          payload: CMD_REBOOT_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_frontroom"
          payload: CMD_REBOOT_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_garage"
          payload: CMD_REBOOT_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_guest1"
          payload: CMD_REBOOT_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_guest2"
          payload: CMD_REBOOT_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_masterbedroom"
          payload: CMD_REBOOT_PI
          retain: false

  restart_all_snapclients:
    sequence:
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_basement"
          payload: CMD_RESTART_SNAPCLIENT
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_theater"
          payload: CMD_RESTART_SNAPCLIENT
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_kitchen"
          payload: CMD_RESTART_SNAPCLIENT
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_familyroom"
          payload: CMD_RESTART_SNAPCLIENT
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_frontroom"
          payload: CMD_RESTART_SNAPCLIENT
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_garage"
          payload: CMD_RESTART_SNAPCLIENT
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_guest1"
          payload: CMD_RESTART_SNAPCLIENT
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_guest2"
          payload: CMD_RESTART_SNAPCLIENT
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_masterbedroom"
          payload: CMD_RESTART_SNAPCLIENT
          retain: false

  shutdown_all_raspberrypis:
    sequence:
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_basement"
          payload: CMD_SHUTDOWN_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_theater"
          payload: CMD_SHUTDOWN_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_kitchen"
          payload: CMD_SHUTDOWN_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_familyroom"
          payload: CMD_SHUTDOWN_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_frontroom"
          payload: CMD_SHUTDOWN_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_garage"
          payload: CMD_SHUTDOWN_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_guest1"
          payload: CMD_SHUTDOWN_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_guest2"
          payload: CMD_SHUTDOWN_PI
          retain: false
      - service: mqtt.publish
        data_template:
          topic: "/server/pi_masterbedroom"
          payload: CMD_SHUTDOWN_PI
          retain: false

  # Script to Query Disk Information - Publishes message 'CMD_REPORT_WIFI_DETAILS'
  # to MQTT, so that the message gets received by individual Raspberry Pi
  ###############################################################################
  query_wifi_info:
    sequence:
      - service: mqtt.publish
        data:
          topic: /server/pi_basement
          payload: "CMD_REPORT_WIFI_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_theater
          payload: "CMD_REPORT_WIFI_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_kitchen
          payload: "CMD_REPORT_WIFI_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_familyroom
          payload: "CMD_REPORT_WIFI_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_frontroom
          payload: "CMD_REPORT_WIFI_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_garage
          payload: "CMD_REPORT_WIFI_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_guest1
          payload: "CMD_REPORT_WIFI_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_guest2
          payload: "CMD_REPORT_WIFI_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_masterbedroom
          payload: "CMD_REPORT_WIFI_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pizero_green
          payload: "CMD_REPORT_WIFI_DETAILS"
          retain: false

  # Script to Query Disk Information - Publishes message 'CMD_REPORT_DISK_DETAILS'
  # to MQTT, so that the message gets received by individual Raspberry Pi
  ###############################################################################
  query_disk_info:
    sequence:
      - service: mqtt.publish
        data:
          topic: /server/pi_basement
          payload: "CMD_REPORT_DISK_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_theater
          payload: "CMD_REPORT_DISK_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_kitchen
          payload: "CMD_REPORT_DISK_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_familyroom
          payload: "CMD_REPORT_DISK_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_frontroom
          payload: "CMD_REPORT_DISK_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_garage
          payload: "CMD_REPORT_DISK_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_guest1
          payload: "CMD_REPORT_DISK_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_guest2
          payload: "CMD_REPORT_DISK_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pi_masterbedroom
          payload: "CMD_REPORT_DISK_DETAILS"
          retain: false
      - service: mqtt.publish
        data:
          topic: /server/pizero_green
          payload: "CMD_REPORT_DISK_DETAILS"
          retain: false

###############################################################################
#                _                        _   _
#     /\        | |                      | | (_)
#    /  \  _   _| |_ ___  _ __ ___   __ _| |_ _  ___  _ __  ___
#   / /\ \| | | | __/ _ \| '_ ` _ \ / _` | __| |/ _ \| '_ \/ __|
#  / ____ \ |_| | || (_) | | | | | | (_| | |_| | (_) | | | \__ \
# /_/    \_\__,_|\__\___/|_| |_| |_|\__,_|\__|_|\___/|_| |_|___/
#
###############################################################################

# Automations: Various automations to query and issue commands to Raspberry Pis
###############################################################################
automation:
  # Queries WiFi Signal Level and Link Quality - it loads during startup, and
  # updates once every 30 minutes
  ###############################################################################
  - alias: Query RPi Wifi Data
    initial_state: true
    trigger:
      - platform: time_pattern
        minutes: "/30"
      - platform: homeassistant
        event: start
    action:
      - service: script.query_wifi_info

  # Queries Disk Available and % Used - it loads during startup, and
  # updates once every 30 minutes
  ###############################################################################
  - alias: Query RPi Disk Data
    initial_state: true
    trigger:
      - platform: time_pattern
        minutes: "/30"
      - platform: homeassistant
        event: start
    action:
      - service: script.query_disk_info

  # Issues commands to Raspberry Pi via MQTT - On Demand
  ###############################################################################
  - alias: Raspberry Pi Command
    initial_state: true
    trigger:
      - platform: state
        entity_id: input_select.rpi_commands
    condition:
      - condition: template
        value_template: "{{ states('input_select.raspberry_pis') | lower != 'select one' }}"
      - condition: template
        value_template: "{{ states('input_select.rpi_commands') | lower != 'select one' }}"
    action:
      service: mqtt.publish
      data_template:
        retain: false
        topic: "{{ '/server/' ~ states('input_select.raspberry_pis') }}"
        payload: >-
          {% if states('input_select.rpi_commands') | lower == "restart server" %}
            CMD_REBOOT_PI
          {% elif states('input_select.rpi_commands') | lower == "restart snapcast" %}
            CMD_RESTART_SNAPCLIENT
          {% else %}
            CMD_SHUTDOWN_PI
          {% endif %}
