Skip to content

Commit 43ff9ee

Browse files
Merge branch 'fixes1' of github.com:springfall2008/batpred into fixes1
2 parents 8b09d41 + 95e2d47 commit 43ff9ee

4 files changed

Lines changed: 53 additions & 135 deletions

File tree

‎apps/predbat/execute.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ def publish_inverter_data(self):
783783
"unit_of_measurement": "kW",
784784
"icon": "mdi:battery",
785785
},
786-
)
786+
)
787787
self.dashboard_item(
788788
self.prefix + ".battery_power",
789789
state=dp3(self.battery_power / 1000.0),
@@ -795,7 +795,6 @@ def publish_inverter_data(self):
795795
},
796796
)
797797

798-
799798
def balance_inverters(self):
800799
"""
801800
Attempt to balance multiple inverters

‎apps/predbat/fetch.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ def minute_data(
761761
last_sample_value = mdata[last_sample_minute]
762762
last_but_one_minute_sample = mdata[last_sample_minute + 1] if (last_sample_minute + 1) in mdata else last_sample_value
763763
step = last_sample_value - last_but_one_minute_sample
764-
if last_sample_minute < 10*60 and last_sample_minute > 0:
764+
if last_sample_minute < 10 * 60 and last_sample_minute > 0:
765765
for minute in range(last_sample_minute):
766766
if minute not in mdata:
767767
mdata[minute] = dp4(newest_state + step * (last_sample_minute - minute))

‎apps/predbat/gecloud.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
RETRIES = 10
157157
RETRY_FACTOR = 1
158158
MAX_THREADS = 2
159-
MAX_START_TIME = 10*60
159+
MAX_START_TIME = 10 * 60
160160

161161
attribute_table = {
162162
"time": {"friendly_name": "Time", "icon": "mdi:clock", "unit_of_measurement": "Time", "state_class": "timestamp"},
@@ -696,7 +696,7 @@ async def async_automatic_config(self, devices):
696696

697697
if "givtcp_rest" in self.base.args:
698698
del self.base.args["givtcp_rest"]
699-
699+
700700
# Use the first battery serial for the ge_cloud_serial (for status)
701701
self.base.args["ge_cloud_serial"] = devices["battery"][0]
702702

@@ -1255,7 +1255,7 @@ async def async_get_inverter_data(self, endpoint, serial="", setting_id="", post
12551255
self.log(f"Warn: GECloud: Exception during request to {url}: {e}")
12561256
self.failures_total += 1
12571257
return None
1258-
1258+
12591259
try:
12601260
data = response.json()
12611261
except requests.exceptions.JSONDecodeError:

‎apps/predbat/unit_test.py‎

Lines changed: 48 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -8832,12 +8832,13 @@ def test_minute_data(my_predbat):
88328832
"""
88338833
failed = False
88348834
print("**** Testing minute_data function ****")
8835-
8835+
88368836
# Create test datetime objects with timezone awareness
88378837
import pytz
8838+
88388839
utc = pytz.UTC
88398840
now = datetime(2024, 10, 4, 12, 5, 0, tzinfo=utc) # Fixed time for testing
8840-
8841+
88418842
# Test 1: Basic functionality with simple history data
88428843
print("Test 1: Basic functionality")
88438844
history = [
@@ -8846,69 +8847,46 @@ def test_minute_data(my_predbat):
88468847
{"state": "20.0", "last_updated": "2024-10-04T11:30:00+00:00"},
88478848
{"state": "30.0", "last_updated": "2024-10-04T12:00:00+00:00"},
88488849
]
8849-
8850-
result = my_predbat.minute_data(
8851-
history=history,
8852-
days=1,
8853-
now=now,
8854-
state_key="state",
8855-
last_updated_key="last_updated",
8856-
backwards=True,
8857-
smoothing=False
8858-
)
8850+
8851+
result = my_predbat.minute_data(history=history, days=1, now=now, state_key="state", last_updated_key="last_updated", backwards=True, smoothing=False)
88598852

88608853
points = [0, 5, 34, 35, 65]
88618854
result_points = [result.get(p) for p in points]
88628855
expected_points = [30.0, 20.0, 20.0, 10.0, 0.0]
88638856

88648857
# Check that we have data and it's reasonable
8865-
if len(result) != 24*60:
8858+
if len(result) != 24 * 60:
88668859
print("ERROR: Basic test failed - no data returned")
88678860
# Print result sorted by key for easier reading
88688861
failed = True
88698862
elif result_points != expected_points:
8870-
print("ERROR: Basic test failed - values incorrect, data points were {} expected {}".format(
8871-
result_points, expected_points))
8863+
print("ERROR: Basic test failed - values incorrect, data points were {} expected {}".format(result_points, expected_points))
88728864
for key in sorted(result.keys()):
88738865
print(" {}: {}".format(key, result[key]))
88748866
failed = True
8875-
8867+
88768868
# Test 2: Empty history
88778869
print("Test 2: Empty history")
8878-
empty_result = my_predbat.minute_data(
8879-
history=[],
8880-
days=1,
8881-
now=now,
8882-
state_key="state",
8883-
last_updated_key="last_updated"
8884-
)
8885-
8870+
empty_result = my_predbat.minute_data(history=[], days=1, now=now, state_key="state", last_updated_key="last_updated")
8871+
88868872
if empty_result != {}:
88878873
print("ERROR: Empty history test failed - should return empty dict")
88888874
failed = True
88898875
else:
88908876
print("Empty history test passed")
8891-
8877+
88928878
# Test 3: Scale parameter
88938879
print("Test 3: Scale parameter")
8894-
scaled_result = my_predbat.minute_data(
8895-
history=history,
8896-
days=1,
8897-
now=now,
8898-
state_key="state",
8899-
last_updated_key="last_updated",
8900-
backwards=True,
8901-
scale=2.0
8902-
)
8903-
8880+
scaled_result = my_predbat.minute_data(history=history, days=1, now=now, state_key="state", last_updated_key="last_updated", backwards=True, scale=2.0)
8881+
89048882
if 0 not in scaled_result:
89058883
print("ERROR: Scale test failed - no data at minute 0")
89068884
failed = True
89078885
elif scaled_result[0] != result[0] * 2.0:
89088886
print("Scale test passed - values scaled correctly from {} to {}".format(result[0], scaled_result[0]))
89098887
else:
89108888
print("Scale test passed")
8911-
8889+
89128890
# Test 4: Smoothing enabled vs disabled comparison
89138891
print("Test 4: Smoothing")
89148892
result = my_predbat.minute_data(
@@ -8922,20 +8900,18 @@ def test_minute_data(my_predbat):
89228900
)
89238901

89248902
points = [0, 5, 34, 35, 65]
8925-
expected_points = [30.0, 30.0, dp4(20.0 + 10/30), 20.0, 10.0]
8903+
expected_points = [30.0, 30.0, dp4(20.0 + 10 / 30), 20.0, 10.0]
89268904
result_points = [result.get(p) for p in points]
8927-
if len(result) != 24*60:
8905+
if len(result) != 24 * 60:
89288906
print("ERROR: Smoothing test failed - no data returned")
89298907
failed = True
89308908
elif result_points != expected_points:
8931-
print("ERROR: Smoothing test - unsmoothed values incorrect, data points were {} expected {}".format(
8932-
result_points, expected_points))
8909+
print("ERROR: Smoothing test - unsmoothed values incorrect, data points were {} expected {}".format(result_points, expected_points))
89338910
for key in sorted(result.keys()):
89348911
print(" {}: {}".format(key, result[key]))
89358912
failed = True
89368913

89378914
failed = True
8938-
89398915

89408916
# Test 4.1: Smoothing clean increment
89418917
print("Test 4.1: Smoothing clean increment")
@@ -8951,14 +8927,13 @@ def test_minute_data(my_predbat):
89518927
max_increment=0,
89528928
)
89538929
points = [0, 5, 34, 35, 65]
8954-
expected_points = [dp4(30.0 + 5*(10/30)), 30.0, dp4(20.0 + 10/30), 20.0, 10.0]
8930+
expected_points = [dp4(30.0 + 5 * (10 / 30)), 30.0, dp4(20.0 + 10 / 30), 20.0, 10.0]
89558931
result_points = [result.get(p) for p in points]
8956-
if len(result) != 24*60:
8932+
if len(result) != 24 * 60:
89578933
print("ERROR: Smoothing test failed - no data returned")
89588934
failed = True
89598935
elif result_points != expected_points:
8960-
print("ERROR: Smoothing test - unsmoothed values incorrect, data points were {} expected {}".format(
8961-
result_points, expected_points))
8936+
print("ERROR: Smoothing test - unsmoothed values incorrect, data points were {} expected {}".format(result_points, expected_points))
89628937
for key in sorted(result.keys()):
89638938
print(" {}: {}".format(key, result[key]))
89648939
failed = True
@@ -8969,49 +8944,29 @@ def test_minute_data(my_predbat):
89698944
{"attributes": {"power": "15.0"}, "last_updated": "2024-10-04T11:00:00+00:00"},
89708945
{"attributes": {"power": "25.0"}, "last_updated": "2024-10-04T11:30:00+00:00"},
89718946
]
8972-
8973-
attrs_result = my_predbat.minute_data(
8974-
history=history_with_attrs,
8975-
days=1,
8976-
now=now,
8977-
state_key="power",
8978-
last_updated_key="last_updated",
8979-
backwards=True,
8980-
attributes=True
8981-
)
8982-
8947+
8948+
attrs_result = my_predbat.minute_data(history=history_with_attrs, days=1, now=now, state_key="power", last_updated_key="last_updated", backwards=True, attributes=True)
8949+
89838950
if len(attrs_result) == 0:
89848951
print("ERROR: Attributes test failed - no data returned")
89858952
failed = True
89868953
else:
89878954
print("Attributes test passed - returned {} data points".format(len(attrs_result)))
8988-
8955+
89898956
# Test 6: Unit conversion
89908957
print("Test 6: Unit conversion")
89918958
history_with_units = [
8992-
{
8993-
"state": "1000.0",
8994-
"last_updated": "2024-10-04T11:00:00+00:00",
8995-
"attributes": {"unit_of_measurement": "W"}
8996-
},
8959+
{"state": "1000.0", "last_updated": "2024-10-04T11:00:00+00:00", "attributes": {"unit_of_measurement": "W"}},
89978960
]
8998-
8999-
converted_result = my_predbat.minute_data(
9000-
history=history_with_units,
9001-
days=1,
9002-
now=now,
9003-
state_key="state",
9004-
last_updated_key="last_updated",
9005-
backwards=True,
9006-
required_unit="kWh"
9007-
)
9008-
8961+
8962+
converted_result = my_predbat.minute_data(history=history_with_units, days=1, now=now, state_key="state", last_updated_key="last_updated", backwards=True, required_unit="kWh")
8963+
90098964
if len(converted_result) == 0:
90108965
print("ERROR: Unit conversion test failed - no data returned")
90118966
failed = True
90128967
else:
90138968
print("Unit conversion test passed - function handles unit conversion")
9014-
8969+
90158970
# Test 7: Invalid/unavailable data filtering
90168971
print("Test 7: Invalid data filtering")
90178972
history_with_invalid = [
@@ -9020,37 +8975,22 @@ def test_minute_data(my_predbat):
90208975
{"state": "unknown", "last_updated": "2024-10-04T11:30:00+00:00"},
90218976
{"state": "20.0", "last_updated": "2024-10-04T11:45:00+00:00"},
90228977
]
9023-
9024-
filtered_result = my_predbat.minute_data(
9025-
history=history_with_invalid,
9026-
days=1,
9027-
now=now,
9028-
state_key="state",
9029-
last_updated_key="last_updated",
9030-
backwards=True
9031-
)
9032-
8978+
8979+
filtered_result = my_predbat.minute_data(history=history_with_invalid, days=1, now=now, state_key="state", last_updated_key="last_updated", backwards=True)
8980+
90338981
# Function should filter out invalid data
90348982
if len(filtered_result) == 0:
90358983
print("ERROR: Invalid data filtering test failed - no data returned")
90368984
failed = True
90378985
else:
90388986
print("Invalid data filtering test passed - function filters invalid data")
9039-
8987+
90408988
# Test 8: Accumulate parameter
90418989
print("Test 8: Accumulate parameter")
90428990
accumulate_data = {0: 5.0, 30: 10.0, 60: 15.0}
9043-
9044-
accumulated_result = my_predbat.minute_data(
9045-
history=history,
9046-
days=1,
9047-
now=now,
9048-
state_key="state",
9049-
last_updated_key="last_updated",
9050-
backwards=True,
9051-
accumulate=accumulate_data
9052-
)
9053-
8991+
8992+
accumulated_result = my_predbat.minute_data(history=history, days=1, now=now, state_key="state", last_updated_key="last_updated", backwards=True, accumulate=accumulate_data)
8993+
90548994
if 0 not in accumulated_result:
90558995
print("ERROR: Accumulate test failed - no data at minute 0")
90568996
failed = True
@@ -9063,62 +9003,41 @@ def test_minute_data(my_predbat):
90639003
print("Accumulate test passed - values correctly accumulated")
90649004
else:
90659005
print("Accumulate test passed - function handles accumulation (original: {}, accumulated: {})".format(original, accumulated))
9066-
9006+
90679007
# Test 9: Forward time direction
90689008
print("Test 9: Forward time direction")
9069-
forward_result = my_predbat.minute_data(
9070-
history=history,
9071-
days=1,
9072-
now=now,
9073-
state_key="state",
9074-
last_updated_key="last_updated",
9075-
backwards=False
9076-
)
9077-
9009+
forward_result = my_predbat.minute_data(history=history, days=1, now=now, state_key="state", last_updated_key="last_updated", backwards=False)
9010+
90789011
# Function should handle forward direction without error
90799012
print("Forward time test passed - function handles forward direction")
9080-
9013+
90819014
# Test 10: Missing keys handling
90829015
print("Test 10: Missing keys handling")
90839016
history_missing_keys = [
90849017
{"state": "10.0"}, # Missing last_updated
90859018
{"last_updated": "2024-10-04T11:30:00+00:00"}, # Missing state
90869019
]
9087-
9088-
missing_keys_result = my_predbat.minute_data(
9089-
history=history_missing_keys,
9090-
days=1,
9091-
now=now,
9092-
state_key="state",
9093-
last_updated_key="last_updated",
9094-
backwards=True
9095-
)
9096-
9020+
9021+
missing_keys_result = my_predbat.minute_data(history=history_missing_keys, days=1, now=now, state_key="state", last_updated_key="last_updated", backwards=True)
9022+
90979023
# Function should handle missing keys gracefully
90989024
print("Missing keys test passed - function handles missing keys gracefully")
9099-
9025+
91009026
# Test 11: Different state key
91019027
print("Test 11: Different state key")
91029028
history_different_key = [
91039029
{"value": "50.0", "last_updated": "2024-10-04T11:00:00+00:00"},
91049030
{"value": "60.0", "last_updated": "2024-10-04T11:30:00+00:00"},
91059031
]
9106-
9107-
different_key_result = my_predbat.minute_data(
9108-
history=history_different_key,
9109-
days=1,
9110-
now=now,
9111-
state_key="value",
9112-
last_updated_key="last_updated",
9113-
backwards=True
9114-
)
9115-
9032+
9033+
different_key_result = my_predbat.minute_data(history=history_different_key, days=1, now=now, state_key="value", last_updated_key="last_updated", backwards=True)
9034+
91169035
if len(different_key_result) == 0:
91179036
print("ERROR: Different state key test failed - no data returned")
91189037
failed = True
91199038
else:
91209039
print("Different state key test passed")
9121-
9040+
91229041
print("**** minute_data tests completed ****")
91239042
return failed
91249043

0 commit comments

Comments
 (0)