Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Dec 9, 2023
1 parent ba6cdc6 commit 564281d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 43 deletions.
26 changes: 20 additions & 6 deletions custom_components/powercalc/group_include/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
from homeassistant.helpers.area_registry import AreaEntry
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType

from custom_components.powercalc.const import CONF_AREA, CONF_GROUP, CONF_TEMPLATE, CONF_WILDCARD
from custom_components.powercalc.errors import SensorConfigurationError

if AwesomeVersion(HA_VERSION) >= AwesomeVersion("2023.8.0"):
Expand All @@ -30,14 +32,26 @@ class FilterOperator(StrEnum):
OR = "or"


def create_filter(filter_config: dict) -> IncludeEntityFilter:
def create_filter(filter_config: ConfigType, hass: HomeAssistant, filter_operator: FilterOperator) -> IncludeEntityFilter:
"""Create filter class."""
filters: list[IncludeEntityFilter] = []
if CONF_DOMAIN in filter_config:
domain_config = filter_config.get(CONF_DOMAIN)
filters.append(DomainFilter(domain_config)) # type: ignore

return CompositeFilter(filters, FilterOperator.AND)
for key, val in filter_config.items():
entity_filter = None
if key == CONF_DOMAIN:
entity_filter = DomainFilter(val)
if key == CONF_AREA:
entity_filter = AreaFilter(hass, val)
if key == CONF_WILDCARD:
entity_filter = WildcardFilter(val)
if key == CONF_GROUP:
entity_filter = GroupFilter(hass, val)
if key == CONF_TEMPLATE:
entity_filter = TemplateFilter(hass, val)

if entity_filter:
filters.append(entity_filter)

return CompositeFilter(filters, filter_operator)


class IncludeEntityFilter(Protocol):
Expand Down
25 changes: 3 additions & 22 deletions custom_components/powercalc/group_include/include.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

from homeassistant.components import sensor
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import CONF_DOMAIN, CONF_ENTITY_ID
from homeassistant.const import CONF_ENTITY_ID
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry
from homeassistant.helpers.entity import Entity

from custom_components.powercalc.const import (
CONF_AREA,
CONF_FILTER,
CONF_GROUP,
CONF_TEMPLATE,
CONF_WILDCARD,
DATA_CONFIGURED_ENTITIES,
DOMAIN,
ENTRY_DATA_ENERGY_ENTITY,
Expand All @@ -22,13 +18,8 @@
from custom_components.powercalc.sensors.power import RealPowerSensor

from .filter import (
AreaFilter,
CompositeFilter,
DomainFilter,
FilterOperator,
GroupFilter,
TemplateFilter,
WildcardFilter,
create_filter,
)

Expand Down Expand Up @@ -91,21 +82,11 @@ def resolve_include_source_entities(
hass: HomeAssistant,
include_config: dict,
) -> dict[str, entity_registry.RegistryEntry | None]:
entity_filter = CompositeFilter([], FilterOperator.OR)
if CONF_GROUP in include_config:
entity_filter.append(GroupFilter(hass, include_config.get(CONF_GROUP))) # type: ignore
if CONF_WILDCARD in include_config:
entity_filter.append(WildcardFilter(include_config.get(CONF_WILDCARD))) # type: ignore
if CONF_DOMAIN in include_config:
entity_filter.append(DomainFilter(include_config.get(CONF_DOMAIN))) # type: ignore
if CONF_TEMPLATE in include_config:
entity_filter.append(TemplateFilter(hass, include_config.get(CONF_TEMPLATE))) # type: ignore
if CONF_AREA in include_config:
entity_filter.append(AreaFilter(hass, include_config.get(CONF_AREA))) # type: ignore
entity_filter = create_filter(include_config, hass, FilterOperator.OR)

if CONF_FILTER in include_config:
entity_filter = CompositeFilter(
[entity_filter, create_filter(include_config.get(CONF_FILTER))], # type: ignore
[entity_filter, create_filter(include_config.get(CONF_FILTER), hass, FilterOperator.OR)], # type: ignore
FilterOperator.AND,
)

Expand Down
16 changes: 1 addition & 15 deletions docs/source/sensor-types/group/include-entities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,4 @@ Wildcard
.. code-block:: yaml
filter:
wildcard: light.office_spot_*
And / OR
--------

You can combine multiple filters as follows:

.. code-block:: yaml
filter:
or:
- wildcard: light.office_spot_*
- domain:
- light
- switch
wildcard: light.office_spot_*

0 comments on commit 564281d

Please sign in to comment.