[RELEASED] Python plugin to control Onkyo receivers

Python and python framework

Moderator: leecollings

User avatar
jorgh
Posts: 124
Joined: Friday 27 June 2014 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8224
Location: Netherlands
Contact:

Re: Python plugin to control Onkyo receivers

Post by jorgh »

@jake,

In the protocol specification, Onkyo states a response within 50 ms. I don't know what method you mean with the current described method.
I've you refer to my script, I can at least say the following:
The script first discovers the Receiver through a network broadcast. After receiving the response it connects. I use pause of one second after each request, and 5 seconds after a power-on/off request, because it seems the receiver does otherwise sometimes miss a request (at least on my model).

On the plugin I'm developing, the discovery only takes place once (on startup).
After that, the command is issued immediately after an event from domoticz, and should be executed within 50ms.
I, don't know at this time, what the time between a domoticz event and call to a plugin function is. This would obviously need to be added to the response time.

Regards,

Jorg
User avatar
Dnpwwo
Posts: 819
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Python plugin to control Onkyo receivers

Post by Dnpwwo »

@jorgh,

If your actual script is similar to the one you attached to your previous post then the XMLTree issue is probably because you are giving it partial XML.

You are using Protocol "None" which means that Domoticz will pass data to the plugin as it arrives because it does not inspect the data at all. When running your TestServer the data arrives in 2 chunks so onMessage will be called once per chunk. The first time it will fail to load in XMLTree, you need to join the two together and call it.

I tweaked your test plugin:

Code: Select all

    def onConnect(self, Status, Description):
        Domoticz.Log("onConnect called, status:" + str(Status))
        with open(Parameters["HomeFolder"]+"Response.xml", "w") as text_file:
            print("XML Response...", file=text_file)

    def onMessage(self, Data, Status, Extra):
        Domoticz.Log("onMessage called")
        try:
            strData = Data.decode()
            with open(Parameters["HomeFolder"]+"Response.xml", "at") as text_file:
                print(strData, file=text_file)
            XMLRoot = XMLTree.fromstring(strData)
        except Exception as inst:
            Domoticz.Error("Exception in onMessage, called with Data: '"+str(strData)+"'")
            Domoticz.Error("Exception detail: '"+str(inst)+"'")
            raise
so that you can see how the data arrives by looking in the xml file afterwards

EDIT: I played with your TestServer and learnt a few things:
  • If you try and load bad data (such as a byte array) into xml.etree.ElementTree twice on Linux it Seg Faults the 2nd time :shock:
  • The framework's built in support for XML was not flexible enough (fixed once pull request #1336 is merged)
From what you have said about your plugin it has a 'discovery' phase on start up that requires binary data then communicates with the amplifier via XML, if that is true I would set Protocol("None") initially but then change to Protocol("XML") after you connect to the amplifier, that way you will see complete XML messages in onMessage
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
User avatar
jorgh
Posts: 124
Joined: Friday 27 June 2014 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8224
Location: Netherlands
Contact:

Re: Python plugin to control Onkyo receivers

Post by jorgh »

@Dnpwwo,

I'm still figuring out what is going wrong. In the plugin, I'm actually feeding the XML tree, the decoded (string) response. As the XML is packaged in a frame, containing binary data so I still need the 'None' protocol. I'm already checking the frame to see if I've got all data. If not, I'm awaiting another call to onMessage and filling the input buffer.
What I however noticed during testing, it seems that I do get multiple onMessage calls if the amount of received data exceeds 4Kb. However, the 1st message contains all data, and the subsequent onMessage call contains an empty bytes array.
Don't know if I'm at fault here, but it seemed important enough to let you know this finding.
I'll continue my search on the root cause of the issue I'm experiencing.
I'll keep you posted.

Kind regards,

Jorg
jake
Posts: 742
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Python plugin to control Onkyo receivers

Post by jake »

jorgh wrote:@jake,

In the protocol specification, Onkyo states a response within 50 ms. I don't know what method you mean with the current described method
Jorg
I mean the method as described in the Onkyo Wiki op Domoticz
User avatar
jorgh
Posts: 124
Joined: Friday 27 June 2014 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8224
Location: Netherlands
Contact:

Re: Python plugin to control Onkyo receivers

Post by jorgh »

@Jake,

From what I'm seeing now the plugin responses are very fast. The receiver responds just as fast as my z-wave lighting.

Regards,

Jorg
jake
Posts: 742
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Python plugin to control Onkyo receivers

Post by jake »

That is good to hear! Are you planning a first public release of the python plugin already?
JimmyH1969
Posts: 153
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Python plugin to control Onkyo receivers

Post by JimmyH1969 »

Just like to say that i'm following this threath with high expectations and to say that it is highly appreciated. Keep up the good work :D
User avatar
jorgh
Posts: 124
Joined: Friday 27 June 2014 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8224
Location: Netherlands
Contact:

Re: Python plugin to control Onkyo receivers

Post by jorgh »

@Jake,

I've got some things working, but the lockup issue is still there. Basically Domoticz locks up after receiving the receiver configuration. I've found a workaround that allows me to further develop the plugin (basically I load the config from file) but this is not something that is usable for the general public. I doubt that it's an issue with my code, but until we found the real cause it's difficult to say. I'm sure we're going to resolve this.

Powering on/off, volume control and muting are fully functional. I'm currently working on input , listening mode, and tuner preset selection. For this I am figuring out a solution to read the selector names, as this will allow users to manually alter the lists (e.g. reorder or remove unwanted items) without the plugin getting confused. I've posted a question in this forum on this specific issue (https://www.domoticz.com/forum/viewtopi ... 65&t=16683)

Regards,

Jorg
User avatar
mrefex
Posts: 26
Joined: Tuesday 14 March 2017 14:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7041
Contact:

Re: Python plugin to control Onkyo receivers

Post by mrefex »

Hi Jorgh

I tried your script and for a newbie it first didn't work. after searching what was wrong with it i figured out it was a simple action making the script executable ....

I will follow this thread if there are some updates. But for now i can control my onkyo.
But after some experimenting with this script i cannot fully control it. only power on and of and some inputs. volume doesn't responds correctly.

i now tried : https://www.domoticz.com/wiki/Onkyo and this one gives me more control over it. I own the TX-NR656 don't know if that's the reason why your methode doesn't work?
User avatar
jorgh
Posts: 124
Joined: Friday 27 June 2014 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8224
Location: Netherlands
Contact:

Re: Python plugin to control Onkyo receivers

Post by jorgh »

@mrefex,

Could you be more specific on what does not work with the volume? As far as I know, this should be relatively simple and the same for all receivers (although the max volume differs per receiver).
Are you able to get the volume with the --state parameter?
Does the volume work if you specify it as the only parameter after the receiver is powered on?
With the inputs, you can query the input with the --state command. The input numbers differ per reciever, and not all input numbers are used (there could be gaps between numbers that work). If you specify the input with the number returned by --state, it should switch to that input.

Regards,

Jorg
User avatar
mrefex
Posts: 26
Joined: Tuesday 14 March 2017 14:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7041
Contact:

Re: Python plugin to control Onkyo receivers

Post by mrefex »

How do i use the command with PuttY?
go to script dir and type in : onkyocmd.py --status

tried that but didn't work. i don't know how to do that....
User avatar
jorgh
Posts: 124
Joined: Friday 27 June 2014 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8224
Location: Netherlands
Contact:

Re: Python plugin to control Onkyo receivers

Post by jorgh »

@mrefex,

Almost right ;-)
I suspect you use an account where the current directory is not within the search path. The solution is simple
./onkyocmd --status

the ./ specifies it should execute the script in the current directory.

Regards,

Jorg
User avatar
mrefex
Posts: 26
Joined: Tuesday 14 March 2017 14:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7041
Contact:

Re: Python plugin to control Onkyo receivers

Post by mrefex »

tried this and i got a error.

Code: Select all

pi@raspberrypi:~/domoticz/scripts $ ./onkyocmd.py --status
usage: onkyocmd.py [-h] [--power {on,off}]
                   [--volume {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,                20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46                ,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72}]
                   [--input {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,2                0,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46}                ]
                   [--zone2] [--state]
onkyocmd.py: error: unrecognized arguments: --status
pi@raspberrypi:~/domoticz/scripts $
User avatar
jorgh
Posts: 124
Joined: Friday 27 June 2014 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8224
Location: Netherlands
Contact:

Re: Python plugin to control Onkyo receivers

Post by jorgh »

@mrefex,

Sorry my bad, the proper switch is --state as indicated in the usage help you just posted.

Regards,
User avatar
mrefex
Posts: 26
Joined: Tuesday 14 March 2017 14:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7041
Contact:

Re: Python plugin to control Onkyo receivers

Post by mrefex »

Thanx. this works. But still i have issues controlling the correct input.
i want to start my Tunein internet radio on preset 1. and that's a known issue i read form several other people owing a TX-RN656

you can only select the 'NET' source and from there you can't select any further.
User avatar
jorgh
Posts: 124
Joined: Friday 27 June 2014 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8224
Location: Netherlands
Contact:

Re: Python plugin to control Onkyo receivers

Post by jorgh »

@mrefex,

From what I read in the protocol specification, it should be possible by issuing a NPR01 command.
I don't know if the issue you're experiencing is a problem with the receiver or an issue with the software you are using to control the receiver.

Regards,

Jorg
User avatar
jorgh
Posts: 124
Joined: Friday 27 June 2014 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8224
Location: Netherlands
Contact:

[RELEASED] Python plugin to control Onkyo receivers

Post by jorgh »

Hi All,

This is an initial release of the Python plugin to control Onkyo receivers.

Minimum required Domoticz version is: 3.7040

As there are a lot of different receiver types, it could well be, that people experience issues that need code changes to be resolved.
Any feedback on issues, what works, what doesn't and other suggestions are welcome.

Installation instructions:
Unzip the plugin.zip file and put plugin.py within a folder named 'Onkyo' in the Domoticz plugin folder.
On Linux make the file executable (chmod a+x plugin.py)

After you installed the plugin, restart Domoticz and add the Onkyo Receiver through the Setup->Hardware.
It takes around 30 seconds after the hardware is added, before the devices are created.
After creation, you can add them through Setup->Devices, by pressing the green arrow on the devices you want to add.

The lockup issue still exists, I've implemented a workaround that seems to work. The code that causes the lockup is around line 560, the workaround is at line 440. If anyone has an idea what goes wrong, drop me a line. I don't know if it's a problem with my code, or a Domoticz bug that causes the issue. Any help is appreciated.

Regards,

Jorg


Edit: The plugin can now be found on GitHub: https://github.com/jorgh6/domoticz-onkyo-plugin
Last edited by jorgh on Monday 27 March 2017 16:31, edited 1 time in total.
swevm
Posts: 23
Joined: Friday 16 September 2016 20:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 8718
Location: Sweden
Contact:

Re: [RELEASED] Python plugin to control Onkyo receivers

Post by swevm »

Great work on this @joerh,

I installed the plugin on Domoticz 7067 with Python 3.4. Plugin load and discover my TX-NR828 on ip 192.168.10.2 (have previously used Node-Red for some basic integration). However no devices are created, even after several minutes. In Domoticz status log I see that the plugin is looking for an XML file that seem to be missing (enabled debug). I don´t see anything specific log entries that point to a problem.
Do you have any clues?

Code: Select all

2017-03-22 20:49:03.120 (Onkyo AV) Initialized version 0.1.0, author 'jorgh'
2017-03-22 20:49:03.173 (Onkyo AV) Onkyo: onStart called
2017-03-22 20:49:04.854 Hardware Monitor: Fetching data (System sensors)
2017-03-22 20:49:12.640 (Onkyo AV) onHeartbeat called
2017-03-22 20:49:12.640 (Onkyo AV) Creating UDP Socket for sending/receiving discovery data
2017-03-22 20:49:12.661 (Onkyo AV) UDP Socket created succesfully
2017-03-22 20:49:12.661 (Onkyo AV) Sending UDP discovery packet
2017-03-22 20:49:12.662 (Onkyo AV) UDP Discovery packet send succesfully
2017-03-22 20:49:14.665 (Onkyo AV) onHeartbeat called
2017-03-22 20:49:14.665 (Onkyo AV) Checking if discovery data has been received
2017-03-22 20:49:14.666 (Onkyo AV) Receiver found:
2017-03-22 20:49:14.666 (Onkyo AV) Type: AV Receiver or Stereo Receiver
2017-03-22 20:49:14.666 (Onkyo AV) Type: TX-NR828
2017-03-22 20:49:14.666 (Onkyo AV) Region: European or Asian model
2017-03-22 20:49:14.666 (Onkyo AV) IP adress: 192.168.10.2
2017-03-22 20:49:14.666 (Onkyo AV) eISCP port: 60128
2017-03-22 20:49:14.666 (Onkyo AV) MAC: 0009B048AB4C
2017-03-22 20:49:14.666 (Onkyo AV) Connecting to Receiver
2017-03-22 20:49:14.667 PluginSystem: Starting I/O service thread.
2017-03-22 20:49:14.717 (Onkyo AV) onConnect called
2017-03-22 20:49:16.671 (Onkyo AV) onHeartbeat called
2017-03-22 20:49:16.671 (Onkyo AV) Loading XML from file
2017-03-22 20:49:16.671 (Onkyo AV) XML file does not yet exist
2017-03-22 20:49:18.624 (Onkyo AV) onHeartbeat called
2017-03-22 20:49:18.624 (Onkyo AV) Loading XML from file
2017-03-22 20:49:18.625 (Onkyo AV) XML file does not yet exist
2017-03-22 20:49:20.628 (Onkyo AV) onHeartbeat called
2017-03-22 20:49:20.628 (Onkyo AV) Loading XML from file
2017-03-22 20:49:20.629 (Onkyo AV) XML file does not yet exist
2017-03-22 20:49:22.632 (Onkyo AV) onHeartbeat called
2017-03-22 20:49:22.632 (Onkyo AV) Loading XML from file
2017-03-22 20:49:22.632 (Onkyo AV) XML file does not yet exist
2017-03-22 20:49:24.636 (Onkyo AV) onHeartbeat called
2017-03-22 20:49:24.636 (Onkyo AV) Loading XML from file
2017-03-22 20:49:24.636 (Onkyo AV) XML file does not yet exist
2017-03-22 20:49:26.640 (Onkyo AV) onHeartbeat called
2017-03-22 20:49:26.640 (Onkyo AV) Loading XML from file
2017-03-22 20:49:26.640 (Onkyo AV) XML file does not yet exist
Domoticz on RPi3 with Razberry: 10xZME WALLC-S, 14x Fib Switch, 6x Fib Wallplug, 1x Fib Motion Sensor, 2x Fib Smoke Sensor, 1x Fib Universal sensor (with 1-wire), 3x SwiidInter cordsw, ESPEasy [1-wire and relay], Kodi, Node-Red [Onkyo NR-828, UI etc]
User avatar
jorgh
Posts: 124
Joined: Friday 27 June 2014 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8224
Location: Netherlands
Contact:

Re: [RELEASED] Python plugin to control Onkyo receivers

Post by jorgh »

@swevm,

Some explanation on what should happen:
The receiver configuration is requested using an NRIQSTN command. The response contains the receiver configuration in XML, that I use to create the devices.
As the lock up issue arises when I load the XML after receiving it, I write it to a file instead. Somewhere else, this file is monitored and opened to read the XML, this does not lockup Domoticz. (no clue yet why this happens).
Now, in your case the question is:
1. Does the receiver, deliver the XML.
2. Is the XML written to file (the file is called 'XMLDataFile.xml'), maybe an access issue on the file system (on a pi, it is saved in the domoticz folder).
Could you enable the debug option in the plugin configuration and restart the plugin? It will then also log the data received from the receiver. This might give us some more insight on what's happening.
Maybe not all receivers respond to the NRIQSTN command, in which case, I need to implement alternative methods to determine what devices to create.

Edit: From the log you posted, I can see that you've probably already enabled debug. But there are no calls to onMessage. This indicates the plugin does not receive any data. The data is delivered through the plugin framework. Is anything logged if you for instance change the volume on the receiver? Can you control the receiver if you use the smartphone app from Onkyo?

Kind regards,

Jorg
swevm
Posts: 23
Joined: Friday 16 September 2016 20:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 8718
Location: Sweden
Contact:

Re: [RELEASED] Python plugin to control Onkyo receivers

Post by swevm »

@jorgh,

I figured out what the problem was. In fact its problem I have noticed on 3 different Onkyo receivers (2x 806 and the 828). The problem is that after some time, typically a few weeks or more the receiver stop returning any response when a command is sent to it. For example in Node-Red it can easily be spotted ad that module is bi-directional, meaning it catches all events generated by the receiver when for example volume level is changed manually with the knob or using other api interaction. What happen is that suddenly the event stream stop being sent out by the receiver (same behaviour observed on three different units with multiple firmware versions).

Only a pull-powerplug can fix the issue. Guess there may be api calls that can restart the system without needing a hard reboot.

Tested pull-plug fix on the 828 and now Domoticz create devices as expected.

A couple of observations:
* All devices are created as type: Light/Switch
* Master volume is in % (mine say MasterVolume is set to 56% while receiver show a value of 45 in its display

update:
* MasterVolume in Domoticz UI showed 56% but slider was at far left indicating 0%. Adjusting volume work and if manually adjusted using UI slider it stay at defined level and receiver MasterVolume is changed too. Not sure if this is a bug in the plugin or Domoticz UI.
Domoticz on RPi3 with Razberry: 10xZME WALLC-S, 14x Fib Switch, 6x Fib Wallplug, 1x Fib Motion Sensor, 2x Fib Smoke Sensor, 1x Fib Universal sensor (with 1-wire), 3x SwiidInter cordsw, ESPEasy [1-wire and relay], Kodi, Node-Red [Onkyo NR-828, UI etc]
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest