Skip to content

Instantly share code, notes, and snippets.

@CoalaJoe
Created May 1, 2017 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CoalaJoe/8cbfcbe7c7fd9b02d0cd575f0432b83a to your computer and use it in GitHub Desktop.
Save CoalaJoe/8cbfcbe7c7fd9b02d0cd575f0432b83a to your computer and use it in GitHub Desktop.
Updated PS4 script
#!/bin/bash
# ----------------- Information -----------------
# Script to discover names and IDs of playstation 4 games and apps when running.
# Then adds them to input selects within home-assistant configuration and creates a automation for each application.
# So it will be possible to start an application from input select
#
# IMPORTANT: When editing a .sh file, to assign variables, there are not spaces allowed around the equal sign.
# This does not work: `myVar = 234`
# This works: `myVar=234`
#
# For this script to work you need to have ps4-waker installed and working.
# ps4-waker https://github.com/dhleong/ps4-waker
#
# Created by Mister_Espria
# Date 12/02/2017
# Updated:
# Ashura - 28/04/2017:
# Improved format and removed some unnecessary comments.
#
# ----------------- End Information -----------------
# ----------------- Configuration -----------------
# IP address of your PS4 console
ps4ip=192.168.1.75
# The YAML file which represents the input select that allows the user to choose the desired application.
yamlgamename=/home/homeassistant/.homeassistant/input_select/ps4/game_select.yaml
# The YAML file which represents the application id select.
yamlgameid=/home/homeassistant/.homeassistant/input_select/ps4/game_id.yaml
# Directory where the generated automation-script will be placed in.
yamlps4auto=/home/homeassistant/.homeassistant/automations/ps4_dominik/
# Prefix of the generated automation file.
prefixauto=ps4_
# Name of the input select that allows the user to choose the desired application.
inputselect_name_for_game=ps4_game_select
# Name of the input select that you created for the application IDs.
inputselect_name_for_id=ps4_choose_id
# Name of the shellcommand you created to start the selected application.
# Example: ps4_start_selection: ps4-waker -c /home/pi/Documents/.ps4-wake.credentials.json -d 192.168.1.75 start {{ states.input_select.choose_id.state }}
shellcommandname=ps4_start_selection
# Default value for inputselect_name_for_game --> in this case --> Choose Game
standard_value_inputselect_name_for_game="Choose Game"
# You need to create one automation yourself. (See commented automation below)
# So after you choose an application, it will automatically go back to your defined standard value.
# Make sure, you have "Choose Game" and "empty" in your input selects.
# alias: PS4 - Choose Game
# trigger:
# platform: state
# entity_id: input_select.choose_game
# to: "Choose Game"
# action:
# - service: input_select.select_option
# data:
# entity_id: input_select.choose_id
# option: "empty"
# ----------------- End Configuration -----------------
# ----------------- Program -----------------
# Commands to find current app-name and ID.
nameVar=$(ps4-waker search -d "$ps4ip" | grep -w "running-app-name" | cut -c 24- | rev | cut -c 3- | rev | sed 's/[!@#\$%^&*(®)™]//g');
idVar=$(ps4-waker search -d "$ps4ip" | grep -w "running-app-titleid" | cut -c 27- | rev | cut -c 3- | rev | sed 's/^// ');
# Adding spaces to nameVar and idVar value.
formnameVar="\ \ - $nameVar"
formidVar="\ \ - $idVar"
# Replace spaces with underlines.
namenospaceVar=${prefixauto}${nameVar// /_}
# Check if the application name is already in the application input select.
if cat "$yamlgamename" |
grep -qFe "$nameVar"
then
# Application name was found. Closing script.
echo "found"
exit 0
else
echo "not found"
# Check if the application id is already in the id input select.
if cat "$yamlgameid" |
grep -qFe "$idVar"
then
# Application ID was found. Closing script.
echo "found"
exit 0
else
# The running application has not been added yet.
echo "not found"
sed -i "$ a $formnameVar" "$yamlgamename"
sed -i "$ a $formidVar" "$yamlgameid"
# Create the automation YAML for the application.
cat > "$yamlps4auto$namenospaceVar.yaml" <<EOL
alias: PS4 - ${nameVar}
trigger:
platform: state
entity_id: 'input_select.$inputselect_name_for_game'
to: "${nameVar}"
action:
- service: input_select.select_option
data:
entity_id: 'input_select.$inputselect_name_for_id'
option: "${idVar}"
- service: shell_command.$shellcommandname
- service: input_select.select_option
data:
entity_id: 'input_select.$inputselect_name_for_game'
option: "$standard_value_inputselect_name_for_game"
EOL
fi
fi
# ----------------- End Program -----------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment