2014-11-05 08:34:19 +01:00
|
|
|
"""
|
2019-04-14 16:23:01 +02:00
|
|
|
The methods for loading Home Assistant integrations.
|
2014-11-15 08:17:18 +01:00
|
|
|
|
|
|
|
This module has quite some complex parts. I have tried to add as much
|
|
|
|
documentation as possible to keep it understandable.
|
2014-11-05 08:34:19 +01:00
|
|
|
"""
|
2021-02-12 10:58:20 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-04-16 05:38:24 +02:00
|
|
|
import asyncio
|
2022-07-14 22:06:08 +02:00
|
|
|
from collections.abc import Callable, Iterable
|
2021-03-23 14:36:43 +01:00
|
|
|
from contextlib import suppress
|
2017-07-16 18:23:06 +02:00
|
|
|
import functools as ft
|
2014-11-05 08:34:19 +01:00
|
|
|
import importlib
|
|
|
|
import logging
|
2019-04-09 18:30:32 +02:00
|
|
|
import pathlib
|
2016-02-19 06:27:50 +01:00
|
|
|
import sys
|
2016-07-28 05:33:49 +02:00
|
|
|
from types import ModuleType
|
2022-03-21 04:38:13 +01:00
|
|
|
from typing import TYPE_CHECKING, Any, Literal, TypedDict, TypeVar, cast
|
2016-07-28 05:33:49 +02:00
|
|
|
|
2021-05-17 15:48:41 +02:00
|
|
|
from awesomeversion import (
|
|
|
|
AwesomeVersion,
|
|
|
|
AwesomeVersionException,
|
|
|
|
AwesomeVersionStrategy,
|
|
|
|
)
|
2021-02-04 09:59:41 +01:00
|
|
|
|
2022-09-28 17:31:48 +02:00
|
|
|
from . import generated
|
2022-05-02 01:26:22 +02:00
|
|
|
from .generated.application_credentials import APPLICATION_CREDENTIALS
|
2022-07-09 01:55:31 +02:00
|
|
|
from .generated.bluetooth import BLUETOOTH
|
2021-12-23 20:14:47 +01:00
|
|
|
from .generated.dhcp import DHCP
|
|
|
|
from .generated.mqtt import MQTT
|
|
|
|
from .generated.ssdp import SSDP
|
|
|
|
from .generated.usb import USB
|
|
|
|
from .generated.zeroconf import HOMEKIT, ZEROCONF
|
2022-06-28 09:34:56 +02:00
|
|
|
from .helpers.json import JSON_DECODE_EXCEPTIONS, json_loads
|
2020-08-05 15:50:56 +02:00
|
|
|
|
2018-07-13 12:24:51 +02:00
|
|
|
# Typing imports that create a circular dependency
|
|
|
|
if TYPE_CHECKING:
|
2021-12-23 20:14:47 +01:00
|
|
|
from .core import HomeAssistant
|
2018-07-13 12:24:51 +02:00
|
|
|
|
2022-03-17 18:52:38 +01:00
|
|
|
_CallableT = TypeVar("_CallableT", bound=Callable[..., Any])
|
2018-07-23 10:24:39 +02:00
|
|
|
|
2014-11-05 08:34:19 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
DATA_COMPONENTS = "components"
|
|
|
|
DATA_INTEGRATIONS = "integrations"
|
|
|
|
DATA_CUSTOM_COMPONENTS = "custom_components"
|
|
|
|
PACKAGE_CUSTOM_COMPONENTS = "custom_components"
|
|
|
|
PACKAGE_BUILTIN = "homeassistant.components"
|
2019-04-15 07:31:01 +02:00
|
|
|
CUSTOM_WARNING = (
|
2021-06-01 00:32:03 +02:00
|
|
|
"We found a custom integration %s which has not "
|
2019-07-31 21:25:30 +02:00
|
|
|
"been tested by Home Assistant. This component might "
|
|
|
|
"cause stability problems, be sure to disable it if you "
|
2021-02-04 09:59:41 +01:00
|
|
|
"experience issues with Home Assistant"
|
|
|
|
)
|
2021-05-17 15:48:41 +02:00
|
|
|
|
2020-12-19 12:46:27 +01:00
|
|
|
_UNDEF = object() # Internal; not helpers.typing.UNDEFINED due to circular dependency
|
2019-04-09 18:30:32 +02:00
|
|
|
|
2020-11-11 08:34:54 +01:00
|
|
|
MAX_LOAD_CONCURRENTLY = 4
|
|
|
|
|
2021-12-19 09:09:21 +01:00
|
|
|
MOVED_ZEROCONF_PROPS = ("macaddress", "model", "manufacturer")
|
|
|
|
|
2019-04-09 18:30:32 +02:00
|
|
|
|
2022-03-01 05:49:44 +01:00
|
|
|
class DHCPMatcherRequired(TypedDict, total=True):
|
|
|
|
"""Matcher for the dhcp integration for required fields."""
|
|
|
|
|
|
|
|
domain: str
|
|
|
|
|
|
|
|
|
|
|
|
class DHCPMatcherOptional(TypedDict, total=False):
|
|
|
|
"""Matcher for the dhcp integration for optional fields."""
|
|
|
|
|
|
|
|
macaddress: str
|
|
|
|
hostname: str
|
|
|
|
registered_devices: bool
|
|
|
|
|
|
|
|
|
|
|
|
class DHCPMatcher(DHCPMatcherRequired, DHCPMatcherOptional):
|
|
|
|
"""Matcher for the dhcp integration."""
|
|
|
|
|
|
|
|
|
2022-07-09 01:55:31 +02:00
|
|
|
class BluetoothMatcherRequired(TypedDict, total=True):
|
|
|
|
"""Matcher for the bluetooth integration for required fields."""
|
|
|
|
|
|
|
|
domain: str
|
|
|
|
|
|
|
|
|
|
|
|
class BluetoothMatcherOptional(TypedDict, total=False):
|
|
|
|
"""Matcher for the bluetooth integration for optional fields."""
|
|
|
|
|
|
|
|
local_name: str
|
|
|
|
service_uuid: str
|
2022-07-24 23:39:53 +02:00
|
|
|
service_data_uuid: str
|
2022-07-09 01:55:31 +02:00
|
|
|
manufacturer_id: int
|
2022-07-18 00:25:45 +02:00
|
|
|
manufacturer_data_start: list[int]
|
2022-08-22 20:02:26 +02:00
|
|
|
connectable: bool
|
2022-07-09 01:55:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BluetoothMatcher(BluetoothMatcherRequired, BluetoothMatcherOptional):
|
|
|
|
"""Matcher for the bluetooth integration."""
|
|
|
|
|
|
|
|
|
2022-08-23 03:45:08 +02:00
|
|
|
class USBMatcherRequired(TypedDict, total=True):
|
|
|
|
"""Matcher for the usb integration for required fields."""
|
|
|
|
|
|
|
|
domain: str
|
|
|
|
|
|
|
|
|
|
|
|
class USBMatcherOptional(TypedDict, total=False):
|
|
|
|
"""Matcher for the usb integration for optional fields."""
|
|
|
|
|
|
|
|
vid: str
|
|
|
|
pid: str
|
|
|
|
serial_number: str
|
|
|
|
manufacturer: str
|
|
|
|
description: str
|
|
|
|
|
|
|
|
|
|
|
|
class USBMatcher(USBMatcherRequired, USBMatcherOptional):
|
|
|
|
"""Matcher for the bluetooth integration."""
|
|
|
|
|
|
|
|
|
2021-01-18 22:23:25 +01:00
|
|
|
class Manifest(TypedDict, total=False):
|
|
|
|
"""
|
|
|
|
Integration manifest.
|
|
|
|
|
|
|
|
Note that none of the attributes are marked Optional here. However, some of them may be optional in manifest.json
|
|
|
|
in the sense that they can be omitted altogether. But when present, they should not have null values in it.
|
|
|
|
"""
|
|
|
|
|
|
|
|
name: str
|
|
|
|
disabled: str
|
|
|
|
domain: str
|
2022-10-19 12:41:43 +02:00
|
|
|
integration_type: Literal[
|
|
|
|
"entity", "device", "hardware", "helper", "hub", "service", "system"
|
|
|
|
]
|
2021-03-17 17:34:55 +01:00
|
|
|
dependencies: list[str]
|
|
|
|
after_dependencies: list[str]
|
|
|
|
requirements: list[str]
|
2021-01-18 22:23:25 +01:00
|
|
|
config_flow: bool
|
|
|
|
documentation: str
|
|
|
|
issue_tracker: str
|
|
|
|
quality_scale: str
|
2021-04-15 10:21:38 +02:00
|
|
|
iot_class: str
|
2022-07-09 01:55:31 +02:00
|
|
|
bluetooth: list[dict[str, int | str]]
|
2021-03-17 17:34:55 +01:00
|
|
|
mqtt: list[str]
|
|
|
|
ssdp: list[dict[str, str]]
|
|
|
|
zeroconf: list[str | dict[str, str]]
|
2022-02-15 18:02:52 +01:00
|
|
|
dhcp: list[dict[str, bool | str]]
|
2021-08-20 21:04:18 +02:00
|
|
|
usb: list[dict[str, str]]
|
2021-03-17 17:34:55 +01:00
|
|
|
homekit: dict[str, list[str]]
|
2021-01-18 22:23:25 +01:00
|
|
|
is_built_in: bool
|
2021-02-04 09:59:41 +01:00
|
|
|
version: str
|
2021-03-17 17:34:55 +01:00
|
|
|
codeowners: list[str]
|
2022-01-28 22:37:53 +01:00
|
|
|
loggers: list[str]
|
2021-01-18 22:23:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def manifest_from_legacy_module(domain: str, module: ModuleType) -> Manifest:
|
2019-04-09 18:30:32 +02:00
|
|
|
"""Generate a manifest from a legacy module."""
|
|
|
|
return {
|
2019-07-31 21:25:30 +02:00
|
|
|
"domain": domain,
|
|
|
|
"name": domain,
|
|
|
|
"requirements": getattr(module, "REQUIREMENTS", []),
|
|
|
|
"dependencies": getattr(module, "DEPENDENCIES", []),
|
|
|
|
"codeowners": [],
|
2019-04-09 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-09 01:19:37 +02:00
|
|
|
async def _async_get_custom_components(
|
2021-03-18 22:58:19 +01:00
|
|
|
hass: HomeAssistant,
|
2021-03-17 17:34:55 +01:00
|
|
|
) -> dict[str, Integration]:
|
2019-07-09 01:19:37 +02:00
|
|
|
"""Return list of custom integrations."""
|
2020-02-18 20:52:38 +01:00
|
|
|
if hass.config.safe_mode:
|
|
|
|
return {}
|
|
|
|
|
2019-07-09 01:19:37 +02:00
|
|
|
try:
|
2020-04-04 17:07:36 +02:00
|
|
|
import custom_components # pylint: disable=import-outside-toplevel
|
2019-07-09 01:19:37 +02:00
|
|
|
except ImportError:
|
|
|
|
return {}
|
|
|
|
|
2021-03-17 17:34:55 +01:00
|
|
|
def get_sub_directories(paths: list[str]) -> list[pathlib.Path]:
|
2019-07-09 01:19:37 +02:00
|
|
|
"""Return all sub directories in a set of paths."""
|
|
|
|
return [
|
|
|
|
entry
|
|
|
|
for path in paths
|
|
|
|
for entry in pathlib.Path(path).iterdir()
|
|
|
|
if entry.is_dir()
|
|
|
|
]
|
|
|
|
|
|
|
|
dirs = await hass.async_add_executor_job(
|
2019-07-31 21:25:30 +02:00
|
|
|
get_sub_directories, custom_components.__path__
|
|
|
|
)
|
2019-07-09 01:19:37 +02:00
|
|
|
|
2022-07-14 22:06:08 +02:00
|
|
|
integrations = await hass.async_add_executor_job(
|
|
|
|
_resolve_integrations_from_root,
|
|
|
|
hass,
|
|
|
|
custom_components,
|
|
|
|
[comp.name for comp in dirs],
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2019-07-09 01:19:37 +02:00
|
|
|
return {
|
|
|
|
integration.domain: integration
|
2022-07-14 22:06:08 +02:00
|
|
|
for integration in integrations.values()
|
2019-07-09 01:19:37 +02:00
|
|
|
if integration is not None
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_get_custom_components(
|
2021-03-18 22:58:19 +01:00
|
|
|
hass: HomeAssistant,
|
2021-03-17 17:34:55 +01:00
|
|
|
) -> dict[str, Integration]:
|
2019-07-09 01:19:37 +02:00
|
|
|
"""Return cached list of custom integrations."""
|
2021-10-17 20:08:11 +02:00
|
|
|
if (reg_or_evt := hass.data.get(DATA_CUSTOM_COMPONENTS)) is None:
|
2019-07-09 01:19:37 +02:00
|
|
|
evt = hass.data[DATA_CUSTOM_COMPONENTS] = asyncio.Event()
|
|
|
|
|
|
|
|
reg = await _async_get_custom_components(hass)
|
|
|
|
|
|
|
|
hass.data[DATA_CUSTOM_COMPONENTS] = reg
|
|
|
|
evt.set()
|
|
|
|
return reg
|
|
|
|
|
|
|
|
if isinstance(reg_or_evt, asyncio.Event):
|
|
|
|
await reg_or_evt.wait()
|
2022-01-11 21:26:03 +01:00
|
|
|
return cast(dict[str, "Integration"], hass.data.get(DATA_CUSTOM_COMPONENTS))
|
2019-07-09 01:19:37 +02:00
|
|
|
|
2022-01-11 21:26:03 +01:00
|
|
|
return cast(dict[str, "Integration"], reg_or_evt)
|
2019-07-09 01:19:37 +02:00
|
|
|
|
|
|
|
|
2022-03-21 04:38:13 +01:00
|
|
|
async def async_get_config_flows(
|
|
|
|
hass: HomeAssistant,
|
2022-10-19 12:41:43 +02:00
|
|
|
type_filter: Literal["device", "helper", "hub", "service"] | None = None,
|
2022-03-21 04:38:13 +01:00
|
|
|
) -> set[str]:
|
2019-07-09 01:19:37 +02:00
|
|
|
"""Return cached list of config flows."""
|
2020-04-04 17:07:36 +02:00
|
|
|
# pylint: disable=import-outside-toplevel
|
2021-12-23 20:14:47 +01:00
|
|
|
from .generated.config_flows import FLOWS
|
2019-07-31 21:25:30 +02:00
|
|
|
|
2022-03-21 04:38:13 +01:00
|
|
|
integrations = await async_get_custom_components(hass)
|
2021-03-17 17:34:55 +01:00
|
|
|
flows: set[str] = set()
|
2019-07-09 01:19:37 +02:00
|
|
|
|
2022-03-21 04:38:13 +01:00
|
|
|
if type_filter is not None:
|
|
|
|
flows.update(FLOWS[type_filter])
|
|
|
|
else:
|
|
|
|
for type_flows in FLOWS.values():
|
|
|
|
flows.update(type_flows)
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
flows.update(
|
|
|
|
[
|
|
|
|
integration.domain
|
|
|
|
for integration in integrations.values()
|
|
|
|
if integration.config_flow
|
2022-03-21 04:38:13 +01:00
|
|
|
and (type_filter is None or integration.integration_type == type_filter)
|
2019-07-31 21:25:30 +02:00
|
|
|
]
|
|
|
|
)
|
2019-07-09 01:19:37 +02:00
|
|
|
|
|
|
|
return flows
|
|
|
|
|
|
|
|
|
2022-09-28 17:31:48 +02:00
|
|
|
async def async_get_integration_descriptions(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
"""Return cached list of integrations."""
|
|
|
|
base = generated.__path__[0]
|
|
|
|
config_flow_path = pathlib.Path(base) / "integrations.json"
|
|
|
|
|
|
|
|
flow = await hass.async_add_executor_job(config_flow_path.read_text)
|
|
|
|
core_flows: dict[str, Any] = json_loads(flow)
|
|
|
|
custom_integrations = await async_get_custom_components(hass)
|
|
|
|
custom_flows: dict[str, Any] = {
|
2022-10-20 19:25:24 +02:00
|
|
|
"integration": {},
|
2022-09-28 17:31:48 +02:00
|
|
|
"helper": {},
|
|
|
|
}
|
|
|
|
|
|
|
|
for integration in custom_integrations.values():
|
|
|
|
# Remove core integration with same domain as the custom integration
|
|
|
|
if integration.integration_type in ("entity", "system"):
|
|
|
|
continue
|
|
|
|
|
2022-10-26 12:39:23 +02:00
|
|
|
for integration_type in ("integration", "helper"):
|
2022-09-28 17:31:48 +02:00
|
|
|
if integration.domain not in core_flows[integration_type]:
|
|
|
|
continue
|
|
|
|
del core_flows[integration_type][integration.domain]
|
|
|
|
if integration.domain in core_flows["translated_name"]:
|
|
|
|
core_flows["translated_name"].remove(integration.domain)
|
|
|
|
|
2022-10-26 12:39:23 +02:00
|
|
|
if integration.integration_type == "helper":
|
2022-10-20 19:25:24 +02:00
|
|
|
integration_key: str = integration.integration_type
|
|
|
|
else:
|
|
|
|
integration_key = "integration"
|
|
|
|
|
2022-09-28 17:31:48 +02:00
|
|
|
metadata = {
|
|
|
|
"config_flow": integration.config_flow,
|
2022-10-19 12:41:43 +02:00
|
|
|
"integration_type": integration.integration_type,
|
2022-09-28 17:31:48 +02:00
|
|
|
"iot_class": integration.iot_class,
|
|
|
|
"name": integration.name,
|
|
|
|
}
|
2022-10-20 19:25:24 +02:00
|
|
|
custom_flows[integration_key][integration.domain] = metadata
|
2022-09-28 17:31:48 +02:00
|
|
|
|
|
|
|
return {"core": core_flows, "custom": custom_flows}
|
|
|
|
|
|
|
|
|
2022-05-02 01:26:22 +02:00
|
|
|
async def async_get_application_credentials(hass: HomeAssistant) -> list[str]:
|
|
|
|
"""Return cached list of application credentials."""
|
|
|
|
integrations = await async_get_custom_components(hass)
|
|
|
|
|
|
|
|
return [
|
|
|
|
*APPLICATION_CREDENTIALS,
|
|
|
|
*[
|
|
|
|
integration.domain
|
|
|
|
for integration in integrations.values()
|
|
|
|
if "application_credentials" in integration.dependencies
|
|
|
|
],
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-12-19 09:09:21 +01:00
|
|
|
def async_process_zeroconf_match_dict(entry: dict[str, Any]) -> dict[str, Any]:
|
|
|
|
"""Handle backwards compat with zeroconf matchers."""
|
|
|
|
entry_without_type: dict[str, Any] = entry.copy()
|
|
|
|
del entry_without_type["type"]
|
|
|
|
# These properties keys used to be at the top level, we relocate
|
|
|
|
# them for backwards compat
|
|
|
|
for moved_prop in MOVED_ZEROCONF_PROPS:
|
|
|
|
if value := entry_without_type.pop(moved_prop, None):
|
|
|
|
_LOGGER.warning(
|
2022-12-22 10:12:50 +01:00
|
|
|
(
|
|
|
|
'Matching the zeroconf property "%s" at top-level is deprecated and'
|
|
|
|
" should be moved into a properties dict; Check the developer"
|
|
|
|
" documentation"
|
|
|
|
),
|
2021-12-19 09:09:21 +01:00
|
|
|
moved_prop,
|
|
|
|
)
|
|
|
|
if "properties" not in entry_without_type:
|
|
|
|
prop_dict: dict[str, str] = {}
|
|
|
|
entry_without_type["properties"] = prop_dict
|
|
|
|
else:
|
|
|
|
prop_dict = entry_without_type["properties"]
|
|
|
|
prop_dict[moved_prop] = value.lower()
|
|
|
|
return entry_without_type
|
|
|
|
|
|
|
|
|
|
|
|
async def async_get_zeroconf(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> dict[str, list[dict[str, str | dict[str, str]]]]:
|
2020-08-05 15:50:56 +02:00
|
|
|
"""Return cached list of zeroconf types."""
|
2021-12-19 09:09:21 +01:00
|
|
|
zeroconf: dict[str, list[dict[str, str | dict[str, str]]]] = ZEROCONF.copy() # type: ignore[assignment]
|
2020-08-05 15:50:56 +02:00
|
|
|
|
|
|
|
integrations = await async_get_custom_components(hass)
|
|
|
|
for integration in integrations.values():
|
|
|
|
if not integration.zeroconf:
|
|
|
|
continue
|
2020-09-11 12:19:21 +02:00
|
|
|
for entry in integration.zeroconf:
|
2021-12-19 09:09:21 +01:00
|
|
|
data: dict[str, str | dict[str, str]] = {"domain": integration.domain}
|
2020-09-11 12:19:21 +02:00
|
|
|
if isinstance(entry, dict):
|
|
|
|
typ = entry["type"]
|
2021-12-19 09:09:21 +01:00
|
|
|
data.update(async_process_zeroconf_match_dict(entry))
|
2020-09-11 12:19:21 +02:00
|
|
|
else:
|
|
|
|
typ = entry
|
|
|
|
|
|
|
|
zeroconf.setdefault(typ, []).append(data)
|
2020-08-05 15:50:56 +02:00
|
|
|
|
|
|
|
return zeroconf
|
|
|
|
|
|
|
|
|
2022-07-09 01:55:31 +02:00
|
|
|
async def async_get_bluetooth(hass: HomeAssistant) -> list[BluetoothMatcher]:
|
|
|
|
"""Return cached list of bluetooth types."""
|
|
|
|
bluetooth = cast(list[BluetoothMatcher], BLUETOOTH.copy())
|
|
|
|
|
|
|
|
integrations = await async_get_custom_components(hass)
|
|
|
|
for integration in integrations.values():
|
|
|
|
if not integration.bluetooth:
|
|
|
|
continue
|
|
|
|
for entry in integration.bluetooth:
|
|
|
|
bluetooth.append(
|
|
|
|
cast(BluetoothMatcher, {"domain": integration.domain, **entry})
|
|
|
|
)
|
|
|
|
|
|
|
|
return bluetooth
|
|
|
|
|
|
|
|
|
2022-03-01 05:49:44 +01:00
|
|
|
async def async_get_dhcp(hass: HomeAssistant) -> list[DHCPMatcher]:
|
2021-01-14 09:09:08 +01:00
|
|
|
"""Return cached list of dhcp types."""
|
2022-03-01 05:49:44 +01:00
|
|
|
dhcp = cast(list[DHCPMatcher], DHCP.copy())
|
2021-01-14 09:09:08 +01:00
|
|
|
|
|
|
|
integrations = await async_get_custom_components(hass)
|
|
|
|
for integration in integrations.values():
|
|
|
|
if not integration.dhcp:
|
|
|
|
continue
|
|
|
|
for entry in integration.dhcp:
|
2022-03-01 05:49:44 +01:00
|
|
|
dhcp.append(cast(DHCPMatcher, {"domain": integration.domain, **entry}))
|
2021-01-14 09:09:08 +01:00
|
|
|
|
|
|
|
return dhcp
|
|
|
|
|
|
|
|
|
2022-08-23 03:45:08 +02:00
|
|
|
async def async_get_usb(hass: HomeAssistant) -> list[USBMatcher]:
|
2021-08-20 21:04:18 +02:00
|
|
|
"""Return cached list of usb types."""
|
2022-08-23 03:45:08 +02:00
|
|
|
usb = cast(list[USBMatcher], USB.copy())
|
2021-08-20 21:04:18 +02:00
|
|
|
|
|
|
|
integrations = await async_get_custom_components(hass)
|
|
|
|
for integration in integrations.values():
|
|
|
|
if not integration.usb:
|
|
|
|
continue
|
|
|
|
for entry in integration.usb:
|
2021-08-21 21:56:49 +02:00
|
|
|
usb.append(
|
2022-08-23 03:45:08 +02:00
|
|
|
cast(
|
|
|
|
USBMatcher,
|
|
|
|
{
|
|
|
|
"domain": integration.domain,
|
|
|
|
**{k: v for k, v in entry.items() if k != "known_devices"},
|
|
|
|
},
|
|
|
|
)
|
2021-08-21 21:56:49 +02:00
|
|
|
)
|
2021-08-20 21:04:18 +02:00
|
|
|
|
|
|
|
return usb
|
|
|
|
|
|
|
|
|
2021-03-17 17:34:55 +01:00
|
|
|
async def async_get_homekit(hass: HomeAssistant) -> dict[str, str]:
|
2020-08-05 15:50:56 +02:00
|
|
|
"""Return cached list of homekit models."""
|
|
|
|
|
2021-03-17 17:34:55 +01:00
|
|
|
homekit: dict[str, str] = HOMEKIT.copy()
|
2020-08-05 15:50:56 +02:00
|
|
|
|
|
|
|
integrations = await async_get_custom_components(hass)
|
|
|
|
for integration in integrations.values():
|
|
|
|
if (
|
|
|
|
not integration.homekit
|
|
|
|
or "models" not in integration.homekit
|
|
|
|
or not integration.homekit["models"]
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
for model in integration.homekit["models"]:
|
|
|
|
homekit[model] = integration.domain
|
|
|
|
|
|
|
|
return homekit
|
|
|
|
|
|
|
|
|
2021-03-17 17:34:55 +01:00
|
|
|
async def async_get_ssdp(hass: HomeAssistant) -> dict[str, list[dict[str, str]]]:
|
2020-08-05 15:50:56 +02:00
|
|
|
"""Return cached list of ssdp mappings."""
|
|
|
|
|
2021-03-17 17:34:55 +01:00
|
|
|
ssdp: dict[str, list[dict[str, str]]] = SSDP.copy()
|
2020-08-05 15:50:56 +02:00
|
|
|
|
|
|
|
integrations = await async_get_custom_components(hass)
|
|
|
|
for integration in integrations.values():
|
|
|
|
if not integration.ssdp:
|
|
|
|
continue
|
|
|
|
|
|
|
|
ssdp[integration.domain] = integration.ssdp
|
|
|
|
|
|
|
|
return ssdp
|
|
|
|
|
|
|
|
|
2021-03-17 17:34:55 +01:00
|
|
|
async def async_get_mqtt(hass: HomeAssistant) -> dict[str, list[str]]:
|
2020-10-07 18:30:51 +02:00
|
|
|
"""Return cached list of MQTT mappings."""
|
|
|
|
|
2021-03-17 17:34:55 +01:00
|
|
|
mqtt: dict[str, list[str]] = MQTT.copy()
|
2020-10-07 18:30:51 +02:00
|
|
|
|
|
|
|
integrations = await async_get_custom_components(hass)
|
|
|
|
for integration in integrations.values():
|
|
|
|
if not integration.mqtt:
|
|
|
|
continue
|
|
|
|
|
|
|
|
mqtt[integration.domain] = integration.mqtt
|
|
|
|
|
|
|
|
return mqtt
|
|
|
|
|
|
|
|
|
2019-04-09 18:30:32 +02:00
|
|
|
class Integration:
|
|
|
|
"""An integration in Home Assistant."""
|
|
|
|
|
|
|
|
@classmethod
|
2019-07-31 21:25:30 +02:00
|
|
|
def resolve_from_root(
|
2021-03-18 22:58:19 +01:00
|
|
|
cls, hass: HomeAssistant, root_module: ModuleType, domain: str
|
2021-03-17 17:34:55 +01:00
|
|
|
) -> Integration | None:
|
2019-04-09 18:30:32 +02:00
|
|
|
"""Resolve an integration from a root module."""
|
2021-12-27 17:55:17 +01:00
|
|
|
for base in root_module.__path__:
|
2019-07-31 21:25:30 +02:00
|
|
|
manifest_path = pathlib.Path(base) / domain / "manifest.json"
|
2019-04-09 18:30:32 +02:00
|
|
|
|
|
|
|
if not manifest_path.is_file():
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2022-06-28 09:34:56 +02:00
|
|
|
manifest = json_loads(manifest_path.read_text())
|
|
|
|
except JSON_DECODE_EXCEPTIONS as err:
|
2019-07-31 21:25:30 +02:00
|
|
|
_LOGGER.error(
|
|
|
|
"Error parsing manifest.json file at %s: %s", manifest_path, err
|
|
|
|
)
|
2019-04-09 18:30:32 +02:00
|
|
|
continue
|
|
|
|
|
2021-06-01 00:32:03 +02:00
|
|
|
integration = cls(
|
2021-05-17 15:48:41 +02:00
|
|
|
hass,
|
|
|
|
f"{root_module.__name__}.{domain}",
|
|
|
|
manifest_path.parent,
|
|
|
|
manifest,
|
2019-04-09 18:30:32 +02:00
|
|
|
)
|
|
|
|
|
2021-06-01 00:32:03 +02:00
|
|
|
if integration.is_built_in:
|
|
|
|
return integration
|
|
|
|
|
|
|
|
_LOGGER.warning(CUSTOM_WARNING, integration.domain)
|
2021-09-11 20:40:46 +02:00
|
|
|
if integration.version is None:
|
|
|
|
_LOGGER.error(
|
2022-12-22 10:12:50 +01:00
|
|
|
(
|
|
|
|
"The custom integration '%s' does not have a version key in the"
|
|
|
|
" manifest file and was blocked from loading. See"
|
|
|
|
" https://developers.home-assistant.io/blog/2021/01/29/custom-integration-changes#versions"
|
|
|
|
" for more details"
|
|
|
|
),
|
2021-09-11 20:40:46 +02:00
|
|
|
integration.domain,
|
|
|
|
)
|
|
|
|
return None
|
2021-06-01 00:32:03 +02:00
|
|
|
try:
|
|
|
|
AwesomeVersion(
|
|
|
|
integration.version,
|
2022-08-28 20:52:23 +02:00
|
|
|
ensure_strategy=[
|
2021-06-01 00:32:03 +02:00
|
|
|
AwesomeVersionStrategy.CALVER,
|
|
|
|
AwesomeVersionStrategy.SEMVER,
|
|
|
|
AwesomeVersionStrategy.SIMPLEVER,
|
|
|
|
AwesomeVersionStrategy.BUILDVER,
|
|
|
|
AwesomeVersionStrategy.PEP440,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
except AwesomeVersionException:
|
|
|
|
_LOGGER.error(
|
2022-12-22 10:12:50 +01:00
|
|
|
(
|
|
|
|
"The custom integration '%s' does not have a valid version key"
|
|
|
|
" (%s) in the manifest file and was blocked from loading. See"
|
|
|
|
" https://developers.home-assistant.io/blog/2021/01/29/custom-integration-changes#versions"
|
|
|
|
" for more details"
|
|
|
|
),
|
2021-06-01 00:32:03 +02:00
|
|
|
integration.domain,
|
|
|
|
integration.version,
|
|
|
|
)
|
|
|
|
return None
|
|
|
|
return integration
|
|
|
|
|
2019-04-09 18:30:32 +02:00
|
|
|
return None
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-03-18 22:58:19 +01:00
|
|
|
hass: HomeAssistant,
|
2019-07-31 21:25:30 +02:00
|
|
|
pkg_path: str,
|
|
|
|
file_path: pathlib.Path,
|
2021-01-18 22:23:25 +01:00
|
|
|
manifest: Manifest,
|
2021-05-20 17:53:29 +02:00
|
|
|
) -> None:
|
2019-04-09 18:30:32 +02:00
|
|
|
"""Initialize an integration."""
|
|
|
|
self.hass = hass
|
|
|
|
self.pkg_path = pkg_path
|
2019-04-12 19:09:17 +02:00
|
|
|
self.file_path = file_path
|
2019-12-10 09:24:49 +01:00
|
|
|
self.manifest = manifest
|
2020-04-15 22:36:16 +02:00
|
|
|
manifest["is_built_in"] = self.is_built_in
|
2020-06-20 02:24:33 +02:00
|
|
|
|
|
|
|
if self.dependencies:
|
2021-03-17 17:34:55 +01:00
|
|
|
self._all_dependencies_resolved: bool | None = None
|
|
|
|
self._all_dependencies: set[str] | None = None
|
2020-06-20 02:24:33 +02:00
|
|
|
else:
|
|
|
|
self._all_dependencies_resolved = True
|
|
|
|
self._all_dependencies = set()
|
|
|
|
|
2019-04-16 05:38:24 +02:00
|
|
|
_LOGGER.info("Loaded %s from %s", self.domain, pkg_path)
|
2019-04-09 18:30:32 +02:00
|
|
|
|
2019-12-10 09:24:49 +01:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return name."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest["name"]
|
2019-12-10 09:24:49 +01:00
|
|
|
|
2020-08-26 10:20:14 +02:00
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def disabled(self) -> str | None:
|
2020-08-26 10:20:14 +02:00
|
|
|
"""Return reason integration is disabled."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("disabled")
|
2020-08-26 10:20:14 +02:00
|
|
|
|
2019-12-10 09:24:49 +01:00
|
|
|
@property
|
|
|
|
def domain(self) -> str:
|
|
|
|
"""Return domain."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest["domain"]
|
2019-12-10 09:24:49 +01:00
|
|
|
|
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def dependencies(self) -> list[str]:
|
2019-12-10 09:24:49 +01:00
|
|
|
"""Return dependencies."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("dependencies", [])
|
2019-12-10 09:24:49 +01:00
|
|
|
|
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def after_dependencies(self) -> list[str]:
|
2019-12-10 09:24:49 +01:00
|
|
|
"""Return after_dependencies."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("after_dependencies", [])
|
2019-12-10 09:24:49 +01:00
|
|
|
|
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def requirements(self) -> list[str]:
|
2019-12-10 09:24:49 +01:00
|
|
|
"""Return requirements."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("requirements", [])
|
2019-12-10 09:24:49 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def config_flow(self) -> bool:
|
|
|
|
"""Return config_flow."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("config_flow") or False
|
2019-12-10 09:24:49 +01:00
|
|
|
|
2019-12-16 20:16:23 +01:00
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def documentation(self) -> str | None:
|
2019-12-16 20:16:23 +01:00
|
|
|
"""Return documentation."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("documentation")
|
2019-12-16 20:16:23 +01:00
|
|
|
|
2020-05-05 20:00:00 +02:00
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def issue_tracker(self) -> str | None:
|
2020-05-05 20:00:00 +02:00
|
|
|
"""Return issue tracker link."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("issue_tracker")
|
2020-05-05 20:00:00 +02:00
|
|
|
|
2022-01-28 22:37:53 +01:00
|
|
|
@property
|
|
|
|
def loggers(self) -> list[str] | None:
|
|
|
|
"""Return list of loggers used by the integration."""
|
|
|
|
return self.manifest.get("loggers")
|
|
|
|
|
2020-01-07 17:21:56 +01:00
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def quality_scale(self) -> str | None:
|
2020-01-07 17:21:56 +01:00
|
|
|
"""Return Integration Quality Scale."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("quality_scale")
|
2020-01-07 17:21:56 +01:00
|
|
|
|
2021-04-15 10:21:38 +02:00
|
|
|
@property
|
|
|
|
def iot_class(self) -> str | None:
|
|
|
|
"""Return the integration IoT Class."""
|
|
|
|
return self.manifest.get("iot_class")
|
|
|
|
|
2022-03-21 04:38:13 +01:00
|
|
|
@property
|
2022-09-28 14:17:39 +02:00
|
|
|
def integration_type(
|
|
|
|
self,
|
2022-10-19 12:41:43 +02:00
|
|
|
) -> Literal["entity", "device", "hardware", "helper", "hub", "service", "system"]:
|
2022-03-21 04:38:13 +01:00
|
|
|
"""Return the integration type."""
|
2022-10-19 12:41:43 +02:00
|
|
|
return self.manifest.get("integration_type", "hub")
|
2022-03-21 04:38:13 +01:00
|
|
|
|
2020-10-07 18:30:51 +02:00
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def mqtt(self) -> list[str] | None:
|
2020-10-07 18:30:51 +02:00
|
|
|
"""Return Integration MQTT entries."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("mqtt")
|
2020-10-07 18:30:51 +02:00
|
|
|
|
2020-08-05 15:50:56 +02:00
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def ssdp(self) -> list[dict[str, str]] | None:
|
2020-08-05 15:50:56 +02:00
|
|
|
"""Return Integration SSDP entries."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("ssdp")
|
2020-08-05 15:50:56 +02:00
|
|
|
|
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def zeroconf(self) -> list[str | dict[str, str]] | None:
|
2020-08-05 15:50:56 +02:00
|
|
|
"""Return Integration zeroconf entries."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("zeroconf")
|
2020-08-05 15:50:56 +02:00
|
|
|
|
2022-07-09 01:55:31 +02:00
|
|
|
@property
|
|
|
|
def bluetooth(self) -> list[dict[str, str | int]] | None:
|
|
|
|
"""Return Integration bluetooth entries."""
|
|
|
|
return self.manifest.get("bluetooth")
|
|
|
|
|
2021-01-14 09:09:08 +01:00
|
|
|
@property
|
2022-02-15 18:02:52 +01:00
|
|
|
def dhcp(self) -> list[dict[str, str | bool]] | None:
|
2021-01-14 09:09:08 +01:00
|
|
|
"""Return Integration dhcp entries."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("dhcp")
|
2021-01-14 09:09:08 +01:00
|
|
|
|
2021-08-20 21:04:18 +02:00
|
|
|
@property
|
|
|
|
def usb(self) -> list[dict[str, str]] | None:
|
|
|
|
"""Return Integration usb entries."""
|
|
|
|
return self.manifest.get("usb")
|
|
|
|
|
2020-08-05 15:50:56 +02:00
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def homekit(self) -> dict[str, list[str]] | None:
|
2020-08-05 15:50:56 +02:00
|
|
|
"""Return Integration homekit entries."""
|
2021-01-18 22:23:25 +01:00
|
|
|
return self.manifest.get("homekit")
|
2020-08-05 15:50:56 +02:00
|
|
|
|
2019-06-20 22:22:12 +02:00
|
|
|
@property
|
|
|
|
def is_built_in(self) -> bool:
|
|
|
|
"""Test if package is a built-in integration."""
|
|
|
|
return self.pkg_path.startswith(PACKAGE_BUILTIN)
|
|
|
|
|
2021-02-04 09:59:41 +01:00
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def version(self) -> AwesomeVersion | None:
|
2021-02-04 09:59:41 +01:00
|
|
|
"""Return the version of the integration."""
|
|
|
|
if "version" not in self.manifest:
|
|
|
|
return None
|
|
|
|
return AwesomeVersion(self.manifest["version"])
|
|
|
|
|
2020-06-20 02:24:33 +02:00
|
|
|
@property
|
2021-03-17 17:34:55 +01:00
|
|
|
def all_dependencies(self) -> set[str]:
|
2020-06-20 02:24:33 +02:00
|
|
|
"""Return all dependencies including sub-dependencies."""
|
|
|
|
if self._all_dependencies is None:
|
|
|
|
raise RuntimeError("Dependencies not resolved!")
|
|
|
|
|
|
|
|
return self._all_dependencies
|
|
|
|
|
|
|
|
@property
|
|
|
|
def all_dependencies_resolved(self) -> bool:
|
|
|
|
"""Return if all dependencies have been resolved."""
|
|
|
|
return self._all_dependencies_resolved is not None
|
|
|
|
|
|
|
|
async def resolve_dependencies(self) -> bool:
|
|
|
|
"""Resolve all dependencies."""
|
|
|
|
if self._all_dependencies_resolved is not None:
|
|
|
|
return self._all_dependencies_resolved
|
|
|
|
|
|
|
|
try:
|
|
|
|
dependencies = await _async_component_dependencies(
|
|
|
|
self.hass, self.domain, self, set(), set()
|
|
|
|
)
|
|
|
|
dependencies.discard(self.domain)
|
|
|
|
self._all_dependencies = dependencies
|
|
|
|
self._all_dependencies_resolved = True
|
|
|
|
except IntegrationNotFound as err:
|
|
|
|
_LOGGER.error(
|
2022-12-22 10:12:50 +01:00
|
|
|
(
|
|
|
|
"Unable to resolve dependencies for %s: we are unable to resolve"
|
|
|
|
" (sub)dependency %s"
|
|
|
|
),
|
2020-06-20 02:24:33 +02:00
|
|
|
self.domain,
|
|
|
|
err.domain,
|
|
|
|
)
|
|
|
|
self._all_dependencies_resolved = False
|
|
|
|
except CircularDependency as err:
|
|
|
|
_LOGGER.error(
|
2022-12-22 10:12:50 +01:00
|
|
|
(
|
|
|
|
"Unable to resolve dependencies for %s: it contains a circular"
|
|
|
|
" dependency: %s -> %s"
|
|
|
|
),
|
2020-06-20 02:24:33 +02:00
|
|
|
self.domain,
|
|
|
|
err.from_domain,
|
|
|
|
err.to_domain,
|
|
|
|
)
|
|
|
|
self._all_dependencies_resolved = False
|
|
|
|
|
|
|
|
return self._all_dependencies_resolved
|
|
|
|
|
2019-04-11 10:26:36 +02:00
|
|
|
def get_component(self) -> ModuleType:
|
2019-04-09 18:30:32 +02:00
|
|
|
"""Return the component."""
|
2022-01-26 05:39:32 +01:00
|
|
|
cache: dict[str, ModuleType] = self.hass.data.setdefault(DATA_COMPONENTS, {})
|
|
|
|
if self.domain in cache:
|
|
|
|
return cache[self.domain]
|
|
|
|
|
|
|
|
try:
|
2019-04-11 10:26:36 +02:00
|
|
|
cache[self.domain] = importlib.import_module(self.pkg_path)
|
2022-01-26 05:39:32 +01:00
|
|
|
except ImportError:
|
|
|
|
raise
|
|
|
|
except Exception as err:
|
|
|
|
_LOGGER.exception(
|
|
|
|
"Unexpected exception importing component %s", self.pkg_path
|
|
|
|
)
|
|
|
|
raise ImportError(f"Exception importing {self.pkg_path}") from err
|
|
|
|
|
|
|
|
return cache[self.domain]
|
2019-04-09 18:30:32 +02:00
|
|
|
|
2019-04-11 10:26:36 +02:00
|
|
|
def get_platform(self, platform_name: str) -> ModuleType:
|
2019-04-09 18:30:32 +02:00
|
|
|
"""Return a platform for an integration."""
|
2022-01-26 05:39:32 +01:00
|
|
|
cache: dict[str, ModuleType] = self.hass.data.setdefault(DATA_COMPONENTS, {})
|
2019-08-23 18:53:33 +02:00
|
|
|
full_name = f"{self.domain}.{platform_name}"
|
2022-01-26 05:39:32 +01:00
|
|
|
if full_name in cache:
|
|
|
|
return cache[full_name]
|
|
|
|
|
|
|
|
try:
|
2020-10-19 17:09:57 +02:00
|
|
|
cache[full_name] = self._import_platform(platform_name)
|
2022-01-26 05:39:32 +01:00
|
|
|
except ImportError:
|
|
|
|
raise
|
|
|
|
except Exception as err:
|
|
|
|
_LOGGER.exception(
|
|
|
|
"Unexpected exception importing platform %s.%s",
|
|
|
|
self.pkg_path,
|
|
|
|
platform_name,
|
|
|
|
)
|
|
|
|
raise ImportError(
|
|
|
|
f"Exception importing {self.pkg_path}.{platform_name}"
|
|
|
|
) from err
|
|
|
|
|
|
|
|
return cache[full_name]
|
2019-04-09 18:30:32 +02:00
|
|
|
|
2020-10-19 17:09:57 +02:00
|
|
|
def _import_platform(self, platform_name: str) -> ModuleType:
|
|
|
|
"""Import the platform."""
|
|
|
|
return importlib.import_module(f"{self.pkg_path}.{platform_name}")
|
|
|
|
|
2019-04-15 01:59:06 +02:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""Text representation of class."""
|
2019-08-23 18:53:33 +02:00
|
|
|
return f"<Integration {self.domain}: {self.pkg_path}>"
|
2019-04-15 01:59:06 +02:00
|
|
|
|
2019-04-09 18:30:32 +02:00
|
|
|
|
2022-07-14 22:06:08 +02:00
|
|
|
def _resolve_integrations_from_root(
|
|
|
|
hass: HomeAssistant, root_module: ModuleType, domains: list[str]
|
|
|
|
) -> dict[str, Integration]:
|
|
|
|
"""Resolve multiple integrations from root."""
|
|
|
|
integrations: dict[str, Integration] = {}
|
|
|
|
for domain in domains:
|
|
|
|
try:
|
|
|
|
integration = Integration.resolve_from_root(hass, root_module, domain)
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Error loading integration: %s", domain)
|
|
|
|
else:
|
|
|
|
if integration:
|
|
|
|
integrations[domain] = integration
|
|
|
|
return integrations
|
|
|
|
|
|
|
|
|
2021-03-18 22:58:19 +01:00
|
|
|
async def async_get_integration(hass: HomeAssistant, domain: str) -> Integration:
|
2022-07-14 22:06:08 +02:00
|
|
|
"""Get integration."""
|
|
|
|
integrations_or_excs = await async_get_integrations(hass, [domain])
|
|
|
|
int_or_exc = integrations_or_excs[domain]
|
|
|
|
if isinstance(int_or_exc, Integration):
|
|
|
|
return int_or_exc
|
|
|
|
raise int_or_exc
|
|
|
|
|
|
|
|
|
|
|
|
async def async_get_integrations(
|
|
|
|
hass: HomeAssistant, domains: Iterable[str]
|
|
|
|
) -> dict[str, Integration | Exception]:
|
|
|
|
"""Get integrations."""
|
2021-10-17 20:08:11 +02:00
|
|
|
if (cache := hass.data.get(DATA_INTEGRATIONS)) is None:
|
2019-04-09 18:30:32 +02:00
|
|
|
if not _async_mount_config_dir(hass):
|
2022-07-14 22:06:08 +02:00
|
|
|
return {domain: IntegrationNotFound(domain) for domain in domains}
|
2019-04-09 18:30:32 +02:00
|
|
|
cache = hass.data[DATA_INTEGRATIONS] = {}
|
|
|
|
|
2022-07-14 22:06:08 +02:00
|
|
|
results: dict[str, Integration | Exception] = {}
|
|
|
|
needed: dict[str, asyncio.Event] = {}
|
|
|
|
in_progress: dict[str, asyncio.Event] = {}
|
|
|
|
for domain in domains:
|
|
|
|
int_or_evt: Integration | asyncio.Event | None = cache.get(domain, _UNDEF)
|
|
|
|
if isinstance(int_or_evt, asyncio.Event):
|
|
|
|
in_progress[domain] = int_or_evt
|
|
|
|
elif int_or_evt is not _UNDEF:
|
|
|
|
results[domain] = cast(Integration, int_or_evt)
|
|
|
|
elif "." in domain:
|
|
|
|
results[domain] = ValueError(f"Invalid domain {domain}")
|
|
|
|
else:
|
|
|
|
needed[domain] = cache[domain] = asyncio.Event()
|
|
|
|
|
|
|
|
if in_progress:
|
|
|
|
await asyncio.gather(*[event.wait() for event in in_progress.values()])
|
|
|
|
for domain in in_progress:
|
|
|
|
# When we have waited and it's _UNDEF, it doesn't exist
|
|
|
|
# We don't cache that it doesn't exist, or else people can't fix it
|
|
|
|
# and then restart, because their config will never be valid.
|
|
|
|
if (int_or_evt := cache.get(domain, _UNDEF)) is _UNDEF:
|
|
|
|
results[domain] = IntegrationNotFound(domain)
|
|
|
|
else:
|
|
|
|
results[domain] = cast(Integration, int_or_evt)
|
|
|
|
|
|
|
|
# First we look for custom components
|
|
|
|
if needed:
|
|
|
|
# Instead of using resolve_from_root we use the cache of custom
|
|
|
|
# components to find the integration.
|
|
|
|
custom = await async_get_custom_components(hass)
|
|
|
|
for domain, event in needed.items():
|
|
|
|
if integration := custom.get(domain):
|
|
|
|
results[domain] = cache[domain] = integration
|
|
|
|
event.set()
|
|
|
|
|
|
|
|
for domain in results:
|
|
|
|
if domain in needed:
|
|
|
|
del needed[domain]
|
|
|
|
|
|
|
|
# Now the rest use resolve_from_root
|
|
|
|
if needed:
|
|
|
|
from . import components # pylint: disable=import-outside-toplevel
|
|
|
|
|
|
|
|
integrations = await hass.async_add_executor_job(
|
|
|
|
_resolve_integrations_from_root, hass, components, list(needed)
|
|
|
|
)
|
|
|
|
for domain, event in needed.items():
|
|
|
|
int_or_exc = integrations.get(domain)
|
|
|
|
if not int_or_exc:
|
|
|
|
cache.pop(domain)
|
|
|
|
results[domain] = IntegrationNotFound(domain)
|
|
|
|
elif isinstance(int_or_exc, Exception):
|
|
|
|
cache.pop(domain)
|
|
|
|
exc = IntegrationNotFound(domain)
|
|
|
|
exc.__cause__ = int_or_exc
|
|
|
|
results[domain] = exc
|
|
|
|
else:
|
|
|
|
results[domain] = cache[domain] = int_or_exc
|
|
|
|
event.set()
|
2021-05-17 15:48:41 +02:00
|
|
|
|
2022-07-14 22:06:08 +02:00
|
|
|
return results
|
2016-10-27 09:16:23 +02:00
|
|
|
|
2014-11-05 16:56:36 +01:00
|
|
|
|
2019-02-07 22:56:40 +01:00
|
|
|
class LoaderError(Exception):
|
|
|
|
"""Loader base error."""
|
|
|
|
|
|
|
|
|
2019-04-09 18:30:32 +02:00
|
|
|
class IntegrationNotFound(LoaderError):
|
2019-02-07 22:56:40 +01:00
|
|
|
"""Raised when a component is not found."""
|
|
|
|
|
|
|
|
def __init__(self, domain: str) -> None:
|
|
|
|
"""Initialize a component not found error."""
|
2019-09-27 17:48:48 +02:00
|
|
|
super().__init__(f"Integration '{domain}' not found.")
|
2019-02-07 22:56:40 +01:00
|
|
|
self.domain = domain
|
|
|
|
|
|
|
|
|
|
|
|
class CircularDependency(LoaderError):
|
|
|
|
"""Raised when a circular dependency is found when resolving components."""
|
|
|
|
|
|
|
|
def __init__(self, from_domain: str, to_domain: str) -> None:
|
|
|
|
"""Initialize circular dependency error."""
|
2019-08-23 18:53:33 +02:00
|
|
|
super().__init__(f"Circular dependency detected: {from_domain} -> {to_domain}.")
|
2019-02-07 22:56:40 +01:00
|
|
|
self.from_domain = from_domain
|
|
|
|
self.to_domain = to_domain
|
|
|
|
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
def _load_file(
|
2021-03-18 22:58:19 +01:00
|
|
|
hass: HomeAssistant, comp_or_platform: str, base_paths: list[str]
|
2021-03-17 17:34:55 +01:00
|
|
|
) -> ModuleType | None:
|
2019-01-11 20:30:22 +01:00
|
|
|
"""Try to load specified file.
|
|
|
|
|
2018-05-07 11:25:48 +02:00
|
|
|
Looks in config dir first, then built-in components.
|
|
|
|
Only returns it if also found to be valid.
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2021-03-23 14:36:43 +01:00
|
|
|
with suppress(KeyError):
|
2022-02-18 08:09:22 +01:00
|
|
|
return hass.data[DATA_COMPONENTS][comp_or_platform] # type: ignore[no-any-return]
|
2016-10-27 09:16:23 +02:00
|
|
|
|
2021-10-17 20:08:11 +02:00
|
|
|
if (cache := hass.data.get(DATA_COMPONENTS)) is None:
|
2019-04-09 18:30:32 +02:00
|
|
|
if not _async_mount_config_dir(hass):
|
2018-07-23 10:24:39 +02:00
|
|
|
return None
|
2019-04-15 04:07:05 +02:00
|
|
|
cache = hass.data[DATA_COMPONENTS] = {}
|
2014-11-12 06:39:17 +01:00
|
|
|
|
2019-08-23 18:53:33 +02:00
|
|
|
for path in (f"{base}.{comp_or_platform}" for base in base_paths):
|
2018-05-01 20:57:30 +02:00
|
|
|
try:
|
2018-05-07 11:25:48 +02:00
|
|
|
module = importlib.import_module(path)
|
2014-11-15 08:17:18 +01:00
|
|
|
|
2018-05-07 11:25:48 +02:00
|
|
|
# In Python 3 you can import files from directories that do not
|
|
|
|
# contain the file __init__.py. A directory is a valid module if
|
|
|
|
# it contains a file with the .py extension. In this case Python
|
|
|
|
# will succeed in importing the directory as a module and call it
|
|
|
|
# a namespace. We do not care about namespaces.
|
|
|
|
# This prevents that when only
|
|
|
|
# custom_components/switch/some_platform.py exists,
|
|
|
|
# the import custom_components.switch would succeed.
|
2018-07-07 16:48:02 +02:00
|
|
|
# __file__ was unset for namespaces before Python 3.7
|
2019-07-31 21:25:30 +02:00
|
|
|
if getattr(module, "__file__", None) is None:
|
2018-05-07 11:25:48 +02:00
|
|
|
continue
|
2014-11-05 08:34:19 +01:00
|
|
|
|
2018-05-07 11:25:48 +02:00
|
|
|
cache[comp_or_platform] = module
|
2018-05-01 20:57:30 +02:00
|
|
|
|
2018-05-07 11:25:48 +02:00
|
|
|
return module
|
2018-05-01 20:57:30 +02:00
|
|
|
|
2018-05-07 11:25:48 +02:00
|
|
|
except ImportError as err:
|
|
|
|
# This error happens if for example custom_components/switch
|
|
|
|
# exists and we try to load switch.demo.
|
2018-05-07 19:12:12 +02:00
|
|
|
# Ignore errors for custom_components, custom_components.switch
|
|
|
|
# and custom_components.switch.demo.
|
|
|
|
white_listed_errors = []
|
|
|
|
parts = []
|
2019-07-31 21:25:30 +02:00
|
|
|
for part in path.split("."):
|
2018-05-07 19:12:12 +02:00
|
|
|
parts.append(part)
|
2020-04-12 17:18:09 +02:00
|
|
|
white_listed_errors.append(f"No module named '{'.'.join(parts)}'")
|
2018-05-07 19:12:12 +02:00
|
|
|
|
|
|
|
if str(err) not in white_listed_errors:
|
2018-05-07 11:25:48 +02:00
|
|
|
_LOGGER.exception(
|
2022-12-22 10:12:50 +01:00
|
|
|
"Error loading %s. Make sure all dependencies are installed", path
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2018-05-01 20:57:30 +02:00
|
|
|
|
2018-05-07 11:25:48 +02:00
|
|
|
return None
|
2014-11-29 00:34:42 +01:00
|
|
|
|
|
|
|
|
2018-07-23 10:24:39 +02:00
|
|
|
class ModuleWrapper:
|
|
|
|
"""Class to wrap a Python module and auto fill in hass argument."""
|
|
|
|
|
2021-03-18 22:58:19 +01:00
|
|
|
def __init__(self, hass: HomeAssistant, module: ModuleType) -> None:
|
2018-07-23 10:24:39 +02:00
|
|
|
"""Initialize the module wrapper."""
|
|
|
|
self._hass = hass
|
|
|
|
self._module = module
|
|
|
|
|
|
|
|
def __getattr__(self, attr: str) -> Any:
|
|
|
|
"""Fetch an attribute."""
|
|
|
|
value = getattr(self._module, attr)
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
if hasattr(value, "__bind_hass"):
|
2018-07-23 10:24:39 +02:00
|
|
|
value = ft.partial(value, self._hass)
|
|
|
|
|
|
|
|
setattr(self, attr, value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2017-07-16 18:23:06 +02:00
|
|
|
class Components:
|
|
|
|
"""Helper to load components."""
|
|
|
|
|
2021-02-12 10:58:20 +01:00
|
|
|
def __init__(self, hass: HomeAssistant) -> None:
|
2017-07-16 18:23:06 +02:00
|
|
|
"""Initialize the Components class."""
|
|
|
|
self._hass = hass
|
|
|
|
|
2018-07-23 10:24:39 +02:00
|
|
|
def __getattr__(self, comp_name: str) -> ModuleWrapper:
|
2017-07-16 18:23:06 +02:00
|
|
|
"""Fetch a component."""
|
2019-04-15 04:07:05 +02:00
|
|
|
# Test integration cache
|
|
|
|
integration = self._hass.data.get(DATA_INTEGRATIONS, {}).get(comp_name)
|
|
|
|
|
2019-04-16 05:38:24 +02:00
|
|
|
if isinstance(integration, Integration):
|
2021-03-17 17:34:55 +01:00
|
|
|
component: ModuleType | None = integration.get_component()
|
2019-04-15 04:07:05 +02:00
|
|
|
else:
|
|
|
|
# Fallback to importing old-school
|
2020-02-18 20:52:38 +01:00
|
|
|
component = _load_file(self._hass, comp_name, _lookup_path(self._hass))
|
2019-04-15 04:07:05 +02:00
|
|
|
|
2017-07-16 18:23:06 +02:00
|
|
|
if component is None:
|
2019-08-23 18:53:33 +02:00
|
|
|
raise ImportError(f"Unable to load {comp_name}")
|
2019-04-15 04:07:05 +02:00
|
|
|
|
2017-10-08 17:17:54 +02:00
|
|
|
wrapped = ModuleWrapper(self._hass, component)
|
2017-07-16 18:23:06 +02:00
|
|
|
setattr(self, comp_name, wrapped)
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
2017-10-08 17:17:54 +02:00
|
|
|
class Helpers:
|
|
|
|
"""Helper to load helpers."""
|
2017-07-16 18:23:06 +02:00
|
|
|
|
2021-02-12 10:58:20 +01:00
|
|
|
def __init__(self, hass: HomeAssistant) -> None:
|
2017-10-08 17:17:54 +02:00
|
|
|
"""Initialize the Helpers class."""
|
|
|
|
self._hass = hass
|
|
|
|
|
2018-07-23 10:24:39 +02:00
|
|
|
def __getattr__(self, helper_name: str) -> ModuleWrapper:
|
2017-10-08 17:17:54 +02:00
|
|
|
"""Fetch a helper."""
|
2019-08-23 18:53:33 +02:00
|
|
|
helper = importlib.import_module(f"homeassistant.helpers.{helper_name}")
|
2017-10-08 17:17:54 +02:00
|
|
|
wrapped = ModuleWrapper(self._hass, helper)
|
|
|
|
setattr(self, helper_name, wrapped)
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
2022-03-17 18:52:38 +01:00
|
|
|
def bind_hass(func: _CallableT) -> _CallableT:
|
2018-01-21 07:35:38 +01:00
|
|
|
"""Decorate function to indicate that first argument is hass."""
|
2019-07-31 21:25:30 +02:00
|
|
|
setattr(func, "__bind_hass", True)
|
2017-07-16 18:23:06 +02:00
|
|
|
return func
|
|
|
|
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
async def _async_component_dependencies(
|
2021-03-18 22:58:19 +01:00
|
|
|
hass: HomeAssistant,
|
2020-06-20 02:24:33 +02:00
|
|
|
start_domain: str,
|
|
|
|
integration: Integration,
|
2021-03-17 17:34:55 +01:00
|
|
|
loaded: set[str],
|
|
|
|
loading: set[str],
|
|
|
|
) -> set[str]:
|
2019-02-07 22:56:40 +01:00
|
|
|
"""Recursive function to get component dependencies.
|
2016-10-27 09:16:23 +02:00
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2020-06-20 02:24:33 +02:00
|
|
|
domain = integration.domain
|
2019-04-09 18:30:32 +02:00
|
|
|
loading.add(domain)
|
2014-11-29 00:34:42 +01:00
|
|
|
|
2019-04-09 18:30:32 +02:00
|
|
|
for dependency_domain in integration.dependencies:
|
2014-11-29 00:34:42 +01:00
|
|
|
# Check not already loaded
|
2019-04-09 18:30:32 +02:00
|
|
|
if dependency_domain in loaded:
|
2015-08-03 17:05:33 +02:00
|
|
|
continue
|
2014-11-29 00:34:42 +01:00
|
|
|
|
2016-03-08 00:06:04 +01:00
|
|
|
# If we are already loading it, we have a circular dependency.
|
2019-04-09 18:30:32 +02:00
|
|
|
if dependency_domain in loading:
|
|
|
|
raise CircularDependency(domain, dependency_domain)
|
2014-11-29 00:34:42 +01:00
|
|
|
|
2020-06-20 02:24:33 +02:00
|
|
|
loaded.add(dependency_domain)
|
|
|
|
|
|
|
|
dep_integration = await async_get_integration(hass, dependency_domain)
|
|
|
|
|
|
|
|
if start_domain in dep_integration.after_dependencies:
|
|
|
|
raise CircularDependency(start_domain, dependency_domain)
|
|
|
|
|
|
|
|
if dep_integration.dependencies:
|
|
|
|
dep_loaded = await _async_component_dependencies(
|
|
|
|
hass, start_domain, dep_integration, loaded, loading
|
|
|
|
)
|
2014-11-29 00:34:42 +01:00
|
|
|
|
2020-06-20 02:24:33 +02:00
|
|
|
loaded.update(dep_loaded)
|
2014-11-29 00:34:42 +01:00
|
|
|
|
2019-04-09 18:30:32 +02:00
|
|
|
loaded.add(domain)
|
|
|
|
loading.remove(domain)
|
2014-11-29 00:34:42 +01:00
|
|
|
|
2019-02-07 22:56:40 +01:00
|
|
|
return loaded
|
2019-04-09 18:30:32 +02:00
|
|
|
|
|
|
|
|
2021-02-12 10:58:20 +01:00
|
|
|
def _async_mount_config_dir(hass: HomeAssistant) -> bool:
|
2019-04-09 18:30:32 +02:00
|
|
|
"""Mount config dir in order to load custom_component.
|
|
|
|
|
|
|
|
Async friendly but not a coroutine.
|
|
|
|
"""
|
|
|
|
if hass.config.config_dir is None:
|
2020-02-13 17:27:00 +01:00
|
|
|
_LOGGER.error("Can't load integrations - configuration directory is not set")
|
2019-04-09 18:30:32 +02:00
|
|
|
return False
|
|
|
|
if hass.config.config_dir not in sys.path:
|
|
|
|
sys.path.insert(0, hass.config.config_dir)
|
|
|
|
return True
|
2020-02-18 20:52:38 +01:00
|
|
|
|
|
|
|
|
2021-03-17 17:34:55 +01:00
|
|
|
def _lookup_path(hass: HomeAssistant) -> list[str]:
|
2020-02-18 20:52:38 +01:00
|
|
|
"""Return the lookup paths for legacy lookups."""
|
|
|
|
if hass.config.safe_mode:
|
|
|
|
return [PACKAGE_BUILTIN]
|
|
|
|
return [PACKAGE_CUSTOM_COMPONENTS, PACKAGE_BUILTIN]
|