@@ -456,7 +456,6 @@ def __init__(
456456 self ._attr_suggested_display_precision = self ._rounding_digits
457457 if unique_id :
458458 self ._attr_unique_id = unique_id
459- self ._prev_state_store : PreviousStateStore = PreviousStateStore (hass )
460459 self ._native_value_exact = Decimal (0 )
461460 self ._member_states : dict [str , Decimal ] = {}
462461 self ._ignore_unavailable_state = bool (self ._sensor_config .get (CONF_IGNORE_UNAVAILABLE_STATE ))
@@ -470,7 +469,6 @@ async def async_added_to_hass(self) -> None:
470469 """Register state listeners."""
471470 await super ().async_added_to_hass ()
472471
473- self ._prev_state_store = await PreviousStateStore .async_get_instance (self .hass )
474472 if self ._update_interval > 0 :
475473 self .async_on_remove (self ._cancel_update_interval_exceeded_callback )
476474
@@ -479,6 +477,12 @@ async def async_added_to_hass(self) -> None:
479477 if CONF_HIDE_MEMBERS in self ._sensor_config :
480478 self ._async_hide_members (bool (self ._sensor_config .get (CONF_HIDE_MEMBERS )))
481479
480+ if not self ._sensor_config .get (CONF_DISABLE_EXTENDED_ATTRIBUTES , False ):
481+ self ._attr_extra_state_attributes = {
482+ ATTR_ENTITIES : self ._entities ,
483+ ATTR_IS_GROUP : True ,
484+ }
485+
482486 async def async_will_remove_from_hass (self ) -> None :
483487 """
484488 This will trigger when entity is about to be removed from HA
@@ -547,12 +551,6 @@ async def on_start(self, _: Any) -> None: # noqa
547551 ),
548552 )
549553
550- if not self ._sensor_config .get (CONF_DISABLE_EXTENDED_ATTRIBUTES , False ):
551- self ._attr_extra_state_attributes = {
552- ATTR_ENTITIES : self ._entities ,
553- ATTR_IS_GROUP : True ,
554- }
555-
556554 await self .initial_update ()
557555
558556 async def initial_update (self ) -> None :
@@ -720,9 +718,15 @@ def __init__(
720718 sensor_config .get (CONF_ENERGY_SENSOR_UNIT_PREFIX , UnitPrefix .NONE ),
721719 UnitOfEnergy .WATT_HOUR ,
722720 )
721+ self ._prev_state_store : PreviousStateStore = PreviousStateStore (hass )
723722
724723 async def async_added_to_hass (self ) -> None :
725724 """Register state listeners."""
725+
726+ self ._prev_state_store = await PreviousStateStore .async_get_instance (self .hass )
727+ # Clean up any entities that are no longer part of the group
728+ self ._prev_state_store .cleanup_entity_states (self .entity_id , self ._entities )
729+
726730 await self .restore_last_state ()
727731
728732 await super ().async_added_to_hass ()
@@ -900,6 +904,20 @@ def set_entity_state(self, group: str, entity_id: str, state: State) -> None:
900904 """Set the state for an energy sensor."""
901905 self .states .setdefault (group , {})[entity_id ] = state
902906
907+ def cleanup_entity_states (self , group : str , current_entities : set [str ]) -> None :
908+ """Remove entity states that are no longer part of the group."""
909+ group_states = self .states .get (group )
910+ if group_states is None :
911+ return
912+
913+ # Find entities that are in the store but not in the current set
914+ entities_to_remove = set (group_states .keys ()) - current_entities
915+
916+ # Remove those entities from the store
917+ for entity_id in entities_to_remove :
918+ _LOGGER .debug ("Removing entity %s from group %s in PreviousStateStore" , entity_id , group )
919+ group_states .pop (entity_id , None )
920+
903921 async def persist_states (self ) -> None :
904922 """Save the current states to storage."""
905923 try :
0 commit comments