Page 4 of 8

Re: Python plugin: Kodi

Posted: Tuesday 12 December 2017 9:42
by Dnpwwo
Latest Update: v2.0.6 - December 12th, 2017 requires recent beta
  • Optionally highlights devices that can not be found on the network (red banner in web UI)
  • Supports screensaver events (Kodi 17+) and shows Kodi as 'Sleeping' when screensaver is on

Re: Python plugin: Kodi

Posted: Wednesday 27 December 2017 1:06
by jake
This is a very welcome update, it saves installing the Kodi plugin to let Kodi sent a JSON signa to Domoticz to tell when Kodi is sleeping / not sleeping.

I updated the plugin and indeed, it now has the extra option. I will keep a look on it and eventually kill the Kodi plugin and use the Domoticz plugin as an input to switch off the devices.

Re: Python plugin: Kodi

Posted: Wednesday 27 December 2017 22:34
by TheCondor
Dnpwwo wrote: Tuesday 12 December 2017 9:42 Latest Update: v2.0.6 - December 12th, 2017 requires recent beta
  • Optionally highlights devices that can not be found on the network (red banner in web UI)
  • Supports screensaver events (Kodi 17+) and shows Kodi as 'Sleeping' when screensaver is on


Thanks, finally i can manage the TVs under domoticz. Just a suggest: why not add an option in the hardware settings -> shutdown command that start the screensaver. Should be this json:

Code: Select all

{"jsonrpc":"2.0","method":"GUI.ActivateWindow","id":1,"params":{"window":"screensaver"}}

In this way it's possible to set in kodi -> cec option to shutdown tv when screensaver start, so it's possible to shutdown all by domoticz in Just one tap!

Re: Python plugin: Kodi

Posted: Friday 29 December 2017 2:02
by Dnpwwo
@TheCondor,

Not a bad idea so I've added it, I run Pi3's so never shut them down (because you can't turn them back on :() so it may be useful but it might not do what you want.

The Kodi screensaver is a bit weird because in a perfect world you would be able to:
  • Know when the screen saver activates
  • Know when the screen save deactivates
  • Be able to activate the screen saver
  • Query if the screensaver is active
with the Kodi V8 API you seem to be able to do the first 2 1/2 of the 4 (mostly).

The command you mentioned switches the window to the screen saver but not properly. Have a look at:
Spoiler: show
2017-12-29 11:02:42.065 (Тест) onCommand called for Unit 1: Parameter 'Home', Level: 0, Connected: True
2017-12-29 11:02:42.125 (Тест) GUI.OnScreensaverDeactivated recieved, Player ID: -1
2017-12-29 11:05:42.537 (Тест) GUI.OnScreensaverActivated recieved, Player ID: -1
2017-12-29 11:05:43.166 EventSystem: script device trigger (Test)
2017-12-29 11:05:43.166 (Тест) Update 9:'Screensaver' (Тест - Status)
2017-12-29 11:06:08.202 User: Admin initiated a switch command (42/Тест - Status/Off)
2017-12-29 11:06:08.261 (Тест) onCommand called for Unit 1: Parameter 'Off', Level: 0, Connected: True
2017-12-29 11:06:08.313 (Тест) GUI.OnScreensaverDeactivated recieved, Player ID: -1
2017-12-29 11:06:08.528 EventSystem: script device trigger (Test)
2017-12-29 11:06:08.529 (Тест) Update 1:'' (Тест - Status)
2017-12-29 11:06:08.729 (Тест) Sleep command accepted.
2017-12-29 11:06:08.986 EventSystem: script device trigger (Test)
2017-12-29 11:06:08.987 (Тест) Update 9:'Screensaver' (Тест - Status)
2017-12-29 11:17:16.395 (Тест) GUI.OnScreensaverDeactivated recieved, Player ID: -1
2017-12-29 11:17:17.004 EventSystem: script device trigger (Test)
2017-12-29 11:17:17.005 (Тест) Update 1:'' (Тест - Status)
Normally Activate and Deactivate events are paired (as you would expect) but setting the window Deactivates the screen saver (regardless of whether it is on or not) but there is never an Activate event afterwards. Even more strange is that the log shows another Deactivate later when I hit a key on a remote. This may be a bug on the Kodi end.

It will look okay on the Domoticz end because I have hardcoded the Kodi to show sleeping if you tell it to switch to the screensaver but have no idea what it will mean for CEC.

Version 2.1.6 pushed to the git repository.

Re: Python plugin: Kodi

Posted: Saturday 30 December 2017 11:30
by TheCondor
Dnpwwo wrote: Friday 29 December 2017 2:02 @TheCondor,

Not a bad idea so I've added it, I run Pi3's so never shut them down (because you can't turn them back on :() so it may be useful but it might not do what you want.

The Kodi screensaver is a bit weird because in a perfect world you would be able to:
  • Know when the screen saver activates
  • Know when the screen save deactivates
  • Be able to activate the screen saver
  • Query if the screensaver is active
with the Kodi V8 API you seem to be able to do the first 2 1/2 of the 4 (mostly).

The command you mentioned switches the window to the screen saver but not properly. Have a look at:
Spoiler: show
2017-12-29 11:02:42.065 (Тест) onCommand called for Unit 1: Parameter 'Home', Level: 0, Connected: True
2017-12-29 11:02:42.125 (Тест) GUI.OnScreensaverDeactivated recieved, Player ID: -1
2017-12-29 11:05:42.537 (Тест) GUI.OnScreensaverActivated recieved, Player ID: -1
2017-12-29 11:05:43.166 EventSystem: script device trigger (Test)
2017-12-29 11:05:43.166 (Тест) Update 9:'Screensaver' (Тест - Status)
2017-12-29 11:06:08.202 User: Admin initiated a switch command (42/Тест - Status/Off)
2017-12-29 11:06:08.261 (Тест) onCommand called for Unit 1: Parameter 'Off', Level: 0, Connected: True
2017-12-29 11:06:08.313 (Тест) GUI.OnScreensaverDeactivated recieved, Player ID: -1
2017-12-29 11:06:08.528 EventSystem: script device trigger (Test)
2017-12-29 11:06:08.529 (Тест) Update 1:'' (Тест - Status)
2017-12-29 11:06:08.729 (Тест) Sleep command accepted.
2017-12-29 11:06:08.986 EventSystem: script device trigger (Test)
2017-12-29 11:06:08.987 (Тест) Update 9:'Screensaver' (Тест - Status)
2017-12-29 11:17:16.395 (Тест) GUI.OnScreensaverDeactivated recieved, Player ID: -1
2017-12-29 11:17:17.004 EventSystem: script device trigger (Test)
2017-12-29 11:17:17.005 (Тест) Update 1:'' (Тест - Status)
Normally Activate and Deactivate events are paired (as you would expect) but setting the window Deactivates the screen saver (regardless of whether it is on or not) but there is never an Activate event afterwards. Even more strange is that the log shows another Deactivate later when I hit a key on a remote. This may be a bug on the Kodi end.

It will look okay on the Domoticz end because I have hardcoded the Kodi to show sleeping if you tell it to switch to the screensaver but have no idea what it will mean for CEC.

Version 2.1.6 pushed to the git repository.

you are right, the rpc screensaver command is broken in some way, or works in a different way than the same command works locally.
Much pepole claim that RPC API need to support CEC direct command but these posts are at least one year older so probably will never be implemented.
I've found a workaround, i'll send you in private maybe you can integrate in some way in your script. Thanks and happy new year!

Re: Python plugin: Kodi

Posted: Saturday 13 January 2018 14:27
by TheCondor
Hi, i've found this way and it's working for send the screensaver command to a kodi box.
My box need to be always on, otherwise i cannot wake up from sleep and need to unplug/plug the wire, it's the same as a raspberry, but in the kodi options (libreelec), system, input, cec adapter i can set this options for shutdown tv:

DEVICE TO POWER ON AT START -> TV
DEVICE TO SHUTDOWN AT STOP -> TV
SEND "INACTIVE SOURCE" COMMAND AT STOP
PUT DEVICE IN STANDBY WHEN SCREENSAVER START -> YES
WAKE UP DEVICE WHEN SCREENSAVER DISACTIVE -> YES
WHEN TV IS OFF -> STOP PLAYING
WHEN SOURCE CHANGE -> STOP PLAYING


and with this bash script i can connect trough ssh to the box and send kodi the command to start screensaver:

Code: Select all

#!/bin/bash
SSHPASS='libreelec' sshpass -e ssh [email protected] "sleep 2; kodi-send --action="Stop" && sleep 2; kodi-send --action="ActivateScreensaver""
you need to install sshpass on the domoticz machine, but probably it's possible to send the password using default ssh and a bit of scripting.
With all of this i can use the tv remote and also domoticz to controll all.

Re: Python plugin: Kodi

Posted: Saturday 20 January 2018 17:20
by EdwinK
Can I use this plugin for two (or more) Kodi devices ?

Re: Python plugin: Kodi

Posted: Saturday 20 January 2018 22:16
by Dnpwwo
@EdwinK,

Just create multiple instances of the hardware plugin, I run three and they don't clash with each other.

Re: Python plugin: Kodi

Posted: Saturday 20 January 2018 23:01
by EdwinK
Okay. Thanks. Will do.

Re: Python plugin: Kodi

Posted: Tuesday 30 January 2018 19:19
by Kivi
After installing this plugin I'm getting this error in domoticz log whit debug turned on

Code: Select all

 2018-01-30 20:10:33.134 (HTPC) Calling message handler 'onHeartbeat'.
2018-01-30 20:10:33.134 (HTPC) Sending 62 bytes of data:.
2018-01-30 20:10:33.134 (HTPC) 7b 22 6a 73 6f 6e 72 70 63 22 3a 22 32 2e 30 22 2c 22 6d 65 {"jsonrpc":"2.0","me
2018-01-30 20:10:33.134 (HTPC) 74 68 6f 64 22 3a 22 50 6c 61 79 65 72 2e 47 65 74 41 63 74 thod":"Player.GetAct
2018-01-30 20:10:33.134 (HTPC) 69 76 65 50 6c 61 79 65 72 73 22 2c 22 69 64 22 3a 31 30 30 ivePlayers","id":100
2018-01-30 20:10:33.134 (HTPC) 31 7d .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 1}
2018-01-30 20:10:33.184 (HTPC) Received 68 bytes of data:.
2018-01-30 20:10:33.184 (HTPC) 7b 22 69 64 22 3a 31 30 30 31 2c 22 6a 73 6f 6e 72 70 63 22 {"id":1001,"jsonrpc"
2018-01-30 20:10:33.184 (HTPC) 3a 22 32 2e 30 22 2c 22 72 65 73 75 6c 74 22 3a 5b 7b 22 70 :"2.0","result":[{"p
2018-01-30 20:10:33.185 (HTPC) 6c 61 79 65 72 69 64 22 3a 31 2c 22 74 79 70 65 22 3a 22 76 layerid":1,"type":"v
2018-01-30 20:10:33.185 (HTPC) 69 64 65 6f 22 7d 5d 7d .. .. .. .. .. .. .. .. .. .. .. .. ideo"}]}
2018-01-30 20:10:33.185 (HTPC) Calling message handler 'onMessage'.
2018-01-30 20:10:33.185 Error: (HTPC) 'onMessage' failed 'TypeError':'onMessage() takes 2 positional arguments but 4 were given'. 

Re: Python plugin: Kodi

Posted: Tuesday 30 January 2018 21:50
by Dnpwwo
@Kivi,

The version of Domoticz you are running is too old.

Re: Python plugin: Kodi

Posted: Sunday 11 February 2018 11:05
by Dnpwwo
@TheCondor,

New version of Kodi plugin (2.2.1) pushed to github that activates the screensaver using the proper "ActivateScreensaver" function via the UDP interface.

Saves you using SSH and a bash script.

Re: Python plugin: Kodi

Posted: Sunday 11 February 2018 20:29
by jake
Thanks for the extension of the plugin!

May I throw in my old request again: the option to give Kodi a 'rewind' command?

My stereo is turned on as soon as Domoticz detects that Kodi plays audio/video. However, before the stereo outputs sound, the track already is at 10 seconds. I would prefer to 'pause', turn on the receiver, wait x seconds, 'REWIND' and 'play' again. This would greatly improve the user experience and the WAF ;-)

'pause' and 'play' are already implemented, therefore the 'rewind' command would be very welcome to improve the script!

Re: Python plugin: Kodi

Posted: Tuesday 13 February 2018 11:16
by Dnpwwo
Bug Fix version 2.2.3 added that resolved issue with source selector and added 'Weather' as an option.

@jake,

From a Domoticz script I think you could already do something like:
  • Pause
  • Turn on reciever
  • Send 'Set Level 0 AFTER 10' to the '<myKodi'sName> - Playing' device (this seeks to start of the file)
  • Play

Re: Python plugin: Kodi

Posted: Tuesday 13 February 2018 11:56
by TheCondor
Dnpwwo wrote: Sunday 11 February 2018 11:05 @TheCondor,

New version of Kodi plugin (2.2.1) pushed to github that activates the screensaver using the proper "ActivateScreensaver" function via the UDP interface.

Saves you using SSH and a bash script.
:) Thanks awesome integration. This kodi plugin should now replace the default IMHO

Re: Python plugin: Kodi

Posted: Wednesday 14 February 2018 22:55
by jake
Dnpwwo wrote:Bug Fix version 2.2.3 added that resolved issue with source selector and added 'Weather' as an option.

@jake,

From a Domoticz script I think you could already do something like:
  • Pause
  • Turn on reciever
  • Send 'Set Level 0 AFTER 10' to the '<myKodi'sName> - Playing' device (this seeks to start of the file)
  • Play
Ah, good idea.

However, I don't get it to work

First of all, this is the first time I am trying to control Kodi, where I before acted on the status of Kodi only
Secondly, I use dzVents, which has kodi commands, but obviously pointed towards the standard 'Kodi media server'. I tried to rework that in the the deviceadapter folder of dzVents, but so far no luck.

Where the integrated Kodi media server had 1? device, your plugin creates several. Which device should I use to pause/play?
What is the code for it in standard LUA? I tried this:

Code: Select all

commandArray['Kodi Woonkamer - Status']='Pause'
or

Code: Select all

commandArray['Kodi Woonkamer - Playing']='Pause'
but that piece of code doesn't do anything. Can this be related to the fact that the status device is just started showing 'Audio' (my trigger) and therefore 'pause' comes to soon after 'play'?

Can I use

Code: Select all

commandArray['Kodi Woonkamer - Status']='Play AFTER' .. variablexyz
It doesn't seem to do anything either.

Re: Python plugin: Kodi

Posted: Thursday 15 February 2018 9:32
by TheCondor
Dnpwwo wrote: Sunday 11 February 2018 11:05 @TheCondor,

New version of Kodi plugin (2.2.1) pushed to github that activates the screensaver using the proper "ActivateScreensaver" function via the UDP interface.

Saves you using SSH and a bash script.
I was think yesterday evening on something usefull maybe:

add an option in the hardware plugin panel -> RASPBERRY/AMLOGIC and when selected map the 'status' device in this way:

when screensaver is on (sleeping) -> the selector appear as OFF (like it appear with ping failed)
when screensaver is off (source on) -> selector appear as currently.

In this way maybe is possible to let the status switch works in two different states:

SWITCH ON -> send screensaver INACTIVE
SWITCH OFF ->send screensaver ACTIVE (like now after your update).


The "bug" is thate currently, when the source is sleeping and you click (or tap in domotociz app) the status device it send again the SCREENSAVER ON command and the kodi box wakeup and istantly goes sleep again.
Let me know if you think this could be usefull, we are all here for help you as better as we can :)

Re: Python plugin: Kodi

Posted: Saturday 17 February 2018 2:05
by Dnpwwo
@jake,

I haven't looked at dzVents but I would recommend doing it simple Lua first then converting to dzVents if you want to.

Play and Pause commands can be sent to any of the devices in Domoticz and they will be applied to the currently playing media. If there is nothing playing the commands were silently ignored, I've added an log message now when that happens. I would suggest:

Code: Select all

commandArray['Kodi Woonkamer - Playing']='Pause AFTER 2'
though to make sure that timing is not an issue. From what you have said the amp will still be turning on so you won't hear the audio.

I realised that

Code: Select all

commandArray['Kodi Woonkamer - Playing']='Set Level 0 AFTER 10'
won't work because Domotics interprets a 'set level 0' command as an 'Off' command which isn't great so I added a 'Rewind' command as you suggested. I didn't have time to test it but it just seeks to '0' so it should work.

You will need to download version 2.3.11 from https://github.com/dnpwwo/Domoticz-Kodi-Plugin

If you are still having issues turn debug on execute you script and post the output

Re: Python plugin: Kodi

Posted: Saturday 17 February 2018 10:50
by jake
I will indeed go for a stripped down LUA script, purely to test functionality, before adding it to a complex dzVents script. Too many trigger influence that script during execution of the kodi command. I think I will add a multi selector with play paus rewind.

Re: Python plugin: Kodi

Posted: Saturday 17 February 2018 11:28
by jake
I just created the proposed script and virtual switch selector:

Code: Select all

--test functionality of kodi python plugin
-- add a switch selector with name 'Koditest'
-- Define 3 buttons, Pause, Rewind and Play

commandArray = {} 


if devicechanged['Koditest'] == 'Pause' then
	commandArray['Kodi Woonkamer - Playing']='Pause AFTER 2'
	print('given the Pause command after 2 sec')
elseif devicechanged['Koditest'] == 'Rewind' then
	commandArray['Kodi Woonkamer - Playing']='Rewind'
	print('given the rewind command')
elseif devicechanged['Koditest'] == 'Play' then
	commandArray['Kodi Woonkamer - Playing']='Play'
	print('given the Play command')
end

return commandArray
Pressing the Pause button, gave the exptected reaction

Pressing the Play button, didn't make Kodi go 'Play', but give this message in the log:

Code: Select all

2018-02-17 11:15:28.229 User: Admin initiated a switch command (285/Koditest/Set Level)
2018-02-17 11:15:28.238 (Virtual Devices) Light/Switch (Koditest)
2018-02-17 11:15:28.498 LUA: given the Play command
2018-02-17 11:15:28.500 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_Koditest.lua
2018-02-17 11:15:28.570 (Kodi Woonkamer) onCommand called for Unit 4: Parameter 'Play', Level: 0, Connected: True
The status device displays 'Pause'
The playing device displays 'Off' (while the track was at 70%)

While still in this state, I pressed the 'Rewind' button. Nothing happens, the log message is quite similar

Code: Select all

2018-02-17 11:22:27.776 (Kodi Woonkamer) onCommand called for Unit 4: Parameter 'Rewind', Level: 0, Connected: True
With the Kodi remote I put the track to Play again and that works

Then I clicked on my selector swich Play button (while Kodi was playing already). The log:

Code: Select all

2018-02-17 11:29:53.516 User: Admin initiated a switch command (285/Koditest/Set Level)
2018-02-17 11:29:53.525 (Virtual Devices) Light/Switch (Koditest)
2018-02-17 11:29:53.781 LUA: given the Play command
2018-02-17 11:29:53.783 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_Koditest.lua
2018-02-17 11:29:53.807 (Kodi Woonkamer) onCommand called for Unit 4: Parameter 'Play', Level: 0, Connected: True
2018-02-17 11:29:53.963 (Kodi Woonkamer) Update 2:'William Walton:Symphony no 1' (Kodi Woonkamer - Status)
2018-02-17 11:29:53.973 (Kodi Woonkamer) Update 0:'71' (Kodi Woonkamer - Playing)
2018-02-17 11:29:55.231 (Kodi Woonkamer) Update 0:'72' (Kodi Woonkamer - Playing)
The status device displays 'Pause'
The playing device displays 'Off' (while the track was at 72%). The 'Off' seems normal, since it does the same when I 'Pause' with the Kodi remote.