Controlling my Hisense TV from a Sofabaton X1S with MQTT

I’ve just bought a Sofabaton X1S and have created a virtual device on it for my Hisense TV. I added all of the commands I needed as URL’s that will trigger scripts with webhooks in Home Assistant.

You will need to add the IP address of your TV and HomeAssistant to your DHCP reservations to ensure they are static. The sofabaton resolves the home assistant IP address when you save the settings in the app instead of keeping the local domain name.

You will need to enable remote control on your TV, which may already be enabled if you’ve used their mobile phone app.

You will need the Wake On LAN installed Wake on LAN - Home Assistant

You will also need the Hisense TV MQTT Bridge GitHub - sehaas/ha_hisense_tv: Hisense TV integration for Home Assistant
That requires some setup, so check the documentation until you can reliably discover the state of your TV from Home Assistant.

I don’t use my TV tuner, so if you need those functions you might want extra automations to pick that source. Similarly if you use other HDMI sources, as mine all come from a receiver into a single port (HDMI2) that supports audio-return.

There’s essentially 5 different automations I run on HomeAssistant:

  1. Power On - http://homeassistant.local/api/webhook/hisensetv_poweron
  2. Power Off - http://homeassistant.local/api/webhook/hisensetv_poweron
  3. HDMI2 - http://homeassistant.local/api/webhook/hisensetv_hdmi2
  4. Launcher - http://homeassistant.local/api/webhook/hisensetv_launcher
  5. Key Press - http://homeassistant.local/api/webhook/hisensetv?key=up

Note: All of these use the GET method, rather than POST, which makes it a lot easier to copy and paste into the remote app or a web browser address bar for testing.

The first 4 are special cases. That 5th one is a generic command that will take any value in the querystring for the key, so there’s the same command for each standard button for the TV remote: up, down, back, home, exit, left, right, menu, ok, play, pause, youtube, rewind, fast_forward, etc.

Once you have all of the commands defined, you assign them to the various buttons on the remote within the app.

Now for the Home Assistant setup:

This automation handles all of the 5th Key Press commands. The “upper” function in the payload ensures any lowercase letters are capitals as my TV requires this for key names. My TV also prefixes all key names with KEY_ also.

alias: Hisense Button
description: >-
  When a button for the hisense tv is received by webhook, normally from the
  sofabaton, the querystring is forwarded to the tv as a button press
triggers:
  - webhook_id: hisensetv
    allowed_methods:
      - GET
    local_only: true
    trigger: webhook
conditions: []
actions:
  - action: mqtt.publish
    metadata: {}
    data:
      evaluate_payload: false
      qos: "0"
      topic: hisense/remoteapp/tv/remote_service/HomeAssistant/actions/sendkey
      payload: KEY_{{ trigger.query.key | upper }}
mode: single

This automation performs the TV ON command, and tries to handle the quirks of a TV with a single power on/off toggle and an MQTT broker that has to be woken with a Wake On LAN. You will need your own MAC address for the WOL. My TV sleeps with a state of “fake_sleep_0” when it’s off and “sourceswitch” when it’s on.

alias: TV On by Remote
description: ""
triggers:
  - trigger: webhook
    allowed_methods:
      - GET
    local_only: true
    webhook_id: hisensetv_poweron
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.hisense_tv_state
            state: fake_sleep_0
        sequence:
          - action: script.hisense_tv_sleep
            metadata: {}
            data: {}
      - conditions:
          - condition: state
            entity_id: sensor.hisense_tv_state
            state: sourceswitch
        sequence: []
  - action: wake_on_lan.send_magic_packet
    metadata: {}
    data:
      broadcast_port: 9
      mac: XXXXXXXXXXXX
mode: single

This automation performs a TV Off command. There’s a check that the TV isn’t already off, to stop us toggling it back on.

alias: TV Off with Remote
description: ""
triggers:
  - trigger: webhook
    allowed_methods:
      - GET
    local_only: true
    webhook_id: hisensetv_poweroff
conditions:
  - condition: device
    device_id: ed3c86d94a024a1bed8df1fddf73616b
    domain: media_player
    entity_id: ae9013d151f00cf3dcc9c57d4a0d2f5c
    type: is_on
  - condition: not
    conditions:
      - condition: state
        entity_id: sensor.hisense_tv_state
        state: fake_sleep_0
actions:
  - action: script.hisense_tv_sleep
    metadata: {}
    data: {}
mode: single

This automation switches the TV source to HDMI2. If you need others, I guess you could take that as a querystring as I do for buttons, but I just needed this one. It does check we’re not already on HDMI2 to prevent the unnecessary notifications on the display.

alias: TV to HDMI2 with Remote
description: ""
triggers:
  - trigger: webhook
    allowed_methods:
      - GET
    local_only: true
    webhook_id: hisensetv_hdmi2
conditions:
  - condition: not
    conditions:
      - condition: state
        entity_id: sensor.hisense_tv_source
        state: HDMI2
actions:
  - action: script.hisense_tv_hdmi2
    metadata: {}
    data: {}
mode: single

This automation switches the TV to the Launcher, the home page of the TV with the app picker etc. I’m delaying it 1 second just to give the TV time to wake.

alias: TV to Launcher with Remote
description: ""
triggers:
  - trigger: webhook
    allowed_methods:
      - GET
    local_only: true
    webhook_id: hisensetv_launcher
conditions:
  - condition: not
    conditions:
      - condition: state
        entity_id: sensor.hisense_tv
        state: remote_launcher
actions:
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - action: script.mqtt_tv_home
    metadata: {}
    data: {}
mode: single

There’s 3 scripts I wrote used by these automations:
Power Button

sequence:
  - action: mqtt.publish
    metadata: {}
    data:
      qos: "0"
      topic: hisense/remoteapp/tv/remote_service/HomeAssistant/actions/sendkey
      payload: KEY_POWER
      evaluate_payload: false
alias: Hisense TV (Power Button)
description: ""

Home Button

sequence:
  - action: mqtt.publish
    metadata: {}
    data:
      evaluate_payload: false
      qos: "2"
      topic: hisense/remoteapp/tv/remote_service/HomeAssistant/actions/sendkey
      payload: KEY_HOME
alias: Hisense TV (Home Button)
description: ""

HDMI2 selection

sequence:
  - action: mqtt.publish
    metadata: {}
    data:
      qos: "1"
      topic: hisense/remoteapp/tv/ui_service/HomeAssistant/actions/changesource
      payload: |-
        {
          "sourceid" : "HDMI2", 
          "sourcename": "HDMI2" 
        }
      evaluate_payload: false
alias: Hisense TV (Choose HDMI2)
description: ""

I think that’s all there was to it. I probably missed some detail?