#!/usr/bin/env python3 """Check decoded NEC32 hex samples; does not capture or transmit infrared.""" import argparse from collections import Counter def check(lines): counts = Counter() for number, line in enumerate(lines, 1): value = line.partition('#')[0].strip() if not value: continue try: frame = bytes.fromhex(value) except ValueError as error: raise ValueError(f'line {number}: invalid hex') from error if len(frame) != 4: raise ValueError(f'line {number}: expected 4 bytes') if frame[2] ^ frame[3] != 0xff: raise ValueError(f'line {number}: command complement mismatch') counts[frame.hex(' ').upper()] += 1 return counts def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument('samples', help='UTF-8 text: one decoded 4-byte frame per line') args = p.parse_args() try: with open(args.samples, encoding='utf-8') as stream: counts = check(stream) except (OSError, ValueError) as error: p.exit(2, str(error) + '\n') if not counts: p.exit(2, 'no frames\n') for value, count in counts.most_common(): print(f'{value}: {count} frame(s); repeated={count >= 2}') print('This checks bytes only, not timing, origin or appliance response.') if __name__ == '__main__': main()