Close
0%
0%

HEOS MQTT Translation

Commanding your HEOS system with Home Assistant using MQTT

Similar projects worth following
597 views
0 followers
The Home Assistant integration for my HEOS devices never worked for me. This is my solution: making my own python service to run on a Raspberry Pi that translates MQTT messages to the TPC/IP commands.

Before using Home Assistant, I had my own homebrew web-server setup to monitor and control lots of things around the house. This included a way to control my HEOS speakers and AVR. This worked perfectly and was very stable. With only the minor inconvenience of friends announcing their arrival by getting on my WiFi and turning up the volume or messing with the music instead of ringing the doorbell. 

In the age of Home Assistant it should be much easier though. It SHOULD, but is isn't for me and I don't know why. I'm running Home Assistant Core on a Raspberry Pi 4 with an SSD. 

Home Assistant has an integration for HEOS:

https://www.home-assistant.io/integrations/heos/

I've never been able to get it to operate smoothly. It won't find my devices. When it does, it complains about needing to be logged in. I don't understand, because I can telnet in to the devices and everything works and responds as expected. 

Anyway, this frustrated me so much, I decided to roll my own. With all the work I've already done on running python scripts as a service, Raspberry Pi, Home Assistant MQTT autodiscover and commanding the HEOS what tot do over a TCP-IP socket, this should be easy. 

It's a nice exercise and continuation for some of my other projects. Mostly, it's just a quick and dirty way to set up the speaker in my bedroom to be included in my wake-up sequence. Also, to set scenes and start or stop playing music when I use the NFC-tags I have all over the house. 

What it WON'T do: Get any information back from the HEOS devices or do any automatic configuration of names or media info. 


Basic setup:

Python script will run as a service/daemon on a Raspberry Pi. This is the same Pi that runs HA, and the MQTT-broker, but that's not required. These three tings can be setup on three different machines, that's the beauty of the MQTT-protocol. 

The script will publish messages to config topics for Home Assistant to setup devices with some action/buttons automatically. For each device, it will have buttons to:

  • Play
  • Pause
  • Volume up
  • Volume down
  • Next
  • Group 
  • Ungroup

It will also listen to a specific topic for raw commands, witch it will pipeline directly to the HEOS-device. Maybe its easier to just use the TCP-integration in Home Assistant though. 

https://www.home-assistant.io/integrations/tcp/

Because now,  I still have to setup scripts in Home Assistant and using this MQTT-service will just add another layer. Anyway, I will add it. It's never bad to expose it to all other devices. Now my Roomba can play some tunes if it feels lonely when I'm away. That will keep it from chewing on loose cables.

I have three separate HEOS devices:

  1. An AVR-X2400H: https://www.denon.com/en-us/product/av-receivers/avr-x2400h
  2. A HEOS3: https://www.denon.com/en-za/shop/speakers/heos3hs2
  3. A HEOS Link: https://www.denon.com/nl-nl/product/network-audio-players/heos-link-hs2

These will have their own "device" in Home Assistant, with buttons/actions. All buttons of the same device will listen to the same command topic. 

The code is not pretty. With a lot of repetition and stuff hardcoded in. But hey, if it works... 

MQTT_Config.py

Details for MQTT-broker

x-python - 107.00 bytes - 08/23/2022 at 17:10

Download

Heos_MQTT_daemon.py

Python script

x-python - 13.73 kB - 08/23/2022 at 16:58

Download

Heos_MQTT.service

Systemctl service file needed to run the python script as a daemon/service

service - 319.00 bytes - 08/23/2022 at 16:58

Download

AVR-3808CISerialProtocol_Ver520a.pdf

Adobe Portable Document Format - 742.86 kB - 08/23/2022 at 08:11

Preview

AVRX1000_E300_PROTOCOL_1000__V04.pdf

Adobe Portable Document Format - 512.57 kB - 08/23/2022 at 08:11

Preview

View all 6 files

  • 1 × Any HEOS by Denon component To be controlled over TCP-IP
  • 1 × Running MQTT-broker I'm running this on the same Raspberry Pi 4 as HA
  • 1 × Active Home Assistant setup with MQTT-integration The MQTT-integration will pick up the autoconfig messages for the buttons
  • 1 × Raspberry Pi or other linux setup using systemctl Maybe the script can be changed to work on different systems. This is the setup I use.
  • 1 × Files from the download section

View all 6 components

  • 1
    Download and edit

    Download the files from here:

    https://hackaday.io/project/186946-heos-mqtt-translation#menu-files

    Edit the MQTT_config.py with the details of your own MQTT-broker;

    Edit the Heos_MQTT_daemon.py file with the details of your HEOS systems. You can let it do anything you want. 

    Edit the Heos_MQTT.service file with the details of your Raspberry System.

    Upload the files to your Raspberry Pi, somewhere in your home directory for instance (should match content of the service file)

    Make the Heos_MQTT_daemon.py executable: "chmod +x Heos_MQTT_daemon.py"

    Test the edited Heos_MQTT_daemon by running it: "./Heos_MQTT_daemon.py"

  • 2
    Check on Home Assistant

    On the first run, all buttons are created in Home Assistant. With the python script still running, test the buttons and functions. If you're happy, stop the python script and clap your hands. There is much rejoicing!

  • 3
    Make the python script run as a service and start at bootup

    Copy Heos_MQTT.service into /etc/systemd/system as root, for example:

    sudo cp Heos_MQTT.service /etc/systemd/system/Heos_MQTT.service

    Once this has been copied, you have to inform systemd that a new service has been added. This is done with the following command:

    sudo systemctl daemon-reload

     Now you can attempt to start the service using the following command:

    sudo systemctl start Heos_MQTT.service
    

     And check if its running with:

    sudo systemctl status Heos_MQTT.service

    This should show something like:

    Stop it using following command:

    sudo systemctl stop Heos_MQTT.service

     When you are happy that this starts and stops your app, you can have it start automatically on reboot by using this command:

    sudo systemctl enable myscript.service

    sudo systemctl enable myscript.service

    And once more start it and check if its running

View all 4 instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates