Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple fixes for energy sensor calculation. Beta causes spikes on first run #1648

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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