1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00

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
This commit is contained in:
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

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,12 +165,20 @@ class AFSAPIDevice(MediaPlayerEntity):
}
self._attr_source_list = list(self.__modes_by_label)
if not self._attr_sound_mode_list:
self.__sound_modes_by_label = {
sound_mode.label: sound_mode.key
for sound_mode in await afsapi.get_equalisers()
}
self._attr_sound_mode_list = list(self.__sound_modes_by_label)
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 equalisers
}
self._attr_sound_mode_list = list(self.__sound_modes_by_label)
# 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.
@ -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()
eq_preset = await afsapi.get_eq_preset()
self._attr_sound_mode = eq_preset.label if eq_preset is not None else None
if self._supports_sound_mode:
try:
eq_preset = await afsapi.get_eq_preset()
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()