Skip to content

Commit 72b4022

Browse files
authored
metadata for optional (#2976)
1 parent cef39d7 commit 72b4022

7 files changed

Lines changed: 62 additions & 54 deletions

File tree

packages/control/ocpp.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ def _process_call(self: OptionalProtocol,
2727
fault_state: FaultState,
2828
func: Callable) -> Optional[websockets.WebSocketClientProtocol]:
2929
async def make_call() -> websockets.WebSocketClientProtocol:
30-
async with websockets.connect(self.data.ocpp.url+chargebox_id,
31-
subprotocols=[self.data.ocpp.version]) as ws:
30+
url = self.data.ocpp.config.url
31+
async with websockets.connect(f"{url}{'' if url.endswith('/') else '/'}{chargebox_id}",
32+
subprotocols=[self.data.ocpp.config.version]) as ws:
3233
try:
3334
cp = OcppChargepoint(chargebox_id, ws, 2)
3435
await cp.call(func)
@@ -37,7 +38,7 @@ async def make_call() -> websockets.WebSocketClientProtocol:
3738
pass
3839
return ws
3940
try:
40-
if self.data.ocpp.active and chargebox_id:
41+
if self.data.ocpp.config.active and chargebox_id:
4142
return asyncio.run(make_call())
4243
except websockets.exceptions.InvalidStatusCode:
4344
fault_state.warning(f"Chargebox ID {chargebox_id} konnte nicht im OCPP-Backend gefunden werden oder "

packages/control/ocpp_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
@pytest.fixture()
1414
def mock_data() -> None:
1515
data.data_init(Mock())
16-
data.data.optional_data.data.ocpp.active = True
17-
data.data.optional_data.data.ocpp.url = "ws://localhost:9000/"
16+
data.data.optional_data.data.ocpp.config.active = True
17+
data.data.optional_data.data.ocpp.config.url = "ws://localhost:9000/"
1818

1919

2020
def test_start_transaction(mock_data, monkeypatch):

packages/control/optional.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,10 @@ def remove(price_data: Dict) -> Dict:
157157
if len(self.data.electricity_pricing.get.prices) >= 0:
158158
ep = self.data.electricity_pricing
159159
ep.get.prices = remove(ep.get.prices)
160-
Pub().pub("openWB/set/optional/ep/get/prices", ep.get.prices)
161160
if self._flexible_tariff_module:
162161
ep.flexible_tariff.get.prices = remove(ep.flexible_tariff.get.prices)
163-
Pub().pub("openWB/set/optional/ep/flexible_tariff/get/prices", ep.flexible_tariff.get.prices)
164162
if self._grid_fee_module:
165163
ep.grid_fee.get.prices = remove(ep.grid_fee.get.prices)
166-
Pub().pub("openWB/set/optional/ep/grid_fee/get/prices", ep.grid_fee.get.prices)
167164
except Exception:
168165
log.exception("Fehler beim Entfernen veralteter Preise")
169166

@@ -255,7 +252,7 @@ def et_price_update_required(self) -> bool:
255252

256253
def ocpp_transfer_meter_values(self):
257254
try:
258-
if self.data.ocpp.active:
255+
if self.data.ocpp.config.active:
259256
thread_handler(Thread(target=self._transfer_meter_values, args=(), name="OCPP Client"))
260257
except Exception as e:
261258
log.exception("Fehler im OCPP-Optional-Modul: %s", e)
@@ -264,13 +261,12 @@ def _transfer_meter_values(self):
264261
for cp in data.data.cp_data.values():
265262
try:
266263
if self.data.ocpp.boot_notification_sent is False:
264+
self.data.ocpp.boot_notification_sent = True
267265
# Boot-Notification nicht in der init-Funktion aufrufen, da noch nicht alles initialisiert ist
268266
self.boot_notification(cp.data.config.ocpp_chargebox_id,
269267
cp.chargepoint_module.fault_state,
270268
cp.chargepoint_module.config.type,
271269
cp.data.get.serial_number)
272-
self.data.ocpp.boot_notification_sent = True
273-
Pub().pub("openWB/set/optional/ocpp/boot_notification_sent", True)
274270
if cp.data.set.ocpp_transaction_id is not None:
275271
self.send_heart_beat(cp.data.config.ocpp_chargebox_id, cp.chargepoint_module.fault_state)
276272
self.transfer_values(cp.data.config.ocpp_chargebox_id,

packages/control/optional_data.py

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,31 @@
1414

1515
@dataclass
1616
class PricingGet:
17-
fault_state: int = 0
18-
fault_str: str = NO_ERROR
17+
fault_state: int = field(default=0)
18+
fault_str: str = field(default=NO_ERROR)
1919
prices: Dict = field(default_factory=empty_dict_factory)
2020

2121

22-
def get_factory() -> PricingGet:
23-
return PricingGet()
22+
def create_pricing_get_with_topics(topic_prefix: str) -> PricingGet:
23+
"""Factory function to create PricingGet with custom topic prefix"""
24+
pricing_get = PricingGet()
25+
pricing_get.__dataclass_fields__['fault_state'].metadata = {"topic": f"{topic_prefix}/get/fault_state"}
26+
pricing_get.__dataclass_fields__['fault_str'].metadata = {"topic": f"{topic_prefix}/get/fault_str"}
27+
pricing_get.__dataclass_fields__['prices'].metadata = {"topic": f"{topic_prefix}/get/prices"}
28+
return pricing_get
29+
30+
31+
def flexible_tariff_get_factory() -> PricingGet:
32+
return create_pricing_get_with_topics("ep/flexible_tariff")
33+
34+
35+
def grid_fee_get_factory() -> PricingGet:
36+
return create_pricing_get_with_topics("ep/grid_fee")
2437

2538

2639
@dataclass
2740
class FlexibleTariff:
28-
get: PricingGet = field(default_factory=get_factory)
41+
get: PricingGet = field(default_factory=flexible_tariff_get_factory)
2942

3043

3144
def get_flexible_tariff_factory() -> FlexibleTariff:
@@ -34,7 +47,7 @@ def get_flexible_tariff_factory() -> FlexibleTariff:
3447

3548
@dataclass
3649
class GridFee:
37-
get: PricingGet = field(default_factory=get_factory)
50+
get: PricingGet = field(default_factory=grid_fee_get_factory)
3851

3952

4053
def get_grid_fee_factory() -> GridFee:
@@ -43,8 +56,8 @@ def get_grid_fee_factory() -> GridFee:
4356

4457
@dataclass
4558
class ElectricityPricingGet:
46-
next_query_time: Optional[float] = None
47-
_prices: Dict = field(default_factory=empty_dict_factory)
59+
next_query_time: Optional[float] = field(default=None, metadata={"topic": "ep/next_query_time"})
60+
_prices: Dict = field(default_factory=empty_dict_factory, metadata={"topic": "ep/prices"})
4861

4962
@property
5063
def prices(self) -> Dict:
@@ -78,7 +91,7 @@ def electricity_pricing_get_factory() -> ElectricityPricingGet:
7891

7992
@dataclass
8093
class ElectricityPricing:
81-
configured: bool = False
94+
configured: bool = field(default=False, metadata={"topic": "ep/configured"})
8295
flexible_tariff: FlexibleTariff = field(default_factory=get_flexible_tariff_factory)
8396
grid_fee: GridFee = field(default_factory=get_grid_fee_factory)
8497
get: ElectricityPricingGet = field(default_factory=electricity_pricing_get_factory)
@@ -88,55 +101,52 @@ def ep_factory() -> ElectricityPricing:
88101
return ElectricityPricing()
89102

90103

104+
def cards_display_theme_factory() -> CardsDisplayTheme:
105+
return CardsDisplayTheme()
106+
107+
91108
@dataclass
92109
class InternalDisplay:
93-
active: bool = False
94-
on_if_plugged_in: bool = True
95-
pin_active: bool = False
96-
pin_code: str = "0000"
97-
standby: int = 60
98-
theme: CardsDisplayTheme = CardsDisplayTheme()
110+
active: bool = field(default=False, metadata={"topic": "int_display/active"})
111+
detected: bool = field(default=False, metadata={"topic": "int_display/detected"})
112+
on_if_plugged_in: bool = field(default=True, metadata={"topic": "int_display/on_if_plugged_in"})
113+
only_local_charge_points: bool = field(default=False, metadata={"topic": "int_display/only_local_charge_points"})
114+
pin_active: bool = field(default=False, metadata={"topic": "int_display/pin_active"})
115+
pin_code: str = field(default="0000", metadata={"topic": "int_display/pin_code"})
116+
rotation: int = field(default=0, metadata={"topic": "int_display/rotation"})
117+
standby: int = field(default=60, metadata={"topic": "int_display/standby"})
118+
theme: CardsDisplayTheme = field(default_factory=cards_display_theme_factory,
119+
metadata={"topic": "int_display/theme"})
99120

100121

101122
def int_display_factory() -> InternalDisplay:
102123
return InternalDisplay()
103124

104125

105-
@dataclass
106-
class Led:
107-
active: bool = False
108-
109-
110-
def led_factory() -> Led:
111-
return Led()
112-
113-
114126
@dataclass
115127
class Rfid:
116-
active: bool = False
128+
active: bool = field(default=False, metadata={"topic": "rfid/active"})
117129

118130

119131
def rfid_factory() -> Rfid:
120132
return Rfid()
121133

122134

123135
@dataclass
124-
class Ocpp:
136+
class OcppConfig:
125137
active: bool = False
126-
boot_notification_sent: bool = False
127-
_url: Optional[str] = None
138+
url: Optional[str] = None
128139
version: str = "ocpp1.6"
129140

130-
@property
131-
def url(self) -> Optional[str]:
132-
return self._url
133141

134-
@url.setter
135-
def url(self, value: Optional[str]):
136-
if value is not None and not value.endswith("/"):
137-
self._url = value + "/"
138-
else:
139-
self._url = value
142+
def ocpp_config_factory() -> OcppConfig:
143+
return OcppConfig()
144+
145+
146+
@dataclass
147+
class Ocpp:
148+
config: OcppConfig = field(default_factory=ocpp_config_factory, metadata={"topic": "ocpp/config"})
149+
boot_notification_sent: bool = field(default=False, metadata={"topic": "ocpp/boot_notification_sent"})
140150

141151

142152
def ocpp_factory() -> Ocpp:
@@ -147,9 +157,8 @@ def ocpp_factory() -> Ocpp:
147157
class OptionalData:
148158
electricity_pricing: ElectricityPricing = field(default_factory=ep_factory)
149159
int_display: InternalDisplay = field(default_factory=int_display_factory)
150-
led: Led = field(default_factory=led_factory)
151160
rfid: Rfid = field(default_factory=rfid_factory)
152-
dc_charging: bool = False
161+
dc_charging: bool = field(default=False, metadata={"topic": "dc_charging"})
153162
ocpp: Ocpp = field(default_factory=ocpp_factory)
154163

155164

packages/helpermodules/changed_values_handler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def pub_changed_values(self):
8383
data.data.cp_all_data.data.get)
8484
self._update_value("openWB/set/counter/", self.prev_data.counter_all_data.data,
8585
data.data.counter_all_data.data)
86+
self._update_value("openWB/set/optional/", self.prev_data.optional_data.data,
87+
data.data.optional_data.data)
8688
for key, value in data.data.cp_data.items():
8789
self._update_value(f"openWB/set/chargepoint/{value.num}/", self.prev_data.cp_data[key].data, value.data)
8890
for key, value in data.data.bat_data.items():

packages/helpermodules/subdata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage):
700700
self.set_json_payload_class(var.data.led, msg)
701701
elif re.search("/optional/rfid/", msg.topic) is not None:
702702
self.set_json_payload_class(var.data.rfid, msg)
703+
elif re.search("/optional/ocpp/config", msg.topic) is not None:
704+
self.set_json_payload_class(var.data.ocpp.config, msg)
703705
elif re.search("/optional/ocpp/", msg.topic) is not None:
704706
self.set_json_payload_class(var.data.ocpp, msg)
705707
elif re.search("/optional/int_display/", msg.topic) is not None:

packages/helpermodules/update_config.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from control.ev import ev
4242
from control.ev.ev_template import EvTemplateData
4343
from control.general import Prices, PvCharging
44-
from control.optional_data import Ocpp
44+
from control.optional_data import OcppConfig
4545
from modules.common.abstract_vehicle import GeneralVehicleConfig
4646
from modules.common.component_type import ComponentType
4747
from modules.devices.sungrow.sungrow.version import Version
@@ -325,7 +325,6 @@ class UpdateConfig:
325325
"^openWB/optional/int_display/rotation$",
326326
"^openWB/optional/int_display/theme$",
327327
"^openWB/optional/int_display/only_local_charge_points",
328-
"^openWB/optional/led/active$",
329328
"^openWB/optional/monitoring/config$",
330329
"^openWB/optional/rfid/active$",
331330
"^openWB/optional/ocpp/config$",
@@ -584,9 +583,8 @@ class UpdateConfig:
584583
("openWB/optional/int_display/rotation", 0),
585584
("openWB/optional/int_display/theme", dataclass_utils.asdict(CardsDisplayTheme())),
586585
("openWB/optional/int_display/only_local_charge_points", False),
587-
("openWB/optional/led/active", False),
588586
("openWB/optional/monitoring/config", NO_MODULE),
589-
("openWB/optional/ocpp/config", dataclass_utils.asdict(Ocpp())),
587+
("openWB/optional/ocpp/config", dataclass_utils.asdict(OcppConfig())),
590588
("openWB/optional/rfid/active", False),
591589
("openWB/system/backup_password", None),
592590
("openWB/system/backup_cloud/config", NO_MODULE),

0 commit comments

Comments
 (0)