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
- # Issue: https://salsa.debian.org/Mobian-team/devices/sunxi-support/-/work_items/7 / https://gitlab.gnome.org/GNOME/gnome-calendar/-/work_items/1579
- # 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 run the script, 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).
- # If Phosh crashes and restarts during an alarm, you will not see the event reminder notification so it is recommended to open Gnome Calendar 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). In addition, we can restart the alarm with the script gnome-calendar_wake_up_autostart.sh.
- # Other issues / mobile specific issues noticed with Gnome Calendar: events happening next year are not displayed on the calendar so you should click on the day or week to see the event (another workaround without guessing the day / week is to view the following month and go back to the previous month); events created accidentally on several months by changing the year of an event cannot be edited so you should edit the ICS file and restart Phosh with "sudo systemctl restart phosh" (maybe there is a better solution to restart Gnome Calendar but executing "killall evolution-calendar-factory" fails to show visual notifications: https://askubuntu.com/questions/1359547/how-do-fix-my-gnome-calendar/1359559#1359559); landscape view crashes the app; editing the date of an event does not display a calendar unless we scroll down to have enough space.
- # 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:
- # "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)"
- #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 # preserve new lines using quotes in echo (using tee to display data)
- # 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) or use RuntimeMaxSec (which takes more time before going to sleep)
- # also using RuntimeMaxSec to avoid issue of automatic sleep before a second alarm ended like it happened with the script for Gnome Clocks
- # RuntimeMaxSec is 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 (710 seconds which includes by default the 2 sleeps of 300 seconds to allow repeating the sound 3 times within around 10 minutes, adding 105 seconds if we play a sound of 35 seconds by using mpv --loop=4 to play 5 times a sound of less than 7 seconds, adding 5 seconds to have time to finish script by ending process gnome-session-inhibit and going to sleep)
- # 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"
- RuntimeMaxSec=710
- 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
- # gnome-calendar_wake_up_autostart.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_autostart.sh
- #!/usr/bin/env bash
- # Script to call in a desktop file in ~/.config/autostart/ to play an alarm sound for Gnome Calendar event reminders from the last 15 minutes in case a UI like Phosh restarts after a crash that can happen even without user interaction (tested with Mobian / Phosh on the PinePhone)
- # A desktop file gnome-calendar_wake_up_autostart.desktop can be created with this content (without # at the beginning of lines, adjust executable path if needed):
- # [Desktop Entry]
- # Type=Application
- # Exec=bash -c 'source ~/.local/share/pipx/venvs/icalendar/bin/activate && ~/Downloads/scripts/gnome-calendar_wake_up_autostart.sh'
- python_script="gnome-calendar_wake_up_autostart.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`
- time=`$path/$python_script`
- date=`date +%s`
- for item in ${time[@]}
- do
- diff=$(($date - $item))
- if (($diff >= 0 && $diff <= 900)); then
- 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"
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment