create sleep timer hotkey
#1
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.
Reply
#2
Realizing this is an old thread, it is the exact thing I am also looking to do.

I understand that there are idle off-timers built into XBMC but I want to add a direct hotkey for setting up standard off-timers during non-idle times. Mainly, I would like to have this feature since I often go to sleep watching a movie using my XBMC Windows 7 based HTPC (specs are in my sig). While it works sometimes, there are times that XBMC does not put the PC into sleep mode and I find the PC still running on full power in the morning.
HTPC #1: Acer AX3200-U3600A Slimline PC | Win7 64bit | AMD Phenom X3 | 4gb ram | 320gb hd | Onboard nvidia 8200 w/HDMI out

HTPC #2: HP Slimline S3220n | Win7 64bit | AMD X2 | 2gb ram | 160gb hd | Onboard nvidia 6150se (nforce 430) w/VGA out

Lenovo N5901 Mini Wireless KB | Harmony One & 650
Reply
#3
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 Laugh! 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.
Reply
#4
Star 
Sorry for the extremely delayed response.

Can you comment up the script a bit? I'd like to make some changes, like to changes times, etc. (I can generally understand it)

My ideal script would be the following: (emulating a TV)

Function SleepButtonPushed()
If sleeptimer.display off
If sleeptimer = 0
Then Sleep.Time = timeX
If sleeptimer > 0
If sleeptimer.display off
sleeptimer.display on (for a couple seconds)
If sleeptimer.display on
sleep.Time = timeX+1


time1=10
time2=30
time3=60
time4=90
time5=0

I think this needs to expanded a bit, but I'm not in the right state of mind at the moment. Does this make sense?
Reply
#5
Sad 
sufreak Wrote:My ideal script would be the following: (emulating a TV)

This would be great! I'm looking for such an addon/function for hours and didn't find anything Sad
Reply

Logout Mark Read Team Forum Stats Members Help
create sleep timer hotkey0