Skip to content

Commit 7e2ec48

Browse files
[pre-commit.ci lite] apply automatic fixes
1 parent 27da0e7 commit 7e2ec48

File tree

1 file changed

+83
-89
lines changed

1 file changed

+83
-89
lines changed

apps/predbat/predbat.py

Lines changed: 83 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -4150,33 +4150,32 @@ def get_html_plan_header(self, plan_debug):
41504150
html += "</tr>"
41514151
return html
41524152

4153-
41544153
def publish_json_plan(self, pv_forecast_minute_step, pv_forecast_minute_step10, load_minutes_step, load_minutes_step10, end_record):
41554154
output = {}
41564155

41574156
plan_debug = self.get_arg("plan_debug")
41584157
"""
41594158
Publish the current plan in JSON format
41604159
"""
4161-
output['plan_starts'] = self.now_utc.strftime("%Y-%m-%d %H:%M")
4162-
output['last_updated'] = self.now_utc_real.strftime("%H:%M:%S")
4163-
output['version'] = THIS_VERSION
4164-
output['config'] = {}
4165-
output['config']['best_soc_min'] = self.best_soc_min
4166-
output['config']['best_soc_max'] = self.best_soc_max
4167-
output['config']['best_soc_keep'] = self.best_soc_keep
4168-
output['config']['carbon_metric'] = self.carbon_metric
4169-
output['config']['metric_self_sufficiency'] = self.metric_self_sufficiency
4170-
output['config']['metric_battery_value_scaling'] = self.metric_battery_value_scaling
4171-
output['config']['currency'] = self.currency_symbols
4160+
output["plan_starts"] = self.now_utc.strftime("%Y-%m-%d %H:%M")
4161+
output["last_updated"] = self.now_utc_real.strftime("%H:%M:%S")
4162+
output["version"] = THIS_VERSION
4163+
output["config"] = {}
4164+
output["config"]["best_soc_min"] = self.best_soc_min
4165+
output["config"]["best_soc_max"] = self.best_soc_max
4166+
output["config"]["best_soc_keep"] = self.best_soc_keep
4167+
output["config"]["carbon_metric"] = self.carbon_metric
4168+
output["config"]["metric_self_sufficiency"] = self.metric_self_sufficiency
4169+
output["config"]["metric_battery_value_scaling"] = self.metric_battery_value_scaling
4170+
output["config"]["currency"] = self.currency_symbols
41724171

41734172
minute_now_align = int(self.minutes_now / 30) * 30
41744173
end_plan = min(end_record, self.forecast_minutes) + minute_now_align
41754174
rowspan = 0
41764175
in_span = False
41774176
start_span = False
41784177

4179-
output['slots'] = []
4178+
output["slots"] = []
41804179

41814180
for minute in range(minute_now_align, end_plan, 30):
41824181
slot = {}
@@ -4263,123 +4262,120 @@ def publish_json_plan(self, pv_forecast_minute_step, pv_forecast_minute_step10,
42634262
metric_end = self.predict_metric_best.get(minute_relative_slot_end, metric_start)
42644263
metric_change = metric_end - metric_start
42654264

4266-
slot['state'] = {}
4267-
4268-
slot['state']['soc'] = {}
4269-
slot['state']['soc']['percent'] = soc_percent
4270-
slot['state']['soc']['percent_end'] = soc_percent_end
4271-
slot['state']['soc']['percent_end_window'] = soc_percent_end_window
4272-
slot['state']['soc']['percent_max'] = soc_percent_max
4273-
slot['state']['soc']['percent_min'] = soc_percent_min
4274-
slot['state']['soc']['percent_max_window'] = soc_percent_max_window
4275-
slot['state']['soc']['percent_min_window'] = soc_percent_min_window
4276-
slot['state']['soc']['change'] = self.dp2(soc_change)
4265+
slot["state"] = {}
42774266

4267+
slot["state"]["soc"] = {}
4268+
slot["state"]["soc"]["percent"] = soc_percent
4269+
slot["state"]["soc"]["percent_end"] = soc_percent_end
4270+
slot["state"]["soc"]["percent_end_window"] = soc_percent_end_window
4271+
slot["state"]["soc"]["percent_max"] = soc_percent_max
4272+
slot["state"]["soc"]["percent_min"] = soc_percent_min
4273+
slot["state"]["soc"]["percent_max_window"] = soc_percent_max_window
4274+
slot["state"]["soc"]["percent_min_window"] = soc_percent_min_window
4275+
slot["state"]["soc"]["change"] = self.dp2(soc_change)
42784276

42794277
if minute in self.manual_idle_times:
4280-
slot['state']['override'] = True
4278+
slot["state"]["override"] = True
42814279

42824280
split = False
42834281

4284-
slot['pv'] = {}
4285-
slot['pv']['forecast'] = pv_forecast
4286-
slot['pv']['forecast10'] = pv_forecast10
4282+
slot["pv"] = {}
4283+
slot["pv"]["forecast"] = pv_forecast
4284+
slot["pv"]["forecast10"] = pv_forecast10
42874285

42884286
if plan_debug and pv_forecast10 > 0.0:
4289-
slot['pv']['debug'] = str(pv_forecast10)
4287+
slot["pv"]["debug"] = str(pv_forecast10)
42904288

4291-
slot['load'] = {}
4292-
slot['load']['forecast'] = load_forecast
4293-
slot['load']['forecast10'] = load_forecast10
4289+
slot["load"] = {}
4290+
slot["load"]["forecast"] = load_forecast
4291+
slot["load"]["forecast10"] = load_forecast10
42944292

42954293
if plan_debug and load_forecast10 > 0.0:
4296-
slot['load']['debug'] = str(load_forecast10)
4294+
slot["load"]["debug"] = str(load_forecast10)
42974295

42984296
if charge_window_n >= 0:
42994297
limit = self.charge_limit_best[charge_window_n]
43004298
limit_percent = int(self.charge_limit_percent_best[charge_window_n])
43014299
if limit > 0.0:
43024300
if self.set_charge_freeze and (limit == self.reserve):
4303-
slot['state']['mode'] = 'Freeze charge'
4301+
slot["state"]["mode"] = "Freeze charge"
43044302
limit_percent = soc_percent
43054303
elif limit_percent == soc_percent_min_window:
4306-
slot['state']['mode'] = 'Hold charge'
4304+
slot["state"]["mode"] = "Hold charge"
43074305
elif limit_percent < soc_percent_min_window:
4308-
slot['state']['mode'] = 'No charge'
4306+
slot["state"]["mode"] = "No charge"
43094307
else:
4310-
slot['state']['mode'] = 'Charge'
4308+
slot["state"]["mode"] = "Charge"
43114309

43124310
if self.charge_window_best[charge_window_n]["start"] in self.manual_charge_times:
4313-
slot['state']['override'] = True
4311+
slot["state"]["override"] = True
43144312
elif self.charge_window_best[charge_window_n]["start"] in self.manual_freeze_charge_times:
4315-
slot['state']['override'] = True
4313+
slot["state"]["override"] = True
43164314

4317-
slot['state']['limit'] = limit_percent
4315+
slot["state"]["limit"] = limit_percent
43184316
else:
43194317
if discharge_window_n >= 0:
43204318
start = self.discharge_window_best[discharge_window_n]["start"]
43214319
if start > minute:
43224320
soc_change_this = self.predict_soc_best.get(max(start - self.minutes_now, 0), 0.0) - self.predict_soc_best.get(minute_relative_start, 0.0)
4323-
slot['state']['soc']['change'] = self.dp2(soc_change_this)
4321+
slot["state"]["soc"]["change"] = self.dp2(soc_change_this)
43244322

43254323
if discharge_window_n >= 0:
43264324
limit = self.discharge_limits_best[discharge_window_n]
43274325
if limit == 99:
43284326
# TODO: figure out the split state logic, and how to reflect it here
4329-
slot['state']['mode'] = 'Freeze discharge'
4330-
slot['state']['limit'] = limit
4327+
slot["state"]["mode"] = "Freeze discharge"
4328+
slot["state"]["limit"] = limit
43314329
elif limit < 100:
43324330
# TODO: figure out the split state logic, and how to reflect it here
43334331
if limit > soc_percent_max_window:
4334-
slot['state']['mode'] = 'Hold discharge'
4332+
slot["state"]["mode"] = "Hold discharge"
43354333
else:
4336-
slot['state']['mode'] = 'Discharge'
4337-
slot['state']['limit'] = limit
4334+
slot["state"]["mode"] = "Discharge"
4335+
slot["state"]["limit"] = limit
43384336

43394337
if self.discharge_window_best[discharge_window_n]["start"] in self.manual_discharge_times:
4340-
slot['state']['override'] = True
4338+
slot["state"]["override"] = True
43414339
elif self.discharge_window_best[discharge_window_n]["start"] in self.manual_freeze_discharge_times:
4342-
slot['state']['override'] = True
4343-
4340+
slot["state"]["override"] = True
43444341

4345-
slot['rate'] = {}
4346-
slot['rate']['start'] = rate_start.strftime("%a %H:%M")
4342+
slot["rate"] = {}
4343+
slot["rate"]["start"] = rate_start.strftime("%a %H:%M")
43474344

43484345
# Import and export rates -> to JSON
4349-
slot['rate']['import'] = {}
4350-
slot['rate']['import']['value'] = rate_value_import
4351-
slot['rate']['import']['adjust_type'] = self.rate_import_replicated.get(minute, None)
4346+
slot["rate"]["import"] = {}
4347+
slot["rate"]["import"]["value"] = rate_value_import
4348+
slot["rate"]["import"]["adjust_type"] = self.rate_import_replicated.get(minute, None)
43524349

43534350
if plan_debug:
4354-
slot['rate']['import']['debug'] = "%02.02f" % (rate_value_import / self.battery_loss / self.inverter_loss + self.metric_battery_cycle)
4351+
slot["rate"]["import"]["debug"] = "%02.02f" % (rate_value_import / self.battery_loss / self.inverter_loss + self.metric_battery_cycle)
43554352

43564353
if charge_window_n >= 0:
4357-
slot['rate']['import']['charge'] = True
4354+
slot["rate"]["import"]["charge"] = True
43584355

4359-
slot['rate']['export'] = {}
4360-
slot['rate']['export']['value'] = rate_value_export
4361-
slot['rate']['export']['adjust_type'] = self.rate_export_replicated.get(minute, None)
4356+
slot["rate"]["export"] = {}
4357+
slot["rate"]["export"]["value"] = rate_value_export
4358+
slot["rate"]["export"]["adjust_type"] = self.rate_export_replicated.get(minute, None)
43624359

43634360
if plan_debug:
4364-
slot['rate']['export']['debug'] = "%02.02f" % (rate_value_export * self.battery_loss_discharge * self.inverter_loss - self.metric_battery_cycle)
4361+
slot["rate"]["export"]["debug"] = "%02.02f" % (rate_value_export * self.battery_loss_discharge * self.inverter_loss - self.metric_battery_cycle)
43654362

43664363
if discharge_window_n >= 0:
4367-
slot['rate']['export']['discharge'] = True
4364+
slot["rate"]["export"]["discharge"] = True
43684365

4369-
4370-
slot['cost'] = {}
4366+
slot["cost"] = {}
43714367

43724368
# Total cost at start of slot, add leading minus if negative
4373-
slot['cost']['start'] = metric_start
4374-
slot['cost']['change'] = self.dp2(metric_change)
4369+
slot["cost"]["start"] = metric_start
4370+
slot["cost"]["change"] = self.dp2(metric_change)
43754371

43764372
# Car charging?
4377-
slot['car'] = {}
4373+
slot["car"] = {}
43784374
if self.num_cars > 0:
4379-
slot['car']['charging_kwh'] = self.car_charge_slot_kwh(minute_start, minute_end)
4375+
slot["car"]["charging_kwh"] = self.car_charge_slot_kwh(minute_start, minute_end)
43804376

43814377
# iBoost
4382-
slot['iboost'] = {}
4378+
slot["iboost"] = {}
43834379
if self.iboost_enable:
43844380
iboost_slot_end = minute_relative_slot_end
43854381
iboost_amount = self.predict_iboost_best.get(minute_relative_start, 0)
@@ -4394,40 +4390,38 @@ def publish_json_plan(self, pv_forecast_minute_step, pv_forecast_minute_step10,
43944390
)
43954391
else:
43964392
iboost_change = max(iboost_amount_end - iboost_amount, 0.0)
4397-
slot['iboost']['change'] = iboost_change
4398-
slot['iboost']['amount'] = iboost_amount
4399-
slot['iboost']['amount_end'] = iboost_amount_end
4400-
slot['iboost']['amount_prev'] = iboost_amount_prev
4393+
slot["iboost"]["change"] = iboost_change
4394+
slot["iboost"]["amount"] = iboost_amount
4395+
slot["iboost"]["amount_end"] = iboost_amount_end
4396+
slot["iboost"]["amount_prev"] = iboost_amount_prev
44014397

4402-
slot['carbon'] = {}
4398+
slot["carbon"] = {}
44034399
if self.carbon_enable:
44044400
# Work out carbon intensity and carbon use
4405-
slot['carbon']['amount'] = self.predict_carbon_best.get(minute_relative_start, 0)
4406-
slot['carbon']['amount_end'] = self.predict_carbon_best.get(minute_relative_slot_end, 0)
4407-
slot['carbon']['change'] = self.dp2(carbon_amount_end - carbon_amount)
4408-
slot['carbon']['intensity'] = self.dp0(self.carbon_intensity.get(minute_relative_start, 0))
4409-
4401+
slot["carbon"]["amount"] = self.predict_carbon_best.get(minute_relative_start, 0)
4402+
slot["carbon"]["amount_end"] = self.predict_carbon_best.get(minute_relative_slot_end, 0)
4403+
slot["carbon"]["change"] = self.dp2(carbon_amount_end - carbon_amount)
4404+
slot["carbon"]["intensity"] = self.dp0(self.carbon_intensity.get(minute_relative_start, 0))
44104405

44114406
if start_span:
4412-
slot['span'] = 'start'
4407+
slot["span"] = "start"
44134408
elif in_span:
4414-
slot['span'] = 'in_span'
4409+
slot["span"] = "in_span"
44154410
elif not in_span:
4416-
slot['span'] = 'not in_span'
4411+
slot["span"] = "not in_span"
44174412
else:
4418-
slot['span'] = 'unknown'
4413+
slot["span"] = "unknown"
44194414

4420-
output['slots'].append(slot)
4415+
output["slots"].append(slot)
44214416

4422-
#output['raw'] = {}
4423-
#output['raw']['pv_forecast_minute_step'] = pv_forecast_minute_step
4424-
#output['raw']['pv_forecast_minute_step10'] = pv_forecast_minute_step10
4425-
#output['raw']['load_minutes_step'] = load_minutes_step
4426-
#output['raw']['load_minutes_step10'] = load_minutes_step10
4417+
# output['raw'] = {}
4418+
# output['raw']['pv_forecast_minute_step'] = pv_forecast_minute_step
4419+
# output['raw']['pv_forecast_minute_step10'] = pv_forecast_minute_step10
4420+
# output['raw']['load_minutes_step'] = load_minutes_step
4421+
# output['raw']['load_minutes_step10'] = load_minutes_step10
44274422

44284423
self.dashboard_item(self.prefix + ".plan_json", state="", attributes={"json": json.dumps(output), "friendly_name": "Plan in JSON", "icon": "mdi:web-box"})
44294424

4430-
44314425
def publish_html_plan(self, pv_forecast_minute_step, pv_forecast_minute_step10, load_minutes_step, load_minutes_step10, end_record):
44324426
"""
44334427
Publish the current plan in HTML format

0 commit comments

Comments
 (0)