From f2675956a54cebfe145ceb07ecc88e531c64464e Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Mon, 12 Feb 2024 21:46:44 +0100 Subject: [PATCH] Inject constructed transport --- RFXtrx/__init__.py | 6 +++--- examples/receive.py | 2 +- tests/test_base.py | 15 +++++++-------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/RFXtrx/__init__.py b/RFXtrx/__init__.py index ef536a2..bb9d986 100644 --- a/RFXtrx/__init__.py +++ b/RFXtrx/__init__.py @@ -1027,8 +1027,8 @@ class Connect: Has methods for sensors. """ # pylint: disable=too-many-instance-attributes, too-many-arguments - def __init__(self, device, event_callback=None, - transport_protocol=PySerialTransport, + def __init__(self, event_callback=None, + transport=None, modes=None): self._run_event = threading.Event() self._sensors = {} @@ -1036,7 +1036,7 @@ def __init__(self, device, event_callback=None, self._modes = modes self._thread = threading.Thread(target=self._connect, daemon=True) self.event_callback = event_callback - self.transport: RFXtrxTransport = transport_protocol(device) + self.transport: RFXtrxTransport = transport def connect(self, timeout=None): """Connect to device.""" diff --git a/examples/receive.py b/examples/receive.py index eed72b9..26c42f1 100644 --- a/examples/receive.py +++ b/examples/receive.py @@ -38,7 +38,7 @@ def main(): modes_list = sys.argv[2].split() if len(sys.argv) > 2 else None print ("modes: ", modes_list) - core = RFXtrx.Core(rfxcom_device, print_callback, modes=modes_list) + core = RFXtrx.Connect(print_callback, modes=modes_list, transport=RFXtrx.PySerialTransport(rfxcom_device)) core.connect() print (core) diff --git a/tests/test_base.py b/tests/test_base.py index bbacb6b..8c631e9 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -19,7 +19,7 @@ def setUp(self): def test_constructor(self): global num_calbacks - core = RFXtrx.Core(self.path, event_callback=_callback, transport_protocol=RFXtrx.DummyTransport2) + core = RFXtrx.Connect(event_callback=_callback, transport=RFXtrx.DummyTransport2(self.path)) core.connect() while num_calbacks < 7: time.sleep(0.1) @@ -31,14 +31,14 @@ def test_constructor(self): def test_invalid_packet(self): bytes_array = bytearray([0x09, 0x11, 0xd7, 0x00, 0x01, 0x1d, 0x14, 0x02, 0x79, 0x0a]) - core = RFXtrx.Connect(self.path, event_callback=_callback, transport_protocol=RFXtrx.DummyTransport) + core = RFXtrx.Connect(event_callback=_callback, transport=RFXtrx.DummyTransport(self.path)) core.connect() event = core.transport.parse(bytes_array) self.assertIsNone(event) def test_format_packet(self): # Lighting1 - core = RFXtrx.Connect(self.path, event_callback=_callback, transport_protocol=RFXtrx.DummyTransport) + core = RFXtrx.Connect(event_callback=_callback, transport=RFXtrx.DummyTransport(self.path)) core.connect() bytes_array = bytearray([0x07, 0x10, 0x00, 0x2a, 0x45, 0x05, 0x01, 0x70]) event = core.transport.parse(bytes_array) @@ -361,7 +361,7 @@ def test_equal_check(self): self.assertFalse(temphum==energy) def test_equal_device_check(self): - core = RFXtrx.Connect(self.path, event_callback=_callback, transport_protocol=RFXtrx.DummyTransport) + core = RFXtrx.Connect(event_callback=_callback, transport=RFXtrx.DummyTransport(self.path)) core.connect() data1 = bytearray(b'\x11\x5A\x01\x00\x2E\xB2\x03\x00\x00' b'\x02\xB4\x00\x00\x0C\x46\xA8\x11\x69') @@ -395,7 +395,7 @@ def test_equal_device_check(self): core.close_connection() def test_get_device(self): - core = RFXtrx.Connect(self.path, event_callback=_callback, transport_protocol=RFXtrx.DummyTransport) + core = RFXtrx.Connect(event_callback=_callback, transport=RFXtrx.DummyTransport(self.path)) core.connect() # Lighting1 bytes_array = bytearray([0x07, 0x10, 0x00, 0x2a, 0x45, 0x05, 0x01, 0x70]) @@ -442,8 +442,7 @@ def test_get_device(self): core.close_connection() def test_set_recmodes(self): - core = RFXtrx.Connect(self.path, event_callback=_callback, - transport_protocol=RFXtrx.DummyTransport) + core = RFXtrx.Connect(event_callback=_callback, transport=RFXtrx.DummyTransport(self.path)) core.connect() time.sleep(0.2) self.assertEqual(None, core._modes) @@ -465,7 +464,7 @@ def test_set_recmodes(self): core.set_recmodes(['arc', 'oregon', 'unknown-mode']) def test_receive(self): - core = RFXtrx.Connect(self.path, event_callback=_callback, transport_protocol=RFXtrx.DummyTransport) + core = RFXtrx.Connect(event_callback=_callback, transport=RFXtrx.DummyTransport(self.path)) core.connect() time.sleep(0.2) # Lighting1