Make Airplay speakers the voice of HA & other fun MacOS tricks

As a little update here, Better Touch Tool wasn’t working when the screen locked (even with the server set to never sleep) so I went back to the drawing board and figured out how to send keystrokes to Airfoil to select speaker groups, change volume and mute. Very easy once one learns a little bit of AppleScript.
Stereo pairs now working too

#!/bin/bash

osascript <<EOD

  tell application "Airfoil" to activate
  tell application "Airfoil"
  	disconnect from (every speaker)
  end tell
delay 1
  tell application "System Events"
      tell application "System Events" to keystroke "2" using command down
  end tell
delay 2
  tell application "Airfoil"
    set (volume of every speaker) to 0.5
  end tell

EOD
#!/bin/bash

osascript <<EOD

  tell application "Airfoil" to activate
  tell application "System Events"
    key code 125 using {shift down, command down}
  end tell
  
EOD

Yet another update- the above was NOT working with the screen locked, so I’ve come full circle and am back to something similar to my original example which for whatever reason DOES work with the screen locked. It also skips the step of disconnecting from everything before only connecting to the desired outputs.

shell_command:
  play_to_br_hps: "osascript /users/username/.homeassistant/scripts/play_to_br_hps.scpt"
#!/usr/bin/osascript

tell application "Airfoil"
    launch
    activate
	get every speaker
	disconnect from (every speaker whose id contains "Left")
	disconnect from (every speaker whose id contains "Right")
	disconnect from (every speaker whose id contains "Ecob")
	connect to (every speaker whose id contains "Bath")
end tell

-- make sure volume not muted

set vol to output volume of (get volume settings)

if vol < 20 then
	set volume output volume 50
end if

delay 1

While I’m at it here are volume controls directed at the system (so set Airfoil to link volume to the system)- these also work w/ screen locked

#!/usr/bin/osascript

set volume with output muted
#!/usr/bin/osascript

set volume output volume (output volume of (get volume settings) + 5)

And this will make a sensor so you can have your system volume displayed in HA:

sensor:
  - platform: command_line
    name: mini_vol
    command: 'osascript /users/username/.homeassistant/scripts/get_mini_vol.scpt'
    scan_interval: 5
    unit_of_measurement: '%'
#!/usr/bin/osascript

output volume of (get volume settings)
script:
  hps_vol_up:
    sequence:
      - service: shell_command.hps_vol_up
      - service: homeassistant.update_entity
        target:
          entity_id: sensor.mini_vol

Just came across this, this is pretty sweet. How are you targeting different HomePods? Just sshing and running the tts script with the text parameter seems like it would only target everything airfoil is connected to.

This is REAL cool and the solution to my home security system EXCEPT as of Montery the shell command for speaker no longer works… The suggest path "/users/username/.homeassistant/… no long appears valid as Ap[ple continues its user friendly practices

Ok I just can’t seem to find the hidden .homeassisstant. in my home directory or any where else unless I do a link via Samba

Mike- open Script_editor.app and put this code in, then run it. It will list your HomePods for you and you can pick a keyword from each of them:

tell application "Airfoil"
	launch
	activate
	get every speaker
end tell

Then you can use they keyword like I do in the examples above:

	connect to (every speaker whose id contains "Bath")

Stereo pairs appear to be only listed once so Airfoil sees them as a single speaker.

Jason you can hide & unhide hidden files (start with a dot) in Finder using this keyboard combo:
shift-command-period

This is still working for me. Make sure you replace “username” with the actual HA user’s name…

I understand about hidden files and finder.but .homeassistant isn’t there

-r--------    1 jason  staff       7 Nov  1  2021 .CFUserTextEncoding
-rw-r--r--@   1 jason  staff   24580 Jan 13 04:41 .DS_Store
drwx------    3 jason  staff      96 Mar 30 04:35 .Trash
-rw-------    1 jason  staff    9167 Oct 29 14:07 .bash_history
drwx------   33 jason  staff    1056 Oct 26 13:52 .bash_sessions
-rw-r--r--    1 jason  staff      23 Apr 22  2022 .bashrc
drwx------    3 jason  staff      96 Nov  2  2021 .cups
drwxr-xr-x   16 jason  staff     512 Mar 30 05:32 .dropbox
drwxr-xr-x    9 jason  staff     288 Jan 12 13:50 .dynupdater
-rw-------    1 jason  staff    1843 Jan 15 08:41 .history
drwxr-xr-x    4 jason  staff     128 Sep 18  2022 .local
-rw-r--r--    1 jason  staff     257 Apr 22  2022 .profile
drwxr-xr-x    6 jason  staff     192 Sep 18  2022 .pyenv
drwx------    4 jason  staff     128 Jan 12 12:47 .ssh
-rw-r--r--    1 jason  staff    1401 Oct 26 12:27 .tcshrc
drwx------    6 jason  staff     192 Nov  8  2021 .vnc
-rw-r--r--    1 jason  staff     130 Dec  3  2021 .vuescanrc
-rw-r--r--    1 jason  staff      33 Apr 22  2022 .zprofile

I have no problem running the applcript form te Mac I just can’t see how to run from homeassistant

You’re probably starting HA as root, in which case look for your config files here:
/private/var/root/

I talk about this confusion in the fourth or fifth paragraph here.

I will read your link wenI have a clear head. as the configuration files are not in my

/private/var/root/

what. have there is

drwxr-x---   5 root  wheel   160 Nov  1  2021 .
drwxr-xr-x  34 root  wheel  1088 Mar 31 10:09 ..
-rw-r--r--   1 root  wheel     3 Nov  1  2021 .CFUserTextEncoding
-r--r--r--   1 root  wheel    10 Aug 17  2018 .forward
drwx------  21 root  wheel   672 Oct 29 15:10 Library
sh-3.2# 

and they dot appear in the Library

Well if you’re running Catalina or Ventura and you’re not running in docker or something (in which case I have no idea) then it’s about what user starts home assistant. If you have a user specifically for HA then it’ll be in that user’s main folder. If all else fails…
sudo find / -type d -name '.homeassistant'

I am Monterey and Home Assistant as follow

Home Assistant 2023.1.7
Supervisor 2023.01.1
Operating System 9.5
Frontend 20230110.0 - latest

I’m Virtual Machine running Under Virtual Box. I have no idea what docker is. That may be my core problem

Ok did some research seems docker is a hole different approach but it better access to MacOS
statement?

I don’t use it- seems like unnecessary complexity to me

Your

sudo find / -type d -name ‘.homeassistant’

finds nothing and give some a series operations to permitted even though my sudo password is accepted

I am running HA under my user ID. and .homeasistant doe not appear in my home directory . re you running it under a virtual machine?

Hmm you’ve lost your config files.

This must have something to do with the structure of the files in the VM- do you have to search those separately? I use a VENV but not a hypervisor so I don’t know how that works.

Now you are talking way above me… As fa as I can figure out VENV create a virtual enviroment using python Reviewing https://www.home-assistant.io/installation/macos. it would appear that ti create t virtual “somethings” oner fo HA OS and other for the Core