Fix volume control for Frontier Silicon media players (#32040)

* Fixed volume control for Frontier Silicon media players.

* Removed unnecessary else which caused pylint test to fail.

* Removed whitespace on empty line.

* Tweaks from on springstan's suggestions and other fixes

* Apply suggestions from code review

Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com>

* Comment to explain why 1 is subtracted from volume_Steps

Also reformatted using black after code review changes.

* Split up the set volume functions into steps rather than all in-line as suggested.

* Renamed _volume_steps to _max_volume.

* Prevent asnyc_update from failing if we can't get the volume steps

Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
This commit is contained in:
jezcooke 2020-02-23 16:37:31 +00:00 committed by GitHub
parent 458e47f981
commit a533b7a746
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -112,6 +112,8 @@ class AFSAPIDevice(MediaPlayerDevice):
self._source = None
self._source_list = None
self._media_image_url = None
self._max_volume = None
self._volume_level = None
# Properties
@property
@ -181,6 +183,11 @@ class AFSAPIDevice(MediaPlayerDevice):
"""Image url of current playing media."""
return self._media_image_url
@property
def volume_level(self):
"""Volume level of the media player (0..1)."""
return self._volume_level
async def async_update(self):
"""Get the latest date and update device state."""
fs_device = self.fs_device
@ -191,6 +198,12 @@ class AFSAPIDevice(MediaPlayerDevice):
if not self._source_list:
self._source_list = await fs_device.get_mode_list()
# The API seems to include 'zero' in the number of steps (e.g. if the range is
# 0-40 then get_volume_steps returns 41) subtract one to get the max volume.
# If call to get_volume fails set to 0 and try again next time.
if not self._max_volume:
self._max_volume = int(await fs_device.get_volume_steps() or 1) - 1
if await fs_device.get_power():
status = await fs_device.get_play_status()
self._state = {
@ -214,6 +227,11 @@ class AFSAPIDevice(MediaPlayerDevice):
self._source = await fs_device.get_mode()
self._mute = await fs_device.get_mute()
self._media_image_url = await fs_device.get_play_graphic()
volume = await self.fs_device.get_volume()
# Prevent division by zero if max_volume not known yet
self._volume_level = float(volume or 0) / (self._max_volume or 1)
else:
self._title = None
self._artist = None
@ -223,6 +241,8 @@ class AFSAPIDevice(MediaPlayerDevice):
self._mute = None
self._media_image_url = None
self._volume_level = None
# Management actions
# power control
async def async_turn_on(self):
@ -274,16 +294,20 @@ class AFSAPIDevice(MediaPlayerDevice):
async def async_volume_up(self):
"""Send volume up command."""
volume = await self.fs_device.get_volume()
await self.fs_device.set_volume(volume + 1)
volume = int(volume or 0) + 1
await self.fs_device.set_volume(min(volume, self._max_volume))
async def async_volume_down(self):
"""Send volume down command."""
volume = await self.fs_device.get_volume()
await self.fs_device.set_volume(volume - 1)
volume = int(volume or 0) - 1
await self.fs_device.set_volume(max(volume, 0))
async def async_set_volume_level(self, volume):
"""Set volume command."""
await self.fs_device.set_volume(int(volume * 20))
if self._max_volume: # Can't do anything sensible if not set
volume = int(volume * self._max_volume)
await self.fs_device.set_volume(volume)
async def async_select_source(self, source):
"""Select input source."""