With the media center remote now supporting a basic Kodi integration via webservice calls, the next step is to expand it to send RS-232 control codes to a connected television.

The goals of this stage are to:

A subtle but important note is that the RS-232 module is connected to the ESP8266 in "crossover" mode, to simulate a null-modem cable connection.

Television UART codes

I'm using an older LG plasma TV, but the control codes found in the LG UV340C instruction manual appear to work on mine. A few codes stand out as being more useful for remote controller:

ka 0 00
Turn the television on
ka 0 01
Turn the television off
xb 0 90
Set the input to HDMI 1
xb 0 91
Set the input to HDMI 2

ESPHome configuration

Highlighting the differences from before, this configuration:

  1. Runs an automation when an "lg" infrared signal is received that updates a "command_for_television" sensor to track the command to be sent to the television
  2. Runs an automation when the "command_for_television" is updated, which sends a UART code to the television.

media-center-remote.yaml

# ...

uart:
  tx_pin: GPIO1
  rx_pin: GPIO3
  baud_rate: 9600

text_sensor:
# ...
- platform: template
  id: command_for_television
  internal: true # Do not expose to Home Assistant
  on_value:
    # Take the value of command_for_television, convert it to a vector and append a \r (carriage return).
    - uart.write: !lambda |-
        std::vector<uint8_t> command = std::vector<uint8_t>(x.begin(), x.end());
        command.push_back('\r');
        return command;

switch:
  # Allow the television to be switched on and off via Home Assistant
  - platform: template
    name: "Television"
    id: television_on
    icon: mdi:power
    optimistic: true
    on_turn_on:
      text_sensor.template.publish:
        id: command_for_television
        state: "ka 0 01"
    on_turn_off:
      text_sensor.template.publish:
        id: command_for_television
        state: "ka 0 00"

number:
  # Allow the television HDMI input to be changed via Home Assistant
  - platform: template
    name: "Television HDMI input"
    id: television_hdmi_input
    icon: mdi:video-input-hdmi
    optimistic: true
    min_value: 1
    max_value: 4
    step: 1
    on_value:
      - lambda: |-
          switch((int) x) {
            case 1:
              id(command_for_television).publish_state("xb 0 90");
              break;
            case 2:
              id(command_for_television).publish_state("xb 0 91");
              break;
            case 3:
              id(command_for_television).publish_state("xb 0 92");
              break;
            case 4:
              id(command_for_television).publish_state("xb 0 93");
              break;
          }

remote_receiver:
  # ... 
  on_lg:
  # In this lambda, x is an LGData. x.data is a uint32_t that identifies the signal pressed.
  - lambda: |-
      switch (x.data) {
        // ...
        case 0x00FF38C7: // 16
          id(television_hdmi_input).publish_state(1);
          break;
        case 0x00FF5AA5: // 17
          id(television_hdmi_input).publish_state(2);
          break;
        // ...
        case 0x00FF4AB5: // 19
          id(television_on).publish_state(false);
          break;
        case 0x00FF52AD: // 20
          id(television_on).publish_state(true);
          break;
        // ...
        }

Log output

Powering up the device and pressing buttons on the remote controller yields logs similar to the following:

...
[17:28:27][D][number:036]: 'Television HDMI input': Sending state 1.000000
[17:28:27][D][text_sensor:015]: 'command_for_television': Sending state 'xb 0 90'
[17:28:35][D][number:036]: 'Television HDMI input': Sending state 2.000000
[17:28:35][D][text_sensor:015]: 'command_for_television': Sending state 'xb 0 91'
[17:28:38][D][switch:045]: 'Television': Sending state OFF
[17:28:38][D][text_sensor:015]: 'command_for_television': Sending state 'ka 0 00'
[17:28:43][D][switch:045]: 'Television': Sending state ON
[17:28:43][D][text_sensor:015]: 'command_for_television': Sending state 'ka 0 01'

Home Assistant integration

Using the intermediary switch and number components in the configuration allows the values to be updated remotely, such as via Home Assistant. The media center remote device exposes these two template sensors as controllable entities.

Next steps

Controlling Kodi is good, but we can go one step further and control the television in Stage 3: Television integration.

This article is part of the Media center remote set. If you have any feedback or questions related to this article, please reply to my post on Twitter.