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
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 87 additions and 49 deletions

View file

@ -23,6 +23,7 @@ from homeassistant.core import (
from homeassistant.exceptions import HomeAssistantError, PlatformNotReady
from homeassistant.helpers import (
area_registry as ar,
config_validation as cv,
device_registry as dr,
entity_platform,
entity_registry as er,
@ -1812,31 +1813,32 @@ async def test_register_entity_service_none_schema(
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."""
entity_platform = MockEntityPlatform(
hass, domain="mock_integration", platform_name="mock_platform", platform=None
)
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})),
):
with pytest.raises(
HomeAssistantError,
match="The schema is not an entity service schema",
):
entity_platform.async_register_entity_service("hello", schema, 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})),
)
):
entity_platform.async_register_entity_service(
"hello",
vol.Schema({"some": str}),
Mock(),
f"test_service_{idx}", schema, Mock()
)
# The check currently does not recurse into vol.All or vol.Any allowing these
# non-compliant schemas to pass
entity_platform.async_register_entity_service(
"hello", vol.All(vol.Schema({"some": str})), Mock()
)
entity_platform.async_register_entity_service(
"hello", vol.Any(vol.Schema({"some": str})), Mock()
)
@pytest.mark.parametrize("update_before_add", [True, False])