AlexGustafsson / homebridge-wol

A Wake on Lan plugin for Homebridge
MIT License
301 stars 30 forks source link

Status not correct when sleeping #49

Closed yepecece closed 4 years ago

yepecece commented 6 years ago

Backstory

Status not correct when asleep and wake not working

Issue

I can successfully switch off my mac but not waking up. Also, the switch turns off, but after a couple of seconds the status is on again even thought the mac is still sleeping.

If i run the wakeCommand from the terminal the mac wakes up correctly.

Configuration

My configuration looks like this:

{
            "accessory": "NetworkDevice",
            "name": "Macbook Pro",
            "mac": <mac-address>,
            "ip": "192.168.0.100",
            "pingInterval": 20,
            "wakeGraceTime": 10,
            "wakeCommand": "ssh user@192.168.0.100 caffeinate -u -t 300",
            "shutdownGraceTime": 15,
            "shutdownCommand": "ssh user@192.168.0.100 pmset sleepnow"
 },

Environment

Log

Jul 26 19:57:37 pi homebridge[419]: [2018-7-26 19:57:37] [Macbook Pro] NetworkDevice shutdown cycle started for "Macbook Pro" (192.168.0.100)
Jul 26 19:57:37 pi homebridge[419]: [2018-7-26 19:57:37] [Macbook Pro] Attempting to shut down "Macbook Pro" (192.168.0.100) using "ssh user@192.168.0.100 pmset sleepnow"
Jul 26 19:57:37 pi homebridge[419]: [2018-7-26 19:57:37] [Macbook Pro] NetworkDevice "Macbook Pro" (192.168.0.100) went from status "Online" to "Shutting Down"
Jul 26 19:57:52 pi homebridge[419]: [2018-7-26 19:57:52] [Macbook Pro] NetworkDevice "Macbook Pro" (192.168.0.100) went from status "Shutting Down" to "Online"
domeOo commented 6 years ago

Im facing the exact same issue with my iMac any updates on this ?

I can successfully switch off my mac but not waking up. Also, the switch turns off, but after a couple of seconds the status is on again even thought the mac is still sleeping. If i run the wakeCommand from the terminal the mac wakes up correctly.

I thinks the Problem is that the wake command isn't fired because the ping says the mac is online.

AlexGustafsson commented 6 years ago

I think you’re right. Could you try to make it occur again and try to ping the iMac from a console when you think the device is off but the plugin says otherwise?

If the iMac responds to those pings, then there’s nothing that can be done as pings are the only way we can tell a device’s state.

yepecece commented 6 years ago

I just tried to ping the Mac and there is no packet lost.

Would there be another way to check if Mac is sleeping?

Maybe something like this: https://stackoverflow.com/questions/39468539/osx-check-if-screen-is-sleep-or-awake-from-command-line

Or

https://apple.stackexchange.com/questions/74383/how-can-i-determine-if-the-screen-is-asleep-in-mountain-lion-from-the-command-l

Thanks

AlexGustafsson commented 6 years ago

Thanks for checking!

That would require the plugin to SSH onto the machine to execute the command. I’d have to add an option for that. I’ll create an issue about it, but I can’t guarantee that I’ll get around it today or the coming few days.

nickcom commented 5 years ago

was there ever any update to this? I get the exact same issue - Mac sleeps fine but won't wake up, and the status for the sleeping mac changes to online after a while. Meanwhile the caffeinate command run manually in a terminal wakes the mac as intended.

AlexGustafsson commented 5 years ago

@nickcom Well, no. The issue in this thread is mostly identified to be how Macs handle pings. This plugin uses pings to identify whether or not a device is online. It works in the vast majority of use cases, but Macs are different. They may respond to pings even when they are technically sleeping. Therefore the only alternative is to use SSH instead of pinging to see whether the device is totally off or not. Currently there is no such feature available in the plugin.

nickcom commented 5 years ago

I understand that might explain why the status ends up being invalid (saying online even though the Mac is sleeping) but I am not clear if this is the same reason why the plugin can’t wake the Mac up.

I don’t mind too much if the plugin reports the Mac is awake when it is actually asleep, but it is a problem the Mac can’t be worken using the plugin at all. Would there be any work around, other than creating a new plugin?

AlexGustafsson commented 5 years ago

@nickcom I don’t think the SSH feature would necessarily need to be a new plugin, it could well be added to this plugin and in fact it is on the roadmap as #50.

Internally this plugin uses a finite state machine to make sure we can know what state a device is in (awake, waking up, turning off, turned off). Since we only have one press of a button to use as input, each press needs to go through the state machine to make sure that we don’t try to shut down computers that’s already shut down. In your case it seems like the computer is in the awake state, which will make the plugin try to shut the computer down instead. If you’re not interested in ever shutting down the Mac, you could try to use the same command for both the wake and shutdown command.

netmikey commented 5 years ago

I came to the exact same conclusion. In fact: unplugging the ethernet cable makes the mac go to "offline" state. Plugging the cable back in and turning it on via this plugin actually makes it wake as it should.

Personally, I want to use this plugin to trigger my mac out of sleep to then run some other stuff on it. I'm not a fan of loosening the security by enabling SSH on my main workstation, so I won't do anything that requires logging in via SSH.

A solution that would fit my needs perfectly and that might even be easier to implement for you would be adding a special mode that would get rid of the pinging altogether. Let me explain: instead of a switch that represents the network device's state and allowing me to turn it on and off, I'd be okay with a switch that would simply send the magic packets whenever I hit it. It wouldn't reflect the actual state of the network device, it would just send its WOL packets to it and then immediately reset its state to "off".

It could be implemented as a second mode and the configuration could look like this:

"accessories": [
  {
    "accessory": "NetworkDevice",
    "name": "My Mac Pro",
    "mode": "trigger",
    "ip": "192.168.1.51",
    "mac": "aa:bb:cc:dd:ee:ff"
  }
]

mode could then be one of:

What do you think?

AlexGustafsson commented 5 years ago

@netmikey Sounds good to me. I'll have some time next week to take a long-needed look into the state of this plugin.

AlexGustafsson commented 5 years ago

While we're at it, be sure to send any feedback or ideas any of you have for the project.

nickcom commented 5 years ago

just wondering if you still have a plan to look into this? I know only too well how life has a habit of making maintenance of something like this hard, but just wondering if I should hold out for an update or move on. Thanks!

AlexGustafsson commented 5 years ago

That's a valid question, @nickcom. Are you after the same trigger / monitor solution as @netmikey explained above? I'll implement the SSH version as well.

I'll start looking into it starting now. I will probably push some changes here and wait until i can make some tests myself before publishing a new version. I'll let you know when there are enough changes on here for you to test.

nickcom commented 5 years ago

A trigger solution (without the state switch) would work fine for me - I don't need to know the current state of the machine (although it would be nice), but I'd do need the ability to trigger a sleep and to separately trigger a wake and just assume it was successful.

AlexGustafsson commented 5 years ago

Turns out most of the trigger / monitor function is already implemented. Basically, if you disable pinging, no state is kept by the plugin. I found some weird things though which I will try to finish up before the end of the day.

AlexGustafsson commented 4 years ago

I've rewritten almost all of the plugin to use more modern features of JS which should help readability and stability.

Please try the latest version (currently available in the master branch) and open new issues if any of you feel like this has not been resolved.