Kaynağa Gözat

Add HomeAssisstant Binary Sensor Screensaver

Curl command showing example update of a Homeassistant binary sensor.
master
Nate Bohman 4 yıl önce
ebeveyn
işleme
1718485c51
İmzalayan: Nate Bohman <natrinicle@gmail.com> GPC Anahtar Kimliği: C10546A54ABA1CE5
2 değiştirilmiş dosya ile 37 ekleme ve 0 silme
  1. +12
    -0
      screensaver_end.sh
  2. +25
    -0
      screensaver_start.sh

+ 12
- 0
screensaver_end.sh Dosyayı Görüntüle

@@ -0,0 +1,12 @@
#!/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"

ps -e -o stat,command,pid | grep '^T' | grep -P "(firefox|atom)" | awk '{print $NF}' | xargs -P0 -I {} kill -CONT {}

# 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\": \"off\", \"attributes\": {\"friendly_name\": \"$(hostname) Screensaver\"}}" "${homeassistant_screensaver_binary_sensor}"
fi

+ 25
- 0
screensaver_start.sh Dosyayı Görüntüle

@@ -0,0 +1,25 @@
#!/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

Yükleniyor…
İptal
Kaydet