From 72417cb6474e58fd54bacaefc4980f0c0840fd64 Mon Sep 17 00:00:00 2001 From: tronikos Date: Sun, 8 Jan 2023 09:22:22 +0000 Subject: [PATCH 1/2] Google Assistant SDK: support Korean and Japanese --- .../components/google_assistant_sdk/const.py | 2 + .../google_assistant_sdk/helpers.py | 2 + .../components/google_assistant_sdk/notify.py | 18 ++-- .../google_assistant_sdk/test_notify.py | 86 ++++++++++++------- 4 files changed, 68 insertions(+), 40 deletions(-) diff --git a/homeassistant/components/google_assistant_sdk/const.py b/homeassistant/components/google_assistant_sdk/const.py index acd9a40534337e..8458145caace9d 100644 --- a/homeassistant/components/google_assistant_sdk/const.py +++ b/homeassistant/components/google_assistant_sdk/const.py @@ -20,5 +20,7 @@ "fr-CA", "fr-FR", "it-IT", + "ja-JP", + "ko-KR", "pt-BR", ] diff --git a/homeassistant/components/google_assistant_sdk/helpers.py b/homeassistant/components/google_assistant_sdk/helpers.py index f91f8f4241f086..f78643d8f8d356 100644 --- a/homeassistant/components/google_assistant_sdk/helpers.py +++ b/homeassistant/components/google_assistant_sdk/helpers.py @@ -18,6 +18,8 @@ "es": "es-ES", "fr": "fr-FR", "it": "it-IT", + "ja": "ja-JP", + "ko": "ko-KR", "pt": "pt-BR", } diff --git a/homeassistant/components/google_assistant_sdk/notify.py b/homeassistant/components/google_assistant_sdk/notify.py index 3872a1df2a39b6..a12d917c97ce0f 100644 --- a/homeassistant/components/google_assistant_sdk/notify.py +++ b/homeassistant/components/google_assistant_sdk/notify.py @@ -13,12 +13,14 @@ # https://support.google.com/assistant/answer/9071582?hl=en LANG_TO_BROADCAST_COMMAND = { - "en": ("broadcast", "broadcast to"), - "de": ("Nachricht an alle", "Nachricht an alle an"), - "es": ("Anuncia", "Anuncia en"), - "fr": ("Diffuse", "Diffuse dans"), - "it": ("Trasmetti", "Trasmetti in"), - "pt": ("Transmite", "Transmite para"), + "en": ("broadcast {0}", "broadcast to {1} {0}"), + "de": ("Nachricht an alle {0}", "Nachricht an alle an {1} {0}"), + "es": ("Anuncia {0}", "Anuncia en {1} {0}"), + "fr": ("Diffuse {0}", "Diffuse dans {1} {0}"), + "it": ("Trasmetti {0}", "Trasmetti in {1} {0}"), + "ja": ("{0}、とブロードキャストして", "{1}に、{0}、とブロードキャストして"), + "ko": ("{0} 방송", "{1}에 방송해 줘, {0}"), + "pt": ("Transmite {0}", "Transmite para {1} {0}"), } @@ -62,10 +64,10 @@ async def async_send_message(self, message: str = "", **kwargs: Any) -> None: commands = [] targets = kwargs.get(ATTR_TARGET) if not targets: - commands.append(f"{broadcast_commands(language_code)[0]} {message}") + commands.append(broadcast_commands(language_code)[0].format(message)) else: for target in targets: commands.append( - f"{broadcast_commands(language_code)[1]} {target} {message}" + broadcast_commands(language_code)[1].format(message, target) ) await async_send_text_commands(commands, self.hass) diff --git a/tests/components/google_assistant_sdk/test_notify.py b/tests/components/google_assistant_sdk/test_notify.py index 5dbaa3aa79b939..7bff15978c04bc 100644 --- a/tests/components/google_assistant_sdk/test_notify.py +++ b/tests/components/google_assistant_sdk/test_notify.py @@ -1,6 +1,8 @@ """Tests for the Google Assistant notify.""" from unittest.mock import call, patch +import pytest + from homeassistant.components import notify from homeassistant.components.google_assistant_sdk import DOMAIN from homeassistant.components.google_assistant_sdk.const import SUPPORTED_LANGUAGE_CODES @@ -10,14 +12,29 @@ from .conftest import ComponentSetup, ExpectedCredentials +@pytest.mark.parametrize( + "language_code,message,expected_command", + [ + ("en-US", "Dinner is served", "broadcast Dinner is served"), + ("es-ES", "La cena está en la mesa", "Anuncia La cena está en la mesa"), + ("ko-KR", "저녁 식사가 준비됐어요", "저녁 식사가 준비됐어요 방송"), + ("ja-JP", "晩ご飯できたよ", "晩ご飯できたよ、とブロードキャストして"), + ], + ids=["english", "spanish", "korean", "japanese"], +) async def test_broadcast_no_targets( - hass: HomeAssistant, setup_integration: ComponentSetup + hass: HomeAssistant, + setup_integration: ComponentSetup, + language_code: str, + message: str, + expected_command: str, ) -> None: """Test broadcast to all.""" await setup_integration() - message = "time for dinner" - expected_command = "broadcast time for dinner" + entry = hass.config_entries.async_entries(DOMAIN)[0] + entry.options = {"language_code": language_code} + with patch( "homeassistant.components.google_assistant_sdk.helpers.TextAssistant" ) as mock_text_assistant: @@ -27,19 +44,44 @@ async def test_broadcast_no_targets( {notify.ATTR_MESSAGE: message}, ) await hass.async_block_till_done() - mock_text_assistant.assert_called_once_with(ExpectedCredentials(), "en-US") + mock_text_assistant.assert_called_once_with(ExpectedCredentials(), language_code) mock_text_assistant.assert_has_calls([call().__enter__().assist(expected_command)]) +@pytest.mark.parametrize( + "language_code,message,target,expected_command", + [ + ( + "en-US", + "it's time for homework", + "living room", + "broadcast to living room it's time for homework", + ), + ( + "es-ES", + "Es hora de hacer los deberes", + "el salón", + "Anuncia en el salón Es hora de hacer los deberes", + ), + ("ko-KR", "숙제할 시간이야", "거실", "거실에 방송해 줘, 숙제할 시간이야"), + ("ja-JP", "宿題の時間だよ", "リビングルーム", "リビングルームに、宿題の時間だよ、とブロードキャストして"), + ], + ids=["english", "spanish", "korean", "japanese"], +) async def test_broadcast_one_target( - hass: HomeAssistant, setup_integration: ComponentSetup + hass: HomeAssistant, + setup_integration: ComponentSetup, + language_code: str, + message: str, + target: str, + expected_command: str, ) -> None: """Test broadcast to one target.""" await setup_integration() - message = "time for dinner" - target = "basement" - expected_command = "broadcast to basement time for dinner" + entry = hass.config_entries.async_entries(DOMAIN)[0] + entry.options = {"language_code": language_code} + with patch( "homeassistant.components.google_assistant_sdk.helpers.TextAssistant.assist" ) as mock_assist_call: @@ -95,30 +137,6 @@ async def test_broadcast_empty_message( mock_assist_call.assert_not_called() -async def test_broadcast_spanish( - hass: HomeAssistant, setup_integration: ComponentSetup -) -> None: - """Test broadcast in Spanish.""" - await setup_integration() - - entry = hass.config_entries.async_entries(DOMAIN)[0] - entry.options = {"language_code": "es-ES"} - - message = "comida" - expected_command = "Anuncia comida" - with patch( - "homeassistant.components.google_assistant_sdk.helpers.TextAssistant" - ) as mock_text_assistant: - await hass.services.async_call( - notify.DOMAIN, - DOMAIN, - {notify.ATTR_MESSAGE: message}, - ) - await hass.async_block_till_done() - mock_text_assistant.assert_called_once_with(ExpectedCredentials(), "es-ES") - mock_text_assistant.assert_has_calls([call().__enter__().assist(expected_command)]) - - def test_broadcast_language_mapping( hass: HomeAssistant, setup_integration: ComponentSetup ) -> None: @@ -128,4 +146,8 @@ def test_broadcast_language_mapping( assert cmds assert len(cmds) == 2 assert cmds[0] + assert "{0}" in cmds[0] + assert "{1}" not in cmds[0] assert cmds[1] + assert "{0}" in cmds[1] + assert "{1}" in cmds[1] From 22f67edfa012bb9f856a9831621c4060cefa03a0 Mon Sep 17 00:00:00 2001 From: tronikos Date: Sun, 8 Jan 2023 11:44:14 +0000 Subject: [PATCH 2/2] Fix Korean and Japanese broadcast commands --- homeassistant/components/google_assistant_sdk/notify.py | 4 ++-- tests/components/google_assistant_sdk/test_notify.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/google_assistant_sdk/notify.py b/homeassistant/components/google_assistant_sdk/notify.py index a12d917c97ce0f..245ea935d461b5 100644 --- a/homeassistant/components/google_assistant_sdk/notify.py +++ b/homeassistant/components/google_assistant_sdk/notify.py @@ -18,8 +18,8 @@ "es": ("Anuncia {0}", "Anuncia en {1} {0}"), "fr": ("Diffuse {0}", "Diffuse dans {1} {0}"), "it": ("Trasmetti {0}", "Trasmetti in {1} {0}"), - "ja": ("{0}、とブロードキャストして", "{1}に、{0}、とブロードキャストして"), - "ko": ("{0} 방송", "{1}에 방송해 줘, {0}"), + "ja": ("{0}とほうそうして", "{0}と{1}にブロードキャストして"), + "ko": ("{0} 라고 방송해 줘", "{0} 라고 {1}에 방송해 줘"), "pt": ("Transmite {0}", "Transmite para {1} {0}"), } diff --git a/tests/components/google_assistant_sdk/test_notify.py b/tests/components/google_assistant_sdk/test_notify.py index 7bff15978c04bc..949cd228f7770f 100644 --- a/tests/components/google_assistant_sdk/test_notify.py +++ b/tests/components/google_assistant_sdk/test_notify.py @@ -17,8 +17,8 @@ [ ("en-US", "Dinner is served", "broadcast Dinner is served"), ("es-ES", "La cena está en la mesa", "Anuncia La cena está en la mesa"), - ("ko-KR", "저녁 식사가 준비됐어요", "저녁 식사가 준비됐어요 방송"), - ("ja-JP", "晩ご飯できたよ", "晩ご飯できたよ、とブロードキャストして"), + ("ko-KR", "저녁 식사가 준비됐어요", "저녁 식사가 준비됐어요 라고 방송해 줘"), + ("ja-JP", "晩ご飯できたよ", "晩ご飯できたよとほうそうして"), ], ids=["english", "spanish", "korean", "japanese"], ) @@ -63,8 +63,8 @@ async def test_broadcast_no_targets( "el salón", "Anuncia en el salón Es hora de hacer los deberes", ), - ("ko-KR", "숙제할 시간이야", "거실", "거실에 방송해 줘, 숙제할 시간이야"), - ("ja-JP", "宿題の時間だよ", "リビングルーム", "リビングルームに、宿題の時間だよ、とブロードキャストして"), + ("ko-KR", "숙제할 시간이야", "거실", "숙제할 시간이야 라고 거실에 방송해 줘"), + ("ja-JP", "宿題の時間だよ", "リビング", "宿題の時間だよとリビングにブロードキャストして"), ], ids=["english", "spanish", "korean", "japanese"], )