Fix translation keys for Yamaha MusicCast selectors (#85292)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Franck Nijhof 2023-01-09 16:03:29 +01:00 committed by GitHub
parent 1766df3faa
commit 8747d01e7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 10 deletions

View File

@ -55,3 +55,12 @@ TRANSLATION_KEY_MAPPING = {
"zone_LINK_CONTROL": "zone_link_control",
"zone_LINK_AUDIO_DELAY": "zone_link_audio_delay",
}
ZONE_SLEEP_STATE_MAPPING = {
"off": "off",
"30 min": "30_min",
"60 min": "60_min",
"90 min": "90_min",
"120 min": "120_min",
}
STATE_ZONE_SLEEP_MAPPING = {val: key for key, val in ZONE_SLEEP_STATE_MAPPING.items()}

View File

@ -9,7 +9,11 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DOMAIN, MusicCastCapabilityEntity, MusicCastDataUpdateCoordinator
from .const import TRANSLATION_KEY_MAPPING
from .const import (
STATE_ZONE_SLEEP_MAPPING,
TRANSLATION_KEY_MAPPING,
ZONE_SLEEP_STATE_MAPPING,
)
async def async_setup_entry(
@ -44,6 +48,10 @@ class SelectableCapapility(MusicCastCapabilityEntity, SelectEntity):
async def async_select_option(self, option: str) -> None:
"""Select the given option."""
value = {val: key for key, val in self.capability.options.items()}[option]
# If the translation key is "zone_sleep", we need to translate
# Home Assistant state back to the MusicCast value
if self.translation_key == "zone_sleep":
value = STATE_ZONE_SLEEP_MAPPING[value]
await self.capability.set(value)
@property
@ -54,9 +62,20 @@ class SelectableCapapility(MusicCastCapabilityEntity, SelectEntity):
@property
def options(self) -> list[str]:
"""Return the list possible options."""
# If the translation key is "zone_sleep", we need to translate
# the options to make them compatible with Home Assistant
if self.translation_key == "zone_sleep":
return list(STATE_ZONE_SLEEP_MAPPING)
return list(self.capability.options.values())
@property
def current_option(self) -> str | None:
"""Return the currently selected option."""
return self.capability.options.get(self.capability.current)
# If the translation key is "zone_sleep", we need to translate
# the value to make it compatible with Home Assistant
if (
value := self.capability.current
) is not None and self.translation_key == "zone_sleep":
return ZONE_SLEEP_STATE_MAPPING[value]
return value

View File

@ -30,10 +30,10 @@
"zone_sleep": {
"state": {
"off": "Off",
"30 min": "30 Minutes",
"60 min": "60 Minutes",
"90 min": "90 Minutes",
"120 min": "120 Minutes"
"30_min": "30 Minutes",
"60_min": "60 Minutes",
"90_min": "90 Minutes",
"120_min": "120 Minutes"
}
},
"zone_tone_control_mode": {

View File

@ -58,10 +58,10 @@
},
"zone_sleep": {
"state": {
"120 min": "120 Minutes",
"30 min": "30 Minutes",
"60 min": "60 Minutes",
"90 min": "90 Minutes",
"120_min": "120 Minutes",
"30_min": "30 Minutes",
"60_min": "60 Minutes",
"90_min": "90 Minutes",
"off": "Off"
}
},