-
-
Notifications
You must be signed in to change notification settings - Fork 28.4k
/
integration_platform.py
256 lines (225 loc) · 8.56 KB
/
integration_platform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""Helpers to help with integration platforms."""
from __future__ import annotations
import asyncio
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from functools import partial
import logging
from types import ModuleType
from typing import Any
from homeassistant.const import EVENT_COMPONENT_LOADED
from homeassistant.core import Event, HassJob, HomeAssistant, callback
from homeassistant.loader import (
Integration,
async_get_integrations,
async_get_loaded_integration,
async_register_preload_platform,
bind_hass,
)
from homeassistant.setup import ATTR_COMPONENT, EventComponentLoaded
from homeassistant.util.logging import catch_log_exception
_LOGGER = logging.getLogger(__name__)
DATA_INTEGRATION_PLATFORMS = "integration_platforms"
@dataclass(slots=True, frozen=True)
class IntegrationPlatform:
"""An integration platform."""
platform_name: str
process_job: HassJob[[HomeAssistant, str, Any], Awaitable[None] | None]
seen_components: set[str]
@callback
def _async_integration_platform_component_loaded(
hass: HomeAssistant,
integration_platforms: list[IntegrationPlatform],
event: Event[EventComponentLoaded],
) -> None:
"""Process integration platforms for a component."""
if "." in (component_name := event.data[ATTR_COMPONENT]):
return
integration = async_get_loaded_integration(hass, component_name)
# First filter out platforms that the integration already processed.
integration_platforms_by_name: dict[str, IntegrationPlatform] = {}
for integration_platform in integration_platforms:
if component_name in integration_platform.seen_components:
continue
integration_platform.seen_components.add(component_name)
integration_platforms_by_name[integration_platform.platform_name] = (
integration_platform
)
if not integration_platforms_by_name:
return
# Next, check which platforms exist for this integration.
platforms_that_exist = integration.platforms_exists(integration_platforms_by_name)
if not platforms_that_exist:
return
# If everything is already loaded, we can avoid creating a task.
can_use_cache = True
platforms: dict[str, ModuleType] = {}
for platform_name in platforms_that_exist:
if platform := integration.get_platform_cached(platform_name):
platforms[platform_name] = platform
else:
can_use_cache = False
break
if can_use_cache:
_process_integration_platforms(
hass,
integration,
platforms,
integration_platforms_by_name,
)
return
# At least one of the platforms is not loaded, we need to load them
# so we have to fall back to creating a task.
hass.async_create_task_internal(
_async_process_integration_platforms_for_component(
hass, integration, platforms_that_exist, integration_platforms_by_name
),
eager_start=True,
)
async def _async_process_integration_platforms_for_component(
hass: HomeAssistant,
integration: Integration,
platforms_that_exist: list[str],
integration_platforms_by_name: dict[str, IntegrationPlatform],
) -> None:
"""Process integration platforms for a component."""
# Now we know which platforms to load, let's load them.
try:
platforms = await integration.async_get_platforms(platforms_that_exist)
except ImportError:
_LOGGER.debug(
"Unexpected error importing integration platforms for %s",
integration.domain,
)
return
if futures := _process_integration_platforms(
hass,
integration,
platforms,
integration_platforms_by_name,
):
await asyncio.gather(*futures)
@callback
def _process_integration_platforms(
hass: HomeAssistant,
integration: Integration,
platforms: dict[str, ModuleType],
integration_platforms_by_name: dict[str, IntegrationPlatform],
) -> list[asyncio.Future[Awaitable[None] | None]]:
"""Process integration platforms for a component.
Only the platforms that are passed in will be processed.
"""
return [
future
for platform_name, platform in platforms.items()
if (integration_platform := integration_platforms_by_name[platform_name])
and (
future := hass.async_run_hass_job(
integration_platform.process_job,
hass,
integration.domain,
platform,
)
)
]
def _format_err(name: str, platform_name: str, *args: Any) -> str:
"""Format error message."""
return f"Exception in {name} when processing platform '{platform_name}': {args}"
@bind_hass
async def async_process_integration_platforms(
hass: HomeAssistant,
platform_name: str,
# Any = platform.
process_platform: Callable[[HomeAssistant, str, Any], Awaitable[None] | None],
wait_for_platforms: bool = False,
) -> None:
"""Process a specific platform for all current and future loaded integrations."""
if DATA_INTEGRATION_PLATFORMS not in hass.data:
integration_platforms: list[IntegrationPlatform] = []
hass.data[DATA_INTEGRATION_PLATFORMS] = integration_platforms
hass.bus.async_listen(
EVENT_COMPONENT_LOADED,
partial(
_async_integration_platform_component_loaded,
hass,
integration_platforms,
),
)
else:
integration_platforms = hass.data[DATA_INTEGRATION_PLATFORMS]
async_register_preload_platform(hass, platform_name)
top_level_components = hass.config.top_level_components.copy()
process_job = HassJob(
catch_log_exception(
process_platform,
partial(_format_err, str(process_platform), platform_name),
),
f"process_platform {platform_name}",
)
integration_platform = IntegrationPlatform(
platform_name, process_job, top_level_components
)
# Tell the loader that it should try to pre-load the integration
# for any future components that are loaded so we can reduce the
# amount of import executor usage.
async_register_preload_platform(hass, platform_name)
integration_platforms.append(integration_platform)
if not top_level_components:
return
# We create a task here for two reasons:
#
# 1. We want the integration that provides the integration platform to
# not be delayed by waiting on each individual platform to be processed
# since the import or the integration platforms themselves may have to
# schedule I/O or executor jobs.
#
# 2. We want the behavior to be the same as if the integration that has
# the integration platform is loaded after the platform is processed.
#
# We use hass.async_create_task instead of asyncio.create_task because
# we want to make sure that startup waits for the task to complete.
#
future = hass.async_create_task_internal(
_async_process_integration_platforms(
hass, platform_name, top_level_components.copy(), process_job
),
eager_start=True,
)
if wait_for_platforms:
await future
async def _async_process_integration_platforms(
hass: HomeAssistant,
platform_name: str,
top_level_components: set[str],
process_job: HassJob,
) -> None:
"""Process integration platforms for a component."""
integrations = await async_get_integrations(hass, top_level_components)
loaded_integrations: list[Integration] = [
integration
for integration in integrations.values()
if not isinstance(integration, Exception)
]
# Finally, fetch the platforms for each integration and process them.
# This uses the import executor in a loop. If there are a lot
# of integration with the integration platform to process,
# this could be a bottleneck.
futures: list[asyncio.Future[None]] = []
for integration in loaded_integrations:
if not integration.platforms_exists((platform_name,)):
continue
try:
platform = await integration.async_get_platform(platform_name)
except ImportError:
_LOGGER.debug(
"Unexpected error importing %s for %s",
platform_name,
integration.domain,
)
continue
if future := hass.async_run_hass_job(
process_job, hass, integration.domain, platform
):
futures.append(future)
if futures:
await asyncio.gather(*futures)