Jump to content

Play Preshow Experience from Home Assistant

Recommended Posts

You might want to look into kodi.call_method in Home Assistant.  I haven't played a movie from inside home assistant, though when I've got more time I intend to do it.  For now I have a powershell script that can play a movie, thanks to some guidance from a user on the old addon board called shredder(?).  Here's the chunk of powershell code I use to generate the call.  You can pick out your method/parameters etc from in there.  It may not be much, but it should be able to get you started.  Let me know what you come up with--so I can steal that too!

 

$data = @{
    'jsonrpc' = '2.0'
    'method' = 'Addons.ExecuteAddon'
    'id' = 1
    params = @{
        addonid = 'script.preshowexperience'
        params = @{movieid = "$pickedmovieid"}
 
    }
}
$json = $data | ConvertTo-Json -Depth 100
 
$url = 'http://'+$playerip+':'+$playerport+'/jsonrpc'
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpwd)
Invoke-WebRequest -Uri $url -Credential $mycreds -Body $json  -ContentType application/json -Method POST #-AllowUnencryptedAuthentication

 

  • Like 1
Link to comment
Share on other sites

It's been a looooong while since I've messed with this , but here is the full thing to play a movie with preshow using HA. 

 

The script below requires parameters unique to your setup at the top, replace everything inside the '' including the brackets. This script is designed to return a list of all movies that have been unwatched and does not contain any movies with the tag of christmas, holiday, or Concert. It also excludes movies with the genre of Documentary (you can change this if you like). Once a random movie is selected, a few data points are output to a csv file for history tracking, this is not required for functionality, just a fun thing to look back on the history of what has been watched. If all has gone well, the movie should start with the preshow experience sequence designated for that movie based the on criteria that is set for sequences in the preshow experience addon. 

 

#Returns a list movie and movie ID
#required param ID
$username ='[kodi web username]'
$pawd = '[kodi web userpassword]'
$hostname = '[host ip address]'
$port = '[web port number]'
$secpwd = ConvertTo-SecureString $pawd -AsPlainText -Force
$CSVfilepath = '[full path of csv file, including the name of the file]'
###########################################################################
###########################################################################
$data = @{
    'jsonrpc' = '2.0'
    'method' = 'VideoLibrary.GetMovies'
    'id' = 1
    params = @{
        properties = @('genre','playcount','year')
        filter = @{
            and = @{
                field = 'playcount'
                operator = 'lessthan'
                value = '1'
            },
            @{
                field = "tag"
                operator = "doesnotcontain"
                value = @('Christmas','Holiday','Concert')
            },
            @{
                field = "genre"
                operator = "doesnotcontain"
                value = "Documentary"
            },
            @{
                field = "tag"
                operator = "doesnotcontain"
                value = "concert" 
            }
        }
    }
}

$json = $data | ConvertTo-Json -Depth 100
$url = 'http://'+$hostname+':'+$port+'/jsonrpc'
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpwd)
$webreq = Invoke-WebRequest -Uri $url -Credential $mycreds -Body $json  -ContentType application/json -Method POST 
$unwatchedrandommovie = ($webreq.content|convertfrom-json).result.movies|get-random
###########################################################################
###########################################################################
#Store the movie ID in a variable
$pickedmovie = $unwatchedrandommovie
###########################################################################
#Append movie to CSV file for tracking
$now = Get-Date
$Content = [PSCustomObject]@{MovieName = $pickedmovie.label; DateWatched=$now; Genre=$pickedmovie.genre[0];ReleaseYear=$pickedmovie.year|out-string}
#Append to CSV
$Content| Export-Csv -Path $CSVfilepath -Append -NoTypeInformation
###########################################################################
###########################################################################
#Starts a PreShow Experience sequence
$movieid = $pickedmovie.movieid
$data = @{
    'jsonrpc' = '2.0'
    'method' = 'Addons.ExecuteAddon'
    'id' = 1
    params = @{
        addonid = 'script.preshowexperience'
        params = @{movieid = "$movieid"}

    } 
}
$json = $data | ConvertTo-Json -Depth 100

$url = 'http://'+$hostname+':'+$port+'/jsonrpc'
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpwd)
Invoke-WebRequest -Uri $url -Credential $mycreds -Body $json  -ContentType application/json -Method POST |out-null

Store this in a powershell file somewhere on your desktop or server. 

 

Next, you will need the HA agent installed on the machine that the script lives on...this is the forked version of the agent that is actively supported, but you can use the OG version if you like, but it is no longer actively being updated. 
https://github.com/hass-agent/HASS.Agent

Once installed, and configured for your HA instance; create a Powershell command with entity type 'button'..give it a name and friendly name and point to the script using the full path of the script 
spacer.png

 

Afterwards you should have a button in the MQTT integration that initiates the script...you can do whatever you like with it from here. 

I have an automation that stops whatever is playing, clears the playlist, then starts the script which kicks off the preshow sequence. If you use this automation you will need to replace the media_player names with your kodi instance name and also the name of the button to whatever you named it in the Hass Agent. 

alias: Andrew Office - Kodi - PreShow Experience - Random Movie - Everyday
sequence:
  - alias: Stop Currently Playing Item
    service: kodi.call_method
    metadata: {}
    data:
      method: Input.ExecuteAction
      action: stop
    target:
      entity_id: media_player.kodi_instancename
  - alias: Clear Current Playlist
    service: kodi.call_method
    metadata: {}
    data:
      method: Playlist.Clear
      playlistid: 0
    target:
      entity_id: media_player.kodi_instancename
  - service: button.press
    metadata: {}
    data: {}
    target:
      entity_id: button.name of button entity for kodi script
  - alias: Open Player
    service: kodi.call_method
    metadata: {}
    data:
      method: Player.Open
      item:
        playlistid: 0
        position: 0
    target:
      entity_id: media_player.kodi_instancename
mode: single
icon: mdi:kodi

Hope that helps, let me know if you have any questions. I don't come here much so you can send me a message on discord if you like. Discord name: shredder5262

Edited by shredder5262
  • Like 1
Link to comment
Share on other sites

13 hours ago, SFP_Matt said:

You might want to look into kodi.call_method in Home Assistant.  I haven't played a movie from inside home assistant, though when I've got more time I intend to do it.  For now I have a powershell script that can play a movie, thanks to some guidance from a user on the old addon board called shredder(?).  Here's the chunk of powershell code I use to generate the call.  You can pick out your method/parameters etc from in there.  It may not be much, but it should be able to get you started.  Let me know what you come up with--so I can steal that too!

 

$data = @{
    'jsonrpc' = '2.0'
    'method' = 'Addons.ExecuteAddon'
    'id' = 1
    params = @{
        addonid = 'script.preshowexperience'
        params = @{movieid = "$pickedmovieid"}
 
    }
}
$json = $data | ConvertTo-Json -Depth 100
 
$url = 'http://'+$playerip+':'+$playerport+'/jsonrpc'
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpwd)
Invoke-WebRequest -Uri $url -Credential $mycreds -Body $json  -ContentType application/json -Method POST #-AllowUnencryptedAuthentication

 

Thanks for your help !

It's been a success, i used the service "Kodi.callmethod" in Home Assistant with this YAML script :

"

service: kodi.call_method
target:
  entity_id: media_player.192_168_1_54
data:
  method: 'Addons.ExecuteAddon'
  addonid: 'script.preshowexperience'
  params:
    movieid: '140'

"

And the movie start now with PSE !

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share


 Share

×
×
  • Create New...