###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   04/15/2017
#   @package        :   Home Assistant
#   @description    :   Bunch of Sensors that are system or environment related
###############################################################################
homeassistant:

shell_command:
  stop_hass: >-
    hassctl stop

  restart_hass: >-
    hassctl restart

  update_hass: >-
    hassctl update-hass && hassctl config && hassctl restart

speedtestdotnet:
  scan_interval:
    hours: 1
  monitored_conditions:
    - ping
    - download
    - upload

sensor:
  - platform: uptime
    name: Home Assistant Up Time
    unit_of_measurement: hours

  - platform: rest
    resource: http://icanhazip.com
    name: external_ip
    value_template: "{{ value }}"
    scan_interval: 86400

  - platform: systemmonitor
    resources:
      - type: disk_free
        arg: /
      - type: memory_free
      - type: processor_use
      - type: ipv4_address
        arg: ens160
      - type: last_boot

  - platform: rest
    resource: https://pypi.python.org/pypi/homeassistant/json
    name: HA Current Version
    value_template: "{{ value_json.info.version }}"

  - platform: command_line
    name: HA Installed Version
    command: /srv/homeassistant/bin/hass --version
    scan_interval: 86400

  - platform: mqtt
    state_topic: "iotlink/domain/winsvr2016/lwt"
    name: "Windows Server"
    value_template: "{{ value }}"

  - platform: mqtt
    state_topic: "iotlink/domain/winsvr2016/windows-monitor/stats/cpu/usage"
    name: "Windows Server CPU Usage"
    value_template: "{{ value }}"

  - platform: mqtt
    state_topic: "iotlink/domain/winsvr2016/windows-monitor/stats/memory/used"
    name: "Windows Server Memory Used"
    value_template: "{{ value }}"

  - platform: mqtt
    state_topic: "iotlink/domain/winsvr2016/windows-monitor/stats/memory/available"
    name: "Windows Server Memory Available"
    value_template: "{{ value }}"

  - platform: mqtt
    state_topic: "iotlink/domain/winsvr2016/windows-monitor/stats/system/idle-time"
    name: "Windows Server Idle Time"
    value_template: "{{ value }}"

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

automation:
  # Notify me when I get a new public IP from my ISP
  ######################################################

  - alias: Notify Of New External IP
    initial_state: true
    trigger:
      platform: state
      entity_id: sensor.external_ip
    condition:
      - condition: template
        value_template: "{% if trigger.from_state and trigger.to_state %} true {% else %} false {% endif %}"
      - condition: template
        value_template: "{% if trigger.from_state.state == 'unknown' %} false {% else %} true {% endif %}"
      - condition: template
        value_template: "{% if trigger.to_state.state == 'unknown' %} false {% else %} true {% endif %}"
      - condition: template
        value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
    action:
      - service: script.notify_me
        data_template:
          message: "Your External IP changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}"

  - alias: Update Available Notification
    initial_state: true
    trigger:
      platform: state
      entity_id: updater.updater
    action:
      - service: script.notify_me
        data: { "message": "New HASS update is available. Please update!" }

  - alias: Remind domain renewal
    initial_state: true
    trigger:
      - platform: numeric_state
        entity_id: sensor.my_domain
        below: 30
    action:
      - service: persistent_notification.create
        data:
          title: "Domain Renew"
          message: "It is time to renew your domain. The domain will expire in {{ states('sensor.my_domain')  }} days."