Skip to content

Commit 348c3d8

Browse files
committed
fix: cleanup filter.py
1 parent 84f3a38 commit 348c3d8

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

  • custom_components/powercalc/group_include

custom_components/powercalc/group_include/filter.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from homeassistant.core import HomeAssistant, split_entity_id
1212
from homeassistant.helpers import area_registry, device_registry, entity_registry, floor_registry, label_registry
1313
import homeassistant.helpers.config_validation as cv
14+
from homeassistant.helpers.entity import Entity
1415
from homeassistant.helpers.entity_component import EntityComponent
1516
from homeassistant.helpers.entity_registry import RegistryEntry
1617
from homeassistant.helpers.template import Template
@@ -148,8 +149,6 @@ def is_valid(self, entity: RegistryEntry) -> bool:
148149

149150
class StandardGroupFilter(EntityFilter):
150151
def __init__(self, hass: HomeAssistant, group_id: str) -> None:
151-
entity_reg = entity_registry.async_get(hass)
152-
entity_reg.async_get(group_id)
153152
group_state = hass.states.get(group_id)
154153
if group_state is None:
155154
raise SensorConfigurationError(f"Group state {group_id} not found")
@@ -161,14 +160,7 @@ def is_valid(self, entity: RegistryEntry) -> bool:
161160

162161
class LightGroupFilter(EntityFilter):
163162
def __init__(self, hass: HomeAssistant, group_id: str) -> None:
164-
light_component = cast(EntityComponent, hass.data.get(LIGHT_DOMAIN))
165-
light_group = next(
166-
filter(
167-
lambda entity: entity.entity_id == group_id,
168-
light_component.entities,
169-
),
170-
None,
171-
)
163+
light_group = self._find_light_group(hass, group_id)
172164
if light_group is None or light_group.platform.platform_name != GROUP_DOMAIN:
173165
raise SensorConfigurationError(f"Light group {group_id} not found")
174166

@@ -177,22 +169,26 @@ def __init__(self, hass: HomeAssistant, group_id: str) -> None:
177169
def is_valid(self, entity: RegistryEntry) -> bool:
178170
return entity.entity_id in self.entity_ids
179171

180-
def find_all_entity_ids_recursively(
181-
self,
182-
hass: HomeAssistant,
183-
group_entity_id: str,
184-
all_entity_ids: list[str],
185-
) -> list[str]:
186-
entity_reg = entity_registry.async_get(hass)
172+
@staticmethod
173+
def _find_light_group(hass: HomeAssistant, group_entity_id: str) -> Entity | None:
187174
light_component = cast(EntityComponent, hass.data.get(LIGHT_DOMAIN))
188-
light_group = next(
175+
return next(
189176
filter(
190177
lambda entity: entity.entity_id == group_entity_id,
191178
light_component.entities,
192179
),
193180
None,
194181
)
195182

183+
def find_all_entity_ids_recursively(
184+
self,
185+
hass: HomeAssistant,
186+
group_entity_id: str,
187+
all_entity_ids: list[str],
188+
) -> list[str]:
189+
entity_reg = entity_registry.async_get(hass)
190+
light_group = self._find_light_group(hass, group_entity_id)
191+
196192
entity_ids: list[str] = light_group.extra_state_attributes.get(ATTR_ENTITY_ID) # type: ignore
197193
for entity_id in entity_ids:
198194
registry_entry = entity_reg.async_get(entity_id)
@@ -373,7 +369,7 @@ def __init__(
373369
self.operator = operator
374370

375371
def is_valid(self, entity: RegistryEntry) -> bool:
376-
evaluations = [entity_filter.is_valid(entity) for entity_filter in self.filters]
372+
evaluations = (entity_filter.is_valid(entity) for entity_filter in self.filters)
377373
if self.operator == FilterOperator.OR:
378374
return any(evaluations)
379375

0 commit comments

Comments
 (0)