Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # gnome-calendar_wake_up.py
- #!/usr/bin/env python3
- # Extract alarm trigger time of calendar events (combining event time and reminder time)
- # https://icalendar.readthedocs.io/en/latest/how-to/usage.html#read-calendar-from-file
- # https://icalendar.readthedocs.io/en/latest/reference/api/icalendar.cal.event.html#icalendar.cal.event.Event.alarms
- # Not displaying a warning if ACTION is of type AUDIO instead of DISPLAY to avoid additional loop (https://github.com/collective/icalendar/issues/1421)
- import icalendar
- from pathlib import Path
- ics_path = Path("/home/mobian/.local/share/evolution/calendar/system/calendar.ics")
- calendar = icalendar.Calendar.from_ical(ics_path)
- for event in calendar.events:
- #print(int(event.alarms.times[0].trigger.timestamp())) # only needed if we want to substract seconds to wake up system earlier
- print("OnCalendar=" + str(event.alarms.times[0].trigger))
- # gnome-calendar_wake_up.sh
- #!/usr/bin/env bash
- # Gnome Calendar event reminders using systemd timers to wake up device during sleep (tested with Mobian / Phosh on the PinePhone)
- # 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
- # Script needs to be run each time we configure a new event reminder.
- # Another script is called to extract event information using the icalendar Python library (https://askubuntu.com/questions/1258143/interacting-with-gnome-calendar-via-the-cli-adding-and-reading-events/1553440#1553440)
- # If you don't see a visual notification, there is a workaround by disabling sound in the event reminder (https://gitlab.gnome.org/GNOME/gnome-calendar/-/work_items/1280).
- # The OS may not produce sound if the event reminder has a DISPLAY action instead of an AUDIO action, so this script handles sound using mpv in addition to the visual notification with the ability to repeat sound notification (by default 3 times every 5 minutes within 10 minutes like done with the calendar and alarm app on Sailfish OS with a ring time of around 30 seconds by looping the sound, long enough to not miss the alarm if a train passes nearby) unless we close a terminal window or press Ctrl+C (device will go back to sleep if there is a 3rd sound notification unless a process is preventing it and blinking notification led will either turn off or have fixed blue light which is random).
- # If you snooze an alarm, it will not wake up the phone, neither use both the visual and sound notification, you need to change the event time instead and run the script again.
- # You can use "pipx install icalendar" instead of configuring a venv manually to also have the icalendar command available globally.
- # Debian package python3-icalendar uses an old version that does not support import from file yet.
- # Before running the script, you may need to change the sound file in the shell script code and the ICS file path in the Python code.
- # To automatically ask to create systemd timers, copy /usr/share/applications/org.gnome.Calendar.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):
- # Exec=bash -c "gnome-calendar %U & kgx -e bash -c 'source ~/.local/share/pipx/venvs/icalendar/bin/activate && /path/to/gnome-calendar_wake_up.sh'"
- # Calendar and script will be started at the same time but script will only run after you type the password in the terminal (if started after the calendar is closed there is delay to display the window which can result in forgetting to save wake up timers).
- # Even if the alarm app and calendar app use the same sound for alarms, you should be able to notice if the alarm app and calendar app are ringing at the same time (view the visual notification to confirm you did not ignore an alarm or have it mistaken with another alarm).
- #python_venv="/home/mobian/.local/share/pipx/venvs/icalendar/bin/python" # not needed if activating venv in desktop file (which avoids unnecessary script customizations and makes script independant from venv)
- python_script="gnome-calendar_wake_up.py" # file is expected to be in the same folder as gnome-calendar_wake_up.sh
- # Copy the calendar original sound file /usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga to another location to avoid missing file after update (canberra-gtk-play only prevents error for path change)
- sound_file="/home/mobian/Downloads/alarm-clock-elapsed.oga" # only works with full path, not ~/Downloads/alarm-clock-elapsed.oga
- path=`dirname $0`
- # Use other files and service names to avoid interference with alarm app Wake Mobile or Birdie (for system and user wake up timer)
- if [ "$1" = "off" ]; then
- if systemctl -q is-enabled system-wake-up_gnome-calendar.timer; then
- sudo systemctl disable system-wake-up_gnome-calendar.timer
- fi
- if systemctl -q is-active system-wake-up_gnome-calendar.timer; then
- sudo systemctl stop system-wake-up_gnome-calendar.timer
- fi
- if systemctl --user -q is-enabled wake-up_gnome-calendar.timer; then
- systemctl --user disable wake-up_gnome-calendar.timer
- fi
- if systemctl --user -q is-active wake-up_gnome-calendar.timer; then
- systemctl --user stop wake-up_gnome-calendar.timer
- fi
- exit 0
- fi
- # System Wake Up Timer (needed to wake up device)
- dir=/etc/systemd/system
- sudo mkdir -p $dir
- file=$dir/system-wake-up_gnome-calendar.timer
- # Overwrite file instead of checking if file exist to avoid handling an additional file and to avoid ignored changes if we want to change configuration of an existing file
- sudo tee $file > /dev/null << EOF
- [Unit]
- Description=System Wake Up Timer
- [Install]
- WantedBy=timers.target
- [Timer]
- Persistent=false
- WakeSystem=true
- AccuracySec=1us
- EOF
- # call script only once to avoid unnecessary ICS file parsing
- #time=`$python_venv $path/$python_script`
- time=`$path/$python_script`
- # https://linuxsimply.com/bash-scripting-tutorial/array/print-array/
- #for item in ${time[@]}
- #do
- # timestamp_system=$(($item - 5)) # wake up system 5 seconds before event alarm is sometimes needed to show visual notification and blinking notification led (should not be needed anymore maybe because of the "Command completed" notification, the visual notification has a random delay of few seconds anyway)
- # date_system=`date +'%Y-%m-%d %H:%M:%S' -d "@$timestamp_system"`
- # echo "OnCalendar=$date_system" | sudo tee -a $file
- #done
- echo "$time" | sudo tee -a $file
- # System Wake Up Action
- file=$dir/system-wake-up_gnome-calendar.service
- sudo tee $file > /dev/null << EOF
- [Unit]
- Description=System Wake Up Action
- [Service]
- ExecStart=/usr/bin/true
- EOF
- sudo systemctl daemon-reload
- if ! systemctl -q is-enabled system-wake-up_gnome-calendar.timer; then
- sudo systemctl enable system-wake-up_gnome-calendar.timer
- fi
- sudo systemctl restart system-wake-up_gnome-calendar.timer
- # User Wake Up Timer (needed to play sound notification)
- dir=~/.config/systemd/user
- mkdir -p $dir
- file=$dir/wake-up_gnome-calendar.timer
- cat > $file << EOF
- [Unit]
- Description=User Wake Up Timer
- [Install]
- WantedBy=timers.target
- [Timer]
- Persistent=true
- AccuracySec=1us
- EOF
- #for item in ${time[@]}
- #do
- # date=`date +'%Y-%m-%d %H:%M:%S' -d "@$item"`
- # echo "OnCalendar=$date" >> $file
- #done
- echo "$time" >> $file
- # User Wake Up Action
- file=$dir/wake-up_gnome-calendar.service
- # When the sleep command waits a few seconds or minutes between sound notifications, automatic sleep is not working even after "gnome-session-inhibit --inhibit suspend --inhibit-only" process ends or starting bash with "gnome-session-inhibit --inhibit suspend" so we need to execute "systemctl sleep" at the end of the service file script (the device will only sleep if there is no other gnome-session-inhibit preventing sleep)
- # RuntimeMaxSec is only needed if we play sound indefinitely (e.g. the command does not terminate like cvlc) and to allow future sound notifications if paused in notifications screen
- # RuntimeMaxSec must be at least 15 seconds since 10 seconds is not enough to display visual notification and make the notification led blink (e.g. 615 seconds to have time to finish script, which includes by default the 2 sleeps of 300 seconds to allow repeating the sound 3 times within 10 minutes, adding 15 seconds if we play a sounds of less than 5 seconds 3 times without looping it)
- # gnome-session-inhibit needed to get the blinking notification led and to allow repeating sound notifications within 10 minutes to bypass the default sleep time after 5 minutes (sleep of 295 instead of 300 seconds is not needed since automatic sleep that starts at 300 seconds by default is inhibited)
- # Trying to disable the blue led that randomly stays on during sleep is not working with "echo 0 > /sys/class/leds/blue\:indicator/brightness" (error "Device or resource busy")
- cat > $file << EOF
- [Unit]
- Description=User Wake Up Action
- [Service]
- ExecStart=kgx -e bash -c "gnome-session-inhibit --inhibit suspend --inhibit-only & pid=\$!; command=\"mpv --loop=4 $sound_file\"; for((i=1;i<3;++i)); do \$command; sleep 300; done; \$command; kill \$pid; systemctl sleep"
- EOF
- systemctl --user daemon-reload
- if ! systemctl --user -q is-enabled wake-up_gnome-calendar.timer; then
- systemctl --user enable wake-up_gnome-calendar.timer
- fi
- systemctl --user restart wake-up_gnome-calendar.timer
Advertisement
Add Comment
Please, Sign In to add comment