Add strict typing to core.py (3) - Service (#63241)

This commit is contained in:
Marc Mueller 2022-01-06 01:18:17 +01:00 committed by GitHub
parent dd118fe013
commit caaa1b32c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ of entities and react to changes.
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from collections.abc import Awaitable, Collection, Coroutine, Iterable, Mapping from collections.abc import Collection, Coroutine, Iterable, Mapping
import datetime import datetime
import enum import enum
import functools import functools
@ -18,7 +18,7 @@ import re
import threading import threading
from time import monotonic from time import monotonic
from types import MappingProxyType from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar, cast from typing import TYPE_CHECKING, Any, Awaitable, Callable, Optional, TypeVar, cast
from urllib.parse import urlparse from urllib.parse import urlparse
import attr import attr
@ -1266,7 +1266,7 @@ class Service:
def __init__( def __init__(
self, self,
func: Callable, func: Callable[[ServiceCall], None | Awaitable[None]],
schema: vol.Schema | None, schema: vol.Schema | None,
context: Context | None = None, context: Context | None = None,
) -> None: ) -> None:
@ -1284,7 +1284,7 @@ class ServiceCall:
self, self,
domain: str, domain: str,
service: str, service: str,
data: dict | None = None, data: dict[str, Any] | None = None,
context: Context | None = None, context: Context | None = None,
) -> None: ) -> None:
"""Initialize a service call.""" """Initialize a service call."""
@ -1408,11 +1408,11 @@ class ServiceRegistry:
self, self,
domain: str, domain: str,
service: str, service: str,
service_data: dict | None = None, service_data: dict[str, Any] | None = None,
blocking: bool = False, blocking: bool = False,
context: Context | None = None, context: Context | None = None,
limit: float | None = SERVICE_CALL_LIMIT, limit: float | None = SERVICE_CALL_LIMIT,
target: dict | None = None, target: dict[str, Any] | None = None,
) -> bool | None: ) -> bool | None:
""" """
Call a service. Call a service.
@ -1430,11 +1430,11 @@ class ServiceRegistry:
self, self,
domain: str, domain: str,
service: str, service: str,
service_data: dict | None = None, service_data: dict[str, Any] | None = None,
blocking: bool = False, blocking: bool = False,
context: Context | None = None, context: Context | None = None,
limit: float | None = SERVICE_CALL_LIMIT, limit: float | None = SERVICE_CALL_LIMIT,
target: dict | None = None, target: dict[str, Any] | None = None,
) -> bool | None: ) -> bool | None:
""" """
Call a service. Call a service.
@ -1467,7 +1467,7 @@ class ServiceRegistry:
if handler.schema: if handler.schema:
try: try:
processed_data = handler.schema(service_data) processed_data: dict[str, Any] = handler.schema(service_data)
except vol.Invalid: except vol.Invalid:
_LOGGER.debug( _LOGGER.debug(
"Invalid data for service call %s.%s: %s", "Invalid data for service call %s.%s: %s",
@ -1523,7 +1523,9 @@ class ServiceRegistry:
return False return False
def _run_service_in_background( def _run_service_in_background(
self, coro_or_task: Coroutine | asyncio.Task, service_call: ServiceCall self,
coro_or_task: Coroutine[Any, Any, None] | asyncio.Task[None],
service_call: ServiceCall,
) -> None: ) -> None:
"""Run service call in background, catching and logging any exceptions.""" """Run service call in background, catching and logging any exceptions."""
@ -1548,11 +1550,15 @@ class ServiceRegistry:
) -> None: ) -> None:
"""Execute a service.""" """Execute a service."""
if handler.job.job_type == HassJobType.Coroutinefunction: if handler.job.job_type == HassJobType.Coroutinefunction:
await handler.job.target(service_call) await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
service_call
)
elif handler.job.job_type == HassJobType.Callback: elif handler.job.job_type == HassJobType.Callback:
handler.job.target(service_call) cast(Callable[[ServiceCall], None], handler.job.target)(service_call)
else: else:
await self._hass.async_add_executor_job(handler.job.target, service_call) await self._hass.async_add_executor_job(
cast(Callable[[ServiceCall], None], handler.job.target), service_call
)
class Config: class Config: