###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   04/15/2017
#   @package        :   TV
#   @description    :   Tv Automations
###############################################################################
binary_sensor:
  - platform: mqtt
    name: Sharp TV
    state_topic: "/home/sharptv"
    payload_on: "on"
    payload_off: "off"
    value_template: "{{ value }}"

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

  - alias: TV Status ON
    initial_state: true
    trigger:
      platform: state
      entity_id: remote.streaming_stick_4k
      to: "on"
    action:
      - service: mqtt.publish
        data_template:
          topic: "/home/sharptv"
          payload: "on"
          retain: true

  - alias: TV Status OFF
    initial_state: true
    trigger:
      platform: state
      entity_id: remote.streaming_stick_4k
      to: "unavailable"
    action:
      - service: mqtt.publish
        data_template:
          topic: "/home/sharptv"
          payload: "off"
          retain: true


  # 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

  # 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
