###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   04/15/2017
#   @package        :   TV
#   @description    :   Tv Automations
###############################################################################

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

# Dim Family Room Lights When TV is Turned ON
###############################################################################
  - alias: TV Dim Indoor Lights when TV is ON
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.sharp_tv
      from: 'off'
      to: 'on'
    condition:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
      - condition: state
        entity_id: input_boolean.light_automations
        state: 'on'
    action:
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_1
          brightness: 5
          color_temp: 154
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_2
          brightness: 5
          color_temp: 154
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_3
          brightness: 5
          color_temp: 154
      - service: mqtt.publish
        data:
          topic: /home/tv/backlight
          payload: 'on'
          retain: true
  
# Restore Family Room Lights to TV mode
###############################################################################
  - alias: TV Restore Family Room Lights To TV Mode
    initial_state: true
    trigger:
      - platform: state
        entity_id: group.family_room_lights
        from: 'off'
        to: 'on'
    condition:
      - condition: state
        entity_id: binary_sensor.sharp_tv
        state: 'on'
      - condition: state
        entity_id: input_boolean.light_automations
        state: 'on'
    action:
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_1
          brightness: 5
          color_temp: 154
          transition: 5
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_2
          brightness: 5
          color_temp: 154
          transition: 5
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_3
          brightness: 5
          color_temp: 154
          transition: 5
  
# Turn Backlights Off when TV is turned OFF
###############################################################################
  - alias: TV Backlight OFF when TV is OFF
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.sharp_tv
      to: 'off'
    action:
      - service: mqtt.publish
        data:
          topic: /home/tv/backlight
          payload: 'off'
          retain: true
  
# Turn TV backlights when TV is turned ON
###############################################################################
  - alias: TV Backlight is ON when TV is ON
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.sharp_tv
      to: 'on'
    action:
      - service: mqtt.publish
        data:
          topic: /home/tv/backlight
          payload: 'on'
          retain: true

# Turn back Family room lights when TV is switched OFF
###############################################################################
  - alias: TV Turn Lights ON When TV is turned OFF
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.sharp_tv
      to: 'off'
    condition:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
      - condition: state
        entity_id: input_boolean.light_automations
        state: 'on'
    action:
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_1
          brightness: 255
          color_temp: 154
          transition: 5
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_2
          brightness: 255
          color_temp: 154
          transition: 5
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_3
          brightness: 255
          color_temp: 154
          transition: 5  
      - service: mqtt.publish
        data:
          topic: /home/tv/backlight
          payload: 'off'
          retain: true