Fix mixed case service schema registration (#96448)

This commit is contained in:
J. Nick Koston 2023-07-12 14:39:51 -10:00 committed by GitHub
parent 7009683226
commit 08af42b00e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 1 deletions

View file

@ -1770,7 +1770,7 @@ class ServiceRegistry:
the context. Will return NONE if the service does not exist as there is the context. Will return NONE if the service does not exist as there is
other error handling when calling the service if it does not exist. other error handling when calling the service if it does not exist.
""" """
if not (handler := self._services[domain][service]): if not (handler := self._services[domain.lower()][service.lower()]):
return SupportsResponse.NONE return SupportsResponse.NONE
return handler.supports_response return handler.supports_response

View file

@ -701,6 +701,9 @@ def async_set_service_schema(
hass: HomeAssistant, domain: str, service: str, schema: dict[str, Any] hass: HomeAssistant, domain: str, service: str, schema: dict[str, Any]
) -> None: ) -> None:
"""Register a description for a service.""" """Register a description for a service."""
domain = domain.lower()
service = service.lower()
descriptions_cache: dict[ descriptions_cache: dict[
tuple[str, str], dict[str, Any] | None tuple[str, str], dict[str, Any] | None
] = hass.data.setdefault(SERVICE_DESCRIPTION_CACHE, {}) ] = hass.data.setdefault(SERVICE_DESCRIPTION_CACHE, {})

View file

@ -762,6 +762,27 @@ async def test_async_get_all_descriptions_dynamically_created_services(
} }
async def test_register_with_mixed_case(hass: HomeAssistant) -> None:
"""Test registering a service with mixed case.
For backwards compatibility, we have historically allowed mixed case,
and automatically converted it to lowercase.
"""
logger = hass.components.logger
logger_config = {logger.DOMAIN: {}}
await async_setup_component(hass, logger.DOMAIN, logger_config)
logger_domain_mixed = "LoGgEr"
hass.services.async_register(
logger_domain_mixed, "NeW_SeRVICE", lambda x: None, None
)
service.async_set_service_schema(
hass, logger_domain_mixed, "NeW_SeRVICE", {"description": "new service"}
)
descriptions = await service.async_get_all_descriptions(hass)
assert "description" in descriptions[logger.DOMAIN]["new_service"]
assert descriptions[logger.DOMAIN]["new_service"]["description"] == "new service"
async def test_call_with_required_features(hass: HomeAssistant, mock_entities) -> None: async def test_call_with_required_features(hass: HomeAssistant, mock_entities) -> None:
"""Test service calls invoked only if entity has required features.""" """Test service calls invoked only if entity has required features."""
test_service_mock = AsyncMock(return_value=None) test_service_mock = AsyncMock(return_value=None)