Graveyard

This file contains a list of previous implementations that have been replaced with newer ones.

CEC through Raspberry PI

I no longer use this method because Home Assistant was regularly losing connection with the Raspberry PI and the only way to resolve it was to restart regularly Homeassistant. See latest CEC implementation .

CEC is a feature of HDMI that allows individual CEC-enabled devices to control each other without user intervention.

Overview

I use CEC protocol control my TV from Home Assistant. Ex: Turn on/off TV, change input source, and control volume.

// https://stackoverflow.com/a/59363959/2115513 digraph SEQ_DIAGRAM { graph [overlap = true, splines = line, nodesep = 1.0, ordering = out]; edge [arrowhead = none]; node [shape = none, width = 0, height = 0, label = ""]; { rank = same; node [shape = rectangle, height = 0.7, width = 2]; homeassistant [label = "Home Assistant"]; cec [label = "cec proxy (raspberry pi)"]; tv [label = "TV"]; } // Draw vertical lines { edge [style = dashed, weight = 6]; homeassistant -> a1 -> a2 -> a3; a3 -> a5; } { edge [style = dashed, weight = 6]; cec -> b1 b1 -> b2 [penwidth = 5, style = solid]; b2 -> b3 -> b4 -> b5; } { edge [style = dashed, weight = 6]; tv -> c1; c1 -> c2; c2 -> c3 [penwidth = 5, style = solid]; c3 -> c4 -> c5; } { rank = same; a1 -> b1 [label = "send message via WIFI to proxy\nEx: 1f:82:10:00", arrowhead = normal]; } { rank = same; b2 -> c2 [label ="relay message to TV through HDMI cable\nEx: 1f:82:10:00", arrowhead = normal]; } }

Proxy configuration

Setup service

Compile/Install libcec

Install pyCEC

pip3 install pyCEC

Configure a service for pyCEC:

[Unit]
Description=PyCEC
Documentation=
After=network.target

[Service]
Type=simple
User=retro
ExecStart=/usr/bin/python3 -m pycec
Restart=always
#RuntimeMaxSec=1d
#MemoryHigh=50M
MemoryMax=60M
MemorySwapMax=0

[Install]
WantedBy=multi-user.target

It appears to have a memory leak and I made sure it kills itself when it gets to 60M of memory.

Home Assistant

Configuration

Documentation

create a file hdmi_cec.yaml and add the hostname/ip of the cec proxy:

host: retro-controller.local # this the hostname of my raspberry pi

in configuration.yaml add:

hdmi_cec: !include hdmi_cec.yaml

Automations

I used CEC-O-Matic to determine the messages for my TV.

CEC messages for my TV

Action

Message

Power on

10:04

Standby

10:36

Change source 1

1f:82:10:00

Change source 2

1f:82:20:00

Change source 3

1f:82:30:00

Volume up

10:44:41:00

Volume down

10:44:42:00

This is an example home assistant automation to switch to hdmi 1:

service: hdmi_cec.send_command
data:
    raw: 1f:82:10:00

Infrared through Arduino

I no longer use this method because Home Assistant was regularly losing connection with the Raspberry PI and the only way to resolve it was to restart regularly Homeassistant. See latest infrared implementation .

Overview

I use Infrared signals to:

This is how the whole process looks like:

// https://stackoverflow.com/a/59363959/2115513 digraph SEQ_DIAGRAM { graph [overlap=true, splines=line, nodesep=1.0, ordering=out]; edge [arrowhead=none]; node [shape=none, width=0, height=0, label=""]; { rank=same; node[shape=rectangle, height=0.7, width=2]; mqtt[label="MQTT Broker (Home Assistant)"]; mqtt2nec[label="mqtt2nec (raspberry pi)"]; arduino[label="infrared-nec (arduino)"]; device[label="device"]; } // Draw vertical lines { edge [style=dashed, weight=6]; mqtt -> a1 -> a2 -> a3; a3 -> a5; } { edge [style=dashed, weight=6]; mqtt2nec -> b1 b1 -> b2 [penwidth=5, style=solid]; b2 -> b3 -> b4 -> b5 ; } { edge [style=dashed, weight=6]; arduino -> c1; c1-> c2; c2 -> c3 [penwidth=5, style=solid]; c3 -> c4 -> c5; } { edge [style=dashed, weight=6]; device -> d1 -> d2 -> d3; d3 -> d4 [penwidth=5, style=solid]; d4 -> d5; } { rank=same; a1 -> b1 [label="publish to topic \"nec/tx\"\nEx: {\"codes\": [\"TINK4K\", \"TINK4K_1\"]}", arrowhead=normal]; } { rank=same; b2 -> c2 [label="write to serial port\nEx: \"73;11\"", arrowhead=normal]; } { rank=same; c3 -> d3 [label="send infrared nec signal\n Ex: pew pew pew", arrowhead=normal]; } }

This is how the whole setup looks like:

Arduino with raspberry pi

This image: 1. Raspberry pi Zero 2W – 2. USB Hub – 3. Arduino Nano Every with board. There’s one cable going to the Retrotink4K and another one going to the HDMI switch.

Keypad

This image: an IR Led just above the keypad pointing at the Retrotink4K.

Home Assistant MQTT

Prerequisites

Setup Home Assistant’s MQTT Broker.

Automation

Create an automation to send the nec codes to mqtt2nec.

The codes match mqtt2nec’s config.csv. You can also send codes as hex strings.

The first value is common for the device and the next ones are the actual code you want to send.

service: mqtt.publish
data:
  topic: nec/tx
  payload: "{\"codes\":  [  \"TINK4K\", \"TINK4K_1\" ]}"

mqtt2nec

Python program interfacing Home assistant with the arduino. It is installed on the Raspberry pi and runs as a service.

sources

Installing the program

git clone git@github.com:jrobichaud/mqtt2nec.git
cd mqtt2nec
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

Running the program

Make sure to change the arguments to match your mqtt broker configuration.

python3 -m "mqtt2nec" "<home assistant url>" -u "<mqtt user>" -p "<mqtt password>" -a "./config.csv"

Service configuration

[Unit]
Description=mqtt2nec
Documentation=
After=network.target

[Service]
Type=simple
User=retro
ExecStart=/usr/bin/python3 -m "mqtt2nec" "<home assistant url>" -u "<mqtt user>" -p "<mqtt password>" -a "/home/retro/mqtt2nec/config.csv"
Restart=always
MemorySwapMax=0

[Install]
WantedBy=multi-user.target

Arduino

I used this kit to prototype: Basic Kit for Arduino

I use the Arduino Nano Every on my setup.