diff --git a/custom_components/powercalc/discovery.py b/custom_components/powercalc/discovery.py index 2a50c7c3a..90fa6af4a 100644 --- a/custom_components/powercalc/discovery.py +++ b/custom_components/powercalc/discovery.py @@ -32,7 +32,6 @@ MANUFACTURER_WLED, CalculationStrategy, ) -from .errors import ModelNotSupportedError from .group_include.filter import CategoryFilter, CompositeFilter, FilterOperator, LambdaFilter, NotFilter, get_filtered_entity_list from .helpers import get_or_create_unique_id from .power_profile.factory import get_power_profile @@ -206,10 +205,8 @@ async def find_power_profiles( profile = await get_power_profile(self.hass, {}, model_info=model_info, process_variables=False) if not profile or profile.discovery_by != discovery_type: # pragma: no cover continue - if discovery_type == DiscoveryBy.ENTITY and not await self.is_entity_supported( + if discovery_type == DiscoveryBy.ENTITY and not profile.is_entity_domain_supported( source_entity.entity_entry, # type: ignore[arg-type] - model_info, - profile, ): continue power_profiles.append(profile) @@ -247,32 +244,6 @@ def is_wled_light(model_info: ModelInfo, entity_entry: er.RegistryEntry) -> bool and not re.search("master|segment", str(entity_entry.entity_id), flags=re.IGNORECASE) ) - async def is_entity_supported( - self, - entity_entry: er.RegistryEntry, - model_info: ModelInfo | None = None, - power_profile: PowerProfile | None = None, - log_profile_loading_errors: bool = True, - ) -> bool: - if not model_info: - model_info = await self.extract_model_info_from_device_info(entity_entry) - if not model_info or not model_info.manufacturer or not model_info.model: - return False - - if not power_profile: - try: - power_profile = await get_power_profile( - self.hass, - {}, - model_info, - log_errors=log_profile_loading_errors, - process_variables=False, - ) - except ModelNotSupportedError: - return False - - return power_profile.is_entity_domain_supported(entity_entry) if power_profile else False - async def get_entities(self) -> list[er.RegistryEntry]: """Get all entities from entity registry which qualifies for discovery.""" diff --git a/custom_components/powercalc/group_include/include.py b/custom_components/powercalc/group_include/include.py index 8a9c5a231..f51bd232c 100644 --- a/custom_components/powercalc/group_include/include.py +++ b/custom_components/powercalc/group_include/include.py @@ -5,13 +5,13 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import Entity -from custom_components.powercalc import DiscoveryManager +from custom_components.powercalc.common import create_source_entity from custom_components.powercalc.const import ( DATA_CONFIGURED_ENTITIES, - DATA_DISCOVERY_MANAGER, DATA_ENTITIES, DOMAIN, ) +from custom_components.powercalc.discovery import get_power_profile_by_source_entity from custom_components.powercalc.sensors.energy import RealEnergySensor from custom_components.powercalc.sensors.power import RealPowerSensor @@ -29,7 +29,6 @@ async def find_entities( Based on given entity filter fetch all power and energy sensors from the HA instance """ domain_data = hass.data.get(DOMAIN, {}) - discovery_manager: DiscoveryManager = domain_data.get(DATA_DISCOVERY_MANAGER) resolved_entities: list[Entity] = [] discoverable_entities: list[str] = [] @@ -55,7 +54,11 @@ async def find_entities( elif device_class == SensorDeviceClass.ENERGY and source_entity.platform != "utility_meter": resolved_entities.append(RealEnergySensor(source_entity.entity_id)) - if source_entity and await discovery_manager.is_entity_supported(source_entity, None, log_profile_loading_errors=False): + power_profile = await get_power_profile_by_source_entity( + hass, + await create_source_entity(source_entity.entity_id, hass), + ) + if power_profile and not await power_profile.needs_user_configuration and power_profile.is_entity_domain_supported(source_entity): discoverable_entities.append(source_entity.entity_id) return resolved_entities, discoverable_entities diff --git a/custom_components/powercalc/power_profile/power_profile.py b/custom_components/powercalc/power_profile/power_profile.py index a22605064..6e6a74268 100644 --- a/custom_components/powercalc/power_profile/power_profile.py +++ b/custom_components/powercalc/power_profile/power_profile.py @@ -348,6 +348,17 @@ def _load_json() -> None: self.sub_profile = sub_profile + @property + async def needs_user_configuration(self) -> bool: + """Check whether this profile needs user configuration.""" + if self.needs_fixed_config or self.needs_linear_config: + return True + + if self.has_custom_fields: + return True + + return await self.has_sub_profiles and not self.sub_profile_select + def is_entity_domain_supported(self, entity_entry: RegistryEntry) -> bool: """Check whether this power profile supports a given entity domain.""" if self.device_type is None: