Support None schema in EntityComponent.async_register_entity_service (#123867)
This commit is contained in:
parent
7063541733
commit
e1a0a855d5
2 changed files with 22 additions and 12 deletions
|
@ -258,13 +258,13 @@ class EntityComponent(Generic[_EntityT]):
|
||||||
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: list[int] | None = None,
|
required_features: list[int] | None = None,
|
||||||
supports_response: SupportsResponse = SupportsResponse.NONE,
|
supports_response: SupportsResponse = SupportsResponse.NONE,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Register an entity service."""
|
"""Register an entity service."""
|
||||||
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]
|
||||||
|
|
|
@ -495,7 +495,19 @@ async def test_extract_all_use_match_all(
|
||||||
) not in caplog.text
|
) not in caplog.text
|
||||||
|
|
||||||
|
|
||||||
async def test_register_entity_service(hass: HomeAssistant) -> None:
|
@pytest.mark.parametrize(
|
||||||
|
("schema", "service_data"),
|
||||||
|
[
|
||||||
|
({"some": str}, {"some": "data"}),
|
||||||
|
({}, {}),
|
||||||
|
(None, {}),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_register_entity_service(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
schema: dict | None,
|
||||||
|
service_data: dict,
|
||||||
|
) -> None:
|
||||||
"""Test registering an enttiy service and calling it."""
|
"""Test registering an enttiy service and calling it."""
|
||||||
entity = MockEntity(entity_id=f"{DOMAIN}.entity")
|
entity = MockEntity(entity_id=f"{DOMAIN}.entity")
|
||||||
calls = []
|
calls = []
|
||||||
|
@ -510,9 +522,7 @@ async def test_register_entity_service(hass: HomeAssistant) -> None:
|
||||||
await component.async_setup({})
|
await component.async_setup({})
|
||||||
await component.async_add_entities([entity])
|
await component.async_add_entities([entity])
|
||||||
|
|
||||||
component.async_register_entity_service(
|
component.async_register_entity_service("hello", schema, "async_called_by_service")
|
||||||
"hello", {"some": str}, "async_called_by_service"
|
|
||||||
)
|
|
||||||
|
|
||||||
with pytest.raises(vol.Invalid):
|
with pytest.raises(vol.Invalid):
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
|
@ -524,24 +534,24 @@ async def test_register_entity_service(hass: HomeAssistant) -> None:
|
||||||
assert len(calls) == 0
|
assert len(calls) == 0
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
DOMAIN, "hello", {"entity_id": entity.entity_id, "some": "data"}, blocking=True
|
DOMAIN, "hello", {"entity_id": entity.entity_id} | service_data, blocking=True
|
||||||
)
|
)
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
assert calls[0] == {"some": "data"}
|
assert calls[0] == service_data
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
DOMAIN, "hello", {"entity_id": ENTITY_MATCH_ALL, "some": "data"}, blocking=True
|
DOMAIN, "hello", {"entity_id": ENTITY_MATCH_ALL} | service_data, blocking=True
|
||||||
)
|
)
|
||||||
assert len(calls) == 2
|
assert len(calls) == 2
|
||||||
assert calls[1] == {"some": "data"}
|
assert calls[1] == service_data
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
DOMAIN, "hello", {"entity_id": ENTITY_MATCH_NONE, "some": "data"}, blocking=True
|
DOMAIN, "hello", {"entity_id": ENTITY_MATCH_NONE} | service_data, blocking=True
|
||||||
)
|
)
|
||||||
assert len(calls) == 2
|
assert len(calls) == 2
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
DOMAIN, "hello", {"area_id": ENTITY_MATCH_NONE, "some": "data"}, blocking=True
|
DOMAIN, "hello", {"area_id": ENTITY_MATCH_NONE} | service_data, blocking=True
|
||||||
)
|
)
|
||||||
assert len(calls) == 2
|
assert len(calls) == 2
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue