# NOTE: THIS CODE WORKS! I NO LONGER USE RPI FOR MY HA ANYMORE. HENCE THE CODE IS COMMENTED.
# PLEASE FEEL FREE TO COPY THE CODE AND UNCOMMENT IT, AND SHOULD WORK OUT OF THE BOX

# ###############################################################################
# #   @author         :   Mahasri Kalavala
# #   @date           :   04/15/2017
# #   @package        :   RF Outlets
# #   @description    :   Etekcity RF Outlet Sensors and Automations
# ###############################################################################
# homeassistant:
#   customize:
  
#     group.etekcity_rf_switches:
#       order: 13

#     switch.switch_one:
#       friendly_name: Switch One
#     switch.switch_two:
#       friendly_name: Switch Two
#     switch.switch_three:
#       friendly_name: Switch Three
#     switch.switch_four:
#       friendly_name: Switch Four
#     switch.switch_five:
#       friendly_name: Switch Five

#     sensor.rf_switch_one:
#       hidden: true
#     sensor.rf_switch_two:
#       hidden: true
#     sensor.rf_switch_three:
#       hidden: true
#     sensor.rf_switch_four:
#       hidden: true
#     sensor.rf_switch_five:
#       hidden: true

# ###############################################################################
# #  RF Switches
# ###############################################################################
# switch:    
#   - platform: rpi_rf
#     gpio: 17
#     switches:
#       switch_one:
#         code_on: !secret rf_switch_1_code_on
#         code_off: !secret rf_switch_1_code_off
#         pulselength: !secret rf_switch_1_pulselength
#       switch_two:
#         code_on: !secret rf_switch_2_code_on
#         code_off: !secret rf_switch_2_code_off
#         pulselength: !secret rf_switch_2_pulselength
#       switch_three:
#         code_on: !secret rf_switch_3_code_on
#         code_off: !secret rf_switch_3_code_off
#         pulselength: !secret rf_switch_3_pulselength
#       switch_four:
#         code_on: !secret rf_switch_4_code_on
#         code_off: !secret rf_switch_4_code_off
#         pulselength: !secret rf_switch_4_pulselength
#       switch_five:
#         code_on: !secret rf_switch_5_code_on
#         code_off: !secret rf_switch_5_code_off
#         pulselength: !secret rf_switch_5_pulselength

# ###############################################################################
# #  Sensor Definitions
# ###############################################################################
# sensor:
#   - platform: mqtt
#     state_topic: "/home/rfswitches/switch_one"
#     name: "RF Switch One"
#     value_template: "{{ value }}"
    
#   - platform: mqtt
#     state_topic: "/home/rfswitches/switch_two"
#     name: "RF Switch Two"
#     value_template: "{{ value }}"
    
#   - platform: mqtt
#     state_topic: "/home/rfswitches/switch_three"
#     name: "RF Switch Three"
#     value_template: "{{ value }}"
    
#   - platform: mqtt
#     state_topic: "/home/rfswitches/switch_four"
#     name: "RF Switch Four"
#     value_template: "{{ value }}"
    
#   - platform: mqtt
#     state_topic: "/home/rfswitches/switch_five"
#     name: "RF Switch Five"
#     value_template: "{{ value }}"

# ###############################################################################
# #  Automation - Saves Switch State to MQTT
# ###############################################################################
# automation:

# # When the switch state changes (programatically or by UI), save the state to MQTT
#   - alias: Save RF Switch State
#     initial_state: true
#     hide_entity: true
#     trigger:
#       platform: state
#       entity_id: 
#         - switch.switch_one
#         - switch.switch_two
#         - switch.switch_three
#         - switch.switch_four
#         - switch.switch_five
#     action:
#       service: mqtt.publish
#       data_template:
#         topic: "/home/rfswitches/{{ trigger.entity_id.split('.')[1] }}"
#         retain: true
#         qos: 1
#         payload: '{{ trigger.to_state.state }}'

# # When the switch state is updated in MQTT (for remove integration), reload the Switch to that state
#   - alias: Sync MQTT and Switch States
#     initial_state: true
#     hide_entity: true
#     trigger:
#       platform: state
#       entity_id: 
#         - sensor.rf_switch_one
#         - sensor.rf_switch_two
#         - sensor.rf_switch_three
#         - sensor.rf_switch_four
#         - sensor.rf_switch_five
#     action:
#       - service_template: >
#           {%- if trigger.to_state.state | lower == "on" -%}
#               homeassistant.turn_on
#           {%- else -%}
#               homeassistant.turn_off
#           {%- endif -%}
#         data_template:
#           entity_id: 'switch.switch_{{ trigger.entity_id.split(".")[1].split("_")[2]}}'

# ###############################################################################
# # Startup event - restores states of dropdowns, and RF Switches...etc
# ###############################################################################
#   - alias: Restore RF Switch Status on Startup
#     initial_state: true
#     hide_entity: true
#     trigger:
#       platform: homeassistant
#       event: start
#     action:
#       - delay:
#           minutes: 1
#       - service_template: >
#           {%- if states.sensor.rf_switch_one.state | lower == "on" -%}
#               homeassistant.turn_on
#           {%- else -%}
#               homeassistant.turn_off
#           {%- endif -%}    
#         entity_id: switch.switch_one
#       - service_template: >
#           {%- if states.sensor.rf_switch_two.state | lower == "on" -%}
#               homeassistant.turn_on
#           {%- else  -%}
#               homeassistant.turn_off
#           {%- endif -%}    
#         entity_id: switch.switch_two
#       - service_template: >
#           {%- if states.sensor.rf_switch_three.state | lower == "on" -%}
#               homeassistant.turn_on
#           {%- else  -%}
#               homeassistant.turn_off
#           {%- endif -%}    
#         entity_id: switch.switch_three
#       - service_template: >
#           {%- if states.sensor.rf_switch_four.state | lower == "on" -%}
#               homeassistant.turn_on
#           {%- else  -%}
#               homeassistant.turn_off
#           {%- endif -%}    
#         entity_id: switch.switch_four
#       - service_template: >
#           {%- if states.sensor.rf_switch_five.state | lower == "on" -%}
#               homeassistant.turn_on
#           {%- else  -%}
#               homeassistant.turn_off
#           {%- endif -%}
#         data:
#           entity_id: switch.switch_five