cristian5th / homebridge-appletv

Configuration of pyatv and cmd4 for reading the Apple TV status in Homebridge
GNU General Public License v3.0
25 stars 2 forks source link

Permission denied #8

Closed villamann closed 2 years ago

villamann commented 2 years ago

Hi, I`m having trouble with permissions. My script:

#!/bin/bash

set -e

# Exit immediately for unbound variables.
set -u

length=$#
device=""
io=""
characteristic=""
option=""
ATV_id="C8:69:CD:45:44:9C"
airplay_credentials="d341adefadc8a6317379cd9ff38a4683738da5c4600f4c26d0d46921ae1e9f9b:4c095c65961d218fbbb4a1175db03ddc3d6cdae141beaa21da9f9219b27d1df9:38434236363633322d363343422d343232462d394645442d393232353637304643393242:66336231336663322d633062332d343635372d616437662d613237666135333238343030"
companion_credentials="d341adefadc8a6317379cd9ff38a4683738da5c4600f4c26d0d46921ae1e9f9b:f46a7e54dc648035f5fa0cebd603a074cf86029c287098248922589d2ea2fee5:38434236363633322d363343422d343232462d394645442d393232353637304643393242:63666138613537312d346239312d343330342d396232362d363737343330623564343233"
atvremote_path="/home/villamann/.local/bin/"

if [ $length -le 1 ]; then
   printf "Usage: $0 Get < AccessoryName > < Characteristic >\n"
   printf "Usage: $0 Set < AccessoryName > < Characteristic > < Value >\n"
   exit -1
fi

# printf "args =$#\n"   # debug
# printf "arg1 =$1\n"   # debug

if [ $length -ge 1 ]; then
    io=$1
   #  printf "io=$io\n"   # debug
fi
if [ $length -ge 2 ]; then
    device=$2
   #  printf "device = ${device}\n"   # debug
fi
if [ $length -ge 3 ]; then
    characteristic=$3
   #  printf "Characteristic = ${characteristic}\n"   # debug
fi
if [ $length -ge 4 ]; then
    option=$4
   #  printf "option = ${option}\n"   # debug
fi

if [ "${io}" == "Get" ]; then
   case $device in
      'Apple TV Power')
         case $characteristic in
            'On')
               # Get Apple TV power state
               ATV_POWER_STATE=$(${atvremote_path}atvremote --id ${ATV_id} --airplay-credentials ${airplay_credentials} power_state)
               if [ "${ATV_POWER_STATE}" = "PowerState.On" ]
               then
                  printf "1\n"
               else
                  printf "0\n"
               fi
               exit 0
               ;;
            *)
               printf "UnHandled Get ${device}  Characteristic ${characteristic}\n"
               exit -1
               ;;
         esac
         exit 0
         ;;
      'Apple TV Play State')
         case $characteristic in
            'On')
               # Get Apple TV play status
               ATV_PLAYING_STATE=$(${atvremote_path}atvremote --id ${ATV_id} --airplay-credentials ${airplay_credentials} playing | grep -oP '(?<=Device state: ).*')
               if [ "${ATV_PLAYING_STATE}" = "Playing" ]
               then
                  printf "1\n"
               else
                  printf "0\n"
               fi
               exit 0
               ;;
            *)
               printf "UnHandled Get ${device}  Characteristic ${characteristic}\n"
               exit -1
               ;;
         esac
         exit 0
         ;;
      'Apple TV Video Play')
         case $characteristic in
            'On')
               # Get Apple TV play status and media type
               ATV_PLAYING=$(${atvremote_path}atvremote --id ${ATV_id} --airplay-credentials ${airplay_credentials} playing)
               ATV_PLAYING_STATE=$(echo "$ATV_PLAYING" | grep -oP '(?<=Device state: ).*')
               ATV_MEDIA_PLAYING=$(echo "$ATV_PLAYING" | grep -oP '(?<=Media type: ).*')
               if [ "${ATV_MEDIA_PLAYING}" = "Video" ]
               then
                  if [ "${ATV_PLAYING_STATE}" = "Playing" ] || [ "${ATV_PLAYING_STATE}" = "Paused" ]
                  then
                     ATV_MEDIA_TITLE=$(echo "$ATV_PLAYING" | grep -oP '(?<=Title: ).*')
                     # When Title is some streaming address or "DW News",
                     # this is my wife watching Shopping Queen on VLC
                     # and shutters must remain up.
                     if [[ $ATV_MEDIA_TITLE =~ ^rtsp://.* ]] || [ "${ATV_MEDIA_TITLE}" = "DW News" ]
                     then
                        printf "0\n"
                     else
                        ATV_MEDIA_ALBUM_COUNT=$(echo "$ATV_PLAYING" | grep -oP '(?<=Album: ).*' | wc -l)
                        if [ "${ATV_MEDIA_ALBUM_COUNT}" -gt 0 ]
                        then
                           ATV_MEDIA_ALBUM=$(echo "$ATV_PLAYING" | grep -oP '(?<=Album: ).*')
                           # When Title is unknown and Album is one of the following,
                           # this is still my wife watching Shopping Queen on OQEE
                           # and shutters must remain up.
                           # Yes, Shopping Queen is very popular around here.
                           if [ "${ATV_MEDIA_ALBUM}" = "Vox" ] || 
                              [ "${ATV_MEDIA_ALBUM}" = "Arte Allemand" ] || 
                              [ "${ATV_MEDIA_ALBUM}" = "RTL" ] || 
                              [ "${ATV_MEDIA_ALBUM}" = "ProSieben" ] || 
                              [ "${ATV_MEDIA_ALBUM}" = "Sat1" ] || 
                              [ "${ATV_MEDIA_ALBUM}" = "NTV" ] || 
                              [ "${ATV_MEDIA_ALBUM}" = "Welt" ]
                           then
                              printf "0\n"
                           else
                              printf "1\n"
                           fi
                        else
                           printf "1\n"
                        fi
                     fi
                  else
                     printf "0\n"
                  fi
               else
                  printf "0\n"
               fi
               exit 0
               ;;
            *)
               printf "UnHandled Get ${device}  Characteristic ${characteristic}\n"
               exit -1
               ;;
         esac
         exit 0
         ;;
      *)
         printf "UnHandled Get ${device}  Characteristic ${characteristic}\n"
         exit -1
         ;;
   esac
fi
if [ "${io}" == 'Set' ]; then
   case $device in
      'Apple TV Power')
         case $characteristic in
            'On')
               # Get Apple TV current power state and switch accordingly
               ATV_POWER_STATE=$(${atvremote_path}atvremote --id ${ATV_id} --airplay-credentials ${airplay_credentials} power_state)
               if [ "${ATV_POWER_STATE}" = "PowerState.On" ]
               then
                  ${atvremote_path}atvremote --id ${ATV_id} --companion-credentials ${companion_credentials} turn_off
               else
                  ${atvremote_path}atvremote --id ${ATV_id} --companion-credentials ${companion_credentials} turn_on
               fi
               exit 0
               ;;
            *)
               printf "UnHandled Set ${device} Characteristic ${characteristic}"
               exit -1
               ;;
         esac
         exit 0
         ;;
      'Apple TV Play State')
         case $characteristic in
            'On')
               # Toggle between play and pause
               ${atvremote_path}atvremote --id ${ATV_id} --airplay-credentials ${airplay_credentials} play_pause
               exit 0
               ;;
            *)
               printf "UnHandled Set ${device} Characteristic ${characteristic}"
               exit -1
               ;;
         esac
         exit 0
         ;;
      *)
         printf "UnHandled Get ${device}  Characteristic ${characteristic}\n"
         exit -1
         ;;
   esac
fi
printf "Unknown io command ${io}\n"
exit -1

Debug:

7/26/2022, 9:05:31 PM] [Cmd4] getValue On function failed for Apple TV Power cmd: bash /var/lib/homebridge/appletv_control.sh Get 'Apple TV Power' 'On' Failed.  Generated Error: Error: Command failed: bash /var/lib/homebridge/appletv_control.sh Get 'Apple TV Power' 'On'
/var/lib/homebridge/appletv_control.sh: line 51: /home/villamann/.local/bin/atvremote: Permission denied

[7/26/2022, 9:05:31 PM] [Cmd4] getValue On function failed for Apple TV Power cmd: bash /var/lib/homebridge/appletv_control.sh Get 'Apple TV Power' 'On' Failed. Error: 126. Perhaps your device is busy?
[7/26/2022, 9:05:31 PM] [Cmd4] getValue: On function for Apple TV Play State streamed to stderr: /var/lib/homebridge/appletv_control.sh: line 71: /home/villamann/.local/bin/atvremote: Permission denied

[7/26/2022, 9:05:31 PM] [Cmd4] getValue On function failed for Apple TV Play State cmd: bash /var/lib/homebridge/appletv_control.sh Get 'Apple TV Play State' 'On' Failed.  Generated Error: Error: Command failed: bash /var/lib/homebridge/appletv_control.sh Get 'Apple TV Play State' 'On'
/var/lib/homebridge/appletv_control.sh: line 71: /home/villamann/.local/bin/atvremote: Permission denied

[7/26/2022, 9:05:31 PM] [Cmd4] getValue On function failed for Apple TV Play State cmd: bash /var/lib/homebridge/appletv_control.sh Get 'Apple TV Play State' 'On' Failed. Error: 1. Perhaps your device is busy?
[7/26/2022, 9:05:31 PM] [Cmd4] getValue: On function for Apple TV Video Play streamed to stderr: /var/lib/homebridge/appletv_control.sh: line 91: /home/villamann/.local/bin/atvremote: Permission denied

[7/26/2022, 9:05:31 PM] [Cmd4] getValue On function failed for Apple TV Video Play cmd: bash /var/lib/homebridge/appletv_control.sh Get 'Apple TV Video Play' 'On' Failed.  Generated Error: Error: Command failed: bash /var/lib/homebridge/appletv_control.sh Get 'Apple TV Video Play' 'On'
/var/lib/homebridge/appletv_control.sh: line 91: /home/villamann/.local/bin/atvremote: Permission denied

[7/26/2022, 9:05:31 PM] [Cmd4] getValue On function failed for Apple TV Video Play cmd: bash /var/lib/homebridge/appletv_control.sh Get 'Apple TV Video Play' 'On' Failed. Error: 126. Perhaps your device is busy?
[7/26/2022, 9:05:31 PM] [Cmd4] More than *900* errors were encountered in a row for "Apple TV Video Play" getValue. Last error found Getting: "On". Perhaps you should run in debug mode to find out what the problem might be.
[7/26/2022, 9:05:31 PM] [Cmd4] getValue: On function for Apple TV Video Play streamed to stderr: /var/lib/homebridge/appletv_control.sh: line 91: /home/villamann/.local/bin/atvremote: Permission denied

[7/26/2022, 9:05:31 PM] [Cmd4] getValue On function failed for Apple TV Video Play cmd: bash /var/lib/homebridge/appletv_control.sh Get 'Apple TV Video Play' 'On' Failed.  Generated Error: Error: Command failed: bash /var/lib/homebridge/appletv_control.sh Get 'Apple TV Video Play' 'On'
/var/lib/homebridge/appletv_control.sh: line 91: /home/villamann/.local/bin/atvremote: Permission denied

Any input that could help me on my way?

cristian5th commented 2 years ago

Hi.

Did you made the script executable? chmod +x /var/lib/homebridge/appletv_control.sh

villamann commented 2 years ago

Yes, followed the guide throughout; with two excemptions:

cristian5th commented 2 years ago

Are you sure that villaman is the user that is executing Homebridge? You can see that at the Homebridge status webpage, under System Information.

villamann commented 2 years ago

I set up Homebridge with homebridge-config-ui-x, so the user executing Homebridge is homebridge. Should I log in with homebridge user and install/setup/configure homebridge-appletv?

villamann commented 2 years ago

The answer is yes. Thank you for this, works very good.

cristian5th commented 2 years ago

The Homebridge user must be the owner of the shell script. Otherwise it will not be authorized to execute it.

I’m glad that you found the solution.