lukasroegner / homebridge-apple-tv-remote

Plugin for controlling Apple TVs in homebridge.
MIT License
132 stars 13 forks source link

On/Off Switch Updated to True #30

Closed emst00 closed 4 years ago

emst00 commented 4 years ago

Thank you for this plug-in, I find it very helpful. My switch gets stuck to "on" the majority of the time when the AppleTV is off and can't figure out why. I enabled debug mode and this is what I get:

[4/25/2020, 9:02:05 PM] [Apple TV Platform] [AppleTV] Sending heartbeat... [4/25/2020, 9:02:05 PM] [Apple TV Platform] [AppleTV] Connecting... [4/25/2020, 9:02:05 PM] [Apple TV Platform] [AppleTV] Force reconnect: false [4/25/2020, 9:02:05 PM] [Apple TV Platform] [AppleTV] Retry count: 10 [4/25/2020, 9:02:05 PM] [Apple TV Platform] [AppleTV] Already connected [4/25/2020, 9:02:05 PM] [Apple TV Platform] [AppleTV] On/off switch updated to true

Kienz commented 4 years ago

I have the same issue - the problem was, that the Apple TV (Home Hub) was used to play music to Airplay Speakers. After that the logicalDeviceCount was 1 even though the Apple TV was shutdown. In this case isGroupLeaderand isAirplayActive are true.

DeviceInfo DeviceInfoMessage {
  airplayReceivers: [ 'TVAirPlay' ],
  uniqueIdentifier: 'xxx',
  name: 'Apple TV Schlafzimmer',
  localizedModelName: 'AppleΒ TV',
  systemBuildVersion: '17L256',
  applicationBundleIdentifier: 'com.apple.mediaremoted',
  protocolVersion: 1,
  lastSupportedMessageType: 78,
  supportsSystemPairing: true,
  allowsPairing: true,
  systemMediaApplication: 'com.apple.TVMusic',
  supportsACL: true,
  supportsSharedQueue: true,
  supportsExtendedMotion: true,
  sharedQueueVersion: 2,
  deviceUID: 'xxx',
  managedConfigDeviceID: 'xxx',
  deviceClass: 4,
  logicalDeviceCount: 1,
  isProxyGroupPlayer: true,
  groupUID: 'xxx',
  isGroupLeader: true,
  isAirplayActive: false,
  systemPodcastApplication: 'com.apple.podcasts',
  enderDefaultGroupUID: 'xxx'
}
lukasroegner commented 4 years ago

@Kienz Those are great findings! Have you figured out a way to determine whether the Apple TV is off despite logicalDeviceCount: 1?

Kienz commented 4 years ago

I've changed the this._isOn = logicalDeviceCount > 0 to the following.

if (m.payload.logicalDeviceCount === 0 || m.payload.logicalDeviceCount > 0) {
    if (
        (m.payload.logicalDeviceCount > 0 && !m.payload.isProxyGroupPlayer) ||
        (m.payload.logicalDeviceCount > 0 && m.payload.isProxyGroupPlayer && m.payload.isAirplayActive)
    ) {
        this._isOn = true;
    } else {
        this._isOn = false;
    }
    this.emit('isOnChanged');
}

Have to check the different variants. With this code the Apple TV is displayed Off. But have to check what happens when AirPlay to a Speaker over HomeKit is started.

lukasroegner commented 4 years ago

@Kienz Have you already checked whether your solutions works for all cases?

Kienz commented 4 years ago

I think everything works - but not sure if I tested all cases. :#

lukasroegner commented 4 years ago

I integrated your checks into the plugin and published a beta version:

npm install -g homebridge-apple-tv-remote@beta
lukasroegner commented 4 years ago

Has anyone had issues with the beta version? Does it enhance the on/off detection for you?

RaymondMouthaan commented 4 years ago

Hi @lukasroegner, just installed the beta, but t seems to be versioned as homebridge-apple-tv-remote v0.7.7?

image

My config is like:

{
            "name": "Apple TV Platform",
            "devices": [
                {
                    "name": "AppleTV",
                    "credentials": "token",
                    "isPlayPauseSwitchEnabled": true,
                    "playPauseSwitchName": "playPause"
                }
            ],
            "platform": "AppleTvPlatform"
        }

I've only configured a PlayPause switch since im interested to control some lights based upon its state. (on play -> lights off, pause lights on). After some testing with a Harmony remote, which play/pauses the Apple TV, the PlayPause switch is not always in sync with the actual status of the AppleTV. The switch even changes its state while doing nothing. Like now a movie is playing, the PlayPause switch has state on, but after a few minutes it changed to state off and while writing this it changed back to state 'on' again ... and now its 'off' again ...

How do I enable debugging, so I can provide the logs?

lukasroegner commented 4 years ago

@RaymondMouthaan see #7. There are several issues when detecting Play/Pause state:

You can see the raw message coming from the Apple TV with the appletv command line tool. If you recognize a pattern, which can enhance the play/pause state, feel free to send me your findings.

sergoo-lbt commented 4 years ago

Has anyone had issues with the beta version? Does it enhance the on/off detection for you?

I tried the beta version. My power switch does not behave adequately at all. I think the latest version is more stable. Sometimes the status stops changing and I restart homebridge, after that everything works perfectly again for a while.

lukasroegner commented 4 years ago

@RaymondMouthaan I published a new beta version (0.8.0) on NPM. Changes:

RaymondMouthaan commented 4 years ago

@lukasroegner just for reference https://github.com/lukasroegner/homebridge-apple-tv-remote/issues/7#issuecomment-623089316.

Just installed beta 0.8.0 and enabled debug option -D to homebridge, this gives enormous amount of information, which is to much to handle ...

However I disabled the debug logging and it looks much more stable then before! The homebridge-apple-tv-remote PlayPause switch seems to follow the actual AppleTV device state as it suppose to do, which is awesome!

Tested also with Prime Video, which is shows exact behaviour, so it looks like you smashed this bug πŸ’―

I'll test for next coming days, to see if weird behaviour will occur and keep you informed.

Thanks so far for all effort you took in this project and the quick responses!

lukasroegner commented 4 years ago

@RaymondMouthaan That's great! As soon as your testing is completed, I'll integrate the beta branch into master. Can you also check whether the on/off state is now working properly for you?

RaymondMouthaan commented 4 years ago

@lukasroegner as for now PlayPause state is working perfectly fine and stable πŸ‘

Combined with OnOff switch it looks like the OnOff switch perfectly follows the actual state of the Apple TV device. However one thing I noticed is that PlayPause doesn't turn off when the device is turned off, while is should - after all, it can never be played when the device is turned off. I guess when OnOff is off than also PlayPause should be turned off :)

update: same behaviour happens when no OnOff switch is configured - when the Apple TV gets turned off by remote, the PlayPause switch gets turned on ...

lukasroegner commented 4 years ago

@RaymondMouthaan New beta version 0.8.1: When the plugin detects that the state of the Apple TV switched to off, the play state is also switched to off. Please check if that works properly for you

RaymondMouthaan commented 4 years ago

@lukasroegner wow that was quick .. Im on it πŸ‘

Two scenarios

update: I did some more testing but the issue remains for both scenarios.

sergoo-lbt commented 4 years ago

@RaymondMouthaan New beta version 0.8.1: When the plugin detects that the state of the Apple TV switched to off, the play state is also switched to off. Please check if that works properly for you

You are an amazing developer! Thank you very much for your work. I'll test everything today!

sergoo-lbt commented 4 years ago

@RaymondMouthaan New beta version 0.8.1: When the plugin detects that the state of the Apple TV switched to off, the play state is also switched to off. Please check if that works properly for you

There's still something wrong. Apple tv itself went to sleep, but the power and pause button status remained on. Turned off the TV, it turns off the apple tv via hdmi cec, but in the home app the power switch and pause are all also enabled.

lukasroegner commented 4 years ago

@sergoo-lbt Please start homebridge with -D parameter and let the Apple TV go to sleep again. During waiting, you should get at least one (large) log entry per minute. I can evaluate the logs to see what data is sent by the Apple TV when it goes to sleep.

sergoo-lbt commented 4 years ago

@sergoo-lbt Please start homebridge with -D parameter and let the Apple TV go to sleep again. During waiting, you should get at least one (large) log entry per minute. I can evaluate the logs to see what data is sent by the Apple TV when it goes to sleep. Here's what I was able to understand. Basically, everything works. But it takes a very long time to change the power state and pause, about a minute. I guess I just didn't wait for them to be fully updated. It turns out that if you turn on the power or pause key several times in a row, the status will not have time to update. I'll try again. I just shared my thoughts. I'm trying to figure out how it all works. I'll attach the logs, but I apologize for the screenshots, I just don't have access to my computer right now. EC513861-652A-40BA-ADAF-2085982C08A9 65356B42-7B2C-43A7-9439-EB8B9D55C378 C3D21C70-4298-4C61-B286-19DDB13CD66F 91DF2089-88DD-4935-80DE-DE8B7083D8F8

sergoo-lbt commented 4 years ago

When I took screenshots, the apple tv was turned off.

lukasroegner commented 4 years ago

So, I did another update of the beta version (0.8.2):

I also tested this version extensively, here are my findings:

Background info: The Apple TV should send messages to the plugin whenever the device state changes (important for detecting on/off state). This, however, does not seem to work reliable. Thus, I implemented a "heartbeat" some time ago, which actively requests the current device state from the Apple TV (this works fine). The current interval is 60 seconds (i.e. every minute the on/off state is updated reliably. You can change the interval with an undocumented config property: in the root of the plugin configuration, add "heartbeatInterval": 10 for a 10 second interval. This can increase the responsiveness, however, will also increase network traffic.

@sergoo-lbt From your logs, I assume that you also have the issue that the on/off state is not updated instantly when turning off the Apple TV. Try (a) the new version, which requests device info update when the play state changes, and (b) to decrease the heartbeat interval.

RaymondMouthaan commented 4 years ago

@lukasroegner, thanks for a new beta. Just updated to this beta (0.8.2) and I'll test it and keep you informed. Good to know about this undocumented heatbeatInterval, i'll leave it as is, but might change it if some weird behaviour occurs.

sergoo-lbt commented 4 years ago

@lukasroegner, I did as you said. Added a heartbeat, 10 seconds. I'll attach the result. I did not wait for the change in the power status. With a pause, everything seems to be working fine! 738E51A1-DE7A-4A3A-97CA-C59A50022B99 8C3744D2-D1C7-4CB6-B2D6-5CE786EC2D39 169D6949-6D8C-42A3-A7C5-994DCE5E261B B5566395-9A8E-4441-B4AA-8673B962D45F

sergoo-lbt commented 4 years ago

@lukasroegner , I still waited. He updated the status himself. But it was really long, 12 minutes... 8600CA53-4FA4-4593-812F-B386C6022C39

sergoo-lbt commented 4 years ago

@lukasroegner , What is the minimum value of "heartbeatInterval"?

sergoo-lbt commented 4 years ago

@lukasroegner , Apple tv has been turned off for 10 minutes. The log is always the same. 8DCF77C8-DF80-48C1-9DCF-E223CBEC7EFF

lukasroegner commented 4 years ago

@sergoo-lbt Even with smaller heartbeat interval, the plugin does not get the Apple TV responses back properly (can be seen from the logs). I would suggest to do a restart of the Apple TV, it seems that there is something wrong.

sergoo-lbt commented 4 years ago

Rebooted apple tv. Not help. Only restarting homebridge temporarily helps.

lukasroegner commented 4 years ago

@sergoo-lbt Have you tried to change the setup (different Homebridge host, Wi-Fi vs. cable) to determine the cause of your issue?

Only restarting homebridge temporarily helps.

Does that mean that the delay is not present when starting up Homebridge and occurs after some time?

sergoo-lbt commented 4 years ago

@sergoo-lbt Have you tried to change the setup (different Homebridge host, Wi-Fi vs. cable) to determine the cause of your issue?

You can't connect the cable. I'll experiment with the host! Thanks!

Only restarting homebridge temporarily helps.

Does that mean that the delay is not present when starting up Homebridge and occurs after some time?

Yes, that's right! The status is still updated, but after a very long period of time.

RaymondMouthaan commented 4 years ago

@lukasroegner, here all seems to work stable now .. as for me you can release the beta :-)

lukasroegner commented 4 years ago

Beta is now merged into master, new version 0.8.3 on NPM.

RaymondMouthaan commented 4 years ago

Beta is now merged into master, new version 0.8.3 on NPM.

Awesome! πŸ‘