luisiam / homebridge-cmdswitch2

CMD Plugin for HomeBridge (API 2.0): https://github.com/nfarina/homebridge
Apache License 2.0
176 stars 29 forks source link

How to I set the state_cmd to look for a specific term #23

Open HunterJKirby opened 7 years ago

HunterJKirby commented 7 years ago

I'm using cmdswitch to turn off and on my computer's display, but sadly I can't get the state. With the state_cmd I have now it will show "4" if the display is on, how to I tell the plugin to look for "4" as the response from the command to change the state to on? I'm very new to this so bear with me, heres the config.json I have

"lock_cmd": "pmset displaysleepnow", "unlock_cmd": "caffeinate -u -t 2", "state_cmd": "pmset -g powerstate IODisplayWrangler | tail -1 | cut -c29",

GatoPharaoh commented 7 years ago

try it with | grep 4

epheterson commented 7 years ago

Here's my setup for doing this:

"on_cmd": "caffeinate -t 1 -u",
"off_cmd": "pmset displaysleepnow",
"state_cmd": "test $(ioreg -n IODisplayWrangler | grep -i IOPowerManagement | grep -o 'DevicePowerState[^,}]*' | cut -d'=' -f2) -eq 4 && echo On",
    "polling": true,
    "interval": 5,
randyinla commented 4 years ago

Thank you for posting your state_cmd commands! Very useful!

I have two computers I want to turn displays off/on remotely. A Mac Mini that uses a time screensaver & a laptop. When I'm going to bed, the displays are too bright, so want to be able to turn them off when going to sleep and turn them back on (and launch the screensaver on the Mac Mini) when waking up. I am able to turn them off, but when turning them back on, the computers require a password for the local account and I don't know how to get past that remotely using bash & ssh. The screens turn on but then turn right back off again a minute later. For now, I've turned off the, "Require password immediately after sleep or screen saver begins" on the Mac Mini but I don't like having that machine wide open like that. The laptop is still set to require a password for my local account and I've been dealing with that by pressing my finger to the fingerprint reader, but it'd be great if I didn't have to.

I setup both computers to allow passwordless remote ssh connection & stored the passphrase to the private key on my homebridge server's ssh-agent.

"on_cmd": "ssh randy@studio.local 'caffeinate -u -t 2;open -a ScreenSaverEngine.app'",
"off_cmd": "ssh randy@studio.local 'pmset displaysleepnow'",
"state_cmd": "ssh randy@studio.local 'test $(ioreg -n IODisplayWrangler | grep -i IOPowerManagement | grep -o 'DevicePowerState[^,}]*' | cut -d'=' -f2) -eq 4 && echo On'"

If anyone can figure out how to get past the remote machine's local user password entry, that'd be awesome! The on_cmd will wake up the laptop's display but it presents a list of local users to choose from. If I'm not physically on the laptop, the display just turns off again after a minute.