88from unittest .mock import ANY , AsyncMock , Mock , patch
99
1010import pytest
11+ from syrupy .assertion import SnapshotAssertion
1112import voluptuous as vol
1213
1314from homeassistant import loader
1415from homeassistant .components .device_automation import toggle_entity
16+ from homeassistant .components .group import DOMAIN as DOMAIN_GROUP
17+ from homeassistant .components .logger import DOMAIN as DOMAIN_LOGGER
1518from homeassistant .components .websocket_api import const
1619from homeassistant .components .websocket_api .auth import (
1720 TYPE_AUTH ,
3437from homeassistant .loader import Integration , async_get_integration
3538from homeassistant .setup import async_set_domains_to_be_loaded , async_setup_component
3639from homeassistant .util .json import json_loads
37- from homeassistant .util .yaml .loader import parse_yaml
40+ from homeassistant .util .yaml .loader import JSON_TYPE , parse_yaml
3841
3942from tests .common import (
4043 MockConfigEntry ,
@@ -671,7 +674,9 @@ async def test_get_states(
671674
672675
673676async def test_get_services (
674- hass : HomeAssistant , websocket_client : MockHAClientWebSocket
677+ hass : HomeAssistant ,
678+ websocket_client : MockHAClientWebSocket ,
679+ snapshot : SnapshotAssertion ,
675680) -> None :
676681 """Test get_services command."""
677682 assert ALL_SERVICE_DESCRIPTIONS_JSON_CACHE not in hass .data
@@ -686,16 +691,18 @@ async def test_get_services(
686691 assert msg == {"id" : 2 , "result" : {}, "success" : True , "type" : "result" }
687692 assert hass .data [ALL_SERVICE_DESCRIPTIONS_JSON_CACHE ] is old_cache
688693
689- # Load a service and check cache is updated
690- assert await async_setup_component (hass , "logger" , {})
694+ # Set up an integration that has services and check cache is updated
695+ assert await async_setup_component (hass , DOMAIN_GROUP , {DOMAIN_GROUP : {} })
691696 await websocket_client .send_json_auto_id ({"type" : "get_services" })
692697 msg = await websocket_client .receive_json ()
693698 assert msg == {
694699 "id" : 3 ,
695- "result" : {"logger" : { "set_default_level" : ANY , "set_level" : ANY } },
700+ "result" : {DOMAIN_GROUP : ANY },
696701 "success" : True ,
697702 "type" : "result" ,
698703 }
704+ group_services = msg ["result" ][DOMAIN_GROUP ]
705+ assert group_services == snapshot
699706 assert hass .data [ALL_SERVICE_DESCRIPTIONS_JSON_CACHE ] is not old_cache
700707
701708 # Check cache is reused
@@ -704,12 +711,70 @@ async def test_get_services(
704711 msg = await websocket_client .receive_json ()
705712 assert msg == {
706713 "id" : 4 ,
707- "result" : {"logger" : { "set_default_level" : ANY , "set_level" : ANY } },
714+ "result" : {DOMAIN_GROUP : group_services },
708715 "success" : True ,
709716 "type" : "result" ,
710717 }
711718 assert hass .data [ALL_SERVICE_DESCRIPTIONS_JSON_CACHE ] is old_cache
712719
720+ # Set up an integration with legacy translations in services.yaml
721+ def _load_services_file (hass : HomeAssistant , integration : Integration ) -> JSON_TYPE :
722+ return {
723+ "set_default_level" : {
724+ "description" : "Translated description" ,
725+ "fields" : {
726+ "level" : {
727+ "description" : "Field description" ,
728+ "example" : "Field example" ,
729+ "name" : "Field name" ,
730+ "selector" : {
731+ "select" : {
732+ "options" : [
733+ "debug" ,
734+ "info" ,
735+ "warning" ,
736+ "error" ,
737+ "fatal" ,
738+ "critical" ,
739+ ],
740+ "translation_key" : "level" ,
741+ }
742+ },
743+ }
744+ },
745+ "name" : "Translated name" ,
746+ },
747+ "set_level" : None ,
748+ }
749+
750+ await async_setup_component (hass , DOMAIN_LOGGER , {DOMAIN_LOGGER : {}})
751+ await hass .async_block_till_done ()
752+
753+ with (
754+ patch (
755+ "homeassistant.helpers.service._load_services_file" ,
756+ side_effect = _load_services_file ,
757+ ),
758+ patch (
759+ "homeassistant.helpers.service.translation.async_get_translations" ,
760+ return_value = {},
761+ ),
762+ ):
763+ await websocket_client .send_json_auto_id ({"type" : "get_services" })
764+ msg = await websocket_client .receive_json ()
765+
766+ assert msg == {
767+ "id" : 5 ,
768+ "result" : {
769+ DOMAIN_LOGGER : ANY ,
770+ DOMAIN_GROUP : group_services ,
771+ },
772+ "success" : True ,
773+ "type" : "result" ,
774+ }
775+ logger_services = msg ["result" ][DOMAIN_LOGGER ]
776+ assert logger_services == snapshot
777+
713778
714779@patch ("annotatedyaml.loader.load_yaml" )
715780@patch .object (Integration , "has_conditions" , return_value = True )
0 commit comments