Can the macOS companion app put my displays to sleep?

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.

3 Likes

I can’t help, but that’s an awesome idea.

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…

So the million dollar question: did you get it to work @lmamakos?

(and if so, how? Would love to be able to do the same!)

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:

  1. SSH from HA machine into MacOS
  2. Execute sleeping command over SSH
  3. Do it via “standard” user, instead of your admin user (for added security)
  4. Cavecat: the standard user has to be logged in in the background (via Fast User Switching)!
  5. 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…

1 Like

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.

1 Like

I think I have you covered, check out this cool app I modified GitHub - ironcrafter54/mac2mqtt: mac2mqtt is a program that allow viewing and controlling some aspects of computers running macOS via MQTT.