Skip to content

Commit 838edff

Browse files
committed
Add ability to retrieve zone from decoded command
1 parent f570239 commit 838edff

2 files changed

Lines changed: 31 additions & 26 deletions

File tree

eiscp/core.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -242,27 +242,27 @@ def command_to_iscp(command, arguments=None, zone=None):
242242
return '{}{}'.format(prefix, value)
243243

244244

245-
def iscp_to_command(iscp_message):
246-
for zone, zone_cmds in commands.COMMANDS.items():
247-
# For now, ISCP commands are always three characters, which
248-
# makes this easy.
249-
command, args = iscp_message[:3], iscp_message[3:]
250-
if command in zone_cmds:
251-
if args in zone_cmds[command]['values']:
252-
return zone_cmds[command]['name'], \
253-
zone_cmds[command]['values'][args]['name']
254-
else:
255-
match = re.match('[+-]?[0-9a-f]+$', args, re.IGNORECASE)
256-
if match:
257-
return zone_cmds[command]['name'], \
258-
int(args, 16)
245+
def iscp_to_command(iscp_message, with_zone=False):
246+
def __iscp_to_command(iscp_message):
247+
for zone, zone_cmds in commands.COMMANDS.items():
248+
# For now, ISCP commands are always three characters, which
249+
# makes this easy.
250+
command, args = iscp_message[:3], iscp_message[3:]
251+
if command in zone_cmds:
252+
if args in zone_cmds[command]['values']:
253+
return zone, zone_cmds[command]['name'], \
254+
zone_cmds[command]['values'][args]['name']
259255
else:
260-
return zone_cmds[command]['name'], args
261-
262-
else:
263-
raise ValueError(
264-
'Cannot convert ISCP message to command: {}'.format(iscp_message))
265-
256+
match = re.match('[+-]?[0-9a-f]+$', args, re.IGNORECASE)
257+
if match:
258+
return zone, zone_cmds[command]['name'], int(args, 16)
259+
else:
260+
return zone, zone_cmds[command]['name'], args
261+
else:
262+
raise ValueError(
263+
'Cannot convert ISCP message to command: {}'.format(iscp_message))
264+
zone, ret_cmd, args = __iscp_to_command(iscp_message)
265+
return (zone, ret_cmd, args) if with_zone else (ret_cmd, args)
266266

267267
def filter_for_message(getter_func, msg):
268268
"""Helper that calls ``getter_func`` until a matching message

tests/test_core.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
from eiscp.core import iscp_to_command, command_to_iscp
2+
from unittest import TestCase
23

3-
4-
class TestIscpToCommand:
4+
class TestIscpToCommand(TestCase):
55
def test(self):
6-
assert iscp_to_command('MVL') == ('master-volume', '')
6+
self.assertEqual(('master-volume', ''), iscp_to_command('MVL'))
77

8+
def test_with_zone(self):
9+
self.assertEqual(('main', 'audio-muting', ''), iscp_to_command('AMT', True))
10+
self.assertEqual(('zone2', 'muting', ''), iscp_to_command('ZMT', True))
11+
self.assertEqual(('zone3', 'muting', ''), iscp_to_command('MT3', True))
12+
self.assertEqual(('zone4', 'muting', ''), iscp_to_command('MT4', True))
813

9-
class TestCommandToIscp:
14+
class TestCommandToIscp(TestCase):
1015
def test(self):
11-
assert command_to_iscp('main.system-power=standby') == 'PWR00'
16+
self.assertEqual('PWR00', command_to_iscp('main.system-power=standby'))
1217

1318
def test_argument_aliases(self):
14-
assert command_to_iscp('main.system-power=off') == 'PWR00'
19+
self.assertEqual('PWR00', command_to_iscp('main.system-power=off'))

0 commit comments

Comments
 (0)