Skip to content

Commit 0ba21e0

Browse files
committed
Simplify on_chrg_mgmt_data
1 parent cd78e1d commit 0ba21e0

File tree

1 file changed

+61
-55
lines changed

1 file changed

+61
-55
lines changed

src/status_publisher/charge/chrg_mgmt_data.py

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,7 @@ def on_chrg_mgmt_data(
7171
transform=lambda x: round(x, 3),
7272
)
7373

74-
obc_voltage = charge_mgmt_data.onBdChrgrAltrCrntInptVol
75-
obc_current = charge_mgmt_data.onBdChrgrAltrCrntInptCrnt
76-
if obc_voltage is not None and obc_current is not None:
77-
self._publish(
78-
topic=mqtt_topics.OBC_CURRENT,
79-
value=round(obc_current / 5.0, 1),
80-
)
81-
self._publish(
82-
topic=mqtt_topics.OBC_VOLTAGE,
83-
value=2 * obc_voltage,
84-
)
85-
self._publish(
86-
topic=mqtt_topics.OBC_POWER_SINGLE_PHASE,
87-
value=round(2.0 * obc_voltage * obc_current / 5.0, 1),
88-
)
89-
self._publish(
90-
topic=mqtt_topics.OBC_POWER_THREE_PHASE,
91-
value=round(math.sqrt(3) * 2 * obc_voltage * obc_current / 15.0, 1),
92-
)
93-
else:
94-
self._publish(
95-
topic=mqtt_topics.OBC_CURRENT,
96-
value=0.0,
97-
)
98-
self._publish(
99-
topic=mqtt_topics.OBC_VOLTAGE,
100-
value=0,
101-
)
74+
self.__publish_obc_data(charge_mgmt_data)
10275

10376
raw_charge_current_limit = charge_mgmt_data.bmsAltngChrgCrntDspCmd
10477
charge_current_limit: ChargeCurrentLimitCode | None = None
@@ -150,35 +123,9 @@ def on_chrg_mgmt_data(
150123
value=charge_mgmt_data.ccuOffBdChrgrPlugOn,
151124
)
152125

153-
scheduled_charging: ScheduledCharging | None = None
154-
if charge_mgmt_data is not None and (
155-
charge_mgmt_data.bmsReserStHourDspCmd is not None
156-
and charge_mgmt_data.bmsReserStMintueDspCmd is not None
157-
and charge_mgmt_data.bmsReserSpHourDspCmd is not None
158-
and charge_mgmt_data.bmsReserSpMintueDspCmd is not None
159-
):
160-
try:
161-
start_hour = charge_mgmt_data.bmsReserStHourDspCmd
162-
start_minute = charge_mgmt_data.bmsReserStMintueDspCmd
163-
start_time = datetime.time(hour=start_hour, minute=start_minute)
164-
end_hour = charge_mgmt_data.bmsReserSpHourDspCmd
165-
end_minute = charge_mgmt_data.bmsReserSpMintueDspCmd
166-
mode = ScheduledChargingMode(charge_mgmt_data.bmsReserCtrlDspCmd)
167-
self._publish(
168-
topic=mqtt_topics.DRIVETRAIN_CHARGING_SCHEDULE,
169-
value={
170-
"startTime": f"{start_hour:02d}:{start_minute:02d}",
171-
"endTime": f"{end_hour:02d}:{end_minute:02d}",
172-
"mode": mode.name,
173-
},
174-
)
175-
scheduled_charging = ScheduledCharging(start_time=start_time, mode=mode)
176-
177-
except ValueError:
178-
LOG.exception("Error parsing scheduled charging info")
126+
scheduled_charging = self.__publish_charging_schedule(charge_mgmt_data)
179127

180128
# Only publish remaining charging time if the car tells us the value is OK
181-
remaining_charging_time: int | None = None
182129
valid_remaining_time, remaining_charging_time = self._transform_and_publish(
183130
topic=mqtt_topics.DRIVETRAIN_REMAINING_CHARGING_TIME,
184131
value=charge_mgmt_data.chrgngRmnngTime,
@@ -220,3 +167,62 @@ def on_chrg_mgmt_data(
220167
power=charge_mgmt_data.decoded_power if is_valid_power else None,
221168
raw_soc=charge_mgmt_data.bmsPackSOCDsp,
222169
)
170+
171+
def __publish_charging_schedule(self, charge_mgmt_data: ChrgMgmtData) -> ScheduledCharging | None:
172+
scheduled_charging: ScheduledCharging | None = None
173+
if charge_mgmt_data is not None and (
174+
charge_mgmt_data.bmsReserStHourDspCmd is not None
175+
and charge_mgmt_data.bmsReserStMintueDspCmd is not None
176+
and charge_mgmt_data.bmsReserSpHourDspCmd is not None
177+
and charge_mgmt_data.bmsReserSpMintueDspCmd is not None
178+
):
179+
try:
180+
start_hour = charge_mgmt_data.bmsReserStHourDspCmd
181+
start_minute = charge_mgmt_data.bmsReserStMintueDspCmd
182+
start_time = datetime.time(hour=start_hour, minute=start_minute)
183+
end_hour = charge_mgmt_data.bmsReserSpHourDspCmd
184+
end_minute = charge_mgmt_data.bmsReserSpMintueDspCmd
185+
mode = ScheduledChargingMode(charge_mgmt_data.bmsReserCtrlDspCmd)
186+
self._publish(
187+
topic=mqtt_topics.DRIVETRAIN_CHARGING_SCHEDULE,
188+
value={
189+
"startTime": f"{start_hour:02d}:{start_minute:02d}",
190+
"endTime": f"{end_hour:02d}:{end_minute:02d}",
191+
"mode": mode.name,
192+
},
193+
)
194+
scheduled_charging = ScheduledCharging(start_time=start_time, mode=mode)
195+
196+
except ValueError:
197+
LOG.exception("Error parsing scheduled charging info")
198+
return scheduled_charging
199+
200+
def __publish_obc_data(self, charge_mgmt_data: ChrgMgmtData) -> None:
201+
obc_voltage = charge_mgmt_data.onBdChrgrAltrCrntInptVol
202+
obc_current = charge_mgmt_data.onBdChrgrAltrCrntInptCrnt
203+
if obc_voltage is not None and obc_current is not None:
204+
self._publish(
205+
topic=mqtt_topics.OBC_CURRENT,
206+
value=round(obc_current / 5.0, 1),
207+
)
208+
self._publish(
209+
topic=mqtt_topics.OBC_VOLTAGE,
210+
value=2 * obc_voltage,
211+
)
212+
self._publish(
213+
topic=mqtt_topics.OBC_POWER_SINGLE_PHASE,
214+
value=round(2.0 * obc_voltage * obc_current / 5.0, 1),
215+
)
216+
self._publish(
217+
topic=mqtt_topics.OBC_POWER_THREE_PHASE,
218+
value=round(math.sqrt(3) * 2 * obc_voltage * obc_current / 15.0, 1),
219+
)
220+
else:
221+
self._publish(
222+
topic=mqtt_topics.OBC_CURRENT,
223+
value=0.0,
224+
)
225+
self._publish(
226+
topic=mqtt_topics.OBC_VOLTAGE,
227+
value=0,
228+
)

0 commit comments

Comments
 (0)