Skip to content

Instantly share code, notes, and snippets.

@lupyuen
Last active May 17, 2024 14:43
Show Gist options
  • Save lupyuen/03a7cc8702085c70893e157d8c3ca3f8 to your computer and use it in GitHub Desktop.
Save lupyuen/03a7cc8702085c70893e157d8c3ca3f8 to your computer and use it in GitHub Desktop.
Home Assistant: Auto-Dim the Xiaomi TV Backlight Every Evening

Home Assistant: Auto-Dim the Xiaomi TV Backlight Every Evening

Suppose we're running Home Assistant on macOS...

  1. Install Rancher Desktop in Docker Mode (Moby):

    • Install Rancher Desktop

    • In Rancher Desktop, click "Settings"...

      Set "Container Engine" to "dockerd (moby)"

      Under "Kubernetes", uncheck "Enable Kubernetes"

      (To reduce CPU Utilisation)

  2. Install Home Assistant as a Docker Container

    mkdir $HOME/home-assistant
    
    docker run -d \
      --name homeassistant \
      --privileged \
      --restart=unless-stopped \
      -e TZ=Asia/Singapore \
      -v $HOME/home-assistant:/config \
      -v /run/dbus:/run/dbus:ro \
      --network=host \
      ghcr.io/home-assistant/home-assistant:stable
    
    # If we need to snoop around the Docker Container, do this:
    docker exec homeassistant ps -a     
  3. Browse to Home Assistant for the Initial Configuration: http://localhost:8123

  4. Add this to the Home Assistant Config: $HOME/home-assistant/configuration.yaml

    # Shell Commands to set the Xiaomi TV Backlight.
    # They will be accessible as Shell Commands `tv_bright` and `tv_dim`
    # We'll call them in our Automated Scheduled Tasks
    # https://www.home-assistant.io/integrations/shell_command/
    
    shell_command:
      tv_bright: ./tv-bright.sh
      tv_dim: ./tv-dim.sh
    
    # Virtual Switch to set the Xiaomi TV Backlight.
    # This will appear in the Home Assistant UI as a Home Automation Device.
    # So humans can click the Virtual Switch to flip the Backlight On and Off.
    # https://www.home-assistant.io/integrations/command_line/
    
    command_line:
      - switch:
          name: TV Backlight
          command_on: ./tv-bright.sh
          command_off: ./tv-dim.sh
  5. Restart Home Assistant to effect the changes:

    Menu > Developer Tools > Check Configuration > Restart

  6. In Home Assistant Settings: Add 2 Automations...

    Menu > Settings > Automations > Create Automation

    • When Time is 7pm: Then Do Shell Command tv_dim

    • When Time is 7am: Then Do Shell Command tv_bright

  7. We'll control Xiaomi TV via the HTTP API...

Create the Shell Script to Dim the TV Backlight: $HOME/home-assistant/tv-dim.sh

#!/bin/bash
## Shell Script to Dim the Xiaomi TV Backlight via HTTP API

set -e  #  Exit when any command fails
set -x  #  Echo commands

## IP Address of Xiaomi TV
ip=192.168.31.8

## Select `Menu > Display`
curl "http://$ip:6095/controller?action=keyevent&keycode=menu"
sleep 0.1
curl "http://$ip:6095/controller?action=keyevent&keycode=up"
sleep 0.1
curl "http://$ip:6095/controller?action=keyevent&keycode=up"
sleep 0.1
curl "http://$ip:6095/controller?action=keyevent&keycode=enter"
sleep 0.1

## Select `Backlight`
curl "http://$ip:6095/controller?action=keyevent&keycode=down"
sleep 0.1
curl "http://$ip:6095/controller?action=keyevent&keycode=down"
sleep 0.1
curl "http://$ip:6095/controller?action=keyevent&keycode=enter"
sleep 0.1

## Decrease the Backlight Level from 99 to 1
for i in {1..100}
do
  curl "http://$ip:6095/controller?action=keyevent&keycode=left"
  sleep 0.1
done

Create the Shell Script to Brighten the TV Backlight: $HOME/home-assistant/tv-bright.sh

#!/bin/bash
## Shell Script to Brighten the Xiaomi TV Backlight via HTTP API

set -e  #  Exit when any command fails
set -x  #  Echo commands

## IP Address of Xiaomi TV
ip=192.168.31.8

## Select `Menu > Display`
curl "http://$ip:6095/controller?action=keyevent&keycode=menu"
sleep 0.1
curl "http://$ip:6095/controller?action=keyevent&keycode=up"
sleep 0.1
curl "http://$ip:6095/controller?action=keyevent&keycode=up"
sleep 0.1
curl "http://$ip:6095/controller?action=keyevent&keycode=enter"
sleep 0.1

## Select `Backlight`
curl "http://$ip:6095/controller?action=keyevent&keycode=down"
sleep 0.1
curl "http://$ip:6095/controller?action=keyevent&keycode=down"
sleep 0.1
curl "http://$ip:6095/controller?action=keyevent&keycode=enter"
sleep 0.1

## Increase the Backlight Level from 1 to 99
for i in {1..100}
do
  curl "http://$ip:6095/controller?action=keyevent&keycode=right"
  sleep 0.1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment