Deduplicate async_register_entity_service (#124045)

This commit is contained in:
Erik Montnemery
2024-08-16 14:06:35 +02:00
committed by GitHub
parent 738cc5095d
commit 69943af68a
3 changed files with 72 additions and 68 deletions

View File

@@ -5,13 +5,11 @@ from __future__ import annotations
import asyncio
from collections.abc import Callable, Iterable
from datetime import timedelta
from functools import partial
import logging
from types import ModuleType
from typing import Any, Generic
from typing_extensions import TypeVar
import voluptuous as vol
from homeassistant import config as conf_util
from homeassistant.config_entries import ConfigEntry
@@ -265,39 +263,16 @@ class EntityComponent(Generic[_EntityT]):
supports_response: SupportsResponse = SupportsResponse.NONE,
) -> None:
"""Register an entity service."""
if schema is None or isinstance(schema, dict):
schema = cv.make_entity_service_schema(schema)
# Do a sanity check to check this is a valid entity service schema,
# the check could be extended to require All/Any to have sub schema(s)
# with all entity service fields
elif (
# Don't check All/Any
not isinstance(schema, (vol.All, vol.Any))
# Don't check All/Any wrapped in schema
and not isinstance(schema.schema, (vol.All, vol.Any))
and any(key not in schema.schema for key in cv.ENTITY_SERVICE_FIELDS)
):
raise HomeAssistantError(
"The schema does not include all required keys: "
f"{", ".join(str(key) for key in cv.ENTITY_SERVICE_FIELDS)}"
)
service_func: str | HassJob[..., Any]
service_func = func if isinstance(func, str) else HassJob(func)
self.hass.services.async_register(
service.async_register_entity_service(
self.hass,
self.domain,
name,
partial(
service.entity_service_call,
self.hass,
self._entities,
service_func,
required_features=required_features,
),
schema,
supports_response,
entities=self._entities,
func=func,
job_type=HassJobType.Coroutinefunction,
required_features=required_features,
schema=schema,
supports_response=supports_response,
)
async def async_setup_platform(