Skip to content

Commit

Permalink
Inject constructed transport
Browse files Browse the repository at this point in the history
  • Loading branch information
elupus committed Feb 12, 2024
1 parent 24f8878 commit f267595
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
6 changes: 3 additions & 3 deletions RFXtrx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,16 +1027,16 @@ 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 = {}
self._status = 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."""
Expand Down
2 changes: 1 addition & 1 deletion examples/receive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit f267595

Please sign in to comment.