Skip to content

Instantly share code, notes, and snippets.

@kellerza
Created May 23, 2016 18:35
Show Gist options
  • Save kellerza/9058851afad560c6b87fbbd982982d96 to your computer and use it in GitHub Desktop.
Save kellerza/9058851afad560c6b87fbbd982982d96 to your computer and use it in GitHub Desktop.
Decode QSUSB 'val' attributes
"""Testing QwikSwitch value attributes."""
def legacy_status(st):
"""legacy_status method from the qsmobile.js.
Pass in the 'val' from &devices or the
'data' received after calling a specific ID.
"""
# 2d0c00002a0000
if st[:2] == '30' or st[:2] == '47': # RX1 CT
o = st[4:5]
# console.log("legstat. " + o);
if o == '0':
return 0
if o == '8':
return 100
if st == '7e':
return 0
if st == '7f':
return 100
if len(st) == 6: # old
l = int(st[4:], 16)
hw = st[:2]
if hw == '01': # old dim
return round(((125-l) / 125) * 100)
if hw == '02': # old rel
return 100 if l == 127 else 0
if hw == '28': # LED DIM
if st[2:4] == "01":
if st[4:] == '78':
return 0
return round(((120 - l) / 120) * 100)
# Additional tests
if st.upper().find('ON') >= 0:
return 100
if len(st) == 0 or st.upper().find('OFF') >= 0:
return 0
return -1 # fallback to return an int
# return st
def test(a, b, s):
"""assert testing."""
if a == b:
print('OK: {} [{}]'.format(s, b))
else:
print('ERR: {} ["{}" should be {}]'.format(s, b, a))
test(0, legacy_status(''), 'PB QC off')
test(100, legacy_status('4733800000d50001'), 'PB QC on')
test(0, legacy_status('280c78'), 'JK dim 0')
test(20, legacy_status('280c60'), 'JK dim 20')
test(100, legacy_status('280c00'), 'JK dim 100')
test(100, legacy_status('ON'), 'JK rel on')
test(0, legacy_status('OFF'), 'JK rel off')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment