sufreak Wrote:Is there a way to add a hotkey to the keyboard keymap to activate a sleep timer?
I want to program my Harmony with a "Sleep Timer" command that will enable the sleep functionality on all my devices. I know the Harmony won't know everything is off, but I can deal with that.
I just did that
! I did it in a couple of minutes so its not perfect but can't waste more time on this.
I'll explain what to do for OSX, but I think it'll work for other OS.
1) Created a script called sleeptimer
in /Applications/XBMC.app/Contents/Resources/XBMC/addons/script.sleeptimer
I've created SleepTimer.py and addon.xml
addon.xml:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.sleeptimer"
name="Sleep Timer XBMC Script"
version="0.0.1"
provider-name="edd">
<requires>
<import addon="xbmc.python" version="1.0"/>
</requires>
<extension point="xbmc.python.library"
library="SleepTimer.py" />
<extension point="xbmc.addon.metadata">
<summary lang="en"></summary>
<description lang="en"></description>
<platform>all</platform>
</extension>
</addon>
SleepTimer.py
Code:
import xbmc
import re
class Main:
def _cancel_alarm( self ):
if self._is_timer_on():
xbmc.executebuiltin( "XBMC.CancelAlarm(shutdowntimer,true)")
def _set_timer( self, time ):
self._cancel_alarm()
xbmc.executebuiltin( "XBMC.AlarmClock(shutdowntimer,XBMC.Shutdown(),%d,false)" % ( time, ) )
def _start_sleep_timer ( self ):
self._set_timer(180)
def _toggle_sleep_timer( self ):
shutdown_time = self._shutdown_time()
self._cancel_alarm()
if (shutdown_time-0.0001) > 10:
new_shutdown_time = (shutdown_time-0.001)//10*10
self._set_timer(new_shutdown_time)
def _is_timer_on( self ):
timer = xbmc.getCondVisibility('System.HasAlarm(shutdowntimer)')
return timer
def _shutdown_time( self ):
shutdown_time_str = xbmc.getInfoLabel('system.alarmpos')
splitter = re.compile(r'[\D]')
shutdown_time_str_tkns = splitter.split(shutdown_time_str)
shutdown_time = int(shutdown_time_str_tkns[0])
return shutdown_time
def __init__( self ):
if self._is_timer_on():
self._toggle_sleep_timer()
else:
self._start_sleep_timer()
if ( __name__ == "__main__" ):
Main()
Ok now that we have a script that performs that simple sleep timer we are used to, we can link it to a shortcut.
2) Add hotkey to script
in joystick.Harmony.xml at /Applications/XBMC.app/Contents/Resources/XBMC/system/keymaps I have
I want to perform this action anytime so I placed the hotkey in the global section of keymap:
Code:
<keymap>
<global>
<joystick name="Harmony">
...
Calls the script SleepTimer.py
Code:
...
<!-- Sleep --> <button id="46">XBMC.RunScript(script.sleeptimer)</button>
...
</joystick>
</global>
Other stuff that I had in my harmony configuration file that may be of use:
Code:
<!-- F4 --> <button id="56">ToggleWatched</button>
<!-- F12 Hide/Unhide Watched --> <button id="76">XBMC.SendClick(14)</button>
<!-- F5 --> <button id="93">XBMC.RunScript(script.xbmc.subtitles)</button>
I had those assigned to my RGB buttons of my Harmony 300i and it works like a charm. I use the ".+ Clear" button for Sleep, because in my case, Sleep was not being mapped to Harmony and .+ Clear was. I also removed any extra F(unction) keys that were not in global section.
Thats it. Hope this helps.
NOTE: There is a script programming detail... the flag "true" in "CancelAlarm" is only available in trunk >r35674!! If you use previous builds you'll see a notice dialog saying the alarm was canceled.