Skip to content

Commit 3a82b49

Browse files
committed
feat(dmd-integration-time): add getter to read demand integration time setting
also added valid tests Signed-off-by: Milad Makdesi <milad@id8-engineering.io>
1 parent 850111f commit 3a82b49

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/em511/em511.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Em511:
3535
STOP_BIT_MAX_VALUE = 1
3636
REPLY_DELAY_MIN_VALUE = 0
3737
REPLY_DELAY_MAX_VALUE = 1000
38+
DMD_TIME_MIN_VALUE = 0
39+
DMD_TIME_MAX_VALUE = 6
3840

3941
EM511_REGISTER_V = 0x0
4042
EM511_REGISTER_A = 0x2
@@ -59,6 +61,7 @@ class Em511:
5961
EM511_REGISTER_RESET_PARTIAL_ENERGY_AND_HOUR_COUNTER = 0x4004
6062
EM511_REGISTER_RESET_DMD_AND_DMD_MAX = 0x4005
6163
EM511_REGISTER_RESET_TO_FACTORY_SETTINGS = 0x4020
64+
EM511_REGISTER_DMD_INTEGRATION_TIME = 0x1010
6265

6366
SCALE_10 = 10
6467
SCALE_100 = 100
@@ -365,6 +368,28 @@ def password(self) -> int:
365368
raise ValueError(msg)
366369
return value
367370

371+
@property
372+
def dmd_integration_time(self) -> int:
373+
"""Demand integration time.
374+
375+
0=1min, 1=5min, 2=10min, 3=15min,
376+
4=20min, 5=30min, 6=60min (default=3).
377+
378+
Returns:
379+
int: Current demand integration time.
380+
381+
Raises:
382+
ValueError: If input is at max value or above.
383+
ValueError: If demand integration time is out of range.
384+
ModbusException: If failed to read input register.
385+
"""
386+
regs = self._read_input_registers(self.EM511_REGISTER_DMD_INTEGRATION_TIME, self.INT32_REG_COUNT)
387+
value = self._unpack(regs, self.EM511_REGISTER_DMD_INTEGRATION_TIME)
388+
if not (self.DMD_TIME_MIN_VALUE <= value <= self.DMD_TIME_MAX_VALUE):
389+
msg = f"Invalid demand integration time value: {value}. Must be between 0 and 6."
390+
raise ValueError(msg)
391+
return value
392+
368393
@property
369394
def device_id(self) -> int:
370395
"""Device address.

src/em511/test_em511.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,44 @@ def test_get_password() -> None:
453453
_ = meter.password
454454

455455

456+
def test_get_dmd_integration_time() -> None:
457+
"""Test Get dmd_integration_time."""
458+
client = MagicMock()
459+
mock_result = MagicMock()
460+
mock_result.isError.return_value = False
461+
meter = Em511(1, client)
462+
463+
"""Test 1: should pass"""
464+
mock_result.registers = [0x0006, 0x0000]
465+
client.read_input_registers.return_value = mock_result
466+
value = meter.dmd_integration_time
467+
assert value == int("6")
468+
469+
"""Test 2: Should pass."""
470+
mock_result.registers = [0x0000, 0x0000]
471+
client.read_input_registers.return_value = mock_result
472+
value = meter.dmd_integration_time
473+
assert value == int("0")
474+
475+
"""Test 3: Should raise exception due to value not in range"""
476+
mock_result.registers = [0x0007, 0x0000]
477+
client.read_input_registers.return_value = mock_result
478+
with pytest.raises(ValueError, match="Invalid demand integration time value: "):
479+
_ = meter.dmd_integration_time
480+
481+
"""Test 4: Should raise exception due to more registers in use than allowed."""
482+
mock_result.registers = [0x1860, 0x0023, 0x4244]
483+
client.read_input_registers.return_value = mock_result
484+
with pytest.raises(ValueError, match="Unexpected register count:"):
485+
_ = meter.dmd_integration_time
486+
487+
"""Test 5: Should raise exception if input value exceeds maximum value, display shows 'EEE', 32-bit register."""
488+
mock_result.registers = [0xFFFF, 0x7FFF]
489+
client.read_input_registers.return_value = mock_result
490+
with pytest.raises(ValueError, match="Input overflow EEE for 32-bit register: "):
491+
_ = meter.dmd_integration_time
492+
493+
456494
def test_get_device_id() -> None:
457495
"""Test Get device id/address."""
458496
client = MagicMock()

0 commit comments

Comments
 (0)