From 8747d01e7b764ff2ca683b77137a63d686680c4f Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 9 Jan 2023 16:03:29 +0100 Subject: [PATCH] Fix translation keys for Yamaha MusicCast selectors (#85292) Co-authored-by: Martin Hjelmare --- .../components/yamaha_musiccast/const.py | 9 ++++++++ .../components/yamaha_musiccast/select.py | 23 +++++++++++++++++-- .../components/yamaha_musiccast/strings.json | 8 +++---- .../yamaha_musiccast/translations/en.json | 8 +++---- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/yamaha_musiccast/const.py b/homeassistant/components/yamaha_musiccast/const.py index 49234ac38ee..5984d73980f 100644 --- a/homeassistant/components/yamaha_musiccast/const.py +++ b/homeassistant/components/yamaha_musiccast/const.py @@ -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()} diff --git a/homeassistant/components/yamaha_musiccast/select.py b/homeassistant/components/yamaha_musiccast/select.py index a8ca6162c91..200d62bde32 100644 --- a/homeassistant/components/yamaha_musiccast/select.py +++ b/homeassistant/components/yamaha_musiccast/select.py @@ -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 diff --git a/homeassistant/components/yamaha_musiccast/strings.json b/homeassistant/components/yamaha_musiccast/strings.json index 0b3a51989b8..9905a8af74b 100644 --- a/homeassistant/components/yamaha_musiccast/strings.json +++ b/homeassistant/components/yamaha_musiccast/strings.json @@ -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": { diff --git a/homeassistant/components/yamaha_musiccast/translations/en.json b/homeassistant/components/yamaha_musiccast/translations/en.json index f15b986ff8d..5b41f24a24e 100644 --- a/homeassistant/components/yamaha_musiccast/translations/en.json +++ b/homeassistant/components/yamaha_musiccast/translations/en.json @@ -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" } },