media_player:
  - platform: universal
    name: SMART TV
    children:
      - media_player.kodi_smarttv
    state_template: >
      {% if is_state('media_player.kodi_smarttv', 'idle') and is_state('switch.smart_tv_power_switch', 'off') %}
        off
      {% else %}
        {{ states('media_player.kodi_smarttv') }}
      {% endif %}
    commands:
      turn_on:
        service: media_player.turn_on
        target:
          entity_id: media_player.kodi_smarttv
      turn_off:
        service: media_player.turn_off
        target:
          entity_id: media_player.kodi_smarttv
    attributes:
      is_volume_muted: media_player.kodi|is_volume_muted
      volume_level: media_player.kodi|volume_level
    browse_media_entity: media_player.kodi_smarttv
    device_class: tv
    unique_id: smarttv_universal

mqtt:
  - switch:
      unique_id: smarttv_power_switch
      name: "Power Switch"
      state_topic: "smarttv/state"
      command_topic: "smarttv/command"
      payload_on: "power_on"
      payload_off: "power_off"
      value_template: "{{ value_json.power_state }}"
      state_on: "on"
      state_off: "off"
      icon: mdi:television-classic
      device: &smarttv_device
        name: "SMARTTV2MQTT"
        identifiers: "SMART TV SPNL-6065"
        manufacturer: "SMART Technologies"
        model: "SPNL-6065"
        serial_number: "K012HW21R0559"

  - switch:
      unique_id: smarttv_mute_or_unmute
      name: "Mute"
      state_topic: "smarttv/state"
      command_topic: "smarttv/command"
      value_template: "{{ value_json.mute }}"
      payload_on: "mute"
      payload_off: "unmute"
      state_on: "on"
      state_off: "off"
      icon: mdi:volume-off
      device:
        <<: *smarttv_device

  - number:
      unique_id: smarttv_volume
      name: "Volume"
      command_topic: "smarttv/command"
      state_topic: "smarttv/state"
      value_template: "{{ value_json.volume }}"
      command_template: "SMARTTV_SET_VOLUME={{ value }}"
      step: 1
      min: 0
      max: 100
      mode: slider
      icon: mdi:volume-high
      optimistic: true
      device:
        <<: *smarttv_device

  - button:
      unique_id: smarttv_get_powerstate
      name: "get_powerstate"
      command_topic: "smarttv/command"
      payload_press: "get_powerstate"
      device:
        <<: *smarttv_device

  - button:
      unique_id: smarttv_get_volume
      name: "get_volume"
      command_topic: "smarttv/command"
      payload_press: "get_volume"
      device:
        <<: *smarttv_device

  - button:
      unique_id: smarttv_get_input
      name: "get_input"
      command_topic: "smarttv/command"
      payload_press: "get_input"
      device:
        <<: *smarttv_device

  - button:
      unique_id: smarttv_get_mute
      name: "get_mute"
      command_topic: "smarttv/command"
      payload_press: "get_mute"
      device:
        <<: *smarttv_device

  - button:
      unique_id: smarttv_set_ops_audio
      name: "Set OPS Audio"
      command_topic: "smarttv/set_tv_audio"
      payload_press: "hdmi"
      icon: mdi:cast-audio
      device:
        <<: *smarttv_device

  - button:
      unique_id: smarttv_reboot_ops
      name: "Reboot OPS"
      command_topic: "smarttv/reboot_ops"
      payload_press: "reboot_ops"
      icon: mdi:restart
      device:
        <<: *smarttv_device

  - button:
      unique_id: smarttv_kodi_fullscreen
      name: "Kodi Fullscreen"
      command_topic: "smarttv/smarttv_kodi_fullscreen"
      payload_press: "smarttv_kodi_fullscreen"
      icon: mdi:restart
      device:
        <<: *smarttv_device

  - select:
      unique_id: smarttv_get_mute
      name: "Input Select"
      command_topic: "smarttv/command"
      state_topic: "smarttv/state"
      value_template: "{{ value_json.input }}"
      icon: mdi:import
      command_template: >-
        {% if value == "HDMI" %}
          "set_input_hdmi"
        {% elif value == "OPS" %}
          "set_input_ops"
        {% endif %}
      options:
        - "OPS"
        - "HDMI"
      device:
        <<: *smarttv_device

# smart_technologies:
#   username: username
#   password: password
#   host: 0.0.0.0