From 2da1434a0208be1c18d4ec113467bc9094ce6201 Mon Sep 17 00:00:00 2001 From: bullitt168 Date: Wed, 4 Sep 2024 19:59:04 +0000 Subject: [PATCH] added min_discharge to set_powerlimits + preserving set values --- custom_components/e3dc_rscp/coordinator.py | 38 ++++++++++++++----- custom_components/e3dc_rscp/services.py | 9 +++-- custom_components/e3dc_rscp/services.yaml | 10 ++++- custom_components/e3dc_rscp/strings.json | 4 ++ .../e3dc_rscp/translations/en.json | 4 ++ 5 files changed, 52 insertions(+), 13 deletions(-) diff --git a/custom_components/e3dc_rscp/coordinator.py b/custom_components/e3dc_rscp/coordinator.py index cfe901c..f81df29 100644 --- a/custom_components/e3dc_rscp/coordinator.py +++ b/custom_components/e3dc_rscp/coordinator.py @@ -583,15 +583,15 @@ async def async_set_wallbox_max_charge_current(self, current: int | None, wallbo _LOGGER.debug("Successfully set the wallbox max charge current to %s", current) async def async_set_power_limits( - self, max_charge: int | None, max_discharge: int | None + self, max_charge: int | None, max_discharge: int | None, min_discharge: int | None ) -> None: """Set the given power limits and enable them.""" # Validate the arguments, at least one has to be set. - if max_charge is None and max_discharge is None: + if max_charge is None and max_discharge is None and min_discharge is None: raise ValueError( "async_set_power_limits must be called with at least one of " - "max_charge or max_discharge." + "max_charge, max_discharge or min_discharge." ) if max_charge is not None and max_charge > self.proxy.e3dc.maxBatChargePower: @@ -599,23 +599,43 @@ async def async_set_power_limits( "Limiting max_charge to %s", self.proxy.e3dc.maxBatChargePower ) max_charge = self.proxy.e3dc.maxBatChargePower - if ( - max_discharge is not None - and max_discharge > self.proxy.e3dc.maxBatDischargePower - ): + elif max_charge is None: + _LOGGER.debug( + "Preserving max_charge at %s", self._mydata["pset-limit-charge"] + ) + max_charge = self._mydata["pset-limit-charge"] + + if max_discharge is not None and max_discharge > self.proxy.e3dc.maxBatDischargePower: _LOGGER.warning( "Limiting max_discharge to %s", self.proxy.e3dc.maxBatDischargePower ) max_discharge = self.proxy.e3dc.maxBatDischargePower + elif max_discharge is None: + _LOGGER.debug( + "Preserving max_discharge at %s", self._mydata["pset-limit-discharge"] + ) + max_discharge = self._mydata["pset-limit-discharge"] + + if min_discharge is not None and min_discharge > self.proxy.e3dc.maxBatDischargePower: + _LOGGER.warning( + "Limiting min_discharge to %s", self.proxy.e3dc.maxBatDischargePower + ) + min_discharge = self.proxy.e3dc.maxBatDischargePower + elif min_discharge is None: + _LOGGER.debug( + "Preserving min_discharge at %s", self._mydata["pset-limit-discharge-minimum"] + ) + min_discharge = self._mydata["pset-limit-discharge-minimum"] _LOGGER.debug( - "Enabling power limits, max_charge: %s, max_discharge: %s", + "Enabling power limits, max_charge: %s, max_discharge: %s, min_discharge: %s", max_charge, max_discharge, + min_discharge ) await self.hass.async_add_executor_job( - self.proxy.set_power_limits, True, max_charge, max_discharge, None + self.proxy.set_power_limits, True, max_charge, max_discharge, min_discharge ) _LOGGER.debug("Successfully set the power limits") diff --git a/custom_components/e3dc_rscp/services.py b/custom_components/e3dc_rscp/services.py index a3e568c..e2fbffa 100644 --- a/custom_components/e3dc_rscp/services.py +++ b/custom_components/e3dc_rscp/services.py @@ -26,6 +26,7 @@ ATTR_DEVICEID = "device_id" ATTR_MAX_CHARGE = "max_charge" ATTR_MAX_DISCHARGE = "max_discharge" +ATTR_MIN_DISCHARGE = "min_discharge" ATTR_CHARGE_AMOUNT = "charge_amount" ATTR_MAX_CHARGE_CURRENT = "max_charge_current" @@ -40,6 +41,7 @@ vol.Required(ATTR_DEVICEID): str, vol.Optional(ATTR_MAX_CHARGE): vol.All(int, vol.Range(min=0)), vol.Optional(ATTR_MAX_DISCHARGE): vol.All(int, vol.Range(min=0)), + vol.Optional(ATTR_MIN_DISCHARGE): vol.All(int, vol.Range(min=0)) } ) @@ -196,12 +198,13 @@ async def _async_set_power_limits(hass: HomeAssistant, call: ServiceCall) -> Non ) max_charge: int | None = call.data.get(ATTR_MAX_CHARGE) max_discharge: int | None = call.data.get(ATTR_MAX_DISCHARGE) - if max_charge is None and max_discharge is None: + min_discharge: int | None = call.data.get(ATTR_MIN_DISCHARGE) + if max_charge is None and max_discharge is None and min_discharge is None: raise HomeAssistantError( - f"{SERVICE_SET_POWER_LIMITS}: Need to set at least one of {ATTR_MAX_CHARGE} or {ATTR_MAX_DISCHARGE}" + f"{SERVICE_SET_POWER_LIMITS}: Need to set at least one of {ATTR_MAX_CHARGE}, {ATTR_MAX_DISCHARGE} or {ATTR_MIN_DISCHARGE}" ) await coordinator.async_set_power_limits( - max_charge=max_charge, max_discharge=max_discharge + max_charge=max_charge, max_discharge=max_discharge, min_discharge=min_discharge ) diff --git a/custom_components/e3dc_rscp/services.yaml b/custom_components/e3dc_rscp/services.yaml index 9f2198a..a8ec05e 100644 --- a/custom_components/e3dc_rscp/services.yaml +++ b/custom_components/e3dc_rscp/services.yaml @@ -35,7 +35,15 @@ set_power_limits: unit_of_measurement: W mode: box step: 100 - + min_discharge: + required: false + example: "100" + selector: + number: + min: 0 + unit_of_measurement: W + mode: box + step: 10 set_wallbox_max_charge_current: fields: device_id: diff --git a/custom_components/e3dc_rscp/strings.json b/custom_components/e3dc_rscp/strings.json index 2a292c1..fd5f292 100644 --- a/custom_components/e3dc_rscp/strings.json +++ b/custom_components/e3dc_rscp/strings.json @@ -303,6 +303,10 @@ "max_discharge": { "name": "Maximum Discharge (W)", "description": "Maximum allowed battery discharge in Watts." + }, + "min_discharge": { + "name": "Minimum Discharge (W)", + "description": "Minimum allowed battery discharge in Watts." } } }, diff --git a/custom_components/e3dc_rscp/translations/en.json b/custom_components/e3dc_rscp/translations/en.json index 0212667..fab185a 100644 --- a/custom_components/e3dc_rscp/translations/en.json +++ b/custom_components/e3dc_rscp/translations/en.json @@ -303,6 +303,10 @@ "max_discharge": { "name": "Maximum Discharge (W)", "description": "Maximum allowed battery discharge in Watts." + }, + "min_discharge": { + "name": "Minimum Discharge (W)", + "description": "Minimum allowed battery discharge in Watts." } } },