Git Product home page Git Product logo

Comments (7)

postlund avatar postlund commented on May 30, 2024

Hi and thanks for the feedback! :)

This is an issue, I agree. I haven't found any way to determine that I'm afraid. But it seems to be a standing issue as I've heard others with basically the same behavior. A question regarding your setup. Is it that your projector wakes up because the HDMI-output on the Apple TV is activated?

from pyatv.

robbiet480 avatar robbiet480 commented on May 30, 2024

The projector is waking up because Apple TV is waking up and sending a Power On command to all HDMI connected devices via CEC.

from pyatv.

philk avatar philk commented on May 30, 2024

from pyatv.

postlund avatar postlund commented on May 30, 2024

Ok, I think I know how to do this :) Bear with me... The device goes to sleep and registers itself to a Bonjour Sleep Proxy, like a computer or AirPort express. You can see if you have one with:

Linux: avahi-browse _sleep-proxy._udp
macOS: dns-sd -B _sleep-proxy._udp local

The Bonjour Sleep Proxy will take responsibility of the device (Apple TV) and answer to ARP requests. So, if you have an Airport Express that acts as a proxy, it will broadcast that the IP-adress of the Apple TV with the MAC-address of itself (ARP). When someone tries to access (e.g. ping) that IP-address, the request will end up at the Airport Express which in turn will wake up the Apple TV with a magic packet. After this the Apple TV takes over its IP-address again as usual.

What I'm thinking is that if we know the MAC-address of the Apple TV in question, we can just compare that with current value in the ARP table and be pretty sure if it's asleep or not. Then we can act based on that. Maybe we can add a new API to check if the device is asleep or not (pyav.is_apple_tv_in_standby?). If we currently don't have that MAC-address in the ARP-table, we just make an ARP-request and wait for the response (an ARP-request will not wake the device). Pretty cool I must say, but it will only work if there is a device acting as proxy on the network.

Good reference: http://www.cnpbagwell.com/mac-os-x/bonjour-sleep-proxy

from pyatv.

postlund avatar postlund commented on May 30, 2024

My theory above did not bear any fruit, it was too unreliable. I implemented a work-around a while back in Home Assistant, see this PR: home-assistant/core#5962. It is probably as good as it's gonna get for now. In order to still use the Apple TV, I would suggest that you write an automation that turns it on/off depending on some other condition, like if a certain source is selected on your receiver. That is probably the best way to do it, as long as CEC is enabled.

from pyatv.

Nedryland avatar Nedryland commented on May 30, 2024

This is an old post but wanted to leave my experience in case someone else stumbles across this and has similar issues. My apple tv wasn't powering on randomly but just the same I wanted to be able to monitor the on/sleep status accurately. I set up ping sensors for two apple tv 3s successfully, however when I put atv1 to sleep, atv2 started to return a ping. After reading about bonjour sleep proxies I determined that atv2 had registered with atv1 spleep proxy, so once that proxy went to sleep it reverted to it's own mac address and answered pings directly. To fix the issue I unplugged both atvs, then powered on atv1 and put it to sleep, then I powered on atv2 and put it to sleep as well. This forced each to register with my time capsule/airport extreme and now the ping sensors work properly once again. (follow up: MacBook also interferes with the sleep process so remove from network or put to sleep before powering on atvs in sequence and sleeping them for the first time)

I woke and slept them again several times in random orders and all seems well for now. Luckily I have a time capsule as I'm pretty certain that this would not work without it. I also could not find a way to force an atv to use a specific sleep proxy without powering things on in specific order, however they should default to the time capsule if available. My theory is that one atv registered to the other when at some point I rearranged some patch cables and the time capsule wasn't available temporarily.

Another less accurate (and messy) method is to (1)create an input_boolean for the power status (2) an automation that turns on the input_boolean whenever the media_player state goes from idle to playing or whenever you send a remote command (3) an automation that puts the apple tv to sleep manually by calling remote_send commands and also turns off the input_boolean. In order for this to work with any accuracy you have to disable the auto sleep function in the apple tv and always call sleep function from HA, as HA will have no way of knowing if the atv puts itself to sleep.

I used this method for awhile until I decided to finally figure out the ping issue and it works reasonably well as long as you get your sleep automation or script working reliably (you have to get the timing right and put delays between remote_send commands because if you issue to many too fast it will skip some). I have one automation that navigates to the same spot on the home screen no matter where I happen to be. I call this first from other navigation automations such as sleep which then goes five spaces to the left, into "settings" and down to "sleep now". I also have single button shortcuts (automations) for HBO, Netflix, Plex, etc. which I can call from siri (very handy).

from pyatv.

Nedryland avatar Nedryland commented on May 30, 2024

As a follow up to the previous post.. After awhile I noticed that the apple tv ping sensors were returning randomly every 10-15 minutes or so when set at 60 second intervals, and every 5-7 minutes or so when set at 25 second intervals. This tells me that it's likely related to the number of pings and not the amount of time that has passed. I tried using an arp command to reference the mac address and same story. The arp command sensor behaved the exact same way. So for some reason after x amount of queries the sleep proxy allows a direct response?

So how did I solve the problem of an occasional false return?

(1) A ping or arp sensor running every 25 seconds and echoing 'on' or 'off'.

(2) An automation triggered by the ping going from 'off' to 'on'; 60 second delay; "homeassistant.turn_on" "input_boolean.yourboolean1"; 2 second delay; "homeassistant.turn_off" "input_boolean.yourboolean1"

(3) Another automation triggered by "input_boolean.yourboolean1"; with condition that the ping sensor is 'on'; then "homeassistant.turn_on" "input_boolean.yourboolean2"

(4) Use "input_boolean.yourboolean2" as your sensor or reference it in a binary or template sensor

In this way we basically skip a ping or two whenever it goes from off to on. By the time the second automation kicks in, the ping sensor has already ran 1-2 more times ensuring that it will be back to 'off' state, therefore the condition will not be met and the second input_boolean (the one we are using for our sensor) will remain off. When the apple tv is actually on it will return all pings, the condition is met, and the second input_boolean is turned on. This has so far worked like a charm.

I also tackled the sleep and display status of my MacBook, which of course have their own idiosyncrasies involving a sleep proxy and what not. I tried everything I could think of. When the mac is asleep with wake for network access enabled and you send an ssh command from HA it will wake the mac and unlike the atvs it always returns a ping. So sending commands of any kind for status while sleeping is pretty much not an option.

What did I come up with in the end?

(1) 2 command_line sensors that read txt files on a network share. One for sleep status "power.txt" and one for display sleep status "display.txt"

(2) A script that runs on the MacBook "displaystatus.sh" and writes the display status to the "display.txt" file every 30 seconds

(3) Sleepwatcher2.2 running on mac that runs a script before sleeping and after waking up. The sleep script stops "displaystatus.sh" and writes 'off' to both "display.txt" and "power.txt". The wake script writes 'on' to "power.txt" and starts "displaystatus.sh"

I configured the display status with the additional script because I couldn't get Sleepwatcher to work with the display status, but it is supposed to. I could only get sleep/wake and plug/unplug triggers to work for now. Sleepwatcher also does not seem to be triggered by login or logout, shutdown or reboot so may need additional script to run a boot to get things started. I have not gotten that far as I very rarely power down or restart. It's not perfect but It's accurate and works fine for now.

Hopefully someone struggling with apple integration can find all this useful. This issue drove me crazy until I came up with something reliable enough that I could trigger automations.

from pyatv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.