diff --git a/pylgbst/comms/__init__.py b/pylgbst/comms/__init__.py index 6c1d4a9..8f88b88 100644 --- a/pylgbst/comms/__init__.py +++ b/pylgbst/comms/__init__.py @@ -48,11 +48,18 @@ class Connection(object): def _is_device_matched(self, address, name, hub_mac): log.debug("Checking device name: %s, MAC: %s", name, address) + matched = False if address != "00:00:00:00:00:00": - if (not hub_mac and name == LEGO_MOVE_HUB) or hub_mac.lower() == address.lower(): + if hub_mac: + if hub_mac.lower() == address.lower(): + matched = True + elif name == LEGO_MOVE_HUB: + matched = True + + if matched: log.info("Found %s at %s", name, address) - return True - return False + + return matched class DebugServer(object): diff --git a/tests/test_comms.py b/tests/test_comms.py new file mode 100644 index 0000000..7f2eeb1 --- /dev/null +++ b/tests/test_comms.py @@ -0,0 +1,38 @@ +import unittest + +from pylgbst.comms import * + +class ConnectionTestCase(unittest.TestCase): + def test_is_device_matched(self): + conn = Connection() + + hub_address = '1a:2A:3A:4A:5A:6A' + other_address = 'A1:a2:a3:a4:a5:a6' + zero_address = '00:00:00:00:00:00' + hub_name = LEGO_MOVE_HUB + other_name = 'HRM' + + test_matrix = [ + # address, name, hub_mac, expected + (hub_address, hub_name, hub_address, True), + (hub_address, hub_name, None, True), + (hub_address, None, hub_address, True), + (hub_address, None, None, False), + (hub_address, other_name, hub_address, True), + (hub_address, other_name, None, False), + (other_address, hub_name, hub_address, False), + (other_address, hub_name, None, True), + (other_address, None, hub_address, False), + (other_address, None, None, False), + (other_address, other_name, hub_address, False), + (other_address, other_name, None, False), + (zero_address, hub_name, hub_address, False), + (zero_address, hub_name, None, False), + (zero_address, None, hub_address, False), + (zero_address, None, None, False), + (zero_address, other_name, hub_address, False), + (zero_address, other_name, None, False), + ] + + for address, name, hub_mac, expected in test_matrix: + self.assertEqual(conn._is_device_matched(address=address, name=name, hub_mac=hub_mac), expected)