Skip to content

Commit 576d0b1

Browse files
authored
Merge pull request #9 from HAEdwin/Replace-constants-by-dynamically-configurable-parameters
Replace constants by dynamically configurable parameters
2 parents 94740f8 + 0a3ba6b commit 576d0b1

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

custom_components/apsystems_ecu_proxy/config_flow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async def async_step_user(self, user_input=None):
2828
vol.Required(KEYS[0], default="3.67.1.32"): str,
2929
vol.Required(KEYS[1], default="1800"): str,
3030
vol.Required(KEYS[2], default="300"): str,
31-
vol.Required(KEYS[3], default="600"): str,
31+
vol.Required(KEYS[3], default="660"): str,
3232
vol.Required(KEYS[4], default=True): bool,
3333
})
3434

custom_components/apsystems_ecu_proxy/const.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
# Datacom handling
2727
SOCKET_PORTS = [8995, 8996, 8997]
28-
# Max summation stub time to add for new period - see code in summation_calculation method in
29-
MAX_STUB_INTERVAL = 300
3028

3129
# Config flow schema. These are also translated through associated json translations
3230
KEYS = [

custom_components/apsystems_ecu_proxy/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
],
1515
"requirements": [],
1616
"single_config_entry": true,
17-
"version": "v1.0.0"
17+
"version": "v1.1.0"
1818
}

custom_components/apsystems_ecu_proxy/sensor.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
SensorStateClass,
1515
)
1616
from homeassistant.config_entries import ConfigEntry
17+
1718
from homeassistant.const import (
1819
ATTR_UNIT_OF_MEASUREMENT,
1920
UnitOfElectricCurrent,
@@ -31,14 +32,14 @@
3132
from homeassistant.helpers.entity_platform import AddEntitiesCallback
3233
from homeassistant.util import dt as dt_util
3334

35+
3436
from .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

custom_components/apsystems_ecu_proxy/translations/en.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"ema_host": "EMA Host (3.67.1.32)",
77
"message_ignore_age": "Message Ignore Age e.g. 1800 (seconds)",
88
"max_stub_interval": "Max Stub Interval e.g. 300 (seconds)",
9-
"no_update_timeout": "No Update Timeout e.g. 600 (seconds)",
9+
"no_update_timeout": "No Update Timeout e.g. 660 (seconds)",
1010
"send_to_ema": "Send to EMA"
1111
},
1212
"title": "Configuration"
@@ -20,7 +20,7 @@
2020
"ema_host": "EMA Host (3.67.1.32)",
2121
"message_ignore_age": "Message Ignore Age e.g. 1800 (seconds)",
2222
"max_stub_interval": "Max Stub Interval e.g. 300 (seconds)",
23-
"no_update_timeout": "No Update Timeout e.g. 600 (seconds) ",
23+
"no_update_timeout": "No Update Timeout e.g. 660 (seconds) ",
2424
"send_to_ema": "Send to EMA"
2525
},
2626
"title": "Configuration"

0 commit comments

Comments
 (0)