homebridge-appletv-enhanced: Accessory "Apple TV" does not "turn on" with native remote

Analysis

I have successfully installed v1.0.0 of the Plugin, all logs are looking good, the Apple TV is discovered and no error shows up. I also get the Accessory running so far and i can turn it on/off successfully by using the Accessory trough Homebridge.

Now, the thing that i actually want to achieve - i have an automation in place that declines the canvas and turn off all lights when the Apple TV is getting activated.

The automation works great when i turn the Apple TV on trough the Accessory, or with the “Turn On” command - but it’s not working great when i just use the native remote.

Expected Behavior

I would like to have a reliable “active state”, especially when i use the native remote.

Steps To Reproduce

  1. Setup Apple TV Enhanced (and freshly boot the homebridge/childbridge)
  2. Add the Apple TV Accessory in Homekit
  3. Turn the Apple TV on with the native remote, it works!
  4. Wait for a few hours or one or two days
  5. Coming back to the setup (logs are showing nothing special), turn on the Apple TV with the native remote and watch how the state doesn’t change at all!

Logs

[02/01/2024, 08:56:08] [Apple TV Enhanced] Child bridge process ended
[02/01/2024, 08:56:08] [Apple TV Enhanced] Process Ended. Code: 143, Signal: null
[02/01/2024, 08:56:15] [Apple TV Enhanced] Restarting Process...
[02/01/2024, 08:56:15] [Apple TV Enhanced] Launched child bridge with PID 523338
[02/01/2024, 08:56:16] Registering platform 'homebridge-appletv-enhanced.AppleTVEnhanced'
[02/01/2024, 08:56:16] [Apple TV Enhanced] Loaded homebridge-appletv-enhanced v1.0.0 child bridge successfully
[02/01/2024, 08:56:16] Loaded 0 cached accessories from cachedAccessories.0ED7621AE9BD.
[02/01/2024, 08:56:16] [Apple TV Enhanced] Platform: Finished initializing platform: Apple TV Enhanced
[02/01/2024, 08:56:16] [Apple TV Enhanced] Python check: Starting python check.
[02/01/2024, 08:56:16] [Apple TV Enhanced] Python check: plugin dir exists.
[02/01/2024, 08:56:16] Homebridge v1.7.0 (HAP v0.11.1) (Apple TV Enhanced) is running on port 30495.
[02/01/2024, 08:56:16] [Apple TV Enhanced] Python check: Python 3.11.2 is installed and supported by the plugin.
[02/01/2024, 08:56:16] [Apple TV Enhanced] Python check: Virtual environment already exists.
[02/01/2024, 08:56:16] [Apple TV Enhanced] Python check: Venv is using current system python version (3.11.2).
[02/01/2024, 08:56:16] [Apple TV Enhanced] Python check: Checking if pip is up-to-date ...
[02/01/2024, 08:56:34] [Apple TV Enhanced] Python check: Pip is up-to-date
[02/01/2024, 08:56:35] [Apple TV Enhanced] Python check: Python requirements are satisfied.
[02/01/2024, 08:56:35] [Apple TV Enhanced] Python check: Finished
[02/01/2024, 08:56:35] [Apple TV Enhanced] Platform: Starting device discovery ...
[02/01/2024, 08:56:42] [Apple TV Enhanced] Platform: Adding Apple TV Apple TV (C0:95:6D:5F:33:4B)
[02/01/2024, 08:56:42] [Apple TV Enhanced] Apple TV (C0:95:6D:5F:33:4B): verifying credentials ...
[02/01/2024, 08:56:44] [Apple TV Enhanced] Apple TV (C0:95:6D:5F:33:4B): Credentials are still valid. Continuing ...
[02/01/2024, 08:56:46] [Apple TV Enhanced] Apple TV (C0:95:6D:5F:33:4B): Finished initializing
[02/01/2024, 08:56:46] Apple TV Apple TV 0760 is running on port 45589.
[02/01/2024, 08:56:46] Please add [Apple TV Apple TV 0760] manually in Home app. Setup Code: 770-02-137
[02/01/2024, 08:56:48] [Apple TV Enhanced] Apple TV (C0:95:6D:5F:33:4B): New Active State: off
[02/01/2024, 08:56:48] [Apple TV Enhanced] Apple TV (C0:95:6D:5F:33:4B): Current App: com.apple.TVMusic

Configuration

{
            "mediaTypes": [
                "music",
                "video"
            ],
            "deviceStates": [
                "idle",
                "paused",
                "playing",
                "stopped"
            ],
            "remoteKeysAsSwitch": [
                "home",
                "turn_off",
                "turn_on",
                "volume_down",
                "volume_up"
            ],
            "customInputURIs": [],
            "discover": {
                "unicast": [],
                "blacklist": [],
                "multicast": true
            },
            "name": "Apple TV Enhanced",
            "deviceStateDelay": 0,
            "avadaKedavraAppAmount": 15,
            "disableVolumeControlRemote": false,
            "forceVenvRecreate": false,
            "logLevel": 3,
            "_bridge": {
                "username": "0E:D7:62:1A:E9:BD",
                "port": 30495
            },
            "platform": "AppleTVEnhanced"
        },

Environment

  • OS: Debian GNU/Linux Bookworm (12)
  • Docker: false
  • Homebridge: 1.7.0
  • Apple TV Enhanced: 1.0.0
  • Node: 20.10.0
  • npm: 10.2.3
  • Python: 3.11.2

Additional Context

Sometimes when i come home after a day, i see the following Warning in the Logs:

Apple TV (C0:95:6D:5F:33:4B): Apple TV can be reached on OSI Layer 2 but not on 3. This is likely a network problem. Restart the plugin after you have fixed the root cause.

Of course, this can be a reason why it suddenly stops working - i just don’t know what to fix in my network, because, the network is working fine! I see my Apple TV online in my connected wifi devices with the static IP it always gets

About this issue

  • Original URL
  • State: closed
  • Created 6 months ago
  • Comments: 52 (26 by maintainers)

Most upvoted comments

@mufler Nothing to do on your side. I will update you guys here once the fix to the problem is released …

@maxileith All good so far and sorry for my passive aggressive tone i stumbled into meanwhile 🙏 I can get very emotional with tech stuff and that’s not good in open source communities (i basically should know that after 20 years of programming -.-)

I have a solution now - it’s the Flirc USB dongle and the Flirc Skip Remote:

  • I throw away the original apple tv remote and use the skip remote now - the skip remote natively supports all Apple TV generations
  • With the skip remote, it’s possible to record keys on the flirc usb dongle (not possible with the apple remote as flirc doesn’t support RF devices)
  • With the Flirc USB, i can record the “power” button of the skip remote and not just power on my apple tv, but also execute a keyboard shortcut on my custom raspberry pi (which is in place now besides the apple tv, just for this single purpose)
  • Next i’m going to use an app launcher for linux, which gives me the possibility to execute a custom “curl” command on that keyboard hotkey -> so i press power on, the flirc dongle receives that and executes a curl request on my network
  • I programmed a new homebridge plugin that gives me virtual buttons, and an HTTP interface to update/toggle these buttons -> so i can send the right HTTP request with my curl command, and toggle a virutal button called “cinema on”
  • After all, i can add an automation in homekit, based on that virtual button, which will execute my wanted logic like “decline the canvas, and during daytime, close all blinds, turn off all lights”

For anybody curious, here is my virtual-button plugin: https://github.com/rocket-monkey/homebridge-virtual-button But it’s not finished yet, i’m working on the HTTP updates right now and will deliver documentation too once done

@maxileith I use 2 HomePods as your speakers.

@felipehjcosta from the README:

  • If using external speakers like HomePods as the default, the Apple TV is always reported as powered on. This is a known issue of the dependency pyatv, see postlund/pyatv#1667. As a result, the Apple TV device will only be shown as off in HomeKit when powered off via the Apple TV device in HomeKit. After restarting the plugin the device will always be shown as on.

@maxileith I have submitted the bug report here: https://github.com/postlund/pyatv/issues/2331

Hi @rvetere,

I think I have found the root cause. However, before that I want to say something …

Way of communication

I’m a programmer myself and i just wanted to investigate further with this little “poller” - but of course, this is not the setup i’m looking for.

I really don’t like to see This plugin slows down Homebridge and doesn’t give me much trust in this project

You can always do your own projects if you do not have the trust into this project. No software project is perfect, there are always some bugs. You will just not get around this! What is bugging me here is that you are only complaining all the time and providing suggestions for solutions that are ridiculous, like …

next i’m going to build a tiny esp32 board with a low-tech camera and just watch the tiny LED that starts shining on the device with tensor flow 😄 there’s already a project in place to do that ^^ https://github.com/jomjol/AI-on-the-edge-device

or

After i personally started investigation “how to determine if an Apple TV is running”, i came up with the idea to develop a small App for Apple TV, which then could be used to expose something like an API for this plugin - maybe much stabler…? How did you implement your “active” state anyway?

… which is kind of concerning. Nevertheless, you have really found a bug here and I am willing to fix that bug with you, but I need you to cooperate on this with me. Open-source should be fun, and I do not have the feeling that you are appreciating what I am doing here … I want this issue to be fixed for you and potentially others, too. I just cannot reproduce the error with my setup. This is why I need you to provide the exact outputs that I need to investigate the problem.

You truely need to work on your tone. This is by far the most disrespectful way of dealing with an issue that I have experienced in this project.

Of course i can do that, but, it doesn’t show anything new to me…

I did not ask you to turn on your Apple TV, then execute the atvscript, terminate it, turn off your Apple TV, then execute arvscript and terminate it again. If you had just looked at the documentation of pyatv once, you would have seen that atvscript push_updates streams all updates regarding an Apple TV. So I still would like to see how it looks like when you are executing atvscript push_updates and keep it running while you are turning on or off your Apple TV. This is a big difference!

I think I have found the root cause …

I think I have found the root cause. Let us look at the following:

{"result": "success", "datetime": "2024-01-11T19:45:57.122827+01:00", "power_state": "on"}
{"result": "success", "datetime": "2024-01-11T19:45:57.123138+01:00", "output_devices": [{"name": "Apple TV", "identifier": "D4ED48E4-4C88-47C0-B05C-077747F85C5D"}]}
{"result": "success", "datetime": "2024-01-11T19:45:57.125393+01:00", "hash": "ca496c14642c78af6dd4250191fe175f6dafd72b4c33bcbab43c454aae051da1", "media_type": "unknown", "device_state": "idle", "title": null, "artist": null, "album": null, "genre": null, "total_time": null, "position": null, "shuffle": "off", "repeat": "off", "series_name": null, "season_number": null, "episode_number": null, "content_identifier": null, "app": null, "app_id": null}
{"result": "success", "datetime": "2024-01-11T19:45:57.126692+01:00", "volume": 50.0}

There are 4 lines in this log, that have different JSON schemas, let just refer to them as line 1, 2, 3 and 4. I did only know of the schema of line 1, 3 and 4 since 2 never appeared on my setup. So this is an unexpected pattern to me.

As it turns out, the developer of sebbo2002/node-pyatv did not know of that either (I suppose). I am using this dependency to integrate pyatv into my plugin. Just recently I have discovered a bug in this dependency which resulted in a very similar error pattern, see #147. In order to fix that, I have opened https://github.com/sebbo2002/node-pyatv/pull/291 (if you want to know the details about the root cause, please take a look into the PR, this will apply for this issue as well). I think we will need a similar fix for this issue, so output_devices in the atvscript push_updates output are handled similar to volume and power_state.

https://github.com/sebbo2002/node-pyatv/blob/60a414daeaac29c53254611ef25a54dbf671a720/src/lib/device-events.ts#L38-L52

I have created an issue here: https://github.com/sebbo2002/node-pyatv/issues/295

So now we need to wait for the developer to review the issue, provide a fix and release it. But I am confident that this is the problem we were looking for.

@rvetere thank you for providing the logs 👍🏻

Hey everybody, here i deliver my debug logs as i found out now what is the debug-level (4!, it’s nowhere documented here…)

It is documented in the config.schema.json which is displayed when using the Cofnig UI 😉

image

After i personally started investigation “how to determine if an Apple TV is running”, i came up with the idea to develop a small App for Apple TV, which then could be used to expose something like an API for this plugin - maybe much stabler…? How did you implement your “active” state anyway?

Way too much overhead … this can definetly be determined the way I am currently doing it. At my side it is rock solid.

How did you implement your “active” state anyway?

Just look at the source code, this is what FOSS is all about 😉

https://github.com/maxileith/homebridge-appletv-enhanced/blob/328e5b86647b1c2e613bd10584918af76ace9e13/src/appleTVEnhancedAccessory.ts#L247

“event deviceState: null”, null is not the state i’m looking for as the device is running 😕

What do you mean with “running”? Is music/video playing? If not, this is the expected behaviour.


What I need you to do @rvetere: Send me the output of /var/lib/homebridge/appletv-enhanced/.venv/bin/atvscript -s xxx.xxx.xxx.xxx push_updates where xxx.xxx.xxx.xxx is the IP of your Apple TV. Then turn on / off your Apple TV a few times and post the logs here. (assuming /var/lib/homebridge is your homebridge dir)

It may be the case that the output of this script differs wether or not one has a HDMI CEC setup or not.

That’s sad. 😢

I will follow the original issue on pyatv thanks!

@maxileith I use 2 HomePods as your speakers.

@felipehjcosta from the README:

  • If using external speakers like HomePods as the default, the Apple TV is always reported as powered on. This is a known issue of the dependency pyatv, see postlund/pyatv#1667. As a result, the Apple TV device will only be shown as off in HomeKit when powered off via the Apple TV device in HomeKit. After restarting the plugin the device will always be shown as on.

Hey @maxileith, I’ve changed to Log Level 4 (Debug) as you suggested however any kind of log appears when I use the native remote controller.