diff --git a/custom_components/powercalc/sensors/group.py b/custom_components/powercalc/sensors/group.py index b4fc01b5d..2449ffded 100644 --- a/custom_components/powercalc/sensors/group.py +++ b/custom_components/powercalc/sensors/group.py @@ -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 ) @@ -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() diff --git a/tests/sensors/test_group.py b/tests/sensors/test_group.py index 37a207a26..4079e7581 100644 --- a/tests/sensors/test_group.py +++ b/tests/sensors/test_group.py @@ -58,6 +58,7 @@ SensorType, UnitPrefix, ) +from custom_components.powercalc.sensors.group import PreviousStateStore from ..common import ( create_input_boolean, @@ -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={ @@ -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