Improve validation of entity service schemas (#124102)

* Improve validation of entity service schemas

* Update tests/helpers/test_entity_platform.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Erik Montnemery
2024-08-27 19:05:49 +02:00
committed by GitHub
parent 0dc1eb8757
commit 55c42fde88
5 changed files with 87 additions and 49 deletions

View File

@@ -23,7 +23,7 @@ from homeassistant.core import (
callback,
)
from homeassistant.exceptions import HomeAssistantError, PlatformNotReady
from homeassistant.helpers import discovery
from homeassistant.helpers import config_validation as cv, discovery
from homeassistant.helpers.entity_component import EntityComponent, async_update_entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@@ -559,28 +559,28 @@ async def test_register_entity_service(
async def test_register_entity_service_non_entity_service_schema(
hass: HomeAssistant,
) -> None:
"""Test attempting to register a service with an incomplete schema."""
"""Test attempting to register a service with a non entity service schema."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
with pytest.raises(
HomeAssistantError,
match=(
"The schema does not include all required keys: entity_id, device_id, area_id, "
"floor_id, label_id"
),
for schema in (
vol.Schema({"some": str}),
vol.All(vol.Schema({"some": str})),
vol.Any(vol.Schema({"some": str})),
):
component.async_register_entity_service(
"hello", vol.Schema({"some": str}), Mock()
)
with pytest.raises(
HomeAssistantError,
match=("The schema is not an entity service schema"),
):
component.async_register_entity_service("hello", schema, Mock())
# The check currently does not recurse into vol.All or vol.Any allowing these
# non-compliant schemas to pass
component.async_register_entity_service(
"hello", vol.All(vol.Schema({"some": str})), Mock()
)
component.async_register_entity_service(
"hello", vol.Any(vol.Schema({"some": str})), Mock()
)
for idx, schema in enumerate(
(
cv.make_entity_service_schema({"some": str}),
vol.Schema(cv.make_entity_service_schema({"some": str})),
vol.All(cv.make_entity_service_schema({"some": str})),
)
):
component.async_register_entity_service(f"test_service_{idx}", schema, Mock())
async def test_register_entity_service_response_data(hass: HomeAssistant) -> None: