Skip to content

Commit

Permalink
multiple fixes for energy sensor calculation. Beta causes spikes on f…
Browse files Browse the repository at this point in the history
…irst run (#1648)
  • Loading branch information
bramstroker committed Apr 23, 2023
1 parent b6b0728 commit dc28d3f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
12 changes: 9 additions & 3 deletions custom_components/powercalc/sensors/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,10 @@ def calculate_new_state(self, member_states: list[State]) -> Decimal:
for entity_state in member_states:
prev_state = self._prev_state_store.get_entity_state(entity_state.entity_id)
cur_state = self._get_state_value_in_native_unit(entity_state)
prev_state = self._get_state_value_in_native_unit(prev_state) or Decimal(0)
if prev_state:
prev_state = self._get_state_value_in_native_unit(prev_state)
else:
prev_state = cur_state if self.state else Decimal(0)
self._prev_state_store.set_entity_state(
entity_state.entity_id, entity_state
)
Expand Down Expand Up @@ -614,9 +617,12 @@ async def async_get_instance(hass: HomeAssistant) -> PreviousStateStore:
stored_states = None

if stored_states is None:
instance.last_states = {}
instance.states = {}
else:
instance.states = stored_states
instance.states = {
entity_id: State.from_dict(json_state)
for (entity_id, json_state) in stored_states.items()
}

instance.async_setup_dump()

Expand Down
46 changes: 45 additions & 1 deletion tests/sensors/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
SensorType,
UnitPrefix,
)
from custom_components.powercalc.sensors.group import PreviousStateStore

from ..common import (
create_input_boolean,
Expand Down Expand Up @@ -988,7 +989,7 @@ async def test_ignore_unavailable_state(hass: HomeAssistant) -> None:
assert hass.states.get("sensor.testgroup_power").state == "0.00"


async def test_energy_sensor_delta_updates(hass: HomeAssistant) -> None:
async def test_energy_sensor_delta_updates_new_sensor(hass: HomeAssistant) -> None:
config_entry_group = MockConfigEntry(
domain=DOMAIN,
data={
Expand Down Expand Up @@ -1022,3 +1023,46 @@ async def test_energy_sensor_delta_updates(hass: HomeAssistant) -> None:
await hass.async_block_till_done()

assert hass.states.get("sensor.testgroup_energy").state == "5.3000"


async def test_energy_sensor_delta_updates_existing_sensor(hass: HomeAssistant) -> None:
config_entry_group = MockConfigEntry(
domain=DOMAIN,
data={
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_NAME: "TestGroup",
CONF_GROUP_ENERGY_ENTITIES: ["sensor.a_energy", "sensor.b_energy"],
},
)
config_entry_group.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry_group.entry_id)
await hass.async_block_till_done()

hass.states.async_set("sensor.testgroup_energy", "5.00")
await hass.async_block_till_done()

hass.states.async_set("sensor.a_energy", "2.00")
hass.states.async_set("sensor.b_energy", "3.00")
await hass.async_block_till_done()

assert hass.states.get("sensor.testgroup_energy").state == "5.0000"

hass.states.async_set("sensor.a_energy", "2.50")
await hass.async_block_till_done()

assert hass.states.get("sensor.testgroup_energy").state == "5.5000"


async def test_storage(hass: HomeAssistant):
state = State("sensor.dummy_power", "20.00")

store = PreviousStateStore(hass)
store.set_entity_state("sensor.dummy", state)
await store.persist_states()
await hass.async_block_till_done()

# Retrieving singleton instance should retrieve and decode persisted states
store: PreviousStateStore = await PreviousStateStore.async_get_instance(hass)
store_state = store.get_entity_state("sensor.dummy")

assert state == store_state

0 comments on commit dc28d3f

Please sign in to comment.