Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions packages/helpermodules/update_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

class UpdateConfig:

DATASTORE_VERSION = 94
DATASTORE_VERSION = 95

valid_topic = [
"^openWB/bat/config/bat_control_permitted$",
Expand Down Expand Up @@ -647,7 +647,7 @@ def __update_topic(self, topic: str, payload):
if payload == "":
del self.all_received_topics[topic]
else:
self.all_received_topics[topic] = payload
self.all_received_topics[topic] = copy.deepcopy(payload)

def __remove_outdated_topics(self):
""" remove outdated topics from all_received_topics and broker
Expand Down Expand Up @@ -1967,7 +1967,7 @@ def upgrade(topic: str, payload) -> None:
# replace smarteq soc module by no_module
if payload.get("type") == "smarteq":
payload = NO_MODULE
Pub().pub(topic, payload)
return {topic: payload}
self._loop_all_received_topics(upgrade)
self.__update_topic("openWB/system/datastore_version", 71)

Expand All @@ -1983,7 +1983,7 @@ def upgrade(topic: str, payload) -> None:
payload["configuration"]["firmware"] = "v1"
elif payload["configuration"].get("firmware") == "v112":
payload["configuration"]["firmware"] = "v2"
Pub().pub(topic, payload)
return {topic: payload}
self._loop_all_received_topics(upgrade)
self.__update_topic("openWB/system/datastore_version", 72)

Expand All @@ -1994,7 +1994,7 @@ def upgrade(topic: str, payload) -> None:
# replace bmw soc module by no_module
if payload.get("type") == "bmw":
payload = NO_MODULE
Pub().pub(topic, payload)
return {topic: payload}
self._loop_all_received_topics(upgrade)
self.__update_topic("openWB/system/datastore_version", 73)

Expand All @@ -2017,7 +2017,7 @@ def upgrade(topic: str, payload) -> None:
if payload.get("type") == "solax":
if "version" not in payload["configuration"]:
payload["configuration"].update({"version": "g3"})
Pub().pub(topic, payload)
return {topic: payload}
self._loop_all_received_topics(upgrade)
self.__update_topic("openWB/system/datastore_version", 75)

Expand Down Expand Up @@ -2129,8 +2129,8 @@ def upgrade(topic: str, payload) -> Optional[dict]:
component_topic):
component_config = decode_payload(component_payload)
if "counter" == component_config["type"]:
Pub().pub((f"openWB/system/device/{device_config['id']}/component/"
f"{component_config['id']}/simulation"), "")
return {(f"openWB/system/device/{device_config['id']}/component/"
f"{component_config['id']}/simulation"): ""}
self._loop_all_received_topics(upgrade)
self.__update_topic("openWB/system/datastore_version", 80)

Expand Down Expand Up @@ -2333,12 +2333,12 @@ def upgrade_datastore_86(self) -> None:

def upgrade_datastore_87(self) -> None:
def upgrade(topic: str, payload) -> None:
if (re.search("openWB/vehicle/template/charge_template/[0-9]+", topic) is not None or
re.search("openWB/vehicle/template/ev_template/[0-9]+", topic) is not None):
if (re.search("openWB/vehicle/template/charge_template/[0-9]+$", topic) is not None or
re.search("openWB/vehicle/template/ev_template/[0-9]+$", topic) is not None):
payload = decode_payload(payload)
index = int(get_index(topic))
payload.update({"id": index})
Pub().pub(topic, payload)
return {topic: payload}
self._loop_all_received_topics(upgrade)
self.__update_topic("openWB/system/datastore_version", 88)

Expand Down Expand Up @@ -2430,6 +2430,7 @@ def upgrade_datastore_93(self) -> None:
# Pläne die keinen plans Key haben, id=None
max_id = -1
none_id = False
modified_topics = {}
for topic, payload in self.all_received_topics.items():
if re.search("openWB/vehicle/template/charge_template/[0-9]+$", topic) is not None:
payload = decode_payload(payload)
Expand All @@ -2444,8 +2445,7 @@ def upgrade_datastore_93(self) -> None:
raise TypeError(f"Plan {plan} hat keinen Key 'id' und ist kein NoneType.")
except KeyError:
payload["chargemode"]["scheduled_charging"].update({"plans": []})
self.all_received_topics[topic] = json.dumps(payload, ensure_ascii=False).encode("utf-8")
Pub().pub(f"openWB/set/vehicle/template/charge_template/{get_index(topic)}", payload)
modified_topics[f"openWB/set/vehicle/template/charge_template/{get_index(topic)}"] = payload
if none_id:
for topic, payload in self.all_received_topics.items():
if re.search("openWB/vehicle/template/charge_template/[0-9]+$", topic) is not None:
Expand All @@ -2454,8 +2454,7 @@ def upgrade_datastore_93(self) -> None:
if plan["id"] is None:
plan["id"] = max_id + 1
max_id += 1
self.all_received_topics[topic] = json.dumps(payload, ensure_ascii=False).encode("utf-8")
Pub().pub(f"openWB/set/vehicle/template/charge_template/{get_index(topic)}", payload)
modified_topics[f"openWB/vehicle/template/charge_template/{get_index(topic)}"] = payload

max_id = -1
none_id = False
Expand All @@ -2473,8 +2472,7 @@ def upgrade_datastore_93(self) -> None:
raise TypeError(f"Plan {plan} hat keinen Key 'id' und ist kein NoneType.")
except KeyError:
payload["time_charging"].update({"plans": []})
self.all_received_topics[topic] = json.dumps(payload, ensure_ascii=False).encode("utf-8")
Pub().pub(f"openWB/set/vehicle/template/charge_template/{get_index(topic)}", payload)
modified_topics[f"openWB/vehicle/template/charge_template/{get_index(topic)}"] = payload
if none_id:
for topic, payload in self.all_received_topics.items():
if re.search("openWB/vehicle/template/charge_template/[0-9]+$", topic) is not None:
Expand All @@ -2483,8 +2481,7 @@ def upgrade_datastore_93(self) -> None:
if plan["id"] is None:
plan["id"] = max_id + 1
max_id += 1
self.all_received_topics[topic] = json.dumps(payload, ensure_ascii=False).encode("utf-8")
Pub().pub(f"openWB/set/vehicle/template/charge_template/{get_index(topic)}", payload)
modified_topics[f"openWB/vehicle/template/charge_template/{get_index(topic)}"] = payload

max_id = -1
none_id = False
Expand All @@ -2502,8 +2499,7 @@ def upgrade_datastore_93(self) -> None:
raise TypeError(f"Plan {plan} hat keinen Key 'id' und ist kein NoneType.")
except KeyError:
payload["autolock"].update({"plans": []})
self.all_received_topics[topic] = json.dumps(payload, ensure_ascii=False).encode("utf-8")
Pub().pub(f"openWB/set/chargepoint/template/{get_index(topic)}", payload)
modified_topics[f"openWB/chargepoint/template/{get_index(topic)}"] = payload
if none_id:
for topic, payload in self.all_received_topics.items():
if re.search("openWB/chargepoint/template/[0-9]+$", topic) is not None:
Expand All @@ -2512,6 +2508,30 @@ def upgrade_datastore_93(self) -> None:
if plan["id"] is None:
plan["id"] = max_id + 1
max_id += 1
self.all_received_topics[topic] = json.dumps(payload, ensure_ascii=False).encode("utf-8")
Pub().pub(f"openWB/set/chargepoint/template/{get_index(topic)}", payload)
modified_topics[f"openWB/chargepoint/template/{get_index(topic)}"] = payload
for topic, payload in modified_topics.items():
self.__update_topic(topic, payload)
self.__update_topic("openWB/system/datastore_version", 94)

def upgrade_datastore_94(self):
def upgrade(topic, payload):
ids = []
if re.search("openWB/vehicle/template/charge_template/[0-9]+$", topic) is not None:
payload = decode_payload(payload)
for plan in payload["chargemode"]["scheduled_charging"]["plans"]:
if plan["id"] is not None:
ids.append(plan["id"])
ids.sort()
unique_ids = set(ids)
if len(ids) != len(unique_ids):
max_id = decode_payload(
self.all_received_topics["openWB/command/max_id/charge_template_scheduled_plan"])
for plan in payload["chargemode"]["scheduled_charging"]["plans"]:
try:
unique_ids.remove(plan["id"])
except KeyError:
max_id += 1
plan["id"] = max_id
return {topic: payload, "openWB/command/max_id/charge_template_scheduled_plan": max_id}
self._loop_all_received_topics(upgrade)
self.__update_topic("openWB/system/datastore_version", 95)
25 changes: 25 additions & 0 deletions packages/helpermodules/update_config_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import json
from pathlib import Path

import pytest
from helpermodules.update_config import UpdateConfig


Expand Down Expand Up @@ -28,3 +32,24 @@ def test_remove_invalid_topics(mock_pub):
assert len(mock_pub.method_calls) == 2
assert mock_pub.method_calls[0][1][0] == 'openWB/chargepoint/5/get/voltages'
assert mock_pub.method_calls[1][1][0] == 'openWB/optional/int_display/theme'


@pytest.mark.parametrize("index_test_template, expected_index", [
pytest.param(0, [2, 1], id="IDs korrekt"),
pytest.param(1, [0, 3], id="IDs gleich"),
pytest.param(2, [], id="keine Pläne"),
])
def test_upgrade_datastore_94(index_test_template, expected_index):
update_con = UpdateConfig()
update_con.all_received_topics = {"openWB/command/max_id/charge_template_scheduled_plan": 2}
with open(Path(__file__).resolve().parents[0]/"upgrade_datastore_94.json", "r") as f:
test_data = f.read()
update_con.all_received_topics.update(json.loads(test_data)[index_test_template])

update_con.upgrade_datastore_94()

plan_ids = []
for plan in update_con.all_received_topics["openWB/vehicle/template/charge_template/0"]["chargemode"][
"scheduled_charging"]["plans"]:
plan_ids.append(plan["id"])
assert plan_ids == expected_index
3 changes: 3 additions & 0 deletions packages/helpermodules/upgrade_datastore_94.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[{"openWB/vehicle/template/charge_template/0": {"name": "Standard-Lade-Profil", "prio": false, "load_default": false, "time_charging": {"active": false, "plans": []}, "chargemode": {"selected": "stop", "pv_charging": {"dc_min_current": 145, "dc_min_soc_current": 145, "min_soc_current": 10, "min_current": 0, "feed_in_limit": false, "min_soc": 0, "phases_to_use": 0, "phases_to_use_min_soc": 3, "limit": {"selected": "soc", "amount": 1000, "soc": 100}}, "scheduled_charging": {"plans": [{"active": true, "frequency": {"selected": "daily", "once": "2021-11-01", "weekly": [false, false, false, false, false, false, false]}, "current": 14, "dc_current": 145, "id": 2, "name": "neuer Zielladen-Plan", "limit": {"selected": "amount", "amount": 1000, "soc_limit": 90, "soc_scheduled": 80}, "time": "07: 00", "phases_to_use": 0, "phases_to_use_pv": 0, "et_active": false, "bidi_power": 10000, "bidi_charging_enabled": false}, {"active": true, "frequency": {"selected": "daily", "once": "2021-11-01", "weekly": [false, false, false, false, false, false, false]}, "current": 14, "dc_current": 145, "id": 1, "name": "neuer Zielladen-Plan", "limit": {"selected": "amount", "amount": 1000, "soc_limit": 90, "soc_scheduled": 80}, "time": "07:00", "phases_to_use": 0, "phases_to_use_pv": 0, "et_active": false, "bidi_power": 10000, "bidi_charging_enabled": false}]}, "instant_charging": {"current": 10, "dc_current": 145, "limit": {"selected": "none", "amount": 1000, "soc": 50}, "phases_to_use": 3}, "eco_charging": {"current": 6, "dc_current": 145, "limit": {"selected": "none", "amount": 1000, "soc": 50}, "max_price": 0.0002, "phases_to_use": 3}}, "id": 0}},
{"openWB/vehicle/template/charge_template/0": {"name": "Standard-Lade-Profil", "prio": false, "load_default": false, "time_charging": {"active": false, "plans": []}, "chargemode": {"selected": "stop", "pv_charging": {"dc_min_current": 145, "dc_min_soc_current": 145, "min_soc_current": 10, "min_current": 0, "feed_in_limit": false, "min_soc": 0, "phases_to_use": 0, "phases_to_use_min_soc": 3, "limit": {"selected": "soc", "amount": 1000, "soc": 100}}, "scheduled_charging": {"plans": [{"active": true, "frequency": {"selected": "daily", "once": "2021-11-01", "weekly": [false, false, false, false, false, false, false]}, "current": 14, "dc_current": 145, "id": 0, "name": "neuer Zielladen-Plan", "limit": {"selected": "amount", "amount": 1000, "soc_limit": 90, "soc_scheduled": 80}, "time": "07: 00", "phases_to_use": 0, "phases_to_use_pv": 0, "et_active": false, "bidi_power": 10000, "bidi_charging_enabled": false}, {"active": true, "frequency": {"selected": "daily", "once": "2021-11-01", "weekly": [false, false, false, false, false, false, false]}, "current": 14, "dc_current": 145, "id": 0, "name": "neuer Zielladen-Plan", "limit": {"selected": "amount", "amount": 1000, "soc_limit": 90, "soc_scheduled": 80}, "time": "07:00", "phases_to_use": 0, "phases_to_use_pv": 0, "et_active": false, "bidi_power": 10000, "bidi_charging_enabled": false}]}, "instant_charging": {"current": 10, "dc_current": 145, "limit": {"selected": "none", "amount": 1000, "soc": 50}, "phases_to_use": 3}, "eco_charging": {"current": 6, "dc_current": 145, "limit": {"selected": "none", "amount": 1000, "soc": 50}, "max_price": 0.0002, "phases_to_use": 3}}, "id": 0}},
{"openWB/vehicle/template/charge_template/0": {"name": "Standard-Lade-Profil", "prio": false, "load_default": false, "time_charging": {"active": false, "plans": []}, "chargemode": {"selected": "stop", "pv_charging": {"dc_min_current": 145, "dc_min_soc_current": 145, "min_soc_current": 10, "min_current": 0, "feed_in_limit": false, "min_soc": 0, "phases_to_use": 0, "phases_to_use_min_soc": 3, "limit": {"selected": "soc", "amount": 1000, "soc": 100}}, "scheduled_charging": {"plans": []}, "instant_charging": {"current": 10, "dc_current": 145, "limit": {"selected": "none", "amount": 1000, "soc": 50}, "phases_to_use": 3}, "eco_charging": {"current": 6, "dc_current": 145, "limit": {"selected": "none", "amount": 1000, "soc": 50}, "max_price": 0.0002, "phases_to_use": 3}}, "id": 0}}]