Ok, but first of all im not a programmer. Everything i did is just trial and error. when i said script i didn’t mean an home assistant script .
You need to have a specific configuration layout for your home-assistant yaml’s. or you need to adjust the script somehow. I use the same layout as Ben from bruhautomation.
Working of the script
The script will add every time a new game is discovered the appname and appid to the input_selects created for this.
It will also create an automation in your home-assistant config directory which take care of changing the second input select based on the first input_select.
In order to make this work the steps are as followed
Steps:
- Create two Input_select files
- Create an automation
- Save the script somewhere
- Create shell command
- Edit the script
1. Create two Input_select files
So then u need to create two different input_select.yaml files
The first one i have is located at /home/homeassistant/.homeassistant/config/input_select/choose_game_ps4_kevin.yaml
it contains:
choose_game:
name: "Play Game"
initial: "Choose Game"
options:
- Choose Game
the second file is located at /home/homeassistant/.homeassistant/config/input_select/choose_id_ps4_kevin.yaml
it contains:
choose_id:
name: "Play id"
initial: empty
options:
- empty
2. Create an automation
My automation is located at /home/homeassistant/.homeassistant/config/automations/playstation_kevin/a_choose_game.yaml
it contains:
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"
3. Save the script somewhere
You can save the file anywhere i think. Mine is located at /home/pi/Documents/myownscripts/ps4/add-current-game
This is the script:
#!/bin/bash
##script to discover game names and id's of playstation 4 games and apps when playing.
##Then adds them to input selects within home-assistant configuration and creates a automation for each game
## So it will be possible to start a game from input select
#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
##########USER INPUT############################################
###Set ip adress for playstation 4 console###
ps4ip=192.168.1.21
###set location of input select.yaml for game names### You nee to create a specific input_select yaml file which will only be used for one input select
yamlgamename=/home/homeassistant/.homeassistant/config/input_select/choose_game_ps4_kevin.yaml #/home/pi/choose_game_ps4_kevin.yaml
###set location of input select.yaml for game ID's###
yamlgameid=/home/homeassistant/.homeassistant/config/input_select/choose_id_ps4_kevin.yaml
###set location for automations file (to be created) ###
yamlps4auto=/home/homeassistant/.homeassistant/config/automations/playstation_kevin/
###the text of prefixauto will come in front of your automation name so in this case the automation name will be a_PS4_gamename
prefixauto=a_PS4_
###name of input select you created for the game names
inputselect_name_for_game=choose_game
###name of input select you created for the game ID's
inputselect_name_for_id=choose_id
###Name of shellcommand you created to start selected game (without shellcommand. prefix)
### For example: ps4_start_selection: ps4-waker -c /home/pi/Documents/.ps4-wake.credentials.json -d 192.168.1.21 start {{ states.input_select.choose_id.state }}
shellcommandname=ps4_start_selection
###see info below
### standard value for inputselect_name_for_game --> in this case --> Choose Game
standard_value_inputselect_name_for_game="Choose Game"
###info
##you need to create one automation yourself this is with the standard values of the input select. (see commented automation below)
##So after you have chosen a game it will autmatically go back to choose game value.
##(make sure (in this case) 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 USER INPUT###########################################
#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"
#add _ instead of spaces in game name
namenospaceVar=${prefixauto}${nameVar// /_}
#check if namevar already is in file if not add it
if sudo cat "$yamlgamename" |
grep -qFe "$nameVar"
then
#if found in file:
echo found
else
#if not:
echo not found
###second check starts here
if sudo cat "$yamlgameid" |
grep -qFe "$idVar"
then
#if found in file:
echo found
else
#if not:
echo not found
sudo sed -i "$ a $formnameVar" "$yamlgamename"
sudo sed -i "$ a $formidVar" "$yamlgameid"
#create automation.yaml for game
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
4. Create two shell command
We create an shell command already to be able to run the script from within home-assitant.
My shell command is:
add_current_game: "/home/pi/Documents/myownscripts/ps4/add-current-game"
ps4_start_selection: "ps4-waker -c /home/pi/Documents/.ps4-wake.credentials.json -d 192.168.1.21 start {{ states.input_select.choose_id.state }}"
The first shell command can be used in an automation or script later if you want to fire the execution of the script.
This second shell command is used in the script when creating the automation. You nee to change the location of your credentials.json en the ip address of your console
5. Edit the script
You need to change a few variables.
- enter the ip address of your console.
- path to the first yaml file we created in step 1
- path to the second yaml file we created in step 1
- the location where newly created automation can be stored in order for home-assistant to read them
- is a prefix which can be changed, but no necessary.
- and 7. if you used the same names for the input select as in step 1 you don’t need to change this. otherwise change to the corresponding name.
- Enter the name of your shell command if you are using the same name as in step 4, you don’t need to change this.
- Change this to the initial value of the first inputs_select we created in step 1 (not needed to change if you are using the same value as in step 1; Choose Game).
Finishing
After this the script should work. You can create an script in home-assistant to fire your shell command or an automation to fire it automatically so now and then.
You may need to set permissions and make the file executable by home-assistant
i had some trouble with this. the script didn’t get executed by home-assistant but was working properly in the console.
I am on an hassbian install and had to add the user home-assistant to the sudoers in order to get it working which is not an nice way.
You can also run the script from the console like this to check if it is working:
/home/pi/Documents/myownscripts/ps4/add-current-game
Note
The script is working in your home-assistant config directory so keep this in mind.
Make backups before messing around there.
You can also change the path variables and check on a safe location if the script is working.