Skip to content

Commit b94dfad

Browse files
author
Brendan Whitfield
committed
allow OBDCommands to return mode/PID of None
1 parent dcd6b78 commit b94dfad

3 files changed

Lines changed: 24 additions & 13 deletions

File tree

‎obd/OBDCommand.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ def mode(self):
7171
isHex(self.command.decode()):
7272
return int(self.command[:2], 16)
7373
else:
74-
return 0
74+
return None
7575

7676
@property
7777
def pid(self):
7878
if len(self.command) > 2 and \
7979
isHex(self.command.decode()):
8080
return int(self.command[2:], 16)
8181
else:
82-
return 0
82+
return None
8383

8484

8585
def __call__(self, messages):

‎tests/test_OBDCommand.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,18 @@ def test_get_mode():
7272
assert cmd.mode == 0x01
7373

7474
cmd = OBDCommand("", "", b"", 4, noop, ECU.ENGINE)
75-
assert cmd.mode == 0
75+
assert cmd.mode == None
7676

7777
cmd = OBDCommand("", "", b"totally not hex", 4, noop, ECU.ENGINE)
78-
assert cmd.mode == 0
78+
assert cmd.mode == None
7979

8080

8181
def test_pid():
8282
cmd = OBDCommand("", "", b"0123", 4, noop, ECU.ENGINE)
8383
assert cmd.pid == 0x23
8484

8585
cmd = OBDCommand("", "", b"01", 4, noop, ECU.ENGINE)
86-
assert cmd.pid == 0
86+
assert cmd.pid == None
8787

8888
cmd = OBDCommand("", "", b"totally not hex", 4, noop, ECU.ENGINE)
89-
assert cmd.mode == 0
89+
assert cmd.mode == None

‎tests/test_commands.py‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ def test_list_integrity():
1414

1515
# make sure the command tables are in mode & PID order
1616
assert mode == cmd.mode, "Command is in the wrong mode list: %s" % cmd.name
17-
assert pid == cmd.pid, "The index in the list must also be the PID: %s" % cmd.name
17+
18+
if len(cmds) > 1:
19+
assert pid == cmd.pid, "The index in the list must also be the PID: %s" % cmd.name
20+
else:
21+
# lone commands in a mode are allowed to have no PID
22+
assert (pid == cmd.pid) or (cmd.pid is None)
1823

1924
# make sure all the fields are set
2025
assert cmd.name != "", "Command names must not be null"
@@ -50,9 +55,12 @@ def test_getitem():
5055
continue # this command is reserved
5156

5257
# by [mode][pid]
53-
mode = cmd.mode
54-
pid = cmd.pid
55-
assert cmd == obd.commands[mode][pid], "mode %d, PID %d could not be accessed through __getitem__" % (mode, pid)
58+
if (cmd.pid is None) and (len(cmds) == 1):
59+
# lone commands in a mode have no PID, and report a pid
60+
# value of None, but can still be accessed by PID 0
61+
assert cmd == obd.commands[cmd.mode][0], "lone command in mode %d could not be accessed through __getitem__" % mode
62+
else:
63+
assert cmd == obd.commands[cmd.mode][cmd.pid], "mode %d, PID %d could not be accessed through __getitem__" % (mode, pid)
5664

5765
# by [name]
5866
assert cmd == obd.commands[cmd.name], "command name %s could not be accessed through __getitem__" % (cmd.name)
@@ -70,9 +78,12 @@ def test_contains():
7078
assert obd.commands.has_command(cmd)
7179

7280
# by (mode, pid)
73-
mode = cmd.mode
74-
pid = cmd.pid
75-
assert obd.commands.has_pid(mode, pid)
81+
if cmd.pid is None:
82+
# lone commands in a mode can have no PID, and report None
83+
# but these commands can still be looked up by (mode, pid=0)
84+
assert obd.commands.has_pid(cmd.mode, 0)
85+
else:
86+
assert obd.commands.has_pid(cmd.mode, cmd.pid)
7687

7788
# by (name)
7889
assert obd.commands.has_name(cmd.name)

0 commit comments

Comments
 (0)