Skip to content

Commit

Permalink
fix: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jan 1, 2025
1 parent 2e3d031 commit e8a2e54
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 34 deletions.
31 changes: 1 addition & 30 deletions custom_components/powercalc/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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."""

Expand Down
11 changes: 7 additions & 4 deletions custom_components/powercalc/group_include/include.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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] = []
Expand All @@ -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
11 changes: 11 additions & 0 deletions custom_components/powercalc/power_profile/power_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e8a2e54

Please sign in to comment.