Delay shutting down the PC (#792)

* ## 2.0

- New option to delay shutting down the Windows PC
- New option to send a message to the PC that is about to be shut down. If somebody is using the PC, you can warn them to save their work.

* delay parameter is int.
check for empty is not needed when we have a default

* Add a default value for Delay.

* update 2.1

* Fixed syntax error
This commit is contained in:
Stefan
2019-10-17 11:13:17 +02:00
committed by Pascal Vizeli
parent 714bcfb34d
commit 89d43e8775
3 changed files with 26 additions and 20 deletions

View File

@@ -1,5 +1,10 @@
# Changelog
## 2.1
- Set delay to zero when it is empty (to not break existing configurations)
- Update from bash to bashio
## 2.0
- New option to delay shutting down the Windows PC

View File

@@ -1,6 +1,6 @@
{
"name": "RPC Shutdown",
"version": "2.0",
"version": "2.1",
"slug": "rpc_shutdown",
"description": "Simple way for remote windows shutdowns",
"url": "https://home-assistant.io/addons/rpc_shutdown/",

View File

@@ -1,32 +1,33 @@
#!/bin/bash
#!/usr/bin/env bashio
set -e
CONFIG_PATH=/data/options.json
COMPUTERS=$(jq --raw-output '.computers | length' $CONFIG_PATH)
# Read from STDIN aliases to send shutdown
while read -r input; do
# remove json stuff
input="$(echo "$input" | jq --raw-output '.')"
echo "[Info] Read alias: $input"
# parse JSON value
input=$(bashio::jq "${input}" '.')
bashio::log.info "Read alias: $input"
# Find aliases -> computer
for (( i=0; i < "$COMPUTERS"; i++ )); do
ALIAS=$(jq --raw-output ".computers[$i].alias" $CONFIG_PATH)
ADDRESS=$(jq --raw-output ".computers[$i].address" $CONFIG_PATH)
CREDENTIALS=$(jq --raw-output ".computers[$i].credentials" $CONFIG_PATH)
DELAY=$(jq --raw-output ".computers[$i].delay" $CONFIG_PATH)
MESSAGE=$(jq --raw-output ".computers[$i].message" $CONFIG_PATH)
for computer in $(bashio::config 'computers|keys'); do
ALIAS=$(bashio::config "computers[${computer}].alias")
ADDRESS=$(bashio::config "computers[${computer}].address")
CREDENTIALS=$(bashio::config "computers[${computer}].credentials")
DELAY=$(bashio::config "computers[${computer}].delay")
MESSAGE=$(bashio::config "computers[${computer}].message")
# Not the correct alias
if [ "$ALIAS" != "$input" ]; then
if ! bashio::var.equals "$ALIAS" "$input"; then
continue
fi
echo "[Info] Shutdown $input -> $ADDRESS"
# Check if delay is not empty
if bashio::var.is_empty "${DELAY}"; then
DELAY="0"
fi
bashio::log.info "Shutdown $input -> $ADDRESS"
if ! msg="$(net rpc shutdown -I "$ADDRESS" -U "$CREDENTIALS" -t "$DELAY" -C "$MESSAGE")"; then
echo "[Error] Shutdown fails -> $msg"
bashio::log.error "Shutdown fails -> $msg"
fi
done
done