gnulabis / devimote

Unofficial remote control for Devialet Expert amplifiers written in Python
3 stars 3 forks source link

Adding MQTT capabilities to Devimote #4

Open pisabell2 opened 2 years ago

pisabell2 commented 2 years ago

I would very much like to be able to talk to Devimote through MQTT strings. One way of thinking of this, is that "command" MQTT strings would be accepted as an alternative to pressing a button on the GUI and "update" MQTT strings would be sent in parallel to updating corresponding elements of the GUI.

In my case, it would also be OK to completely replace the GUI with corresponding read and write MQTT capabilities, because my current goal is to integrate Devialet control to my home automation system, which is implemented within Openhab.

As I am rather new to both MQTT and python programming, I would be delighted to collaborate with anyone else interested in such an MQTT <--> Python <--> Devialet capability.

Let me know.

gnulabis commented 2 years ago

Do you have any good link to read about MQTT?

In principle I could see this working by splitting the code in a backend service that communicates with the amplifier(s) (what is today encapsulated in class DeviMoteBackEnd) and one or more frontends that control the backend. The communication between the two could be MQTT. One such frontend would be the existing GUI.

pisabell2 commented 2 years ago

You will easily find lots of material about MQTT basics, as for example:

https://www.hivemq.com/mqtt-essentials/?utm_source=bing&utm_campaign=&utm_term=mqtt%20paho&utm_medium=ppc&hsa_tgt=kwd-78821797188731:loc-32&hsa_cam=361840365&hsa_src=o&hsa_net=adwords&hsa_kw=mqtt%20paho&hsa_ad=&hsa_grp=1261140765691233&hsa_ver=3&hsa_acc=3585854406&hsa_mt=e&msclkid=34284a61621613daab0c297320341dcc&utm_content=MQTT%20Essentials

I am not expert enough to recommend any one in particular.

For using with Python, you may find PAHO MQTT convenient:

https://pypi.org/project/paho-mqtt/

I have started working on my Devimote augmentation, and that's what I am using. I expect I will have a first cut in no more than a few days.

pisabell2 commented 2 years ago

I have been using bits of MQTT in other projects before, so I am reasonably comfortable with it. For me, the challenge is understanding Python well enough to get the job done. Here is a first attempt that results in a Python issue that I do not understand.

First, I added the following import statement,

import paho.mqtt.client as mqtt

Then I made a first attempt at connecting with my MQTT broker and acting upon specific incoming messages, by modifying your DevimoteApp as follow:

    def build(self):
        '''Kivy build function, runs once'''
        self.BROKER_ADDRESS='127.0.0.1'
        self.gui = DeviMoteWidget()
        self.backend = DeviMoteBackEnd()
        self.status = self.backend.update()
        self._powered(0)
        self.gui.update(self.status)
        self.gui.sw_power.bind(on_press=self.toggle_power_callback)
        self.gui.sw_mute.bind(on_press=self.toggle_mute_callback)
        self.gui.volume.vol_slider.bind(value=self.set_volume_callback)
        self.gui.channels.bind(text=self.set_output_callback)
        Clock.schedule_interval(self.update, 0.1)
        self.mqtt_client = mqtt.Client("Devimote")
        self.mqtt_client.connect(self.BROKER_ADDRESS)
        self.mqtt_client.subscribe("Devimote/#")
        self.mqtt_client.message_callback_add("Devimote/Power/cmd",self.toggle_power_callback) 
        self.mqtt_client.loop_start()
        return self.gui

As you can see, I added my mqtt-specific code around the end of the self-build function. What it purports to do is:

1) create the mqtt client named mqtt_client;

2) connect that client to my Mosquitto mqtt broker which is running on the same Windows machine;

3) subscribe to any topic in the Devimote hierarchy ("Devimote/#"); this means that any topic of that kind that gets posted on the broker will be seen by Devimote;

4) an (unsuccessful) attempt to link to the topic a callback action (self.toggle_power_callback), which I tentatively copied from your corresponding gui action (pressing the power button), just a few lines above. The paho mqtt function "message_callback_add(topic, action)" filters incoming messages so that we can assign specific callback actions to specific types of messages. What I am assuming here is that any occurrence of the topic "Devimote/Power/cmd" is to be interpreted as a request to toggle the power. Generally speaking a particular "message" is associated with a given topic, and it may be necessary to look at that message. For example, the topic "Devimote/Volume/cmd" could have as its associated "message" the target value to which the volume should be set. In the case of a toggle such as power or mute, the message turns out to be superfluous.

5) Start the mqtt loop that will be checking for incoming messages in a separate thread.

I believe all the above is working, except line 4, my callback action, which produces an error as soon as the relevant mqtt message is received. The error is described as follows:

"TypeError: toggle_power_callback() takes 2 positional arguments but 4 were given"

I don't know Python well enough to understand what exactly goes wrong and how I can fix it.

You can probably fix that easily, and if so, we will have a blueprint on which we can base a complete set of mqtt commands. Going in the opposite direction, I think it will prove quite easy to send mqtt-based status updates to any home automation system.

pisabell2 commented 2 years ago

I found a way to fix the problem reported above, and I now have a fully working MQTT interface with Devimote. This has allowed me to set up a Devialet gui within Openhab. The associated OH sitemap portion looks like this:

https://drive.google.com/file/d/1dyoWwi3SVkW90ZLNQyTRdsTpdpqL5dtr/view?usp=sharing

Commands and updates are working fine.

However, I am not at all proud of my code. My knowledge of Python is very crude and I am certain that the stuff I added for integrating MQTT should be rewritten by someone who is more fluent than me in Python.

If there is any volunteer, let me know and I will be happy to share my code.

gnulabis commented 2 years ago

Great news! Do you know enough of git to submit your code here as a new branch or pull request? It will be much easier to comment on it and eventually merge it with my code.

pisabell2 commented 2 years ago

Hi Dimitris,

I could probably manage to do that, but I am not sure my code is really ready for that.

Here are 3 issues that we might like to address if we can manage.

1) Why did I need to change the position of some command bytes in my status packets? Is it because we are using different expert models (D200 versus D220 PRO?)? Or is it because I am chaining several amplifiers? Or for some other reason?

2) In order to get the command packets to work correctly, I have had to hard code the particular bytes needed to select my own seven sources, but I am not sure whether or not this will work in a different environment, and it will definitely not if one has more than 7 different sources. What we would like is to find out is how the correct bytes can be computed in general.

Regarding the above two issues, how do people know whether they should be using your version or mine?

3) The MQTT functionality is optional and unrelated to the other changes above. As I mentioned in a previous message, my MQTT code is probably substandard because I don't know Python well enough. I suppose that a proficient Python writer would have placed the MQTT code in one or more separate Python classes, but I didn't know how to do that.

What do you think?

Le lun. 19 sept. 2022 à 02:57, Dimitris Lampridis @.***> a écrit :

Great news! Do you know enough of git to submit your code here as a new branch or pull request? It will be much easier to comment on it and eventually merge it with my code.

— Reply to this email directly, view it on GitHub https://github.com/gnulabis/devimote/issues/4#issuecomment-1250637055, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMVPUG66XEPFZ7Q6WCL22WTV7AFEBANCNFSM6AAAAAAQCRH47Q . You are receiving this because you authored the thread.Message ID: @.***>

gnulabis commented 2 years ago

I believe that the correct way to proceed is the following:

  1. You fork my repository, to create your own copy of it
  2. You apply your changes there and commit them
  3. You do a pull request here, pointint to your own copy and branch (https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request)
  4. You mark the pull request as "Draft" to make it clear that it is not ready to be merged yet

From there, we continue the discussion inside the pull request, with the code in front of us and try to address any issues before merging it.

pisabell2 commented 2 years ago

Ok, thanks: I will try to do that.

Le lun. 19 sept. 2022, 10 h 15, Dimitris Lampridis @.***> a écrit :

I believe that the correct way to proceed is the following:

  1. You fork my repository, to create your own copy of it
  2. You apply your changes there and commit them
  3. You do a pull request here, pointint to your own copy and branch ( https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request )
  4. You mark the pull request as "Draft" to make it clear that it is not ready to be merged yet From there, we continue the discussion inside the pull request, with the code in front of us and try to address any issues before merging it.

— Reply to this email directly, view it on GitHub https://github.com/gnulabis/devimote/issues/4#issuecomment-1251084543, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMVPUGZRU2SVS7L5PS4KMCLV7BYRDANCNFSM6AAAAAAQCRH47Q . You are receiving this because you authored the thread.Message ID: @.***>

bobmorane06 commented 2 years ago

I'm keen to see your code. I'm toying with the idea of setting up Home Assistant or other home automation server and controlling my Expert 220 with a simple Logitech Harmony remote..

pisabell2 commented 2 years ago

Yesterday, I placed my code in a new fork. Can you see it?

Le mar. 20 sept. 2022 à 04:35, bobmorane06 @.***> a écrit :

I'm keen to see your code. I'm toying with the idea of setting up Home Assistant or other home automation server and controlling my Expert 220 with a simple Logitech Harmony remote..

— Reply to this email directly, view it on GitHub https://github.com/gnulabis/devimote/issues/4#issuecomment-1252023096, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMVPUGYKEYMJH7Q3AASVPPLV7FZNTANCNFSM6AAAAAAQCRH47Q . You are receiving this because you authored the thread.Message ID: @.***>

bobmorane06 commented 2 years ago

I can see your fork, but as far as i can tell it's still holding the original code. No rush for me, I'm not even sure yet which server / ecosystem I'll setup

pisabell2 commented 2 years ago

I am confused about the procedure for sharing under git, but the fork I created is named @pisabell2-patch1 and it is available at the following address:

  https://github.com/pisabell2/devimote/tree/pisabell2-patch-1

Le mar. 20 sept. 2022 à 10:56, bobmorane06 @.***> a écrit :

I can see your fork, but as far as i can tell it's still holding the original code. No rush for me, I'm not even sure yet which server / ecosystem I'll setup

— Reply to this email directly, view it on GitHub https://github.com/gnulabis/devimote/issues/4#issuecomment-1252481223, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMVPUG52DSY45VHYQH5MJGTV7HGBDANCNFSM6AAAAAAQCRH47Q . You are receiving this because you authored the thread.Message ID: @.***>

bobmorane06 commented 2 years ago

I can see your code there, txs. I may be polluting gnulabis' github (github lacks private messages), but I'm wondering how you're using mqtt with the devialet, have you tied the mqtt commands to inputs from a remote? I'm a noob with home automation, just started looking into it, one of my objectives is to use a Harmony Companion remote to control my Expert Pro. I will probably go with Home Assistant that supports the Mosquitto broker and the Harmony Hub.

pisabell2 commented 2 years ago

I am sure it is possible to link MQTT commands to specific keys on an advanced remote. However, this is not what I am doing. Rather, I tend to get rid of all remotes in favor of a unifying gui on my phone (and/or tablet).

In my case, the unifying gui is defined using the Openhab home automation software. A few days ago, I attached the following image to one of my posts about MQTT and Devimote:

https://drive.google.com/file/d/1dyoWwi3SVkW90ZLNQyTRdsTpdpqL5dtr/view?usp=sharing

This shows just one portion of one of the screens of my Openhab home automation gui. When this portion is shown on my phone screen, I can for example press the chevron that appears beside the current source (Roon Ready), and a menu of all available sources will appear, letting me choose a new source. Similarly, I can change the current volume using the slider, toggle the mute function, etc.

Within Openhab, these gui actions are linked to code that sends corresponding mqtt messages to my Mosquitto broker. Devimote will see these messages posted by the broker, and will then trigger the appropriate actions on the amplifier.

My first intended use of Devimote was to make it possible for me to define a "scene" named "Start Cinema". I can now trigger that scene using a corresponding button in the Openhab interface on my phone. The effect is as follows:

1) Schedule the turning off of the lights in the cinema room, 40 seconds later. (Openhab will trigger an insteon switch)

2) Turn on the Devialet amp if it is in "STANDBY" mode. Schedule a change of source to "Cinema" and volume level to -22db on the Devialet, 20 seconds later. (Openhab sends MQTT messages that will be acted upon by Devimote)

3) Turn on the cinema projector (Openhab triggers an infrared code through my Harmony hub)

4) Pull down the electric cinema screen (Openhab sends an MQTT message that will be read by a 8266 chip that I have attached to the trigger switch of the screen).

5) Turn on my HDMI switch and select the pre-chosen input (if defined) or the default input (Openhab sends more infrared codes through my Harmony hub)

6) Turn on the relevant input source (e.g. Cable TV box, Kodi Box, Bluray player, etc. (depends on the box, but most often, more infrared codes)

In order to do so, the "Start Cinema" button is linked to an Openhab "rule" that implements all these actions. Clearly, if I had a popcorn machine , the rule would also start it:-)

And of course, once the cinema session is over, a button will undo all of the above,

Le mar. 20 sept. 2022 à 13:13, bobmorane06 @.***> a écrit :

I can see your code there, txs. I may be polluting gnulabis' github (github lacks private messages), but I'm wondering how you're using mqtt with the devialet, have you tied the mqtt commands to inputs from a remote? I'm a noob with home automation, just started looking into it, one of my objectives is to use a Harmony Companion remote to control my Expert Pro. I will probably go with Home Assistant that supports the Mosquitto broker and the Harmony Hub.

— Reply to this email directly, view it on GitHub https://github.com/gnulabis/devimote/issues/4#issuecomment-1252657293, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMVPUG7CUAR6ERBY6Q7J6IDV7HWDXANCNFSM6AAAAAAQCRH47Q . You are receiving this because you authored the thread.Message ID: @.***>

bobmorane06 commented 2 years ago

Nice. I intend to do about the same, but pressing on a scene button of my Harmony remote. It's faster, doesn't need to be recharged daily, no unlock needed. Once I can get devimote working as a backend nothing stops me from having both the command from a smartphone and the Harmony :-)

I'm looking forward to seeing your mqtt code working :-)

pisabell2 commented 2 years ago

Good!

You may to bring stylistic improvements to my code. No chance I would be offended by that!

On a different topic, I gather that you too are using a D220 PRO? If so it means that the differences that I observed in the Devialet command and update packets does not result from a difference in amplifier models: I am using the same. Do you have core infinity as well?

Le mar. 20 sept. 2022, 16 h 47, bobmorane06 @.***> a écrit :

Nice. I intend to do about the same, but pressing on a scene button of my Harmony remote. It's faster, doesn't need to be recharged daily, no unlock needed. Once I can get devimote working as a backend nothing stops me from having both the command from a smartphone and the Harmony :-)

I'm looking forward to seeing your mqtt code working :-)

— Reply to this email directly, view it on GitHub https://github.com/gnulabis/devimote/issues/4#issuecomment-1252894828, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMVPUGYKP2WMC4EEWIQZ66LV7IPH3ANCNFSM6AAAAAAQCRH47Q . You are receiving this because you authored the thread.Message ID: @.***>

gnulabis commented 2 years ago

@bobmorane06 , any discussion related to "adding MQTT to Devimote" is more than welcome here :)

I'm still not on the home automation train but reading both your comments I'm getting intrigued.

pisabell2 commented 2 years ago

Good, let me know if I can help.

Le jeu. 22 sept. 2022 à 03:23, Dimitris Lampridis @.***> a écrit :

@bobmorane06 https://github.com/bobmorane06 , any discussion related to "adding MQTT to Devimote" is more than welcome here :)

I'm still not on the home automation train but reading both your comments I'm getting intrigued.

— Reply to this email directly, view it on GitHub https://github.com/gnulabis/devimote/issues/4#issuecomment-1254631041, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMVPUGZHHM5IAKUWD2WNRYLV7QCPTANCNFSM6AAAAAAQCRH47Q . You are receiving this because you authored the thread.Message ID: @.***>