mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Speed up run time of admin services by using HassJob (#108623)
This commit is contained in:
parent
dbb5645e63
commit
573de95f21
@ -5,7 +5,7 @@ import asyncio
|
|||||||
from collections.abc import Awaitable, Callable, Iterable
|
from collections.abc import Awaitable, Callable, Iterable
|
||||||
import dataclasses
|
import dataclasses
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from functools import cache, partial, wraps
|
from functools import cache, partial
|
||||||
import logging
|
import logging
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
from typing import TYPE_CHECKING, Any, TypedDict, TypeGuard, TypeVar, cast
|
from typing import TYPE_CHECKING, Any, TypedDict, TypeGuard, TypeVar, cast
|
||||||
@ -965,6 +965,24 @@ async def _handle_entity_call(
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
async def _async_admin_handler(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
service_job: HassJob[[None], Callable[[ServiceCall], Awaitable[None] | None]],
|
||||||
|
call: ServiceCall,
|
||||||
|
) -> None:
|
||||||
|
"""Run an admin service."""
|
||||||
|
if call.context.user_id:
|
||||||
|
user = await hass.auth.async_get_user(call.context.user_id)
|
||||||
|
if user is None:
|
||||||
|
raise UnknownUser(context=call.context)
|
||||||
|
if not user.is_admin:
|
||||||
|
raise Unauthorized(context=call.context)
|
||||||
|
|
||||||
|
result = hass.async_run_hass_job(service_job, call)
|
||||||
|
if result is not None:
|
||||||
|
await result
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
@callback
|
@callback
|
||||||
def async_register_admin_service(
|
def async_register_admin_service(
|
||||||
@ -975,21 +993,16 @@ def async_register_admin_service(
|
|||||||
schema: vol.Schema = vol.Schema({}, extra=vol.PREVENT_EXTRA),
|
schema: vol.Schema = vol.Schema({}, extra=vol.PREVENT_EXTRA),
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Register a service that requires admin access."""
|
"""Register a service that requires admin access."""
|
||||||
|
hass.services.async_register(
|
||||||
@wraps(service_func)
|
domain,
|
||||||
async def admin_handler(call: ServiceCall) -> None:
|
service,
|
||||||
if call.context.user_id:
|
partial(
|
||||||
user = await hass.auth.async_get_user(call.context.user_id)
|
_async_admin_handler,
|
||||||
if user is None:
|
hass,
|
||||||
raise UnknownUser(context=call.context)
|
HassJob(service_func, f"admin service {domain}.{service}"),
|
||||||
if not user.is_admin:
|
),
|
||||||
raise Unauthorized(context=call.context)
|
schema,
|
||||||
|
)
|
||||||
result = hass.async_run_job(service_func, call)
|
|
||||||
if result is not None:
|
|
||||||
await result
|
|
||||||
|
|
||||||
hass.services.async_register(domain, service, admin_handler, schema)
|
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user