###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   07/16/2017
#   @package        :   QNAP
#   @description    :   QNAP NAS Package
###############################################################################
homeassistant:
  customize:
    sensor.kalavala_nas_cpu_usage:
      friendly_name: CPU Usage
    sensor.kalavala_nas_memory_usage:
      friendly_name: Memory Usage
    sensor.kalavala_nas_network_up_eth0:
      friendly_name: Network Speed
    sensor.kalavala_nas_network_up_eth1:
      friendly_name: Network Speed
    sensor.kalavala_nas_status:
      friendly_name: NAS Status
    sensor.kalavala_nas_volume_used_datavol1:
      friendly_name: Percent Disk Used
    sensor.kalavala_nas_system_temperature:
      friendly_name: System Temperature
    sensor.kalavala_nas_cpu_temperature:
      friendly_name: CPU Temperature
    sensor.nas_memory_usage_above_80:
      friendly_name: Is Memory Above 80 Percent?
      hidden: true
    sensor.nas_cpu_usage_above_80:
      friendly_name: Is CPU Above 80 Percent?
      hidden: true

    sensor.kalavala_nas_smart_status_drive_0_1:
      friendly_name: Drive 1 Status
    sensor.kalavala_nas_smart_status_drive_0_2:
      friendly_name: Drive 2 Status
    sensor.kalavala_nas_smart_status_drive_0_3:
      friendly_name: Drive 3 Status
    sensor.kalavala_nas_smart_status_drive_0_4:
      friendly_name: Drive 4 Status
    
    sensor.kalavala_nas_temperature_drive_0_1:
      friendly_name: Drive 1 Temperature
    sensor.kalavala_nas_temperature_drive_0_2:
      friendly_name: Drive 2 Temperature
    sensor.kalavala_nas_temperature_drive_0_3:
      friendly_name: Drive 3 Temperature
    sensor.kalavala_nas_temperature_drive_0_4:
      friendly_name: Drive 4 Temperature

sensor:
  - platform: qnap
    host: !secret qnap_hostname
    username: !secret qnap_username
    password: !secret qnap_password
    monitored_conditions:
      - status
      - cpu_usage
      - memory_percent_used
      - network_tx
      - volume_percentage_used
      - system_temp
      - cpu_temp
      - drive_smart_status
      - drive_temp

# The following sensors are required, as the automations with trigger platform `numeric_state`, 
# above a certain value using `above: x` AND `for: 00:05:00` combination is not supported.
#
# Apparently `for:` is ONLY supportd for trigger platform 'state' 
#
# By creating the folowing sensor, we can now have an automation with trigger platform as 'state'
# with a combination of `to:` and `for:`
#
# See the automations 'Notify NAS Memory Usage' and 'Notify NAS CPU Usage' that uses these sensor values

  - platform: template
    sensors:
      nas_memory_usage_above_80:
        value_template: '{{ states.sensor.kalavala_nas_memory_usage.state | int > 80 }}'

  - platform: template
    sensors:
      nas_cpu_usage_above_80:
        value_template: '{{ states.sensor.kalavala_nas_cpu_usage.state | int > 80 }}'

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

  - alias: Notify NAS Status Change
    initial_state: true
    trigger:
      platform: state
      entity_id: 
       - sensor.kalavala_nas_status
    condition:
      - condition: template
        value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
    action:
      - service: script.notify_me
        data_template:
          message: "NAS Status changed to: {{ trigger.to_state.state| upper }}"

  - alias: Notify NAS Drive Status Change
    initial_state: true
    trigger:
      platform: state
      entity_id: 
       - sensor.kalavala_nas_smart_status_drive_01
       - sensor.kalavala_nas_smart_status_drive_02
       - sensor.kalavala_nas_smart_status_drive_03
       - sensor.kalavala_nas_smart_status_drive_04
    condition:
      - condition: template
        value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"       
    action:
      - service: script.notify_me
        data_template:
          message: "NAS Drive state changed to: 
          {{ trigger.to_state.state| upper }} from 
          '{{ trigger.from_state.state| upper }}'"

  - alias: Notify NAS CPU Temperature
    initial_state: true
    trigger:
      platform: numeric_state
      entity_id:  sensor.kalavala_nas_cpu_temperature
      above: 125
    action:
      - service: script.notify_me
        data_template:
          message: "NAS CPU temperature is going crazy hot, needs your attention!!! 
          The current cpu temp is: {{ trigger.to_state.state }}"

  - alias: Notify NAS Disk Usage
    initial_state: true
    trigger:
      platform: numeric_state
      entity_id:  sensor.kalavala_nas_volume_used_datavol1
      above: 80
    action:
      - service: script.notify_me
        data_template:
          message: "Attention! NAS disk usage is above 80 percent. Time to clean up?"

  - alias: Notify NAS Memory Usage
    initial_state: true
    trigger:
      platform: state
      entity_id: sensor.nas_memory_usage_above_80
      to: 'True'
      for: '00:05:00'
    action:
      - service: script.notify_me
        data_template:
          message: "FYI - NAS Memory is above 80 percent for more than 5 minutes."

  - alias: Notify NAS CPU Usage
    initial_state: true
    trigger:
      platform: state
      entity_id:  sensor.nas_cpu_usage_above_80
      to: 'True'
      for: '00:05:00'
    action:
      - service: script.notify_me
        data_template:
          message: "FYI - NAS CPU is above 80 percent for more than 5 minutes."
