Turn TV on (CEC) when Playing a Video? [Solved]

Hello. I want to make my Pi send a “CECActivateSource” command whenever the Pi begins playback of a video. The peripheral settings for CEC do not have this option (only switch when booting up, and switch when deactivating the screensaver). Activating source when the screensaver goes away is not getting the job done when I push a video using Yatse on my phone.

Some Google searching informed me that this is possible using a Python script so when “xbmc.Player().isPlayingVideo()” it sends an “xbmc.executebuiltin(‘CECActivateSource’)” command. . Simple enough, right?
The problem is I have no idea how Python scripts work, what extension I should give the script’s file, or where should I put it once it is written.

Would somebody with the technical know-how be kind enough to help me out?

Edit:

Further googling suggests a script that goes something like:

import xbmc,xbmcgui
import subprocess,os

 class MyPlayer(xbmc.Player) :

 def __init__(self):
      xbmc.Player.__init__(self) 

 def onPlayBackStarted(self):
      xbmc.executebuiltin('CECActivateSource()')

But that still leaves me stumped as to what do I name the script, where do I put it, and if anything is missing.

1 Like

As i said in this topi, this was working for me on my last RaspBMC setup.
Since I used OSMC this doesn’t work anymore… I don’t know why !

Edit: Solved.
Just name this baby “autoexec.py” and place it at /home/.kodi/userdata
Works like a charm. CEC turns on my TV and switches to the correct input when I push a video to Kodi through my phone.

 import xbmc,xbmcgui

 class Player(xbmc.Player):

     def __init__(self):
         xbmc.Player.__init__(self) 
    
     def onPlayBackStarted(self):
         xbmc.executebuiltin('CECActivateSource()')

 player = Player()
 mon = xbmc.Monitor()

 while(not mon.waitForAbort(10)):
     pass
1 Like

Nice ! And nothing else ? The script it triggered automatically ?

Yep! The only downside is that my TV is kinda old and it can take up to 10 secs from powering up to switching input, and that’s 10 secs of video I missed. Aside from that, it works like a charm!

won’t work on my setup :frowning:

I noticed that when I copy&paste the code from the forum to notepad the spaces are wrong… and Python is very picky about counting spaces. So instead of copy/pasting that code try downloading it from here: Dropbox - autoexec.py - Simplify your life

Then put it at /home/.kodi/userdata and reboot.

One last thought: Perhaps the python scripts request module addon is necessary? I already had it, because it is a requirement for iStream to work. You can find it in the official Kodi repo, or DL it directly from their page: http://addons.kodi.tv/show/script.module.requests/

If it still doesn’t work after all of this, then I have no idea what I did right… I just know I got it working!

1 Like

It’s due to linebreak format on Windows.
You can convert it in Notepad++

I will try your code because this was working wirhout script before OSMC.
And Kodi still wakeup my tv but only on boot…

Edit : work for me. 2 secs to wakeup.

did you had this addon?

Never installed it. How can I check that ?

I already had it because another add-on pre-installed it, as above mentioned. I’m not certain if you need it or not for this… but if you still can’t get it to work, give that a try.