From 48e8004bfca4c6bb6d8a98e39df46876d417cf1d Mon Sep 17 00:00:00 2001 From: Trefor Southwell <48591903+springfall2008@users.noreply.github.com> Date: Wed, 8 May 2024 07:56:40 +0100 Subject: [PATCH] Current from power fixes (#1066) * 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> --- apps/predbat/predbat.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index ba23d460..61771fd5 100644 --- a/apps/predbat/predbat.py +++ b/apps/predbat/predbat.py @@ -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, @@ -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"] @@ -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: @@ -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)