Using the Home Assistant Companion app on my macOS laptops (personal laptop and a work laptop), it’s been very handy to use it to trigger automations when the camera goes active to turn on some lighting in preparation for a video conference, etc.
What I’d like to have is some limited ways to control the macOS system from Home Assistant. The immediate “need” that I have is to be able to tell the laptop to put its displays to sleep. So, at the end of the workday, when I walk away from the work desk and laptop at home, I’d like to have the displays go dark/asleep - this will tell other software like the Slack client that I’m “away” as well as saving a few watts of power.
You can imagine other similar kinds of things that could be useful.
Looking that documentation, I see there’s a bunch of distinguished notification messages to perform various commands for Android, but I haven’t seem something that I could use to address my need to put the displays to sleep. (Today, I use a keyboard shortcut to bring up Alfred and then a quick shortcut an Alfred “system command” to put the displays to sleep.
I’ve discovered that I can put my macOS system display(s) to sleep as I wanted by running a shell command
pmset displaysleepnow
to cause this to happen. So I suspect I could build a script or something to listen to an MQTT topic, maybe expose a switch or button MQTT entity to that end.
It’d be great of the Home Assistant macOS application could do a similar thing for me…
shell_command:
# * "dummy" is a "standard" user (instead of the MacOS default "administrator" type of user) for a little bit added security,
# while having ssh sharing turned on
# * the default StrictHostKeyChecking=yes fails for some reason
# * custom made key has to be used for some reason
# todo: maybe https://github.com/adnanh/webhook instead of SSH–ing?
sleep_mbp13: "ssh [email protected] -i /config/.ssh/id_ed25519 -o StrictHostKeyChecking=no 'pmset sleepnow'"
To sum up:
SSH from HA machine into MacOS
Execute sleeping command over SSH
Do it via “standard” user, instead of your admin user (for added security)
Cavecat: the standard user has to be logged in in the background (via Fast User Switching)!
The setup sucks, gonna find a better way some day…
EDIT: oh, I’ve just noticed the question was about putting display (not Mac) to sleep → I guess this is the way anyway…
It would be handy if the Home Assistant application running on macOS could expose this as a service, or a switch or some other entity. It already has an authenticated connection to Home Assistant that could ideally be leveraged for this purpose. Maybe it could just exec the specific command or something, if there is no API exposed.
I’ve made this work. There are some important steps left out of the earlier suggestions.
The general idea already suggested is correct, you can use shell_command to accomplish the task.
However, you need to set up public/private keys to allow ssh to work without a password. You also need to do that in the right Docker container context. and you need to reduce your protection a bit to do that.
In ‘Addons’ enable the ‘Advanced SSH and Web Terminal’. Click on it and find the button for ‘Protection Mode’ you will have to turn this off to complete the setup. Restart the terminal addon.
Enter the terminal and use ‘docker container ls’ to see the containers. You should see a bunch of containers with ‘homeassistant’ being one of them.
Enter: exec -it homeassistant bash
to work inside this container, being very careful not to destroy your system! You are messing with the deep internals now.
Use ssh-keygen to create a public/private key pair.
Enter: cd ~/.ssh
Copy the XXX.pub public key file to the end of the authorized_keys file in your home .ssh folder on the Mac computer.
You should now be able to ssh to the Mac without using a password.
Now you can setup and use the shell_command action as described earlier.
Remember to turn protection mode back on for the terminal addon.