diff --git a/pymodbus/pdu/device.py b/pymodbus/pdu/device.py index cfd231c33..4d5c499b4 100644 --- a/pymodbus/pdu/device.py +++ b/pymodbus/pdu/device.py @@ -158,6 +158,9 @@ def __init__(self, info=None, info_name=None): :param info: Device information as {int:string} dictionary :param info_name: Device information as {name:string} dictionary """ + # Copy the defaults, so each identity has its own values instead of + # all instances writing into the class-level dict. + self.stat_data = dict(self.stat_data) if isinstance(info_name, dict): for key in info_name: inx = self.__names.index(key) diff --git a/test/pdu/test_device.py b/test/pdu/test_device.py index d73df32d3..b9984e156 100644 --- a/test/pdu/test_device.py +++ b/test/pdu/test_device.py @@ -58,6 +58,19 @@ def test_update_identity(self): assert self.control.Identity.ModelName == "bashwork" assert self.control.Identity.UserApplicationName == "pytest" + def test_identities_do_not_share_data(self): + """Test each identity keeps its own values.""" + first = ModbusDeviceIdentification(info_name={"VendorName": "first"}) + second = ModbusDeviceIdentification(info_name={"VendorName": "second"}) + assert first.VendorName == "first" + assert second.VendorName == "second" + + def test_new_identity_does_not_change_server_identity(self): + """Test creating an identity does not alter the control block.""" + self.control.Identity.update(self.ident) + ModbusDeviceIdentification(info_name={"VendorName": "other"}) + assert self.control.Identity.VendorName == "Bashwork" + def test_device_identification_factory(self): """Test device identification reading.""" self.control.Identity.update(self.ident)