bendavid / aiopylgtv

Library to control webOS based LG Tv devices
MIT License
145 stars 47 forks source link

Support for audio output selection #3

Closed rcarmo closed 4 years ago

rcarmo commented 4 years ago

Happy Holidays!

I've been looking for a way to control my LG TV audio output (I'm currently using this home bridge plugin, which supports it) and was wondering if you have plans to implement that.

From my cursory digging, the relevant service endpoints are:

ssap://com.webos.service.apiadapter/audio/changeSoundOutput
ssap://com.webos.service.apiadapter/audio/getSoundOutput
starkillerOG commented 4 years ago

I have been looking for a way to change the audio output using an API call for ages. I even contacted LG developer support but they just told me it was impossible (although that was a few years back).

@rcarmo thank you for pointing me in the right direction!

Would be awesom if this could be implemented!

bendavid commented 4 years ago

Yes this is straightforward and I'll add it in the near future.

starkillerOG commented 4 years ago

I just experimented a bit and the code below works for my LG EG920V tv to change the sound output and read it back:

import asyncio
from aiopylgtv import WebOsClient

async def runloop(): 
    await client.connect()

    await client.command('request', 'com.webos.service.apiadapter/audio/changeSoundOutput', {'output': "external_speaker"})
            #"tv_speaker",
            #"external_speaker",
            #"external_optical",
            #"external_arc",
            #"lineout",
            #"headphone",
            #"tv_external_speaker",
            #"tv_speaker_headphone",
            #"bt_soundbar"
    ret = await client.request('com.webos.service.apiadapter/audio/getSoundOutput')
    print(ret)

    await client.disconnect()

client = WebOsClient('192.168.1.???')            
asyncio.get_event_loop().run_until_complete(runloop())

Maybe this can help you implement it @bendavid

bendavid commented 4 years ago

If you want to call the lower level functions manually like that, you should still rather use client.request instead of client.command (request calls command underneath, but checks the response and includes all of the error handling.)

bendavid commented 4 years ago

Also if someone has a 2019 model with earc support, would also be useful to know what the corresponding string is.

bendavid commented 4 years ago

Explicit support for this is added by https://github.com/bendavid/aiopylgtv/commit/29110b81163414693cf8ab05fb9a1d04f2c55761 (including also state tracking)

starkillerOG commented 4 years ago

@bendavid greath, thank you! Are you also going to make a PR to integrate this into HomeAssistant (after a version push)? Otherwise I would be willing to make one.

bendavid commented 4 years ago

Yes I will take care of this together with some other changes.

For the moment most likely as an additional service in the webostv domain (ie no nice drop down box in the gui automatically)

starkillerOG commented 4 years ago

Awesom, thanks! I presume also add a state attribute to the media_player for the sound_output.

bendavid commented 4 years ago

So if you'd like to take a stab at a more general implementation in the media player api, feel free to go ahead.

(I already submitted a PR including the version bump here https://github.com/home-assistant/home-assistant/pull/31042)

starkillerOG commented 4 years ago

I looked at the HA PR and it looks nice:) However I did not see the sound_output added as a state attribute, and also no additional service in the webos domain to switch the sound mode. @bendavid are you still going to include it in that PR, or should I start working on a PR for that?

bendavid commented 4 years ago

Right that's what I'm saying, it's not included in that PR (only the bump to the latest aiopylgtv version), so you can start working on a PR for that, especially if you want to integrate it into the media_player api.

starkillerOG commented 4 years ago

Alright, thanks for the info. Will probably start working on it tonight.

starkillerOG commented 4 years ago

@bendavid I just made a PR to integrate it into HA: https://github.com/home-assistant/home-assistant/pull/31121

jacekpaszkowski commented 4 years ago

i try to use webos.select_sound_output service to select bt_soundbar. It looks like the mode is switched but BT device doesn’t connect. Is it possible to connect to BT device?

bendavid commented 4 years ago

It is not possible to send a command to the TV to trigger BT device connection as far as I know.