Skip to content

Commit

Permalink
Current from power fixes (#1066)
Browse files Browse the repository at this point in the history
* Current from power fixes

* [pre-commit.ci lite] apply automatic fixes

* Change SK to current control

* Fix order of int in adjust_battery_target

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
springfall2008 and pre-commit-ci-lite[bot] authored May 8, 2024
1 parent 8435177 commit 48e8004
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions apps/predbat/predbat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,8 @@
"has_rest_api": False,
"has_mqtt_api": False,
"has_service_api": True,
"output_charge_control": "power",
"output_charge_control": "current",
"current_dp": 0,
"has_charge_enable_time": False,
"has_discharge_enable_time": False,
"has_target_soc": True,
Expand Down Expand Up @@ -2255,6 +2256,7 @@ def __init__(self, base, id=0, quiet=False):
self.inv_has_service_api = INVERTER_DEF[self.inverter_type]["has_service_api"]
self.inv_mqtt_topic = self.base.get_arg("mqtt_topic", "Sofar2mqtt")
self.inv_output_charge_control = INVERTER_DEF[self.inverter_type]["output_charge_control"]
self.inv_current_dp = INVERTER_DEF[self.inverter_type].get("current_dp", 1)
self.inv_has_charge_enable_time = INVERTER_DEF[self.inverter_type]["has_charge_enable_time"]
self.inv_has_discharge_enable_time = INVERTER_DEF[self.inverter_type]["has_discharge_enable_time"]
self.inv_has_target_soc = INVERTER_DEF[self.inverter_type]["has_target_soc"]
Expand Down Expand Up @@ -3271,8 +3273,7 @@ def adjust_battery_target(self, soc, isCharging=False):

"""
# SOC has no decimal places and clamp in min
soc = int(soc)
soc = max(soc, self.reserve_percent)
soc = int(max(soc, self.reserve_percent))

# Check current setting and adjust
if SIMULATE:
Expand Down Expand Up @@ -3917,18 +3918,21 @@ def enable_charge_discharge_with_time_current(self, direction, enable):
power = self.base.get_arg(f"{direction}_rate", index=self.id, default=2600.0)
self.set_current_from_power(direction, power)
else:
# To disable we set both times to 00:00
for x in ["start", "end"]:
for y in ["hour", "minute"]:
name = f"{direction}_{x}_{y}"
entity = self.base.get_entity(self.base.get_arg(name, indirect=False, index=self.id))
self.write_and_poll_value(name, entity, 0)
if self.inv_charge_time_format == "H M":
# To disable we set both times to 00:00
for x in ["start", "end"]:
for y in ["hour", "minute"]:
name = f"{direction}_{x}_{y}"
entity = self.base.get_entity(self.base.get_arg(name, indirect=False, index=self.id))
self.write_and_poll_value(name, entity, 0)
else:
self.set_current_from_power(direction, 0)

def set_current_from_power(self, direction, power):
"""
Set the timed charge/discharge current setting by converting power to current
"""
new_current = round(power / self.battery_voltage, 1)
new_current = round(power / self.battery_voltage, self.inv_current_dp)
entity = self.base.get_entity(self.base.get_arg(f"timed_{direction}_current", indirect=False, index=self.id))
self.write_and_poll_value(f"timed_{direction}_current", entity, new_current, fuzzy=1)

Expand Down

0 comments on commit 48e8004

Please sign in to comment.