mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 18:28:14 +02:00
Improve dispatcher helper typing (#75455)
* Improve dispatcher helper typing * Code review
This commit is contained in:
@ -18,6 +18,7 @@ homeassistant.helpers.condition
|
|||||||
homeassistant.helpers.debounce
|
homeassistant.helpers.debounce
|
||||||
homeassistant.helpers.deprecation
|
homeassistant.helpers.deprecation
|
||||||
homeassistant.helpers.discovery
|
homeassistant.helpers.discovery
|
||||||
|
homeassistant.helpers.dispatcher
|
||||||
homeassistant.helpers.entity
|
homeassistant.helpers.entity
|
||||||
homeassistant.helpers.entity_values
|
homeassistant.helpers.entity_values
|
||||||
homeassistant.helpers.event
|
homeassistant.helpers.event
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""Helpers for Home Assistant dispatcher & internal component/platform."""
|
"""Helpers for Home Assistant dispatcher & internal component/platform."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable, Coroutine
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -62,7 +62,9 @@ def dispatcher_send(hass: HomeAssistant, signal: str, *args: Any) -> None:
|
|||||||
hass.loop.call_soon_threadsafe(async_dispatcher_send, hass, signal, *args)
|
hass.loop.call_soon_threadsafe(async_dispatcher_send, hass, signal, *args)
|
||||||
|
|
||||||
|
|
||||||
def _generate_job(signal: str, target: Callable[..., Any]) -> HassJob:
|
def _generate_job(
|
||||||
|
signal: str, target: Callable[..., Any]
|
||||||
|
) -> HassJob[..., None | Coroutine[Any, Any, None]]:
|
||||||
"""Generate a HassJob for a signal and target."""
|
"""Generate a HassJob for a signal and target."""
|
||||||
return HassJob(
|
return HassJob(
|
||||||
catch_log_exception(
|
catch_log_exception(
|
||||||
@ -84,16 +86,18 @@ def async_dispatcher_send(hass: HomeAssistant, signal: str, *args: Any) -> None:
|
|||||||
|
|
||||||
This method must be run in the event loop.
|
This method must be run in the event loop.
|
||||||
"""
|
"""
|
||||||
target_list = hass.data.get(DATA_DISPATCHER, {}).get(signal, {})
|
target_list: dict[
|
||||||
|
Callable[..., Any], HassJob[..., None | Coroutine[Any, Any, None]] | None
|
||||||
|
] = hass.data.get(DATA_DISPATCHER, {}).get(signal, {})
|
||||||
|
|
||||||
run: list[HassJob] = []
|
run: list[HassJob[..., None | Coroutine[Any, Any, None]]] = []
|
||||||
for target, job in target_list.items():
|
for target, job in target_list.items():
|
||||||
if job is None:
|
if job is None:
|
||||||
job = _generate_job(signal, target)
|
job = _generate_job(signal, target)
|
||||||
target_list[target] = job
|
target_list[target] = job
|
||||||
|
|
||||||
# Run the jobs all at the end
|
# Run the jobs all at the end
|
||||||
# to ensure no jobs add more disptachers
|
# to ensure no jobs add more dispatchers
|
||||||
# which can result in the target_list
|
# which can result in the target_list
|
||||||
# changing size during iteration
|
# changing size during iteration
|
||||||
run.append(job)
|
run.append(job)
|
||||||
|
3
mypy.ini
3
mypy.ini
@ -66,6 +66,9 @@ disallow_any_generics = true
|
|||||||
[mypy-homeassistant.helpers.discovery]
|
[mypy-homeassistant.helpers.discovery]
|
||||||
disallow_any_generics = true
|
disallow_any_generics = true
|
||||||
|
|
||||||
|
[mypy-homeassistant.helpers.dispatcher]
|
||||||
|
disallow_any_generics = true
|
||||||
|
|
||||||
[mypy-homeassistant.helpers.entity]
|
[mypy-homeassistant.helpers.entity]
|
||||||
disallow_any_generics = true
|
disallow_any_generics = true
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user