Sweet, Hammerspoon can run macOS shortcuts! So awesome. 🥳
In a similar vein to my tinkering with Hue lights… I have a BenQ ScreenBar Halo on my monitor which I’d like to turn off when I logoff or my Mac goes to sleep. The latest version of the Halo has some sensors that can detect when you are around, but I don’t have that nor have any plans of buying that, so I wondered what else I can do.
I had a Meross SmartPlug lying around, and I figured maybe I could plug the ScreenBar into the Meross. And then use some API commands to turn off the on the SmartPlug maybe? Turning off the SmartPlug will turn off the lights, and turning on the SmartPlug won’t turn on the lights but I can manually turn that on – no big deal. (Update: Turns out the ScreenBar automatically lights up if it was previously lit when power was turned off. So turning on power to the SmartPlug effectively turns on the ScreenBar too).
I tried to find some way of dealing with the Meross API, and also found this Python library. I didn’t want to use that, but looking at the library I realized I could install a proxy app on my phone (e.g. proxypin or proxyman) to see what commands are being sent to the SmartPlug when I turn it on and off. That was pretty straight forward, and I found two HTTP calls too to turn off and on the plug. The HTTP calls worked too, just that they weren’t super reliable. They seemed to work as long as I put a minute or two between them – so I can turn on the plug, but if I send the command to turn off immediately, it doesn’t work. Wait 2 mins and send the command, and that works. Ditto for turning off.
That wasn’t super ideal, and I was wondering what else I can do when the idea struck me of using Shortcuts. The plug in question supports HomeKit so I have it setup in the Home app too; so I could easily create a shortcut to turn on the plug, and another shortcut to turn off the plug. And since I was using Hammerspoon to detect when I log off and login, it would be great if I can use Hammerspoon to run the appropriate shortcut.
Turns out that part is dead easy! All I need do is:
|
1 |
hs.shortcuts.run("<name of the shortcut>") |
So I created a second watcher along the lines of my previous one:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function toggleSocket(eventType) if (eventType == hs.caffeinate.watcher.screensDidUnlock or eventType == hs.caffeinate.watcher.systemDidWake) then hs.shortcuts.run("Desk Socket ON") elseif (eventType == hs.caffeinate.watcher.screensDidLock or eventType == hs.caffeinate.watcher.systemWillSleep or eventType == hs.caffeinate.watcher.systemWillPowerOff or eventType == hs.caffeinate.watcher.screensDidSleep) then hs.shortcuts.run("Desk Socket OFF") end end socketWatcher = hs.caffeinate.watcher.new(toggleSocket) socketWatcher:start() |
And that’s it, really! Job done.
Hammerspoon is sooooo awesome! 🤩
