return {
|
|
on = {
|
|
timer = {
|
|
'every 5 minutes'
|
|
},
|
|
devices = {
|
|
'Kodi_Wohnzimmer',
|
|
-- 'Kodi_Wohnzimmer_Audioausgang'
|
|
},
|
|
httpResponses = {
|
|
'trigger_kodi', -- must match with the callback passed to the openURL command
|
|
}
|
|
},
|
|
data = {
|
|
turn_off_time = { initial = 0 },
|
|
old_state = { initial = 'Off' },
|
|
old_data = { initial = 'Init' },
|
|
turn_off_counter = { initial = 0 }
|
|
},
|
|
|
|
execute = function(domoticz, item)
|
|
|
|
local volume_low = "{\"jsonrpc\":\"2.0\", \"id\":1, \"method\":\"Application.SetVolume\", \"params\":{\"volume\":50}}"
|
|
local volume_high = "{\"jsonrpc\":\"2.0\", \"id\":1, \"method\":\"Application.SetVolume\", \"params\":{\"volume\":100}}"
|
|
local output_HiFi = "{\"jsonrpc\":\"2.0\", \"id\":1, \"method\":\"Settings.SetSettingValue\", \"params\":{\"setting\":\"audiooutput.audiodevice\", \"value\":\"ALSA:@\"}}"
|
|
local output_HDMI = "{\"jsonrpc\":\"2.0\", \"id\":1, \"method\":\"Settings.SetSettingValue\",\"params\":{\"setting\":\"audiooutput.audiodevice\", \"value\":\"PI:HDMI\"}}"
|
|
local active_players = "{\"jsonrpc\":\"2.0\", \"id\":1, \"method\":\"Player.GetActivePlayers\"}"
|
|
local update_time_max = 10 -- Time to wait until the amp is turned off.
|
|
local turn_off_counter_max = 5
|
|
|
|
-- Kodi JSON interface functions.
|
|
|
|
local function setKodiValues(postData)
|
|
domoticz.openURL(
|
|
{
|
|
url = 'http://192.168.1.4:8080/jsonrpc',
|
|
method = 'POST',
|
|
headers = { ['Content-type'] = 'application/json' },
|
|
postData = postData
|
|
}
|
|
)
|
|
end
|
|
|
|
local function getKodiValues(postData)
|
|
domoticz.openURL(
|
|
{
|
|
url = 'http://192.168.1.4:8080/jsonrpc',
|
|
method = 'POST',
|
|
headers = { ['Content-type'] = 'application/json' },
|
|
postData = postData,
|
|
callback = 'trigger_kodi'
|
|
}
|
|
)
|
|
end
|
|
|
|
|
|
current_state = domoticz.devices('Kodi_Wohnzimmer').state
|
|
current_data = domoticz.devices('Kodi_Wohnzimmer').rawData[1] -- Type of media being played.
|
|
|
|
-- Cross check if Kodi is really playing video, sometimes is just audio but video is shown.
|
|
|
|
if((domoticz.data.old_state ~= "Video" and current_state == "Video") or current_data ~= domoticz.data.old_data) then -- Right now this is not working, because the script does not get triggered on song change.
|
|
getKodiValues(active_players)
|
|
end
|
|
|
|
if (item.isHTTPResponse and item.ok) then
|
|
if (string.match(item.data, 'video')) then
|
|
media_type = "Movie"
|
|
elseif (string.match(item.data, 'audio')) then
|
|
media_type = "Music"
|
|
else
|
|
media_type = "Unknown"
|
|
end
|
|
end
|
|
|
|
-- Turn on the amp, if Kodi is playing media.
|
|
|
|
if ((current_state == 'Audio' or current_state == 'Video') and domoticz.devices('Steckdose_Verstaerker').state == 'Off') then
|
|
domoticz.log('Kodi spielt Audio oder Video ab, schalte den Verstärker ein.')
|
|
if (domoticz.devices('Steckdose_Verstaerker').state == 'On') then -- Wenn die Steckdose an war aber der Verstärker im Standby ist.
|
|
domoticz.devices('Steckdose_Verstaerker').switchOff()
|
|
end
|
|
domoticz.devices('Steckdose_Verstaerker').switchOn()
|
|
setKodiValues(volume_low) -- Reduce the volume.
|
|
setKodiValues(output_HiFi) -- Switch Kodi audio output to amp.
|
|
end
|
|
|
|
-- Turn on the TV, if Kodi is playing video.
|
|
|
|
if (current_state == 'Video' and media_type == "Movie" and domoticz.devices('Steckdose_TV').state == 'Off') then
|
|
domoticz.log('Kodi spielt Video ab, schalte den Fernseher ein.')
|
|
domoticz.devices('Steckdose_TV').switchOn()
|
|
elseif (((current_state == 'Video' and media_type == "Music") or current_state == "Audio") and domoticz.devices('Steckdose_TV').state == 'On') then
|
|
domoticz.log('Kodi spielt Audio ab, schalte den Fernseher aus.')
|
|
domoticz.devices('Steckdose_TV').switchOff()
|
|
end
|
|
|
|
-- If Kodi is playing nothing turn off TV and amp.
|
|
|
|
if ((current_state == 'On' or current_state == 'Off' or current_state == 'Unknown') and (domoticz.devices('Steckdose_Verstaerker').state == 'On' or domoticz.devices('Steckdose_TV').state == 'On')) then
|
|
if (domoticz.time.matchesRule('every 5 minutes')) then
|
|
domoticz.data.turn_off_counter = domoticz.data.turn_off_counter + 1
|
|
end
|
|
else
|
|
domoticz.data.turn_off_counter = 0
|
|
end
|
|
|
|
if (domoticz.data.turn_off_counter > turn_off_counter_max) then
|
|
domoticz.log('Es wird nichts gespielt. schalte den Verstärker und Fernseher aus.')
|
|
domoticz.devices('Steckdose_Verstaerker').switchOff()
|
|
domoticz.devices('Steckdose_TV').switchOff()
|
|
domoticz.data.turn_off_counter = 0
|
|
end
|
|
|
|
|
|
domoticz.data.old_state = current_state
|
|
domoticz.data.old_data = current_data
|
|
|
|
end
|
|
}
|