Initil after Upgrade

This commit is contained in:
2026-06-15 10:53:52 -04:00
parent 2fe9bf0dd6
commit 887feaa50a
143 changed files with 2288 additions and 881 deletions
@@ -94,7 +94,7 @@ def create_filter(
CONF_LABEL: lambda: LabelFilter(hass, filter_config), # type: ignore
CONF_WILDCARD: lambda: WildcardFilter(filter_config), # type: ignore
CONF_GROUP: lambda: GroupFilter(hass, filter_config), # type: ignore
CONF_TEMPLATE: lambda: TemplateFilter(hass, filter_config), # type: ignore
CONF_TEMPLATE: lambda: TemplateFilter(filter_config), # type: ignore
CONF_ALL: lambda: NullFilter(),
CONF_OR: lambda: create_composite_filter(filter_config, hass, FilterOperator.OR), # type: ignore
CONF_AND: lambda: create_composite_filter(filter_config, hass, FilterOperator.AND), # type: ignore
@@ -104,7 +104,7 @@ def create_filter(
return filter_mapping.get(filter_type, lambda: NullFilter())()
async def get_filtered_entity_list(
def get_filtered_entity_list(
hass: HomeAssistant,
entity_filter: EntityFilter,
) -> list[entity_registry.RegistryEntry]:
@@ -133,7 +133,11 @@ class GroupFilter(EntityFilter):
filters = []
for single_group_id in group_ids:
domain = split_entity_id(single_group_id)[0]
filter_instance = LightGroupFilter(hass, single_group_id) if domain == LIGHT_DOMAIN else StandardGroupFilter(hass, single_group_id)
filter_instance = (
LightGroupFilter(hass, single_group_id)
if domain == LIGHT_DOMAIN
else StandardGroupFilter(hass, single_group_id)
)
filters.append(filter_instance)
self.filter = CompositeFilter(filters, FilterOperator.OR) if len(filters) > 1 else filters[0]
@@ -227,8 +231,7 @@ class WildcardFilter(EntityFilter):
class TemplateFilter(EntityFilter):
def __init__(self, hass: HomeAssistant, template: Template) -> None:
template.hass = hass
def __init__(self, template: Template) -> None:
self.entity_ids = template.async_render()
def is_valid(self, entity: RegistryEntry) -> bool:
@@ -311,7 +314,9 @@ class AreaFilter(EntityFilter):
)
self.area_ids.append(area.id)
self.area_devices.update([device.id for device in device_registry.async_entries_for_area(device_reg, area.id)])
self.area_devices.update(
[device.id for device in device_registry.async_entries_for_area(device_reg, area.id)],
)
def is_valid(self, entity: RegistryEntry) -> bool:
return entity.area_id in self.area_ids or entity.device_id in self.area_devices
@@ -350,7 +355,9 @@ class FloorFilter(EntityFilter):
self.area_ids.extend([area.id for area in areas if area.id is not None])
for area in areas:
self.devices.extend([device.id for device in device_registry.async_entries_for_area(device_reg, area.id)])
self.devices.extend(
[device.id for device in device_registry.async_entries_for_area(device_reg, area.id)],
)
def is_valid(self, entity: RegistryEntry) -> bool:
return entity.area_id in self.area_ids or entity.device_id in self.devices
@@ -5,6 +5,7 @@ from homeassistant.components import sensor
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_registry import RegistryEntry
from custom_components.powercalc.common import create_source_entity
from custom_components.powercalc.const import (
@@ -52,7 +53,7 @@ async def find_entities(
resolved_entities: list[Entity] = []
discoverable_entities: list[str] = []
source_entities = await get_filtered_entity_list(hass, _build_filter(entity_filter))
source_entities = get_filtered_entity_list(hass, _build_filter(entity_filter))
if _LOGGER.isEnabledFor(logging.DEBUG): # pragma: no cover
_LOGGER.debug("Source entities: %s", [entity.entity_id for entity in source_entities])
@@ -70,29 +71,15 @@ async def find_entities(
resolved_entities.append(existing)
continue
is_real_sensor = False
if source_entity.domain == sensor.DOMAIN:
if source_entity.platform != DOMAIN and not include_non_powercalc:
continue
device_class = source_entity.device_class or source_entity.original_device_class
if device_class == SensorDeviceClass.POWER:
resolved_entities.append(RealPowerSensor(entity_id, source_entity.unit_of_measurement))
is_real_sensor = True
elif device_class == SensorDeviceClass.ENERGY:
resolved_entities.append(RealEnergySensor(entity_id))
is_real_sensor = True
# No need to discover a profile for something we already resolved as a real sensor
if is_real_sensor:
if _should_skip_source_entity(source_entity, include_non_powercalc):
continue
power_profile = await get_power_profile_by_source_entity(
hass,
await create_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):
real_sensor = _create_real_sensor(source_entity)
if real_sensor:
resolved_entities.append(real_sensor)
continue
if await _is_discoverable_source_entity(hass, source_entity):
discoverable_entities.append(entity_id)
if exclude_utility_meters:
@@ -105,6 +92,34 @@ async def find_entities(
return FindEntitiesResult(resolved_entities, discoverable_entities)
def _should_skip_source_entity(source_entity: RegistryEntry, include_non_powercalc: bool) -> bool:
return source_entity.domain == sensor.DOMAIN and source_entity.platform != DOMAIN and not include_non_powercalc
def _create_real_sensor(source_entity: RegistryEntry) -> Entity | None:
if source_entity.domain != sensor.DOMAIN:
return None
device_class = source_entity.device_class or source_entity.original_device_class
if device_class == SensorDeviceClass.POWER:
return RealPowerSensor(source_entity.entity_id, source_entity.unit_of_measurement)
if device_class == SensorDeviceClass.ENERGY:
return RealEnergySensor(source_entity.entity_id)
return None # pragma: no cover
async def _is_discoverable_source_entity(hass: HomeAssistant, source_entity: RegistryEntry) -> bool:
power_profile = await get_power_profile_by_source_entity(
hass,
create_source_entity(source_entity.entity_id, hass),
)
return bool(
power_profile
and not await power_profile.needs_user_configuration
and power_profile.is_entity_domain_supported(source_entity),
)
def _build_filter(entity_filter: EntityFilter | None) -> EntityFilter:
base_filter = CompositeFilter(
[
@@ -112,7 +127,11 @@ def _build_filter(entity_filter: EntityFilter | None) -> EntityFilter:
LambdaFilter(lambda entity: entity.platform != "utility_meter"),
LambdaFilter(lambda entity: not str(entity.unique_id).startswith("powercalc_standby_group")),
LambdaFilter(lambda entity: "tracked_" not in str(entity.unique_id)),
LambdaFilter(lambda entity: entity.platform != "tasmota" or not str(entity.entity_id).endswith(("_yesterday", "_today"))),
LambdaFilter(
lambda entity: (
entity.platform != "tasmota" or not str(entity.entity_id).endswith(("_yesterday", "_today"))
),
),
],
)
if not entity_filter: