Mqtt2hegel home automation

With a python module, i wrote earlier, for remote control of hegel amplifiers and an MQTT queue i integrated the hegel in Home Assistant and Node-Red.
It is now possible to control all functions of my H190 and H390 with standard home automation components and apps.
So native volume control, input selection, on and off .
At the moment it is just a very basic script but soon will be a docker container with a bit more functionality and configuration.

if you use it, please let me know here in the community.
This way i know that i can put time in it thus not wasting it.

This is the work of a few hours tinkering.
Just for kicks and giggles, here is the Proof of Concept code.
It works, but use it at your own risk.

The hegel python module is not on pypi yet but will be.

#!/usr/bin/env python3

# Copyright (C) 2021, https://gitlab.com/ptrdvds
# All rights reserved.
# This file is part of hegel-remote.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import hegel
import time
import paho.mqtt.client as mqtt

MQTT_BASE_TOPIC = 'home/hegel'
MQTT_HOST = "192.168.99.30"
MQTT_PORT = 1883
MQTT_SLS = 60


# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    client.subscribe(MQTT_BASE_TOPIC + '/#')


# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, message):
    """
    This is where the magic happens.
    :return: Not designed to return something
    """
    elements = message.topic.split('/')
    action = elements[-1]
    function = elements[-2]
    device = elements[-3]

    if device.lower() in ['h190', 'h390']:
        target_device = eval(device.lower())

        if function in ['p', 'power',
                        'v', 'volume',
                        'i', 'input'
                        'm', 'mute']:

            if action == 'set':
                value = target_device.remote(function, message.payload)
            if action == 'get':
                value = target_device.remote(function)

            client.publish(MQTT_BASE_TOPIC + '/' + device + '/' + function, value)


def service_loop(client):
    """
    This is a service loop to send e.g. keepalive.
    All to know when this program has stopped.
    :return: Not designed to return something
    """
    while True:
        # TODO make heartbeat asynchronious

        time.sleep(MQTT_SLS)
        client.publish(MQTT_BASE_TOPIC + "/heartbeat", time.time())


def main():
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(MQTT_HOST,
                   MQTT_PORT,
                   MQTT_SLS,
                   )

    client.loop_start()
    service_loop(client)


if __name__ == '__main__':
    h390 = hegel.create('H390', '192.168.20.40')
    h190 = hegel.create('H190', '192.168.20.41')
    main()

1 Like

Played a little with the dockerization of the script and have it running and working as intended.
Again, it is not production material, it is a POC.
It works as intended and now it is up to imagination how to wield this power.

YMMV

# this comment makes colours appear on the forum :)
FROM python:latest

MAINTAINER PeterD

COPY . /
COPY hegel/ /hegel/
WORKDIR /
RUN pip install -r requirements.txt
RUN pip install /hegel/

CMD [ "python3", "/main.py" ]

I found out that one can use Node-Red to send TCP messages triggered by a plethora of things.
This way i can skip my python script and use e.g. Home Assistant and Node-Red.
Triggered by e.g. a zigbee button, MQTT message or whatever.

Many ways to skin a cat :stuck_out_tongue:

YMMV

Node-Red UI widgets Proof of Concept on iPhone.
Slider and up/down volume change working in real time.
Gauge and volume unit “led” bar changing real time.

Next up, feedback loop for remote control detection.
Ain’t tinkering fun, who needs Roon Ready :sunglasses:

YMMV

Edit: i know this tinkering does not support RAAT, so we need RR eventually on our current generation hegel devices.

@PeterD - were you ever able to figure out the feedback loop for remote control detection? My solution isn’t as elegant as yours (a lot of command line switches), but I have all the functionality I want except for remote control detection.

Thanks!

Hi Eric,

Yes, i have found that out.
Depending on what you use as your (HA) solution, there are many solutions that work.

In Node-Red i made a check on the power status of the hegel and made that initiated a loop that queried the function of interest with a “tcp out” node.
In the python solution i ran a container that checked the various functions and posted those to an MQTT queue.
Using a key/value store works as well, they are fast enough, i used Redis and PostgreSQL.
Possibilities are endless, but YMMV.

If you are more specific, i might be able to provide help.

1 Like

Interesting. I appreciate the response. Let me see what I can figure out with node red.