merdok / homebridge-webos-tv

Homebridge plugin for LG webOS TVs
MIT License
656 stars 88 forks source link

Question: Possible to retrieve input 'name' as seen by the TV? #515

Closed DonutEspresso closed 8 months ago

DonutEspresso commented 9 months ago

Hello - this is more of a feature/capability question. First off - much thanks for your help and support in building and maintaining this plugin.

I previously had devices directly wired to the LG TV HDMI inputs; this made it super easy to do automations based on what device was being used (or "activity", in Harmony parlance). LG TV lets you 'name' the inputs; I'm guessing there is some sort of HDMI fingerprint it remembers and displays that name of the device on the screen when you cycle through HDMI inputs.

I recently added an AV Receiver to my setup and now have all my devices wired through the AVR. This makes automating based on the HDMI input port no longer possible. However, interestingly, the LG TV seems to be able to "see" the fingerprint of the device still; maybe the AVR is passing it through somehow. Depending on what signal I have my AVR displaying, the TV can see that I have a "PS4" or an "Apple TV" device on, which are the names I gave these devices when they were directly connected to the TV. I'm wondering if there is a way I can query for the value of this "name" in a Homekit shortcut/automation? This would allow me to do X or Y based on the current device in use w/ the TV.

merdok commented 9 months ago

Would you mind providing me an example as which name exactly you mean? You could enable the deepDebugLogand start homebridge in debug mode, that way you also should be able the inputs retrieved from the TV with all the information., then you could just paste a sniped from an input here which has the property which you seek for.

DonutEspresso commented 9 months ago

OK, looks like this may be related to HDMI CEC (SIMPLINK) capabilities. Here's the retrieved service list from the TV:

[1/2/2024, 5:18:43 PM] [homebridge-webos-tv] [LG CX 77] Retrieved service list [1/2/2024, 5:18:43 PM] [homebridge-webos-tv] [LG CX 77] Message from TV [1/2/2024, 5:18:43 PM] [homebridge-webos-tv] [LG CX 77] { "returnValue": true, "devices": [ ... { "id": "HDMI_2", "label": "Apple TV 4K", "port": 2, "connected": true, "appId": "com.webos.app.hdmi2", "icon": "", "forceIcon": false, "modified": false, "spdProductDescription": "AVR-X4800H", "spdVendorName": "DENON", "spdSourceDeviceInfo": "Unknown", "lastUniqueId": 8, "hdmiPlugIn": true, "subList": [ { "id": "SIMPLINK", "portId": 2, "uniqueId": 4, "vendorId": 4346, "physicalAddress": 8704, "depth": 2, "osdName": "Apple TV zzz", "cecpDevType": 4, "cecpNewType": 9 }, { "id": "SIMPLINK", "portId": 2, "uniqueId": 5, "vendorId": 1485, "physicalAddress": 8192, "depth": 1, "osdName": "AVR-X4800H", "cecpDevType": 5, "cecpNewType": 9 }, { "id": "SIMPLINK", "portId": 2, "uniqueId": 8, "vendorId": 2495, "physicalAddress": 9472, "depth": 2, "osdName": "NintendoSwitch", "cecpDevType": 4, "cecpNewType": 9 } ], "subCount": 3, "favorite": false } ...

A couple of notes on this:

When I was switching between the two live HDMI2 sources using the TV's UI, nothing shows up in the logs, just picture setting changes:

[1/2/2024, 5:28:17 PM] [homebridge-webos-tv] [LG CX 77] Message from TV [1/2/2024, 5:28:17 PM] [homebridge-webos-tv] [LG CX 77] { type: 'utf8', utf8Data: '{"type":"response","id":"f0816f76000d","payload":{"category":"picture","settings":{"brightness":"50","contrast":"85","backlight":"80","color":"50"},"returnValue":true}}' } [1/2/2024, 5:28:17 PM] [homebridge-webos-tv] [LG CX 77] { "category": "picture", "settings": { "brightness": "50", "contrast": "85", "backlight": "80", "color": "50" }, "returnValue": true } [1/2/2024, 5:28:17 PM] [homebridge-webos-tv] [LG CX 77] Picture settings changed. Current picture settings: {"brightness":50,"backlight":80,"contrast":85,"color":50}

The TV itself does show the osdName value on the screen when switching between the two possible HDMI2 inputs - I think this would be the value I'm looking for, if at all possible. Let me know if there's anything else that might be helpful here.

merdok commented 9 months ago

So are you saying that when you turn off the device itself then it will disappear from the sub-list of the HDMI input? That is quite problematic as i would probably need to poll the whole input list on interval just to check if something changed and the input list is quite huge... Also it would make your automations trigger with a delay. I am not sure if the TV sends events when the sub-lists change, and i am not able to verify or check that as i do not use that feature.

You would need to do some investigation and try to subscribe to specific events from the tv and check what comes back. Maybe even the correct events already arrive now? Do any logs appear when you turn off the external device? Other then that, not sure how familiar you are with code, but you would need to do some manual adjustments and check if the TV responds with anything meaningful. A wild guess would be in LgTvController.js to subscribe to the WEBOS_URI_EXTERNAL_INPUT_LIST url and check if that gives any result when the external device turn on/off.

DonutEspresso commented 9 months ago

So are you saying that when you turn off the device itself then it will disappear from the sub-list of the HDMI input?

For the first question, yes, that appears to be the case.

That is quite problematic as i would probably need to poll the whole input list on interval

I don't think you need to know when a device turns on/off, but rather when a new device in the subList of a given port becomes the active source for the TV. Ideally we could capture this event like any other appId or port change, but the TV didn't appear to broadcast any information - at least not that I could see in the logs, likely because it's the same HDMI port. So I'm not sure if this is a TV limitation or something that could be added to the current plugin. I haven't had a chance to dig through the code yet but happy to do so - any pointers on where I can hook into the input source change event?

merdok commented 9 months ago

There is a section for event subscription, you would need to similarly as the subscription already there, try to subscribe to the WEBOS_URI_EXTERNAL_INPUT_LIST events. But as i mentioned, this particular event is only a guess, it also might be another event, so you would have to try them out. An example would be https://github.com/merdok/homebridge-webos-tv/blob/ea245f1bfd35c2f00a486881c86b4a7d47e6e354/lib/LgTvController.js#L418

DonutEspresso commented 9 months ago

The only event that appears to trigger when swapping between sources on the same HDMI port is the picture setting event - so it doesn't look like the info we currently have as-is will be sufficient. I think the only possible way to do this would be via the CEC service's activeSource and osd fields - would probably have to create an internal pointer to track the active device, and update this field on every app launch or picture setting change event. However, when I tried hitting the CEC service using the controller's existing patterns, I get a 404.

[1/3/2024, 6:30:21 PM] [homebridge-webos-tv] [LG CX 77] com.webos.service.cec service - listAdapters requested. [1/3/2024, 6:30:21 PM] [homebridge-webos-tv] [LG CX 77] Message from TV

[1/3/2024, 6:30:21 PM] [homebridge-webos-tv] [LG CX 77] { type: 'utf8', utf8Data: '{"type":"error","id":"475970cb0007","error":"404 no such service or method","payload":{}}' } [1/3/2024, 6:30:21 PM] [homebridge-webos-tv] [LG CX 77] com.webos.service.cec service - listAdapters response [1/3/2024, 6:30:21 PM] [homebridge-webos-tv] [LG CX 77] {} // < trying to log the res [1/3/2024, 6:30:21 PM] [homebridge-webos-tv] [LG CX 77] Unknown error while requesting listAdapters from service com.webos.service.cec

Any context you can share on the services available via WS? I'm not sure where/how I might be able to discover what other services are available, but given that the osd field appears to be a CEC specific thing, this might be end of the road if CEC service isn't accessible.

merdok commented 9 months ago

All the services available are listed on the top of the controller file. I am not aware of any CEC service, did you try to guess that one? You would need to use one of the existing ones, also tvRequestwill basically not help out here as for that polling would need to be used, you should be looking at tvSubscribe

DonutEspresso commented 9 months ago

I found the CEC service via the webOS OSE documentation - I assume that is the full documentation and API for application developers building software on the TV, but likely only a subset of those APIs is exposed via WS. Interestingly not all of the WS services you use are in the OSE documentation so maybe it's a different cross section of APIs?

I tried to hit the name of the CEC service com.webos.service.cec, but no luck. My thinking was we could grab the active CEC device at init, save it, then make updates to it whenever an app launch or picture event change happened. But if the CEC service isn't available via WS we're out of luck. Unfortunately none of the existing services return any information about the currently active source device, and the current foreground app isn't sufficient as that only returns the HDMI port, not any of the CEC information, which is what is responsible for the osdName value.

merdok commented 9 months ago

This is different, those are services on the so called "luna" bus which can be used internally by app developers. The WS API which allows to communicate with the TV from outside exposes different services, some might be the same but are more limited. You have to work with what is already available (and documented) in the controller...

DonutEspresso commented 8 months ago

Dug a bit more, but it doesn't appear the HDMI CEC details are exposed via the WS APIs. I ended up taking a different path here and was able to get my receiver exposed through homebridge to achieve the same outcomes. I'll close out the issue for now. Appreciate your patience and guidance!