mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Support None schema in EntityPlatform.async_register_entity_service (#123064)
This commit is contained in:
parent
5f967fdee2
commit
7063541733
@ -1275,7 +1275,7 @@ BASE_ENTITY_SCHEMA = _make_entity_service_schema({}, vol.PREVENT_EXTRA)
|
|||||||
|
|
||||||
|
|
||||||
def make_entity_service_schema(
|
def make_entity_service_schema(
|
||||||
schema: dict, *, extra: int = vol.PREVENT_EXTRA
|
schema: dict | None, *, extra: int = vol.PREVENT_EXTRA
|
||||||
) -> vol.Schema:
|
) -> vol.Schema:
|
||||||
"""Create an entity service schema."""
|
"""Create an entity service schema."""
|
||||||
if not schema and extra == vol.PREVENT_EXTRA:
|
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
|
# the base schema and avoid compiling a new schema which is the case
|
||||||
# for ~50% of services.
|
# for ~50% of services.
|
||||||
return BASE_ENTITY_SCHEMA
|
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)
|
SCRIPT_CONVERSATION_RESPONSE_SCHEMA = vol.Any(template, None)
|
||||||
|
@ -985,7 +985,7 @@ class EntityPlatform:
|
|||||||
def async_register_entity_service(
|
def async_register_entity_service(
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
schema: VolDictType | VolSchemaType,
|
schema: VolDictType | VolSchemaType | None,
|
||||||
func: str | Callable[..., Any],
|
func: str | Callable[..., Any],
|
||||||
required_features: Iterable[int] | None = None,
|
required_features: Iterable[int] | None = None,
|
||||||
supports_response: SupportsResponse = SupportsResponse.NONE,
|
supports_response: SupportsResponse = SupportsResponse.NONE,
|
||||||
@ -997,7 +997,7 @@ class EntityPlatform:
|
|||||||
if self.hass.services.has_service(self.platform_name, name):
|
if self.hass.services.has_service(self.platform_name, name):
|
||||||
return
|
return
|
||||||
|
|
||||||
if isinstance(schema, dict):
|
if schema is None or isinstance(schema, dict):
|
||||||
schema = cv.make_entity_service_schema(schema)
|
schema = cv.make_entity_service_schema(schema)
|
||||||
|
|
||||||
service_func: str | HassJob[..., Any]
|
service_func: str | HassJob[..., Any]
|
||||||
|
@ -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])
|
@pytest.mark.parametrize("update_before_add", [True, False])
|
||||||
async def test_invalid_entity_id(
|
async def test_invalid_entity_id(
|
||||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, update_before_add: bool
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, update_before_add: bool
|
||||||
|
Loading…
x
Reference in New Issue
Block a user