diff --git a/e3dc/_e3dc.py b/e3dc/_e3dc.py index 2a891c0..f8bd5c0 100644 --- a/e3dc/_e3dc.py +++ b/e3dc/_e3dc.py @@ -2257,6 +2257,35 @@ def get_powermeters_data( return outObj + def get_dpp_target_soc(self, keepAlive: bool = False) -> int: + """Read the Dynamic Power Pricing battery target state of charge (%).""" + return self.sendRequestTag( + RscpTag.EMS_REQ_DPP_SOC_BATTERY, keepAlive=keepAlive + ) + + def get_dpp_price_based_charging_enabled(self, keepAlive: bool = False) -> bool: + """Return whether DPP price-based battery charging is enabled.""" + return self.sendRequestTag( + RscpTag.EMS_REQ_DPP_PRICE_BASED_BATTERY_CHARGE_ENABLED, + keepAlive=keepAlive, + ) + + def set_dpp_target_soc(self, target_soc: int, keepAlive: bool = False) -> int: + """Set the DPP battery target SoC (0–100%). + + This does not enable DPP or override the device's price logic. + Returns the value acknowledged by the device. + """ + if type(target_soc) is not int or not 0 <= target_soc <= 100: + raise ValueError("target_soc must be an integer between 0 and 100") + response = self.sendRequest( + (RscpTag.EMS_REQ_DPP_SET_SOC_BATTERY, RscpType.UChar8, target_soc), + keepAlive=keepAlive, + ) + if response[0] != RscpTag.EMS_DPP_SET_SOC_BATTERY: + raise ValueError("Unexpected DPP target SoC response tag") + return response[2] + def get_power_settings(self, keepAlive: bool = False): """Polls the power settings via rscp protocol. diff --git a/e3dc/_rscpTags.py b/e3dc/_rscpTags.py index 3ab1030..dd8e609 100644 --- a/e3dc/_rscpTags.py +++ b/e3dc/_rscpTags.py @@ -86,6 +86,9 @@ class RscpTag(Enum): EMS_REQ_BAT_DISCHARGE_LIMIT = 0x01000046 EMS_REQ_DCDC_DISCHARGE_LIMIT = 0x01000047 EMS_REQ_USER_DISCHARGE_LIMIT = 0x01000048 + EMS_REQ_DPP_SET_SOC_BATTERY = 0x01000057 + EMS_REQ_DPP_SOC_BATTERY = 0x0100005B + EMS_REQ_DPP_PRICE_BASED_BATTERY_CHARGE_ENABLED = 0x0100005D EMS_REQ_SET_POWER_CONTROL_OFFSET = 0x01000060 EMS_REQ_REMAINING_BAT_CHARGE_POWER = 0x01000071 EMS_REQ_REMAINING_BAT_DISCHARGE_POWER = 0x01000072 @@ -303,6 +306,9 @@ class RscpTag(Enum): EMS_BAT_DISCHARGE_LIMIT = 0x01800046 EMS_DCDC_DISCHARGE_LIMIT = 0x01800047 EMS_USER_DISCHARGE_LIMIT = 0x01800048 + EMS_DPP_SET_SOC_BATTERY = 0x01800057 + EMS_DPP_SOC_BATTERY = 0x0180005B + EMS_DPP_PRICE_BASED_BATTERY_CHARGE_ENABLED = 0x0180005D EMS_SET_POWER_CONTROL_OFFSET = 0x01800060 EMS_REMAINING_BAT_CHARGE_POWER = 0x01800071 EMS_REMAINING_BAT_DISCHARGE_POWER = 0x01800072 diff --git a/tests/test_dpp_target_soc.py b/tests/test_dpp_target_soc.py new file mode 100644 index 0000000..9be226a --- /dev/null +++ b/tests/test_dpp_target_soc.py @@ -0,0 +1,58 @@ +"""Unit tests for Dynamic Power Pricing target SoC support.""" + +from unittest.mock import Mock + +import pytest + +from e3dc._e3dc import E3DC +from e3dc._rscpTags import RscpTag, RscpType + + +@pytest.fixture +def device(): + instance = object.__new__(E3DC) + instance.sendRequest = Mock() + instance.sendRequestTag = Mock() + return instance + + +def test_get_dpp_target_soc(device): + device.sendRequestTag.return_value = 90 + assert device.get_dpp_target_soc() == 90 + device.sendRequestTag.assert_called_once_with( + RscpTag.EMS_REQ_DPP_SOC_BATTERY, keepAlive=False + ) + + +def test_get_dpp_enabled(device): + device.sendRequestTag.return_value = True + assert device.get_dpp_price_based_charging_enabled() is True + device.sendRequestTag.assert_called_once_with( + RscpTag.EMS_REQ_DPP_PRICE_BASED_BATTERY_CHARGE_ENABLED, keepAlive=False + ) + + +def test_set_dpp_target_soc(device): + device.sendRequest.return_value = ( + RscpTag.EMS_DPP_SET_SOC_BATTERY, + RscpType.UChar8, + 89, + ) + assert device.set_dpp_target_soc(89) == 89 + device.sendRequest.assert_called_once_with( + (RscpTag.EMS_REQ_DPP_SET_SOC_BATTERY, RscpType.UChar8, 89), + keepAlive=False, + ) + + +@pytest.mark.parametrize("invalid", [-1, 101, 90.5, True, "90", None]) +def test_set_dpp_target_soc_rejects_invalid_values(device, invalid): + with pytest.raises(ValueError): + device.set_dpp_target_soc(invalid) + device.sendRequest.assert_not_called() + + +def test_set_dpp_target_soc_rejects_unexpected_response(device): + device.sendRequest.return_value = (RscpTag.EMS_BAT_SOC, RscpType.UChar8, 89) + with pytest.raises(ValueError, match="Unexpected"): + device.set_dpp_target_soc(89)