Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a way to run wake up commands without trying wake on lan #149

Closed
UnbendableStraw opened this issue Mar 9, 2021 · 6 comments
Closed

Comments

@UnbendableStraw
Copy link

UnbendableStraw commented Mar 9, 2021

General idea

I'm using this plugin not to actually power machines on and off, but run either on.bat or off.bat. It works as expected when running the off command, but there are minor issues trying to use this exclusively to run commands for the on switch

a) if you don't enter the wake on lan config (i.e. mac address) the on command always fails because no WOL config was present

b) if you do enter wake on lan info it will try to send the magic packet first, then run my on command

It would be nice to see wake on lan info not required and when not provided to try running the off command anyways

@AlexGustafsson
Copy link
Owner

This should already work. See the following part of the README:

Key Description
mac The device's MAC address - used to send Magic Packets. Allows any format such as XX:XX:XX:XX:XX:XX or XXXXXXXXXXXX
broadcastAddress The broadcast address to use when sending the Wake on LAN packet
startCommand Command to run in order to start the machine
startCommandTimeout Timeout for the start command in seconds. Use 0 (default) to disable the timeout
wakeGraceTime Number of seconds to wait after startup before checking online status and issuing the wakeCommand, default 45
wakeCommand Command to run after initial startup, useful for macOS users in need of running caffeinate
wakeCommandTimeout Timeout for the wake command in seconds. Use 0 (default) to disable the timeout

By using startCommand, your command should be used when "starting" the device. You should be able to leave the mac out.

@AlexGustafsson
Copy link
Owner

Perhaps this should be clarified. If you get it working the way you'd like, could you please give some feedback on how to better explain your use case in the README?

Also, see #148 where the idea is to start gathering common use cases.

@UnbendableStraw
Copy link
Author

UnbendableStraw commented Mar 9, 2021

Thank you for the reply! I saw startCommand and thought that was my solution, however I might be using it wrong.

When I add a start command to the plugin's config like so; "startCommand": "sshpass -p 'password' ssh [email protected] -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 'cmd.exe /c C:\\Users\\UnbendableStraw\\Desktop\\start.bat'", I get the following error in the homebridge console

[3/9/2021, 9:33:13 AM] [Nic Main PC] NetworkDevice awake cycle started for "Nic Main PC" (192.168.1.241)
[3/9/2021, 9:33:13 AM] [Nic Main PC] Attempting to wake up "Nic Main PC" (192.168.1.241)
[3/9/2021, 9:33:13 AM] [Nic Main PC] NetworkDevice "Nic Main PC" (192.168.1.241) went from status "Offline" to "Waking Up"
[3/9/2021, 9:33:13 AM] [Nic Main PC] Attempting to start "Nic Main PC" (192.168.1.241) by sending a WoL packet
(node:13716) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'split' of null
    at NetworkDevice.wake (/homebridge/node_modules/homebridge-wol/lib/network-device.js:185:51)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:13716) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13716) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[3/9/2021, 9:33:14 AM] [Nic Main PC] NetworkDevice "Nic Main PC" (192.168.1.241) went from status "Waking Up" to "Offline"

Thus I think I'm stuck with using the wakeCommand which results in the original behavior I described (having to wait for the WoL command to fail then it runs my startcommand)

Full plugin config attempting startCommand:

{
    "name": "Nic Main PC",
    "ip": "192.168.1.241",
    "mac": "xx-xx-xx-xx-xx-E3",
    "host": "192.168.1.241",
    "pingInterval": 2,
    "pingsToChange": 5,
    "pingTimeout": 1,
    "pingCommand": "nc -vz 192.168.1.241 4028 2>&1 | grep -i 'open'",
    "pingCommandTimeout": 3,
    "startCommandTimeout": 5,
    "wakeGraceTime": 1,
    "wakeCommandTimeout": 5,
    "startCommand": "sshpass -p 'password' ssh [email protected] -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 'cmd.exe /c C:\\Users\\UnbendableStraw\\Desktop\\start.bat'",
    "shutdownGraceTime": 1,
    "shutdownCommandTimeout": 5,
    "shutdownCommand": "sshpass -p 'password' ssh [email protected] -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 'cmd.exe /c C:\\Users\\UnbendableStraw\\Desktop\\stop.bat' ",
    "log": true,
    "logPinger": false,
    "debugLog": false,
    "returnEarly": true,
    "accessory": "NetworkDevice"
}

@AlexGustafsson
Copy link
Owner

AlexGustafsson commented Mar 10, 2021

What version of homebridge-wol do you use?

@AlexGustafsson
Copy link
Owner

AlexGustafsson commented Mar 10, 2021

Your usage seems correct, but there seems to be a bug here:

const commandName = this.config.wakeCommand.split(' ')[0];

This is fixed in the latest version. Can you try to install the beta (#124)?

@UnbendableStraw
Copy link
Author

UnbendableStraw commented Mar 11, 2021

That does the trick! I opted to replace those lines in network-device.js , then in my plugin config I can get away with no WoL configuration entered and it will execute the start command right away without failing:

{
    "name": "Plex VM",
    "ip": "192.168.1.172",
    "pingInterval": 10,
    "pingsToChange": 2,
    "pingTimeout": 1,
    "pingCommand": "nc -vz 192.168.1.105 32400 2>&1 | grep -i 'open'",
    "pingCommandTimeout": 1,
    "startCommandTimeout": 5,
    "startCommand": "sshpass -p 'password' ssh [email protected] -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 'powershell -Command Start-VM -Name Plex'",
    "shutdownCommand": "sshpass -p 'password' ssh [email protected] -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 'powershell -Command Stop-VM -Name Plex'",
    "shutdownGraceTime": 10,
    "shutdownCommandTimeout": 2,
    "log": true,
    "logPinger": false,
    "debugLog": false,
    "returnEarly": true,
    "accessory": "NetworkDevice"
}

I was on Homebridge 1.3.2 and I just updated to 1.3.3 and its still working fine : ) Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants