Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bramstroker/homeassistant-powercalc
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jul 8, 2023
2 parents 5bb7efa + 5c47cb8 commit ab4b579
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions custom_components/powercalc/strategy/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def __init__(self, hass: HomeAssistant, strategies: list[SubStrategy]) -> None:
async def calculate(self, entity_state: State) -> Decimal | None:
for sub_strategy in self.strategies:
if sub_strategy.condition and not sub_strategy.condition(
self.hass, {"state": entity_state},
self.hass,
{"state": entity_state},
):
continue

Expand All @@ -36,12 +37,15 @@ def get_entities_to_track(self) -> list[str | TrackTemplate]:
for sub_strategy in self.strategies:
if sub_strategy.condition_config:
self.resolve_track_templates_from_condition(
sub_strategy.condition_config, track_templates,
sub_strategy.condition_config,
track_templates,
)
return track_templates

def resolve_track_templates_from_condition(
self, condition_config: dict, templates: list[str | TrackTemplate],
self,
condition_config: dict,
templates: list[str | TrackTemplate],
) -> None:
for key, value in condition_config.items():
if key == CONF_ENTITY_ID and isinstance(value, list):
Expand Down
8 changes: 6 additions & 2 deletions custom_components/powercalc/strategy/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,16 @@ async def _create_sub_strategy(strategy_config: ConfigType) -> SubStrategy:
condition_config = strategy_config.get(CONF_CONDITION)
if condition_config:
condition_instance = await condition.async_from_config(
self._hass, condition_config,
self._hass,
condition_config,
)

strategy = detect_calculation_strategy(strategy_config, power_profile)
strategy_instance = await self.create(
strategy_config, strategy, power_profile, source_entity,
strategy_config,
strategy,
power_profile,
source_entity,
)
return SubStrategy(condition_config, condition_instance, strategy_instance)

Expand Down
2 changes: 1 addition & 1 deletion tests/strategy/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async def test_template_condition(hass: HomeAssistant) -> None:


async def test_power_sensor_unavailable_when_no_condition_matches(
hass: HomeAssistant,
hass: HomeAssistant,
) -> None:
sensor_config = {
CONF_ENTITY_ID: "light.test",
Expand Down

0 comments on commit ab4b579

Please sign in to comment.