Support None schema in EntityPlatform.async_register_entity_service (#123064)

This commit is contained in:
Erik Montnemery 2024-08-14 10:46:29 +02:00 committed by GitHub
parent 5f967fdee2
commit 7063541733
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 4 deletions

View file

@ -1275,7 +1275,7 @@ BASE_ENTITY_SCHEMA = _make_entity_service_schema({}, vol.PREVENT_EXTRA)
def make_entity_service_schema(
schema: dict, *, extra: int = vol.PREVENT_EXTRA
schema: dict | None, *, extra: int = vol.PREVENT_EXTRA
) -> vol.Schema:
"""Create an entity service schema."""
if not schema and extra == vol.PREVENT_EXTRA:
@ -1283,7 +1283,7 @@ def make_entity_service_schema(
# the base schema and avoid compiling a new schema which is the case
# for ~50% of services.
return BASE_ENTITY_SCHEMA
return _make_entity_service_schema(schema, extra)
return _make_entity_service_schema(schema or {}, extra)
SCRIPT_CONVERSATION_RESPONSE_SCHEMA = vol.Any(template, None)

View file

@ -985,7 +985,7 @@ class EntityPlatform:
def async_register_entity_service(
self,
name: str,
schema: VolDictType | VolSchemaType,
schema: VolDictType | VolSchemaType | None,
func: str | Callable[..., Any],
required_features: Iterable[int] | None = None,
supports_response: SupportsResponse = SupportsResponse.NONE,
@ -997,7 +997,7 @@ class EntityPlatform:
if self.hass.services.has_service(self.platform_name, name):
return
if isinstance(schema, dict):
if schema is None or isinstance(schema, dict):
schema = cv.make_entity_service_schema(schema)
service_func: str | HassJob[..., Any]

View file

@ -1760,6 +1760,34 @@ async def test_register_entity_service_limited_to_matching_platforms(
}
async def test_register_entity_service_none_schema(
hass: HomeAssistant,
) -> None:
"""Test registering a service with schema set to None."""
entity_platform = MockEntityPlatform(
hass, domain="mock_integration", platform_name="mock_platform", platform=None
)
entity1 = SlowEntity(name="entity_1")
entity2 = SlowEntity(name="entity_1")
await entity_platform.async_add_entities([entity1, entity2])
entities = []
@callback
def handle_service(entity, *_):
entities.append(entity)
entity_platform.async_register_entity_service("hello", None, handle_service)
await hass.services.async_call(
"mock_platform", "hello", {"entity_id": "all"}, blocking=True
)
assert len(entities) == 2
assert entity1 in entities
assert entity2 in entities
@pytest.mark.parametrize("update_before_add", [True, False])
async def test_invalid_entity_id(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, update_before_add: bool