Skip to content

Commit

Permalink
Delay shutting down the PC (home-assistant#792)
Browse files Browse the repository at this point in the history
* ## 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
  • Loading branch information
stefanroelofs authored and pvizeli committed Oct 17, 2019
1 parent 714bcfb commit 89d43e8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
5 changes: 5 additions & 0 deletions rpc_shutdown/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion rpc_shutdown/config.json
Original file line number Diff line number Diff line change
@@ -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/",
Expand Down
39 changes: 20 additions & 19 deletions rpc_shutdown/run.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 89d43e8

Please sign in to comment.