#!/usr/bin/env bash # homeassistant_access_token="" homeassistant_url="http://homeassistant.natrinicle.com:8123/api/states" homeassistant_screensaver_binary_sensor="${homeassistant_url}/binary_sensor.$(hostname | tr -s '-' '_' | tr '[:upper:]' '[:lower:]')_screen_locked" # MPRIS = Media Player Remote Interfacing Specification firefox_mpris_dbus=$(dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep -Po "org.mpris.*firefox[^\"]+") if [ -n "${firefox_mpris_dbus}" ]; then playback_string=$(dbus-send --print-reply --dest=${firefox_mpris_dbus} /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'PlaybackStatus' | grep -Po "(?<=string \")[^\"]+") fi # Suspend Firefox processes if not playing media if [ -z "${playback_string}" ] || [ "${playback_string}" != "Playing" ]; then ps aux | grep [f]irefox/[f]irefox | awk '{print $2}' | xargs -P0 -I {} kill -STOP {} fi # Suspend Atom processes ps aux | grep "/[a]tom" | awk '{print $2}' | xargs -P0 -I {} kill -STOP {} # Update Homeassistant if [ -n "${homeassistant_access_token}" ] && [ -n "${homeassistant_screensaver_binary_sensor}" ]; then curl -X POST -H "Authorization: Bearer ${homeassistant_access_token}" -H "Content-Type: application/json" -d "{\"state\": \"on\", \"attributes\": {\"friendly_name\": \"$(hostname) Screensaver\"}}" "${homeassistant_screensaver_binary_sensor}" fi