#!/usr/bin/env python3
import socket
import time
from optparse import OptionParser

"""
Example script to control Allone IR Blaster
Before you use this script you will need to setup the Allone using the WiWo App and give the Allone an IP address
You will also need to add some IR devices to the Wiwo app and make sure that they are controlled properly
Finally you will need to do a packet capture as you are pressing on the remote (one button at a time)
When looking for the UDP packet in wireshark, look for a packet with very long values as examples bellow
copy the value and remove the part with 686401326963 + mac address.
"""


# example: python3 alone.py -m accf2344c5be -r bec54423cfac -i 192.168.0.163 -d fan -s on

parser = OptionParser()
parser.add_option("-m", "--mac", dest="mac",
                  help="mac address of the switch")
parser.add_option("-r", "--reverse",
                  dest="rmac",
                  help="mac address of the switch in reverse order")
parser.add_option("-i", "--ip", dest="ip",
                  help="the ip address of the switch")
parser.add_option("-s", "--state", dest="state",
                  help="on or off")
parser.add_option("-d", "--device", dest="device",
                  help="name of device, air, fan, tv, amp")
(options, args) = parser.parse_args()


def run(mac, rmac, ip, device, state):

	s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
	s.connect((ip, 10000))

	print('Subscribing to Allone')
	s.send(bytes.fromhex('6864001e636C' + mac + '202020202020' + rmac + '202020202020')) # to subscribe
	time.sleep(1)

	if device == 'fan' and state == 'on':
		code = '2020202020206febe111b4af180100000000180100000000000000000801440c55069c018b049c018b049c0177019b017901990177019b018b049c0177019b0177019b018b049c018b04a2017101a00188049a0176019b0177019b018b049c018b049c0176019c018a049c018b049c0177019b018b049c017a01980177019b0177019b0178019a017701960190049c01770198017a016e01a4016e01b8049a017a016d01a4016e01a3016e01a4016e01a4016e01a4016e01a40173019f016e01b8047101b8046d01a4016e01a4016e01b8046f01a4016e01a4016e01a4016e01ba046d01b9046e01a3016f01a4016e01a3016e01a4016e01a4016e01a4016e01a5016d01a3016e01a3016f01a3016e01a3016e01b7047001a4016e01a4016e01ba046d010000'
	if device == 'fan' and state == 'off':
		code = '2020202020206febe111c66d180100000000180100000000000000000801440c55069c018b049c018b049c0177019b017901990177019b018b049c0177019b0177019b018b049c018b04a2017101a00188049a0176019b0177019b018b049c018b049c0176019c018a049c018b049c0177019b018b049c017a01980177019b0177019b0178019a017701960190049c01770198017a016e01a4016e01b8049a017a016d01a4016e01a3016e01a4016e01a4016e01a4016e01a40173019f016e01b8047101b8046d01a4016e01a4016e01b8046f01a4016e01a4016e01a4016e01ba046d01b9046e01a3016f01a4016e01a3016e01a4016e01a4016e01a4016e01a5016d01a3016e01a3016f01a3016e01a3016e01b7047001a4016e01a4016e01ba046d010000'
	if device == 'air' and state == 'on':
		code = '2020202020206febe111c304180100000000180100000000000000000801ea228f110d024102120258021202a3062c023f021202a30629024102120258021202690212025b020f02a30628028d062902430212029a061e0256021202a3061302b4061302580212025b020f025802120259021102590211025802110258021202690214025602120259021102580212025802120258021202580212025802120269021102580211025902110259021202580212025902110259021102580212026902120258021202a3061302580212025b020f02580211025802120258021202b406130258021302a40611025802120258021202580212025802120258021202b406130258021202580212025802120258021202a606100257021302a2062a02440213020000'
	if device == 'air' and state == 'off':
		code = '2020202020206febe111d78c18010000000018010000000000000000080103237d11e5016b02ff016b02ff01b60602026802ff016b02ff016b02ff016b02ff017c02ff016b02ff01b906fc01b60602026902ff01b50600026d02fd01b6060002c70600026b02ff016b02ff016e02fd016b02ff016b02ff016b02ff016b02ff017c02ff016b0203026702ff016b0209026102ff016b02ff016b02ff016b02ff017c0200026b02ff016b02ff016b02ff016b02ff016b02ff016b02ff016d02fd017c02ff01b6060002b60600026b02ff016d02fe016a02ff016b02ff016b02ff01c70600026b02ff01b806fd016b02ff016b02ff016b02ff016b02ff016b020902c106fc016b02ff016b02ff016b02ff016b02ff01b806fd016b02ff01b60600026e02ff010000'
	if device == 'amp' and state == 'on':
		code = '2020202020206febe1114e0ba80100000000a801000000000000000098011d236d1145028e062b028c06460223022c022402440221024f021a022c022402490288062c028d06460223022e028b0646022b02220225024302230245027506430223024502750643022302450223022a02260242028e062a02260242022402450222022a0226024202910628028f064602750642022402460278063f028f0627029106460256a41e2373112a02a9060e02a806460222020f02400228023e02480220020f0241022702a9060f02aa0646022202f901bf0646022202f70159020f02570250026b060f025702460274061002570246022202f70159020f02c506f40159021c024a0246022202f70159020f02c406f501c106470275060f025702460277060d02c106f701c206460251a4f022a7111202bf06f701c10646022302f70159020f02570245022302f70159020f02c206f701c10645022502f601c20645022402f70159020f025702430278060f025702430278060f02570244022402f6015a020f02c406f50159020f02570240022802f70159020f02c406f501c20640027b060f02570228029b060802c206f701c10628020000'
	print('Turning %s %s' %(device,state))
	s.send(bytes.fromhex('686401326963' + mac + code))
  

run(options.mac, options.rmac, options.ip, options.device, options.state)
