Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions custom_components/spook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
from homeassistant.helpers import issue_registry as ir

from .const import DOMAIN, LOGGER, PLATFORMS
from .entity_filtering import async_setup_all_entity_ids_cache_invalidation
from .integration_linking import link_sub_integrations, unlink_sub_integrations
from .repairs import SpookRepairManager
from .services import SpookServiceManager
from .util import (
async_forward_setup_entry,
async_setup_all_entity_ids_cache_invalidation,
link_sub_integrations,
unlink_sub_integrations,
)
from .setup_helpers import async_forward_setup_entry

if TYPE_CHECKING:
from collections.abc import Callable
Expand Down
2 changes: 1 addition & 1 deletion custom_components/spook/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from homeassistant.const import Platform

from .util import async_forward_platform_entry_setups_to_ectoplasm
from .setup_helpers import async_forward_platform_entry_setups_to_ectoplasm

if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry
Expand Down
2 changes: 1 addition & 1 deletion custom_components/spook/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from homeassistant.const import Platform

from .util import async_forward_platform_entry_setups_to_ectoplasm
from .setup_helpers import async_forward_platform_entry_setups_to_ectoplasm

if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....entity_filtering import async_filter_known_area_ids, async_get_all_area_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_area_ids, async_get_all_area_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....entity_filtering import async_filter_known_device_ids, async_get_all_device_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_device_ids, async_get_all_device_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....repairs import AbstractSpookRepair
from ....util import (
from ....entity_filtering import (
ENTITY_ID_PATTERN,
async_extract_entities_from_config,
async_extract_entities_from_template_string,
async_filter_known_entity_ids_with_templates,
async_get_all_entity_ids,
is_template_string,
)
from ....repairs import AbstractSpookRepair

if TYPE_CHECKING:
from homeassistant.core import HomeAssistant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....entity_filtering import async_filter_known_floor_ids, async_get_all_floor_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_floor_ids, async_get_all_floor_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....entity_filtering import async_filter_known_label_ids, async_get_all_label_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_label_ids, async_get_all_label_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....repairs import AbstractSpookRepair
from ....util import (
from ....entity_filtering import (
async_filter_known_services,
async_find_services_in_sequence,
async_get_all_services,
)
from ....repairs import AbstractSpookRepair


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from homeassistant.helpers.entity_platform import DATA_ENTITY_PLATFORM, EntityPlatform

from ....const import LOGGER
from ....entity_filtering import async_filter_known_entity_ids, async_get_all_entity_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_entity_ids, async_get_all_entity_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from homeassistant.helpers.entity_platform import DATA_ENTITY_PLATFORM, EntityPlatform

from ....const import LOGGER
from ....entity_filtering import async_get_all_entity_ids
from ....repairs import AbstractSpookRepair
from ....util import async_get_all_entity_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from homeassistant.helpers import entity_registry as er

from ....const import LOGGER
from ....entity_filtering import async_filter_known_entity_ids, async_get_all_entity_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_entity_ids, async_get_all_entity_ids

if TYPE_CHECKING:
from homeassistant.components.lovelace.dashboard import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from homeassistant.helpers import entity_registry as er

from ....const import LOGGER
from ....entity_filtering import async_filter_known_entity_ids, async_get_all_entity_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_entity_ids, async_get_all_entity_ids

if TYPE_CHECKING:
from homeassistant.components.proximity.coordinator import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from homeassistant.helpers import entity_registry as er

from ....const import LOGGER
from ....entity_filtering import async_filter_known_entity_ids, async_get_all_entity_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_entity_ids, async_get_all_entity_ids

if TYPE_CHECKING:
from homeassistant.components.proximity.coordinator import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from homeassistant.helpers import entity_registry as er

from ....const import LOGGER
from ....entity_filtering import async_get_all_entity_ids
from ....repairs import AbstractSpookRepair
from ....util import async_get_all_entity_ids

if TYPE_CHECKING:
from homeassistant.components.proximity.coordinator import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from homeassistant.helpers import entity_registry as er

from ....const import LOGGER
from ....entity_filtering import async_filter_known_entity_ids, async_get_all_entity_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_entity_ids, async_get_all_entity_ids

if TYPE_CHECKING:
from homeassistant.components.homeassistant import scene
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....entity_filtering import async_filter_known_area_ids, async_get_all_area_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_area_ids, async_get_all_area_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....entity_filtering import async_filter_known_device_ids, async_get_all_device_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_device_ids, async_get_all_device_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....repairs import AbstractSpookRepair
from ....util import (
from ....entity_filtering import (
async_extract_entities_from_config,
async_filter_known_entity_ids_with_templates,
async_get_all_entity_ids,
)
from ....repairs import AbstractSpookRepair

if TYPE_CHECKING:
from homeassistant.core import HomeAssistant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....entity_filtering import async_filter_known_floor_ids, async_get_all_floor_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_floor_ids, async_get_all_floor_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent

from ....const import LOGGER
from ....entity_filtering import async_filter_known_label_ids, async_get_all_label_ids
from ....repairs import AbstractSpookRepair
from ....util import async_filter_known_label_ids, async_get_all_label_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from homeassistant.helpers.entity_platform import DATA_ENTITY_PLATFORM, EntityPlatform

from ....const import LOGGER
from ....entity_filtering import async_get_all_entity_ids
from ....repairs import AbstractSpookRepair
from ....util import async_get_all_entity_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from homeassistant.helpers.entity_platform import DATA_ENTITY_PLATFORM, EntityPlatform

from ....const import LOGGER
from ....entity_filtering import async_get_all_entity_ids
from ....repairs import AbstractSpookRepair
from ....util import async_get_all_entity_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from homeassistant.helpers.entity_platform import DATA_ENTITY_PLATFORM, EntityPlatform

from ....const import LOGGER
from ....entity_filtering import async_get_all_entity_ids
from ....repairs import AbstractSpookRepair
from ....util import async_get_all_entity_ids


class SpookRepair(AbstractSpookRepair):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
"""Spook - Your homie."""
"""Spook - Your homie. Entity, registry, and template filtering helpers."""

from __future__ import annotations

import asyncio
import importlib
from pathlib import Path
import re
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -38,15 +35,12 @@
)
from homeassistant.helpers.template import Template

from .const import DOMAIN, LOGGER
from .const import LOGGER

if TYPE_CHECKING:
from collections.abc import Callable, Iterable, Sequence
from types import ModuleType

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback


# Entity domains to ignore when filtering unknown entities
Expand Down Expand Up @@ -223,98 +217,6 @@
return _CACHED_ALL_ENTITY_IDS.copy()


async def async_forward_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
) -> None:
"""Set up Spook ectoplasms."""
LOGGER.debug("Setting up Spook ectoplasms")

modules: list[ModuleType] = []

def _load_all_ectoplasm_modules() -> None:
"""Load all Spook ectoplasm modules."""
for module_file in Path(__file__).parent.rglob("ectoplasms/*/__init__.py"):
module_path = str(module_file.relative_to(Path(__file__).parent))[
:-3
].replace(
"/",
".",
)
LOGGER.debug("Loading Spook ectoplasm: %s", module_path)
module = importlib.import_module(f".{module_path}", __package__)
if hasattr(module, "async_setup_entry"):
modules.append(module)
LOGGER.debug("Setting up Spook ectoplasm: %s", module_path)

await hass.async_add_import_executor_job(_load_all_ectoplasm_modules)
await asyncio.gather(*(module.async_setup_entry(hass, entry) for module in modules))


async def async_forward_platform_entry_setups_to_ectoplasm(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
platform: Platform,
) -> None:
"""Set up Spook ectoplasm platform."""
LOGGER.debug("Setting up Spook ectoplasm platform: %s", platform)

modules: list[ModuleType] = []

def _load_all_ectoplasm_platform_modules() -> None:
"""Load all Spook ectoplasm platform modules."""
for module_file in Path(__file__).parent.rglob(f"ectoplasms/*/{platform}.py"):
module_path = str(module_file.relative_to(Path(__file__).parent))[
:-3
].replace(
"/",
".",
)
LOGGER.debug("Loading Spook %s from ectoplasm: %s", platform, module_path)
modules.append(importlib.import_module(f".{module_path}", __package__))
LOGGER.debug("Setting up Spook ectoplasm %s: %s", platform, module_path)

await hass.async_add_import_executor_job(_load_all_ectoplasm_platform_modules)
await asyncio.gather(
*(
module.async_setup_entry(hass, entry, async_add_entities)
for module in modules
)
)


def link_sub_integrations(hass: HomeAssistant) -> bool:
"""Link Spook sub integrations."""
LOGGER.debug("Linking up Spook sub integrations")

changes = False
for manifest in Path(__file__).parent.rglob("integrations/*/manifest.json"):
LOGGER.debug("Linking Spook sub integration: %s", manifest.parent.name)
dest = Path(hass.config.config_dir) / "custom_components" / manifest.parent.name
if not dest.exists():
src = (
Path(hass.config.config_dir)
/ "custom_components"
/ DOMAIN
/ "integrations"
/ manifest.parent.name
)
dest.symlink_to(src)
changes = True
return changes


def unlink_sub_integrations(hass: HomeAssistant) -> None:
"""Unlink Spook sub integrations."""
LOGGER.debug("Unlinking Spook sub integrations")
for manifest in Path(__file__).parent.rglob("integrations/*/manifest.json"):
LOGGER.debug("Unlinking Spook sub integration: %s", manifest.parent.name)
dest = Path(hass.config.config_dir) / "custom_components" / manifest.parent.name
if dest.exists():
dest.unlink()


@callback
def async_get_all_area_ids(hass: HomeAssistant) -> set[str]:
"""Return all area IDs, known to Home Assistant."""
Expand Down Expand Up @@ -468,7 +370,7 @@
return ("{{" in value and "}}" in value) or ("{%" in value and "%}" in value)


async def async_extract_entities_from_template_string(

Check warning on line 373 in custom_components/spook/entity_filtering.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use asynchronous features in this function or remove the `async` keyword.

See more on https://sonarcloud.io/project/issues?id=frenck_spook&issues=AZ5G4Wbr2HoFXsIjnyyx&open=AZ5G4Wbr2HoFXsIjnyyx&pullRequest=1256
hass: HomeAssistant, template_str: str
) -> set[str]:
"""Extract entity IDs from a template string using regex analysis.
Expand Down Expand Up @@ -496,7 +398,7 @@
return entities


def extract_entities_from_template_regex(

Check failure on line 401 in custom_components/spook/entity_filtering.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=frenck_spook&issues=AZ5G4Wbr2HoFXsIjnyyy&open=AZ5G4Wbr2HoFXsIjnyyy&pullRequest=1256
hass: HomeAssistant, template_str: str
) -> set[str]:
"""Extract entity IDs from template string using regex patterns.
Expand Down Expand Up @@ -532,7 +434,7 @@
return entities - known_services


async def _process_template_object(

Check warning on line 437 in custom_components/spook/entity_filtering.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use asynchronous features in this function or remove the `async` keyword.

See more on https://sonarcloud.io/project/issues?id=frenck_spook&issues=AZ5G4Wbr2HoFXsIjnyyz&open=AZ5G4Wbr2HoFXsIjnyyz&pullRequest=1256
hass: HomeAssistant,
template: Template,
known_entity_ids: set[str],
Expand Down Expand Up @@ -580,7 +482,7 @@
unknown_entities.add(entity_id)


async def async_filter_known_entity_ids_with_templates(

Check failure on line 485 in custom_components/spook/entity_filtering.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=frenck_spook&issues=AZ5G4Wbr2HoFXsIjnyy0&open=AZ5G4Wbr2HoFXsIjnyy0&pullRequest=1256
hass: HomeAssistant,
entity_ids: Iterable[str],
known_entity_ids: set[str] | None = None,
Expand Down Expand Up @@ -697,7 +599,7 @@


@callback
def async_find_services_in_sequence( # noqa: C901

Check failure on line 602 in custom_components/spook/entity_filtering.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 27 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=frenck_spook&issues=AZ5G4Wbr2HoFXsIjnyy1&open=AZ5G4Wbr2HoFXsIjnyy1&pullRequest=1256
sequence: Sequence[dict[str, Any]],
) -> set[str]:
"""Find all services called in a sequence."""
Expand Down
Loading
Loading