Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions e3dc/_e3dc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
6 changes: 6 additions & 0 deletions e3dc/_rscpTags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions tests/test_dpp_target_soc.py
Original file line number Diff line number Diff line change
@@ -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)