mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 05:37:44 +00:00
Improve type hints in template (#126802)
This commit is contained in:
parent
5a6ce86476
commit
45f92dd981
@ -3,7 +3,9 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from collections.abc import Coroutine
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant import config as conf_util
|
from homeassistant import config as conf_util
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -23,11 +25,13 @@ from homeassistant.helpers.reload import async_reload_integration_platforms
|
|||||||
from homeassistant.helpers.service import async_register_admin_service
|
from homeassistant.helpers.service import async_register_admin_service
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.loader import async_get_integration
|
from homeassistant.loader import async_get_integration
|
||||||
|
from homeassistant.util.hass_dict import HassKey
|
||||||
|
|
||||||
from .const import CONF_MAX, CONF_MIN, CONF_STEP, CONF_TRIGGER, DOMAIN, PLATFORMS
|
from .const import CONF_MAX, CONF_MIN, CONF_STEP, CONF_TRIGGER, DOMAIN, PLATFORMS
|
||||||
from .coordinator import TriggerUpdateCoordinator
|
from .coordinator import TriggerUpdateCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
DATA_COORDINATORS: HassKey[list[TriggerUpdateCoordinator]] = HassKey(DOMAIN)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
@ -102,19 +106,21 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
async def _process_config(hass: HomeAssistant, hass_config: ConfigType) -> None:
|
async def _process_config(hass: HomeAssistant, hass_config: ConfigType) -> None:
|
||||||
"""Process config."""
|
"""Process config."""
|
||||||
coordinators: list[TriggerUpdateCoordinator] | None = hass.data.pop(DOMAIN, None)
|
coordinators = hass.data.pop(DATA_COORDINATORS, None)
|
||||||
|
|
||||||
# Remove old ones
|
# Remove old ones
|
||||||
if coordinators:
|
if coordinators:
|
||||||
for coordinator in coordinators:
|
for coordinator in coordinators:
|
||||||
coordinator.async_remove()
|
coordinator.async_remove()
|
||||||
|
|
||||||
async def init_coordinator(hass, conf_section):
|
async def init_coordinator(
|
||||||
|
hass: HomeAssistant, conf_section: dict[str, Any]
|
||||||
|
) -> TriggerUpdateCoordinator:
|
||||||
coordinator = TriggerUpdateCoordinator(hass, conf_section)
|
coordinator = TriggerUpdateCoordinator(hass, conf_section)
|
||||||
await coordinator.async_setup(hass_config)
|
await coordinator.async_setup(hass_config)
|
||||||
return coordinator
|
return coordinator
|
||||||
|
|
||||||
coordinator_tasks = []
|
coordinator_tasks: list[Coroutine[Any, Any, TriggerUpdateCoordinator]] = []
|
||||||
|
|
||||||
for conf_section in hass_config[DOMAIN]:
|
for conf_section in hass_config[DOMAIN]:
|
||||||
if CONF_TRIGGER in conf_section:
|
if CONF_TRIGGER in conf_section:
|
||||||
@ -138,4 +144,4 @@ async def _process_config(hass: HomeAssistant, hass_config: ConfigType) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if coordinator_tasks:
|
if coordinator_tasks:
|
||||||
hass.data[DOMAIN] = await asyncio.gather(*coordinator_tasks)
|
hass.data[DATA_COORDINATORS] = await asyncio.gather(*coordinator_tasks)
|
||||||
|
@ -5,7 +5,7 @@ import logging
|
|||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
||||||
from homeassistant.core import Context, CoreState, callback
|
from homeassistant.core import Context, CoreState, Event, HomeAssistant, callback
|
||||||
from homeassistant.helpers import condition, discovery, trigger as trigger_helper
|
from homeassistant.helpers import condition, discovery, trigger as trigger_helper
|
||||||
from homeassistant.helpers.script import Script
|
from homeassistant.helpers.script import Script
|
||||||
from homeassistant.helpers.trace import trace_get
|
from homeassistant.helpers.trace import trace_get
|
||||||
@ -22,7 +22,7 @@ class TriggerUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
|
|
||||||
REMOVE_TRIGGER = object()
|
REMOVE_TRIGGER = object()
|
||||||
|
|
||||||
def __init__(self, hass, config):
|
def __init__(self, hass: HomeAssistant, config: dict[str, Any]) -> None:
|
||||||
"""Instantiate trigger data."""
|
"""Instantiate trigger data."""
|
||||||
super().__init__(hass, _LOGGER, name="Trigger Update Coordinator")
|
super().__init__(hass, _LOGGER, name="Trigger Update Coordinator")
|
||||||
self.config = config
|
self.config = config
|
||||||
@ -37,7 +37,7 @@ class TriggerUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
return self.config.get("unique_id")
|
return self.config.get("unique_id")
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_remove(self):
|
def async_remove(self) -> None:
|
||||||
"""Signal that the entities need to remove themselves."""
|
"""Signal that the entities need to remove themselves."""
|
||||||
if self._unsub_start:
|
if self._unsub_start:
|
||||||
self._unsub_start()
|
self._unsub_start()
|
||||||
@ -66,7 +66,7 @@ class TriggerUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
eager_start=True,
|
eager_start=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _attach_triggers(self, start_event=None) -> None:
|
async def _attach_triggers(self, start_event: Event | None = None) -> None:
|
||||||
"""Attach the triggers."""
|
"""Attach the triggers."""
|
||||||
if CONF_ACTION in self.config:
|
if CONF_ACTION in self.config:
|
||||||
self._script = Script(
|
self._script = Script(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user