1414 SensorStateClass ,
1515)
1616from homeassistant .config_entries import ConfigEntry
17+
1718from homeassistant .const import (
1819 ATTR_UNIT_OF_MEASUREMENT ,
1920 UnitOfElectricCurrent ,
3132from homeassistant .helpers .entity_platform import AddEntitiesCallback
3233from homeassistant .util import dt as dt_util
3334
35+
3436from .const import (
3537 ATTR_SUMMATION_FACTOR ,
3638 ATTR_SUMMATION_PERIOD ,
3739 ATTR_SUMMATION_TYPE ,
3840 ATTR_TIMESTAMP ,
3941 ATTR_VALUE_IF_NO_UPDATE ,
4042 DOMAIN ,
41- MAX_STUB_INTERVAL ,
4243 SOLAR_ICON ,
4344 SummationPeriod ,
4445 SummationType ,
@@ -276,7 +277,7 @@ def restore_sensors():
276277 ),
277278 )
278279
279- sensors .append (APSystemsSensor (definition , config ))
280+ sensors .append (APSystemsSensor (definition , config , config_entry ))
280281
281282 if sensors :
282283 add_entities (sensors )
@@ -418,11 +419,15 @@ class APSystemsSensor(RestoreSensor, SensorEntity):
418419 _attr_extra_state_attributes = {}
419420
420421 def __init__ (
421- self , definition : APSystemSensorDefinition , config : APSystemSensorConfig
422+ self ,
423+ definition : APSystemSensorDefinition ,
424+ config : APSystemSensorConfig ,
425+ config_entry : ConfigEntry # Accept ConfigEntry to get dynamic config values
422426 ) -> None :
423427 """Initialise sensor."""
424428 self ._definition = definition
425429 self ._config = config
430+ self .config_entry = config_entry
426431
427432 self ._attr_device_class = definition .device_class
428433 self ._attr_device_info = DeviceInfo (identifiers = self ._config .device_identifier )
@@ -641,12 +646,13 @@ def summation_calculation(
641646 current_value : float ,
642647 value : float ,
643648 ) -> int | float :
644- """Return summation value of value over time.
645649
650+ """Return summation value of value over time.
646651 If change in period, calculates a value over time from start of new period with
647652 max of MAX_STUB_INTERVAL.
648653 If no change in period, assumes value persisted since last timestamp.
649654 """
655+
650656 _LOGGER .debug (
651657 "Summation values: Period: %s, Timestamp - current: %s, last: %s, Value - sensor: %s, current: %s" ,
652658 summation_period ,
@@ -663,6 +669,13 @@ def summation_calculation(
663669
664670 sum_value = None
665671 has_changed = False
672+
673+ # Get configuration. If initial data else options.
674+ self .max_stub_interval = int (
675+ self .config_entry .options .get ('max_stub_interval' ,
676+ self .config_entry .data .get ('max_stub_interval' ))
677+ )
678+ _LOGGER .debug ("Max stub interval = %s" , self .max_stub_interval )
666679
667680 # Has it crossed calculation period boundry?
668681 if has_changed_period (summation_period , last_timestamp , current_timestamp ):
@@ -683,7 +696,7 @@ def summation_calculation(
683696 summation_period , current_timestamp
684697 )
685698 ).total_seconds (),
686- MAX_STUB_INTERVAL ,
699+ self . max_stub_interval ,
687700 )
688701 sum_value = round (
689702 int (value * (new_period_interval / 3600 )) / summation_factor , 2
0 commit comments