Skip to content

Commit

Permalink
fix: issue with playbook returning old values when stopped (#2583)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker authored Oct 13, 2024
1 parent 36ea748 commit 4e39968
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
10 changes: 7 additions & 3 deletions custom_components/powercalc/strategy/playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ def set_update_callback(self, update_callback: Callable[[Decimal], None]) -> Non
self._update_callback = update_callback

async def calculate(self, entity_state: State) -> Decimal | None:
if self._states_trigger and entity_state.state in self._states_trigger:
playbook_id = self._states_trigger[entity_state.state]
await self.activate_playbook(playbook_id)
if self._states_trigger:
if entity_state.state in self._states_trigger:
playbook_id = self._states_trigger[entity_state.state]
await self.activate_playbook(playbook_id)
else:
await self.stop_playbook()

return self._power

Expand Down Expand Up @@ -112,6 +115,7 @@ async def stop_playbook(self) -> None:

_LOGGER.debug("Stopping playbook")
self._active_playbook = None
self._power = Decimal(0)
if self._cancel_timer is not None:
self._cancel_timer()
self._cancel_timer = None
Expand Down
37 changes: 37 additions & 0 deletions tests/strategy/test_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,43 @@ async def test_multiply_factor(hass: HomeAssistant) -> None:
await elapse_and_assert_power(hass, 2, "60.00")


async def test_source_entity_trigger(hass: HomeAssistant) -> None:
hass.config.config_dir = get_test_config_dir()
await run_powercalc_setup(
hass,
{
CONF_ENTITY_ID: "switch.test",
CONF_NAME: "Test",
CONF_PLAYBOOK: {
CONF_PLAYBOOKS: {
"on": "test2.csv",
},
CONF_STATE_TRIGGER: {
STATE_ON: "on",
},
},
},
)

hass.states.async_set("switch.test", STATE_ON)
await hass.async_block_till_done()

assert hass.states.get(POWER_SENSOR_ID).state == "0.00"
await elapse_and_assert_power(hass, 2, "20.00")

hass.states.async_set("switch.test", STATE_OFF)
await hass.async_block_till_done()

async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=60))
await hass.async_block_till_done()

hass.states.async_set("switch.test", STATE_ON)
await hass.async_block_till_done()

assert hass.states.get(POWER_SENSOR_ID).state == "0.00"
await elapse_and_assert_power(hass, 2, "20.00")


async def test_state_trigger(hass: HomeAssistant) -> None:
hass.config.config_dir = get_test_config_dir()
await run_powercalc_setup(
Expand Down
46 changes: 46 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
CONF_CREATE_GROUP,
CONF_CREATE_UTILITY_METERS,
CONF_ENABLE_AUTODISCOVERY,
CONF_ENERGY_INTEGRATION_METHOD,
CONF_ENERGY_SENSOR_FRIENDLY_NAMING,
CONF_ENERGY_SENSOR_NAMING,
CONF_FIXED,
Expand All @@ -62,6 +63,7 @@
CONF_SENSOR_TYPE,
CONF_UTILITY_METER_TYPES,
DOMAIN,
ENERGY_INTEGRATION_METHOD_LEFT,
CalculationStrategy,
SensorType,
)
Expand Down Expand Up @@ -852,3 +854,47 @@ async def test_change_config_entry_entity_id(hass: HomeAssistant) -> None:
power_state = hass.states.get("sensor.testentry_power")
assert power_state.attributes.get(ATTR_SOURCE_ENTITY) == new_light_id
assert power_state.state == "100.00"


async def test_regression(hass: HomeAssistant) -> None:
#
# {
# "entity_id": "light.standing_lamp",
# "mode": "fixed",
# "create_energy_sensor": true,
# "create_utility_meters": false,
# "fixed": {
# "power": 11.0
# },
# "energy_integration_method": "left",
# "unique_id": "pc_91b5b2b04e889549cb19effa3dea97e1",
# "sensor_type": "virtual_power",
# "name": "Standing lamp",
# "_power_entity": "sensor.standing_lamp_power",
# "_energy_entity": "sensor.standing_lamp_power"
# }

entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_CREATE_ENERGY_SENSOR: True,
CONF_CREATE_UTILITY_METERS: False,
CONF_MODE: CalculationStrategy.FIXED,
CONF_NAME: "Standing lamp",
CONF_ENTITY_ID: "light.standing_lamp",
CONF_ENERGY_INTEGRATION_METHOD: ENERGY_INTEGRATION_METHOD_LEFT,
CONF_FIXED: {CONF_POWER: 11},
CONF_UNIQUE_ID: "pc_91b5b2b04e889549cb19effa3dea97e1",
},
unique_id="pc_91b5b2b04e889549cb19effa3dea97e1",
)
entry.add_to_hass(hass)

await run_powercalc_setup(
hass,
{},
)

assert hass.states.get("sensor.standing_lamp_power")
assert hass.states.get("sensor.standing_lamp_energy")

0 comments on commit 4e39968

Please sign in to comment.