dcnielsen90 / python-bravia-tv

MIT License
13 stars 6 forks source link

NaN volume when headphones connected #17

Closed Drafteed closed 3 years ago

Drafteed commented 3 years ago

If headphones connected to TV python-bravia-tv returns {}.

Overview - Home Assistant 2021-04-24 05-25-27

From API received:

{
    "result": [
        [
            {
                "target": "headphone",
                "volume": 7,
                "mute": false,
                "maxVolume": 100,
                "minVolume": 0
            }
        ]
    ],
    "id": 1
}

Suggest to retrieve volume level for any source, by default. https://github.com/dcnielsen90/python-bravia-tv/blob/2fd43d41566fa77e3c0d07a03edd63298b042562/bravia_tv/braviarc.py#L216-L226

dcnielsen90 commented 3 years ago

The intention was to make it a configuration item in Home Assistant.

I'm not opposed to the behavior you suggested -- my question is though, how would you want to handle the case where the api returns multiple? (ie speaker and headphone) I didn't test it extensively, but I plugged in a pair of headphones a couple of times and it returned both in what appeared to be an arbitrary order, both with valid values. I imagine it may vary depending on the state of the TV when I plug in audio outputs.

Drafteed commented 3 years ago

@dcnielsen90 How did you get the API to return multiple? I always get one thing back. I see in reference example with multiple, but can't reproduce that.

Anyway, my suggest: if that return multiple then use speaker field else use any.

def get_volume_info(self, audio_output='speaker'): 
    """Get volume info for specified Output.""" 
    return_value = None 
    jdata = self._jdata_build('getVolumeInformation') 
    resp = self.bravia_req_json('audio', jdata) 
    for output in resp.get('result', [{}])[0]:
        return_value = output
        if output.get('target') == audio_output: 
            break
    return return_value 
dcnielsen90 commented 3 years ago

Doing some more testing.... but I imagine something like this would work:

    def get_volume_info(self, audio_output=None):
        """
           Get volume info for specified Output. 
           If not specified -- returns speakers if found else returns last output found
           If no outputs are found, return empty dict
        """

        jdata = self._jdata_build('getVolumeInformation')
        resp = self.bravia_req_json('audio', jdata)

        return_value = {}
        for output in resp.get('result', [{}])[0]:
            return_value = output
            if audio_output is None:
                if "speaker" in output.get('target'):
                    break
                else:
                    continue
            if audio_output in output.get('target'):
                break

        return return_value
Drafteed commented 3 years ago

@dcnielsen90 Nice! Can you bump new version in HA Core pls?)

dcnielsen90 commented 3 years ago

I haven't put this off. I just wanted to think through how the sets should work. After thinking about it, I don't think I want to change the sets. We'll be able to fix that portion in home assistant. I'm working on adding it. My free time has been limited recently

dcnielsen90 commented 3 years ago

This should be fixed with: https://github.com/dcnielsen90/python-bravia-tv/commit/3c29714d2c6ad47ee06ede3ebc059b0365353ada I have to implement a small change in home assistant as well. I plan to push a release this week.