#-----THESE Entries are entered in your Configuration.yaml

#---- AQUALINK THERMOSTATS (Freeze, Pool, Spa) -------------

climate:

  - platform: mqtt
    unique_id: aqualink_pool_heater
    name: Pool Heater
    modes:
      - "off"
      - "heat"
    send_if_off: true
    initial: 70
    power_command_topic: "aqualinkd/Pool_Heater/set"
    payload_on: "1"
    payload_off: "0"
    current_temperature_topic: "aqualinkd/Temperature/Pool"
    min_temp: 70
    max_temp: 90
    mode_command_topic: "aqualinkd/Pool_Heater/set"
    mode_state_topic: "aqualinkd/Pool_Heater/enabled"
    mode_state_template: >-
      {% set values1 = { '0':'off', '1':'heat'} %}
      {{ values1[value] if value in values1.keys() else 'off' }}
    temperature_command_topic: "aqualinkd/Pool_Heater/setpoint/set"
    temperature_state_topic: "aqualinkd/Pool_Heater/setpoint"
    temperature_state_template: "{{ value_json }}"

  - platform: mqtt
    unique_id: aqualink_spa_heater
    name: Spa Heater
    modes:
      - "off"
      - "heat"
    send_if_off: true
    initial: 100
    power_command_topic: "aqualinkd/Spa_Heater/set"
    payload_on: "1"
    payload_off: "0"
    current_temperature_topic: "aqualinkd/Temperature/Spa"
    min_temp: 80
    max_temp: 104
    mode_command_topic: "aqualinkd/Spa_Heater/set"
    mode_state_topic: "aqualinkd/Spa_Heater/enabled"
    mode_state_template: >-
      {% set values2 = { '0':'off', '1':'heat'} %}
      {{ values2[value] if value in values2.keys() else 'off' }}
    temperature_command_topic: "aqualinkd/Spa_Heater/setpoint/set"
    temperature_state_topic: "aqualinkd/Spa_Heater/setpoint"
    temperature_state_template: "{{ value_json }}"

  - platform: mqtt
    unique_id: aqualink_freeze_protect
    name: Freeze Protect
    modes:
      - "off"
      - "cool"
    send_if_off: true
    initial: 36
    power_command_topic: "aqualinkd/Freeze_Protect/set"
    payload_on: "1"
    payload_off: "0"
    current_temperature_topic: "aqualinkd/Temperature/Air"
    min_temp: 34
    max_temp: 42
    mode_command_topic: "aqualinkd/Freeze_Protect/set"
    mode_state_topic: "aqualinkd/Freeze_Protect/enabled"
    mode_state_template: >-
      {% set values = { '0':'off', '1':'cool'} %}
      {{ values[value] if value in values.keys() else 'off' }}
    temperature_command_topic: "aqualinkd/Freeze_Protect/setpoint/set"
    temperature_state_topic: "aqualinkd/Freeze_Protect/setpoint"
    temperature_state_template: "{{ value_json }}"
    
#----AQUALINK SWITCHES

switch:

  - platform: mqtt
    unique_id: aqualink_freeze_protect
    name: "Freeze Protection"
    state_topic: "aqualinkd/Freeze_Protect/enabled"
    command_topic: "aqualinkd/Freeze_Protect/set" #NOTE: aqualinkd does not support toggling Freeze Protect due to being a menu option rather than a simple command. Use your RS controller to change the setting. This should be configured as a binary_sensor instead of a switch to make it more obvious but I kept it a switch in case the limitation changes in the future.
    json_attributes_topic: "aqualinkd/Freeze_Protect"
    json_attributes_template: "{{ {'active': value|int} | tojson }}"
    qos: 1
    payload_on: "1"
    payload_off: "0"
    retain: false
    icon: mdi:snowflake
  - platform: mqtt
    unique_id: aqualink_filter_pump
    name: "Filter Pump"
    state_topic: "aqualinkd/Filter_Pump"
    command_topic: "aqualinkd/Filter_Pump/set"
    json_attributes_topic: "aqualinkd/Filter_Pump/delay"
    json_attributes_template: "{{ {'delay': value|int} | tojson }}"
    qos: 1
    payload_on: "1"
    payload_off: "0"
    retain: false
  - platform: mqtt
    unique_id: aqualink_pool_light
    name: "Pool Light"
    state_topic: "aqualinkd/Solar_Heater"
    command_topic: "aqualinkd/Solar_Heater/set"
    json_attributes_topic: "aqualinkd/Solar_Heater/delay"
    json_attributes_template: "{{ {'delay': value|int} | tojson }}"
    qos: 1
    payload_on: "1"
    payload_off: "0"
    retain: false
    icon: mdi:lightbulb
  - platform: mqtt
    unique_id: aqualink_spa
    name: "Spa"
    state_topic: "aqualinkd/Spa"
    command_topic: "aqualinkd/Spa/set"
    json_attributes_topic: "aqualinkd/Spa/delay"
    json_attributes_template: "{{ {'delay': value|int} | tojson }}"
    qos: 1
    payload_on: "1"
    payload_off: "0"
    retain: false
    icon: mdi:hot-tub
  - platform: mqtt
    unique_id: aqualink_cleaner
    name: "Cleaner"
    state_topic: "aqualinkd/Aux_1"
    command_topic: "aqualinkd/Aux_1/set"
    json_attributes_topic: "aqualinkd/Aux_1/delay"
    json_attributes_template: "{{ {'delay': value|int} | tojson }}"
    qos: 1
    payload_on: "1"
    payload_off: "0"
    retain: false
    icon: mdi:broom
  - platform: mqtt
    unique_id: aqualink_spa_blower
    name: "Spa Blower"
    state_topic: "aqualinkd/Aux_2"
    command_topic: "aqualinkd/Aux_2/set"
    json_attributes_topic: "aqualinkd/Aux_2/delay"
    json_attributes_template: "{{ {'delay': value|int} | tojson }}"
    qos: 1
    payload_on: "1"
    payload_off: "0"
    retain: false  
    icon: mdi:fan
  - platform: mqtt
    unique_id: aqualink_spa_light
    name: "Spa Light"
    state_topic: "aqualinkd/Aux_3"
    command_topic: "aqualinkd/Aux_3/set"
    json_attributes_topic: "aqualinkd/Aux_3/delay"
    json_attributes_template: "{{ {'delay': value|int} | tojson }}"
    qos: 1
    payload_on: "1"
    payload_off: "0"
    retain: false
    icon: mdi:lightbulb
  - platform: mqtt
    unique_id: aqualink_pool_heater
    name: "Pool Heater"
    state_topic: "aqualinkd/Pool_Heater/enabled"
    command_topic: "aqualinkd/Pool_Heater/set"
    json_attributes_topic: "aqualinkd/Pool_Heater"
    json_attributes_template: "{{ {'active': value|int} | tojson }}"
    qos: 1
    payload_on: "1"
    payload_off: "0"
    retain: false
    icon: mdi:fire
  - platform: mqtt
    unique_id: aqualink_spa_heater
    name: "Spa Heater"
    state_topic: "aqualinkd/Spa_Heater/enabled"
    command_topic: "aqualinkd/Spa_Heater/set"
    json_attributes_topic: "aqualinkd/Spa_Heater"
    json_attributes_template: "{{ {'active': value|int} | tojson }}"
    qos: 1
    payload_on: "1"
    payload_off: "0"
    retain: false
    icon: mdi:fire    

#----AQUALINK SENSORS

sensor:

  - platform: mqtt
    unique_id: aqualink_pool_state
    state_topic: "aqualinkd/Service_Mode"
    name: "Aqualink Mode"
    value_template: "{{ value }}"    
    icon: mdi:wrench
  - platform: mqtt
    unique_id: aqualink_air_temp
    state_topic: "aqualinkd/Temperature/Air"
    name: "Air Temp"
    value_template: "{{ value_json }}"    
    unit_of_measurement: "°F"
    device_class: temperature
  - platform: mqtt
    unique_id: aqualink_spa_temp
    state_topic: "aqualinkd/Temperature/Spa"
    name: "Spa Temp"
    value_template: "{{ value_json }}"    
    unit_of_measurement: "°F"  
    device_class: temperature
  - platform: mqtt
    unique_id: aqualink_pool_temp
    state_topic: "aqualinkd/Temperature/Pool"
    name: "Pool Temp"
    value_template: "{{ value_json }}"    
    unit_of_measurement: "°F"     
    device_class: temperature
  - platform: mqtt
    unique_id: aqualink_pool_heater_temp
    state_topic: "aqualinkd/Pool_Heater/setpoint"
    name: "Pool Heater Temp"
    value_template: "{{ value_json }}"    
    unit_of_measurement: "°F"     
    device_class: temperature
  - platform: mqtt
    unique_id: aqualink_spa_heater_temp
    state_topic: "aqualinkd/Spa_Heater/setpoint"
    name: "Spa Heater Temp"
    value_template: "{{ value_json }}"    
    unit_of_measurement: "°F"        
    device_class: temperature

  - platform: template
    sensors:
      #this sensor simulates a numeric state, but since we only get NORMAL or LOW from Aqualink we can't do a true range
      aqualink_battery_level:
        value_template: >-
            {%- if states('binary_sensor.aqualink_battery') == "off" %}
              80
            {% else %}
              5
            {%- endif %}
        icon_template: >-
            {%- if states('binary_sensor.aqualink_battery') == "off" %}
              mdi:battery
            {% else %}
              mdi:battery-alert
            {%- endif %}        
        friendly_name: 'Aqualink Battery Level'
        unit_of_measurement: '%'
        device_class: battery
      #this sensor translates the service mode values
      aqualink_run_mode:
        value_template: >-
          {%- if states('sensor.aqualink_mode') == '0' %}
            AUTO
          {% elif states('sensor.aqualink_mode') == '1' %}
            SERVICE
          {% elif states('sensor.aqualink_mode') == '2' %}
            TIMEOUT
          {%- endif %}
        icon_template: >-
          {%- if states('sensor.aqualink_mode') == '0' %}
            mdi:robot
          {% elif states('sensor.aqualink_mode') == '1' %}
            mdi:cog
          {% elif states('sensor.aqualink_mode') == '2' %}
            mdi:clock
          {%- endif %}
        friendly_name: 'Aqualink Run Mode'
    
#----AQUALINK BINARY SENSORS    

binary_sensor:

- platform: mqtt
  unique_id: aqualink_pool_heater
  state_topic: "aqualinkd/Pool_Heater"
  name: "Pool Heating"
  qos: 0
  payload_on: "1"
  payload_off: "0"
- platform: mqtt
  unique_id: aqualink_spa_heater
  state_topic: "aqualinkd/Spa_Heater"
  name: "Spa Heating"
  qos: 0
  payload_on: "1"
  payload_off: "0"    
- platform: mqtt
  unique_id: aqualink_freeze_protection
  state_topic: "aqualinkd/Freeze_Protect"
  name: "Freeze Protecting"
  qos: 0
  payload_on: "1"
  payload_off: "0"
- platform: mqtt
  unique_id: aqualink_alive
  state_topic: "aqualinkd/Alive"
  name: "AqualinkD Alive"
  qos: 0
  payload_on: "1"
  payload_off: "0"
- platform: mqtt
  unique_id: aqualink_battery
  state_topic: "aqualinkd/Battery"
  name: "Aqualink Battery"
  qos: 0
  payload_on: "0"
  payload_off: "1"
  device_class: battery
- platform: mqtt
  unique_id: filter_pump_delay
  state_topic: "aqualinkd/Filter_Pump/delay"
  name: "Filter Pump Delay"
  qos: 0
  payload_on: "1"
  payload_off: "0"
- platform: mqtt
  unique_id: spa_delay
  state_topic: "aqualinkd/Spa/delay"
  name: "Spa Delay"
  qos: 0
  payload_on: "1"
  payload_off: "0"
- platform: mqtt
  unique_id: pool_light_delay
  state_topic: "aqualinkd/Solar_Heater/delay"
  name: "Pool Light Delay"
  qos: 0
  payload_on: "1"
  payload_off: "0"
- platform: mqtt
  unique_id: cleaner_delay
  state_topic: "aqualinkd/Aux_1/delay"
  name: "Cleaner Delay"
  qos: 0
  payload_on: "1"
  payload_off: "0"
- platform: mqtt
  unique_id: spa_blower_delay
  state_topic: "aqualinkd/Aux_2/delay"
  name: "Spa Blower Delay"
  qos: 0
  payload_on: "1"
  payload_off: "0"
- platform: mqtt
  unique_id: spa_light_delay
  state_topic: "aqualinkd/Aux_3/delay"
  name: "Spa Light Delay"
  qos: 0
  payload_on: "1"
  payload_off: "0"

#-----Automation Entries---------

automation:

  - alias: 'Notify of Low Pool Battery'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.battery_check
      from: 'OK' 
      to: 'LOW'
    condition:
      condition: time
      after: '08:15:00'
      before: '16:00:00'
    action:
# Change notify.ios.brianphone to whatever your phone name is...
      - service: notify.ios_brianphone 
        data:
          message: 'The Aqualink Pool Controller Battery is LOW'
          title: 'Home Assistant'
          
  - id: disable-heater-when-pool-off
    alias: Disable heater when pool off
    initial_state: on
    trigger:
      platform: state
      entity_id: switch.filter_pump
      from: 'on'
      to: 'off'
    action:
      - service: switch.turn_off
        entity_id: 
          - switch.pool_heater
          - switch.spa_heater
