baptx

gnome-clocks_inhibit_sleep

Sep 2nd, 2026 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. # gnome-clocks_inhibit_sleep.py
  2. #!/usr/bin/env python3
  3. # Extract alarm trigger time including alarms that repeat several days
  4.  
  5. import json
  6. import sys
  7. import re
  8. import time
  9. from datetime import datetime
  10.  
  11. DAYS = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
  12.  
  13. for p in json.load(sys.stdin):
  14. if "ring_time" in p:
  15. days = ",".join([DAYS[d] for d in p["days"]])
  16. h = p["hour"]
  17. m = p["minute"]
  18. #print("# {}".format(p["name"]))
  19. if days:
  20. m -= 1 # needed to have the systemd timer before the alarm trigger time (otherwise device could go randomly to sleep during the alarm)
  21. if m < 0:
  22. m = 59
  23. h -= 1
  24. print("OnCalendar={} {:02d}:{:02d}:55".format(days, h, m))
  25. else:
  26. #print("OnCalendar={:02d}:{:02d}:55".format(h, m))
  27. ring_time = p["ring_time"].replace("T", " ")
  28. ring_time = re.sub(r"\+.*", "", ring_time) # https://stackoverflow.com/questions/52335970/how-to-fix-syntaxwarning-invalid-escape-sequence-in-python/52335971#52335971
  29. timestamp = time.mktime(time.strptime(ring_time, "%Y-%m-%d %H:%M:%S")) - 5 # https://stackoverflow.com/questions/33621639/convert-date-to-timestamp-in-python/33621755#33621755
  30. ring_time = datetime.fromtimestamp(int(timestamp)).strftime("%Y-%m-%d %H:%M:%S") # https://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date/3682808#3682808
  31. print("OnCalendar=" + ring_time) # include date to not execute script when alarms should not repeat and get timestamp to subtract day like done for hours and minutes
  32.  
  33.  
  34. # gnome-clocks_inhibit_sleep.sh
  35. #!/usr/bin/env bash
  36. # Gnome Clocks inhibit sleep (tested with Mobian / Phosh on the PinePhone)
  37. # Without this script, the sleep operation "suspend" is not inhibited during ring time (defaults to 5 minutes) so an alarm can ring for a shorter time than configured and can be missed if the phone was used a bit less than the automatic suspend time (defaults to 5 minutes) before an alarm.
  38. # Based on Gnome Clocks scripts https://github.com/sbeaugrand/debinst/blob/master/mobian/clks.sh and https://github.com/sbeaugrand/debinst/blob/master/mobian/clks.py
  39. # Issue: https://salsa.debian.org/Mobian-team/devices/sunxi-support/-/work_items/8
  40. # Script needs to be run each time we configure a new alarm.
  41. # A terminal window opens to view the inhibit in progress and can be viewed canceled earlier by closing the window or pressing Ctrl+C.
  42. # To automatically run the script, copy /usr/share/applications/org.gnome.clocks.desktop to ~/.local/share/applications/ (which persists after updates) to comment the DBusActivatable=true line by adding "#" before it and replace the Exec= line with this (using bash instead of /bin/bash does not work for the D-BUS service file of apps like Birdie but works for desktop and service files in this script):
  43. # Exec=bash -c "gnome-clocks & kgx -e bash -c 'echo -n Press Enter key after editing alarms: ; read -s; echo; /path/to/gnome-clocks_inhibit_sleep.sh'"
  44. # Escaping single quotes with '\'' to include the ending space in the echo command is not working in the desktop file on Mobian (why?).
  45. # Gnome Clocks and script will be started at the same time but script will only run after you press the Enter key in a terminal.
  46. # The alarm snooze feature is not supported, you need to change the alarm time instead and run the script again.
  47. # The countdown (timer) is not supported but you can use alarms instead until there is an fix upstream or in the Linux distribution.
  48. # If the phone was powered off during an alarm, we don't get a missed alarm notification after booting the phone unlike Gnome Calendar so it is recommended to open Gnome Clocks as a workaround to check if we did not miss an alarm (this can be done automatically after login by copying the desktop file or creating a symlink in ~/.config/autostart/ like mentioned on https://forum.pine64.org/showthread.php?tid=17278 instead of executing commands in ~/.profile which broke login in a infinite loading spinner loop that could be fixed with SSH access from a computer).
  49. # There is a bug if we create an alarm that rings 4 minutes after another alarm, both alarms continued ringing even after the default ring time of 5 minutes.
  50. # Other issues noticed during sleep: when going to sleep, the blue blinking notification led is either always on or always off; when going out of sleep, the network signal bars of Phosh keep changing very fast which seems to display all signal changes that happened while the device was sleeping.
  51. # Make sure to have the volume level loud enough to hear alarms or follow instructions from https://gitlab.gnome.org/GNOME/calls/-/work_items/745#note_2745975:
  52. # "Use media roles in pipewire / feedbackd and you can set the alarm volume indendently, see e.g. https://phosh.mobi/posts/role-based-routing (works for alarms too btw and makes sure the speaker is used for alarm sounds)"
  53.  
  54. python_script="gnome-clocks_inhibit_sleep.py" # file is expected to be in the same folder as gnome-clocks_inhibit_sleep.sh
  55.  
  56. path=`dirname $0`
  57.  
  58. # Use other files and service names to avoid interference with alarm app Wake Mobile or Birdie (for system and user wake up timer)
  59. if [ "$1" = "off" ]; then
  60. if systemctl --user -q is-enabled wake-up_gnome-clocks.timer; then
  61. systemctl --user disable wake-up_gnome-clocks.timer
  62. fi
  63. if systemctl --user -q is-active wake-up_gnome-clocks.timer; then
  64. systemctl --user stop wake-up_gnome-clocks.timer
  65. fi
  66. exit 0
  67. fi
  68.  
  69. # User Wake Up Timer
  70. dir=~/.config/systemd/user
  71. mkdir -p $dir
  72.  
  73. file=$dir/wake-up_gnome-clocks.timer
  74. cat > $file << EOF
  75. [Unit]
  76. Description=User Wake Up Timer
  77. [Install]
  78. WantedBy=timers.target
  79. [Timer]
  80. Persistent=true
  81. AccuracySec=1us
  82. EOF
  83.  
  84. # convert GVariant format to JSON for easier time extraction
  85. result=`gsettings get org.gnome.clocks alarms | sed -e 's/@[a-z{}]* //g' -e 's/[<>]//g' -e "s/'/\"/g" | $path/$python_script`
  86. # avoid error in case all alarms are disabled
  87. if [ "$result" ]; then
  88. echo "$result" | tee -a $file # preserve new lines using quotes in echo (using tee to display data)
  89. else
  90. echo "OnCalendar=1970-01-01 00:00" | tee -a $file
  91. fi
  92.  
  93. # loop only needed if not handled in Python script in case we don't support alarm that repeat several days
  94. #time=`gsettings get org.gnome.clocks alarms | sed -e 's/@[a-z{}]* //g' -e 's/[<>]//g' -e "s/'/\"/g" | jq -r '.[].ring_time | select(. != null)' | sed -e 's/T/ /g' -e 's/+.*//g'`
  95. # https://linuxsimply.com/bash-scripting-tutorial/array/print-array/
  96. #for item in ${time[@]}
  97. #do
  98. # echo "OnCalendar=$item" >> $file
  99. #done
  100.  
  101. # User Wake Up Action
  102. file=$dir/wake-up_gnome-clocks.service
  103. # using RuntimeMaxSec to avoid issue of automatic sleep before a second alarm ended (315 seconds if we want the default automatic suspend time and alarm time of 5 minutes including 5 seconds to end process gnome-session-inhibit to cancel suspend inhibit in a terminal window, sleep of 310 seconds which includes 5 seconds inhibit before alarm and 5 seconds after alarm to hear entire sound)
  104. cat > $file << EOF
  105. [Unit]
  106. Description=User Wake Up Action
  107. [Service]
  108. ExecStart=kgx -e bash -c "gnome-session-inhibit --inhibit suspend --inhibit-only & pid=\$!; sleep 310; kill \$pid; systemctl sleep"
  109. RuntimeMaxSec=315
  110. EOF
  111.  
  112. systemctl --user daemon-reload
  113. if ! systemctl --user -q is-enabled wake-up_gnome-clocks.timer; then
  114. systemctl --user enable wake-up_gnome-clocks.timer
  115. fi
  116. systemctl --user restart wake-up_gnome-clocks.timer
  117.  
Advertisement
Add Comment
Please, Sign In to add comment