Fix frontier silicon EQ Mode not present on all devices (#76200)

* Fix #76159: EQ Mode not present on all devices

* Address review remarks

* Duplicate bookkeeping of sound mode support

* reduce length of try-blocks
pull/77167/head
Thijs W 2022-08-23 09:41:07 +02:00 committed by GitHub
parent be2366d773
commit 680a477009
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 12 deletions

View File

@ -3,7 +3,12 @@ from __future__ import annotations
import logging
from afsapi import AFSAPI, ConnectionError as FSConnectionError, PlayState
from afsapi import (
AFSAPI,
ConnectionError as FSConnectionError,
NotImplementedException as FSNotImplementedException,
PlayState,
)
import voluptuous as vol
from homeassistant.components.media_player import (
@ -113,10 +118,12 @@ class AFSAPIDevice(MediaPlayerEntity):
)
self._attr_name = name
self._max_volume = None
self._max_volume: int | None = None
self.__modes_by_label = None
self.__sound_modes_by_label = None
self.__modes_by_label: dict[str, str] | None = None
self.__sound_modes_by_label: dict[str, str] | None = None
self._supports_sound_mode: bool = True
async def async_update(self):
"""Get the latest date and update device state."""
@ -158,10 +165,18 @@ class AFSAPIDevice(MediaPlayerEntity):
}
self._attr_source_list = list(self.__modes_by_label)
if not self._attr_sound_mode_list:
if not self._attr_sound_mode_list and self._supports_sound_mode:
try:
equalisers = await afsapi.get_equalisers()
except FSNotImplementedException:
self._supports_sound_mode = False
# Remove SELECT_SOUND_MODE from the advertised supported features
self._attr_supported_features ^= (
MediaPlayerEntityFeature.SELECT_SOUND_MODE
)
else:
self.__sound_modes_by_label = {
sound_mode.label: sound_mode.key
for sound_mode in await afsapi.get_equalisers()
sound_mode.label: sound_mode.key for sound_mode in equalisers
}
self._attr_sound_mode_list = list(self.__sound_modes_by_label)
@ -185,8 +200,19 @@ class AFSAPIDevice(MediaPlayerEntity):
self._attr_is_volume_muted = await afsapi.get_mute()
self._attr_media_image_url = await afsapi.get_play_graphic()
if self._supports_sound_mode:
try:
eq_preset = await afsapi.get_eq_preset()
self._attr_sound_mode = eq_preset.label if eq_preset is not None else None
except FSNotImplementedException:
self._supports_sound_mode = False
# Remove SELECT_SOUND_MODE from the advertised supported features
self._attr_supported_features ^= (
MediaPlayerEntityFeature.SELECT_SOUND_MODE
)
else:
self._attr_sound_mode = (
eq_preset.label if eq_preset is not None else None
)
volume = await self.fs_device.get_volume()