Skip to content

Commit d929ed6

Browse files
committed
feat(reset-to-factory-settings): Add reset unction to reset back to factory settings
Aslo add valid tests Signed-off-by: Bobo Bäck Engström <bobo@id8-engineering.io>
1 parent 69c7a8d commit d929ed6

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

src/em511/em511.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Em511:
5757
EM511_REGISTER_REPLY_DELAY = 0x2004
5858
EM511_REGISTER_RESET_TOT_ENERGY_AND_RUN_HOUR_COUNTER = 0x4003
5959
EM511_REGISTER_RESET_PARTIAL_ENERGY_AND_HOUR_COUNTER = 0x4004
60+
EM511_REGISTER_RESET_TO_FACTORY_SETTINGS = 0x4020
6061

6162
SCALE_10 = 10
6263
SCALE_100 = 100
@@ -599,3 +600,14 @@ def reset_partial_energy_and_hour_counter(self) -> None:
599600
ModbusException: If failed to write to single register.
600601
"""
601602
self._write_register(self.EM511_REGISTER_RESET_PARTIAL_ENERGY_AND_HOUR_COUNTER, 1)
603+
604+
def reset_to_factory_settings(self) -> None:
605+
"""Factory Restore (Default settings).
606+
607+
Write 0x0A0A=2570, then within 1s write 0xC1A0=49568 to trigger reset.
608+
609+
Raises:
610+
ModbusException: If failed to write to single register.
611+
"""
612+
self._write_register(self.EM511_REGISTER_RESET_TO_FACTORY_SETTINGS, 0x0A0A)
613+
self._write_register(self.EM511_REGISTER_RESET_TO_FACTORY_SETTINGS, 0xC1A0)

src/em511/test_em511.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""Test file for driver."""
44

55
from decimal import Decimal
6-
from unittest.mock import MagicMock
6+
from unittest.mock import MagicMock, call
77

88
import pytest
99
from pymodbus import ModbusException
@@ -897,8 +897,8 @@ def test_reset_tot_energy_and_run_hour_counter() -> None:
897897
meter.reset_tot_energy_and_run_hour_counter()
898898

899899

900-
def test_reset_partial_energy_and_partial_hour() -> None:
901-
"""Test Reset partial energy + partial run hour counters."""
900+
def test_reset_partial_energy_and_hour_counter() -> None:
901+
"""Test Reset partial energy + partial hour counters."""
902902
client = MagicMock()
903903
mock_result = MagicMock()
904904
mock_result.isError.return_value = False
@@ -917,3 +917,27 @@ def test_reset_partial_energy_and_partial_hour() -> None:
917917
client.write_register.return_value = mock_result
918918
with pytest.raises(ModbusException, match="Failed to write to single register:"):
919919
meter.reset_partial_energy_and_hour_counter()
920+
921+
922+
def test_reset_to_factory_settings() -> None:
923+
"""Test Reset DMD and DMD max values."""
924+
client = MagicMock()
925+
mock_result = MagicMock()
926+
mock_result.isError.return_value = False
927+
client.write_register.return_value = mock_result
928+
meter = Em511(1, client)
929+
930+
"""Test 1: Both writes occur"""
931+
meter.reset_to_factory_settings()
932+
expected_calls = [
933+
call(address=16416, value=0x0A0A, device_id=1),
934+
call(address=16416, value=0xC1A0, device_id=1),
935+
]
936+
assert client.write_register.call_args_list == expected_calls
937+
assert client.write_register.call_count == 2
938+
939+
"""Test 3: Should raise exception due to failed writing to single register."""
940+
mock_result.isError.return_value = True
941+
client.write_register.return_value = mock_result
942+
with pytest.raises(ModbusException, match="Failed to write to single register:"):
943+
meter.reset_to_factory_settings()

0 commit comments

Comments
 (0)