Skip to content

Commit

Permalink
also track changes for calculation_enabled_condition template (#1941)
Browse files Browse the repository at this point in the history
* fix: set device class to energy on startup

* also track changes for calculation_enabled_condition template

* add test
  • Loading branch information
bramstroker authored Dec 6, 2023
1 parent fa74aca commit 5a7502a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
30 changes: 21 additions & 9 deletions custom_components/powercalc/sensors/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ def __init__(
) -> None:
"""Initialize the sensor."""
self._calculation_strategy = calculation_strategy
self._calculation_enabled_condition: Template | None = None
self._source_entity = source_entity
self._attr_name = name
self._power: Decimal | None = None
Expand Down Expand Up @@ -380,6 +381,7 @@ async def async_added_to_hass(self) -> None:
await super().async_added_to_hass()
await self.ensure_strategy_instance()
assert self._strategy_instance is not None
self.init_calculation_enabled_condition()

async def appliance_state_listener(event: Event) -> None:
"""Handle for state changes for dependent sensors."""
Expand Down Expand Up @@ -444,6 +446,8 @@ async def initial_update(hass: HomeAssistant) -> None:
if isinstance(self._standby_power, Template):
self._standby_power.hass = self.hass
track_templates.append(TrackTemplate(self._standby_power, None, None))
if self._calculation_enabled_condition:
track_templates.append(TrackTemplate(self._calculation_enabled_condition, None, None))
if track_templates:
async_track_template_result(
self.hass,
Expand All @@ -463,6 +467,21 @@ def async_update(event_time: datetime | None = None) -> None:

async_track_time_interval(self.hass, async_update, self._update_frequency)

def init_calculation_enabled_condition(self) -> None:
if CONF_CALCULATION_ENABLED_CONDITION not in self._sensor_config:
return

template = self._sensor_config.get(CONF_CALCULATION_ENABLED_CONDITION)
if isinstance(template, str):
template = template.replace("[[entity]]", self.source_entity)
template = Template(template)

if not isinstance(template, Template):
_LOGGER.error("Invalid calculation_enabled_condition: %s", template)
return

self._calculation_enabled_condition = template

async def _handle_source_entity_state_change(
self,
trigger_entity_id: str,
Expand Down Expand Up @@ -620,17 +639,10 @@ def _update_sleep_power(*_: Any) -> None: # noqa: ANN401
return standby_power

async def is_calculation_enabled(self) -> bool:
if CONF_CALCULATION_ENABLED_CONDITION not in self._sensor_config:
template = self._calculation_enabled_condition
if not template:
return True

template = self._sensor_config.get(CONF_CALCULATION_ENABLED_CONDITION)
if isinstance(template, str):
template = template.replace("[[entity]]", self.source_entity)
template = Template(template)

if not isinstance(template, Template):
return True # pragma: no cover

template.hass = self.hass
return bool(template.async_render())

Expand Down
28 changes: 28 additions & 0 deletions tests/sensors/test_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,34 @@ async def test_strategy_enabled_condition(hass: HomeAssistant) -> None:
assert hass.states.get(power_entity_id).state == "1.50"


async def test_strategy_enabled_condition_template_tracking(hass: HomeAssistant) -> None:
await run_powercalc_setup(
hass,
{
CONF_ENTITY_ID: "sensor.my_entity",
CONF_CALCULATION_ENABLED_CONDITION: "{{ is_state('sensor.other_entity', 'foo') }}",
CONF_FIXED: {
CONF_POWER: 5,
},
},
)

hass.states.async_set("sensor.my_entity", STATE_ON)
await hass.async_block_till_done()

assert hass.states.get("sensor.my_entity_power").state == "0.00"

hass.states.async_set("sensor.other_entity", "foo")
await hass.async_block_till_done()

assert hass.states.get("sensor.my_entity_power").state == "5.00"

hass.states.async_set("sensor.other_entity", "bar")
await hass.async_block_till_done()

assert hass.states.get("sensor.my_entity_power").state == "0.00"


async def test_template_entity_tracking(hass: HomeAssistant) -> None:
await create_input_number(hass, "test", 0)
await create_input_boolean(hass)
Expand Down

0 comments on commit 5a7502a

Please sign in to comment.