Sony receiver: STR-DN1080 (linked using JSON)

Others (MiLight, Hue, Toon etc...)

Moderator: leecollings

Post Reply
bimbs
Posts: 3
Joined: Saturday 23 January 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Sony receiver: STR-DN1080 (linked using JSON)

Post by bimbs »

I want to share my struggles with the linking of my Sony STR-DN1080 receiver to Domoticz using JSON.
Since it is quite a popular receiver, someone else might find this information useful.
(I could have used some details when I started this process... could have saved me 10 hours of testing)

My main goal was that I would like to set the Sound settings to "AFD" or "2 channel" instead of "multichannel stereo" which seems to be the default setting when listening to music (which is the only reason that I need the remote). Additionally I would like to be able to set the input to "BD-DVD" for Kodi without having to scroll through them with my TV-remote. The additional downside of my EU model is that I cannot "Power on" using JSON, so I still have to get WOL working (next project, since it doesn't seem to respond yet).

I found out that there are a few Sony receivers/sound bars/speakers that accept instructions using JSON, being:
STR-DN1080
SRS-ZR5
HT-MT500
HT-CT800
HT-ST5000
HT-ZF9

Details on the available instructions can be found on the site below:
https://developer.sony.com/develop/audi ... ew-content

Required tools to test: postman
https://getpostman.com/apps

You should start with fixing the IP address of the receiver (for example using "address reservation" in your router, or in the network settings of the receiver itself).

In postman, start with retrieving the supported API instructions so you get an idea what you can do.
You should use "POST" in all JSON instructions
Address should be: http://192.168.x.xxx:10000/sony/guide (replace with whatever IP you gave the receiver)
Use: raw
Type: JSON (application/json)

Code: Select all

{
 "method":"getSupportedApiInfo",
 "id":5,
 "params":[
  {
   "services":[
    
   ]
  }
 ],
 "version":"1.0"
}
Save the output for future reference.

After testing using Postman I got "setting soundfield" working (it is so sensitive about everything).
Below my raw postman code.

Address:
POST / http://192.168.x.xxx:10000/sony/audio

Code: Select all

{
 "method":"setSoundSettings",
 "id":5,
 "params":[
  {
   "settings":[
    {
     "value":"audioFormatDecoding",
     "target":"soundField"
    }
   ]
  }
 ],
 "version":"1.1"
}
How to get this working in Domoticz. I'm not too good at scripting, but I at least managed to get it working.
You need to create a curl script for every JSON action (there must be smarter solutions, so enlighten me if you know some).

In Postman when you click on "Code", you can select to see the cURL code for the JSON instruction.
In this case you will get the following code (of course I removed the IP address and token).

Code: Select all

curl -X POST \
  http://192.168.0.xxx:10000/sony/audio \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: xxxxxxxxxxxxxxxxxxxxxxxxxxxx' \
  -H 'cache-control: no-cache' \
  -d '{
 "method":"setSoundSettings",
 "id":5,
 "params":[
  {
   "settings":[
    {
     "value":"audioFormatDecoding",
     "target":"soundField"
    }
   ]
  }
 ],
 "version":"1.1"
}'
Since I've got Domoticz (latest beta) running on a Rpi, I went to the scripts folder and created a script named "sony-afd.sh) using.

Code: Select all

sudo nano sony-afd.sh 
To get it working I had to /bin/bash it.

Code: Select all

#!/bin/bash

curl -X POST \
  http://192.168.0.xxx:10000/sony/audio \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: xxxxxxxxxxxxxxxxxxxxxxxxxxxx' \
  -H 'cache-control: no-cache' \
  -d '{
 "method":"setSoundSettings",
 "id":5,
 "params":[
  {
   "settings":[
    {
     "value":"audioFormatDecoding",
     "target":"soundField"
    }
   ]
  }
 ],
 "version":"1.1"
}'
Save the file (CTRL-X / Yes) and make it executable using chmod +x

Code: Select all

sudo chmod +x sony-afd.sh
You can now test it on the commandline using:

Code: Select all

./sony-afd.sh
Now in Domoticz create a virtual button ("switch selector" in my case, since I want to use multiple scripts).
In "Level Action" you can now link to the script that you created, using:

Code: Select all

script://sony-afd.sh
Swich Selector.JPG
Swich Selector.JPG (99.74 KiB) Viewed 3018 times
I hope that someone finds this information useful. Next thing is to get information on the current status into Domoticz.
Please let me know if you have any improvements or suggestions since this is the first time I share something I created and I'm quite sure it can be done better/easier/smarter.
Thorgal789
Posts: 852
Joined: Wednesday 15 August 2018 14:38
Target OS: -
Domoticz version:
Contact:

Re: Sony receiver: STR-DN1080 (linked using JSON)

Post by Thorgal789 »

Harr, I have a SRS-ZR5 and I had same problems than you some month ago, I have make exactly like you but with python and for other command (like on/off) and I haven't take a look at all on this lib part (the "audio" one).

I will not make a post as explanatory and clear than you (not usable as it, too much thing are hardcoded), but the code is

Code: Select all

#!/usr/bin/env python3
# coding: utf-8 -*-

# https://developer.sony.com/develop/audio-control-api/hardware-overview/api-overview
# python3 action.py

import json
import urllib.request
import urllib.parse
import sys

url_System = 'http://192.168.1.97:54480/sony/system'
url_Content = 'http://192.168.1.97:54480/sony/avContent'


Json_Off = '''{
 "method":"setPowerStatus",
 "id":1,
 "params":[
  {
   "status":"off"
  }
 ],
 "version":"1.1"
}
'''

Json_On = '''{
 "method":"setPowerStatus",
 "id":1,
 "params":[
  {
   "status":"active"
  }
 ],
 "version":"1.1"
}
'''

Json_StandBy = '''{
 "method":"setPowerStatus",
 "id":1,
 "params":[
  {
   "standbyDetail":"",
   "status":"standby"
  }
 ],
 "version":"1.1"
}
'''

Json_GetSource = '''
{
 "method":"getSourceList",
 "id":1,
 "params":[
  {
   "scheme":"storage"
  }
 ],
 "version":"1.2"
}
'''

Json_GetPlayingContent = '''
{
 "method":"getSourceList",
 "id":1,
 "params":[
  {
   "scheme":"storage"
  }
 ],
 "version":"1.2"
}
'''

Json_Set_USB = '''
{
 "method":"setPlayContent",
 "id":1,
 "params":[
  {
   "output":"extOutput:zone?zone=1",
   "uri":"storage:usb1"
  }
 ],
 "version":"1.2"
}
'''

Json_Set_Net = '''
{
 "method":"setPlayContent",
 "id":1,
 "params":[
  {
   "output":"extOutput:zone?zone=1",
   "uri":"storage:usb1"
  }
 ],
 "version":"1.2"
}
'''

Json_Set_Volume = '''
{
 "id":"1",
 "method":"setAudioVolume",
 "params":[
  {
   "output":"extOutput:zone?zone=1",
   "volume":"5"
  }
 ],
 "version":"v1.1"
}
'''

command = sys.argv[1]
if command == "on":
    _json = Json_On
    url = url_System
elif command == "off":
    _json = Json_Off
    url = url_System
elif command == "usb":
    _json = Json_Set_USB
    url = url_Content
elif command == "net":
    _json = Json_Set_Net
    url = url_Content
else:
    print ('Unknow command')
    sys.exit(0)
    
_json = _json.replace('\n','').replace(' ','')
#print (_json)

params = _json
params = params.encode('utf-8')

headers = {'Content-Type' : 'text/plain;charset=UTF-8','Accept' : '*/*'}

request = urllib.request.Request(url=url, data=params, headers=headers)
response = urllib.request.urlopen(request)

print(response.read().decode('utf8'))
bimbs
Posts: 3
Joined: Saturday 23 January 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Sony receiver: STR-DN1080 (linked using JSON)

Post by bimbs »

Hi Thorgal,

Thanks for sharing!

If I understand correctly you can do multiple actions with this one script. This way it's much easier to maintain.
(I can do some VBA, but no Python)

In Domoticz, do you activate it like below?

Code: Select all

script://action.py "usb"
Thanks,
Tjeerd
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Sony receiver: STR-DN1080 (linked using JSON)

Post by Nautilus »

Hi,

maybe you have missed this: https://github.com/rytilahti/python-songpal

It should support:
  • HT-XT3
  • HT-NT5
  • HT-MT500
  • HT-ZF9
  • SRS-X77, SRS-X88
  • STR-DN1060, STR-DN1070, STR-DN1080
I have a Harmony remote and thus so far I've only used the Sony Audio Control Api (https://developer.sony.com/develop/audio-control-api/) for setting a specific volume level (for some reason the songpal plugin did not work for +/- volume adjustments, but there's been some updates so I should test it again).

pysongpal is also used for integrating supported devices to Home Assistant so it probably could be used to create a python plugin for Domoticz as well. Unfortunately I'm not capable of doing that so for the time being I'll be using it via scripts and manually added custom devices.
Thorgal789
Posts: 852
Joined: Wednesday 15 August 2018 14:38
Target OS: -
Domoticz version:
Contact:

Re: Sony receiver: STR-DN1080 (linked using JSON)

Post by Thorgal789 »

@bimbs, yes I m using in action level.

Code: Select all

script:///home/pi/hp/action.py off
@Nautilus, yep I have missed it too ^^. Good code, but useless for me. I thought make a plugin for that, but I prefer the @bimbs method.

This code is realy usefull when you don't know how to do, but it's easy to do just with 10 lines of code with natives fonctions.
I m using this code just to turn off the speaker, or select mp3 device, I realy don't need a python plugin that use a library that use others libraries when I can do that just with a 10 lines script that don't stay in memory, faster an lighter.

I m agree it s not ready to use, but a topic as clear than @Bimbs 's one with more explanation is more usefull for me for domoticz.

BTW this lib will be easier to use in a script too, than a plugin, using python and using python framework in domoticz is not the same thing at all.

But it's not my topic, if bimbs want to make a plugin with it, it's his.
bimbs
Posts: 3
Joined: Saturday 23 January 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Sony receiver: STR-DN1080 (linked using JSON)

Post by bimbs »

@Thorgal789, I don't have any plans building a plugin (I wouldn't even know where to start, nor have the time for it). I googled some python basics today and I think I know understand your code (especially the urllib part), but I also understand now that it would take a lot more time to be able to just create some basic script that I could use. :lol: Respect for what you made!

@Nautilus, I missed it too although I don't really understand yet how I could easily integrate this in Domoticz. For me creating a few different cURL scripts for different functions will suffice.

Maybe I'll try the code to get Wake-On-Lan working, as it seems I cannot get the receiver to react to magic packets. Strange thing is that there are android apps (MyAV) that are actually doing exactly that. I noticed a remark on getting "quickstartmode" on, so I think I'll try that.
Fgerrits
Posts: 5
Joined: Monday 14 September 2020 17:41
Target OS: NAS (Synology & others)
Domoticz version: 2020.1
Contact:

Re: Sony receiver: STR-DN1080 (linked using JSON)

Post by Fgerrits »

Is there a way to let this running on a Synology Nas , my Domoticz is running on the nas and i don't want to create a Raspberry with domoticz on it.
ricorico94
Posts: 94
Joined: Monday 26 October 2015 10:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Sony receiver: STR-DN1080 (linked using JSON)

Post by ricorico94 »

@Bimbs: I also own a STR-DN1080 in EU version.. I could never enable the WOL: would you by chance have found a way to wake it up ? I was thinking about adding an IR gtw, but it's a shame..

I had found this forum with some hints and ideas -they seem having succeeded-, but I'm not sure how to use their findings..
https://forum.developer.sony.com/topic/ ... -dn1080/18


br,
Ricorico94
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest