Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for newer IR devices, and several IR format converters #228

Merged
merged 4 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 96 additions & 12 deletions examples/Contrib/IRRemoteControlDevice-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Example script using the community-contributed Python module for Tuya WiFi smart universal remote control simulators

Author: Alexey 'Cluster' Avdyukhin (https://github.com/clusterm)
Rewritten by: uzlonewolf (https://github.com/uzlonewolf)
For more information see https://github.com/jasonacox/tinytuya

"""
Expand All @@ -13,35 +14,112 @@
from tinytuya import Contrib
from time import sleep

# tinytuya.set_debug(toggle=True, color=True)
#tinytuya.set_debug(toggle=True, color=True)

# discrete on/off codes for Samsung



# parsing and converting between data formats


# discrete on/off codes for Samsung in Pronto format
pronto_samsung_on = '0000 006D 0000 0022 00AC 00AC 0015 0040 0015 0040 0015 0040 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0040 0015 0040 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0015 0015 0015 0015 0040 0015 0040 0015 0015 0015 0015 0015 0040 0015 0015 0015 0040 0015 0040 0015 0015 0015 0015 0015 0040 0015 0040 0015 0015 0015 0689'
pronto_samsung_off = '0000 006D 0000 0022 00AC 00AC 0015 0040 0015 0040 0015 0040 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0040 0015 0040 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0040 0015 0015 0015 0015 0015 0040 0015 0040 0015 0040 0015 0040 0015 0015 0015 0015 0015 0040 0015 0040 0015 0015 0015 0689'

# convert the Pronto format into pulses
pulses_samsung_on = Contrib.IRRemoteControlDevice.pronto_to_pulses( pronto_samsung_on )
pulses_samsung_off = Contrib.IRRemoteControlDevice.pronto_to_pulses( pronto_samsung_off )
print( 'Samsung on code:', Contrib.IRRemoteControlDevice.pulses_to_samsung( pulses_samsung_on )[0] )

# decode the pulses as Samsung format (similar to NEC but with a half-width start burst)
# there may be more than one code in the data stream, so this returns a list of codes
samsung_on_code = Contrib.IRRemoteControlDevice.pulses_to_samsung( pulses_samsung_on )
samsung_off_code = Contrib.IRRemoteControlDevice.pulses_to_samsung( pulses_samsung_off )

# print only the first code
print( 'Samsung on code:', samsung_on_code[0] )
# Samsung on code: {'type': 'samsung', 'uint32': 3772815718, 'address': 7, 'data': 153, 'hex': 'E0E09966'}
print( 'Samsung off code:', Contrib.IRRemoteControlDevice.pulses_to_samsung( pulses_samsung_off )[0] )

print( 'Samsung off code:', samsung_off_code[0] )
# Samsung off code: {'type': 'samsung', 'uint32': 3772783078, 'address': 7, 'data': 152, 'hex': 'E0E019E6'}






# discrete on/off codes for LG
hex_lg_on = 0x20DF23DC
hex_lg_off = 0x20DFA35C

# convert the 32-bit integers into a stream of pulses
pulses_lg_on = Contrib.IRRemoteControlDevice.nec_to_pulses( hex_lg_on )
pulses_lg_off = Contrib.IRRemoteControlDevice.nec_to_pulses( hex_lg_off )
print( 'LG on code:', Contrib.IRRemoteControlDevice.pulses_to_nec( pulses_lg_on )[0] )

# decode the pulses to verify and print them like the above Samsung
lg_on_code = Contrib.IRRemoteControlDevice.pulses_to_nec( pulses_lg_on )
print( 'LG on code:', lg_on_code[0] )
# LG on code: {'type': 'nec', 'uint32': 551494620, 'address': 4, 'data': 196, 'hex': '20DF23DC'}
print( 'LG off code:', Contrib.IRRemoteControlDevice.pulses_to_nec( pulses_lg_off )[0] )

lg_off_code = Contrib.IRRemoteControlDevice.pulses_to_nec( pulses_lg_off )
print( 'LG off code:', lg_off_code[0] )
# LG off code: {'type': 'nec', 'uint32': 551527260, 'address': 4, 'data': 197, 'hex': '20DFA35C'}

ir = Contrib.IRRemoteControlDevice( 'abcdefghijklmnop123456', '172.28.321.475', '1234567890123abc' )

# turn the Samsung tv on



# both Pronto codes and pulses can also be turned into head/key format
# Pronto will have the correct frequency in the data
headkey = Contrib.IRRemoteControlDevice.pronto_to_head_key( pronto_samsung_on )
if headkey:
head, key = headkey
# but the pulses frequency needs to be specified manually if it is not 38 kHz
headkey = Contrib.IRRemoteControlDevice.pulses_to_head_key( pulses_samsung_on, freq=38 )
if headkey:
head, key = headkey




# learned codes can also be converted
pulses = Contrib.IRRemoteControlDevice.base64_to_pulses('IyOvEToCZQI5AkoCOgJNAjYCTwI4AlACNQJMAjkCTQI2ApsGSwKZBkkClwZMAp8GLALLBhgC0wYRAtMGEwLRBhMCbgIdAmkCGwLKBhsCagIaAsoGGgJzAhACbwIWAnICFAJvAh0CxgYdAmoCFwLMBhoCcAIUAtAGFALRBhQC0QYUAtAGFQKXnBgjCAkXAiDL')
# default frequency is 38 kHz
headkey = Contrib.IRRemoteControlDevice.pulses_to_head_key( pulses )
if headkey:
head, key = headkey




# now onto talking to the device!


# create the device. this will connect to it to try and determine which DPS it uses
#ir = Contrib.IRRemoteControlDevice( 'abcdefghijklmnop123456', '172.28.321.475', '1234567890123abc', persist=True )
ir = Contrib.IRRemoteControlDevice("8357021598cdac0aff74", address='172.20.10.150', local_key='3ba3a31d71dd9ee8', version=3.3, persist=True)


print( 'Turning the Samsung tv on with pulses' )
ir.send_button( ir.pulses_to_base64( pulses_samsung_on ) )
# turn the LG tv on
sleep(0.5)
print( 'Turning the LG tv on with pulses' )
ir.send_button( ir.pulses_to_base64( pulses_lg_on ) )
sleep(0.5)


print( 'Turning the Samsung tv off with head/key' )
head, key = Contrib.IRRemoteControlDevice.pronto_to_head_key( pronto_samsung_off )
ir.send_key( head, key )
sleep(0.5)
print( 'Turning the LG tv off with head/key' )
head, key = Contrib.IRRemoteControlDevice.pulses_to_head_key( pulses_lg_off )
ir.send_key( head, key )
sleep(0.5)




# learn a new remote
print("Press button on your remote control")
button = ir.receive_button(timeout=15)
if (button == None):
Expand All @@ -50,10 +128,16 @@

print("Received button:", button)
pulses = ir.base64_to_pulses(button)
print("Pulses and gaps (microseconds): " +
' '.join([f'{"p" if i % 2 == 0 else "g"}{pulses[i]}' for i in range(len(pulses))]))
print( Contrib.IRRemoteControlDevice.print_pulses( pulses ) )
headkey = Contrib.IRRemoteControlDevice.pulses_to_head_key( pulses )
if headkey:
head, key = headkey
print( 'Head:', head )
print( 'Key:', key )

for i in range(10):
print("Simulating button press...")
ir.send_button(button)
# either works
#ir.send_button(button)
ir.send_key( head, key )
sleep(1)
Loading