Last active
February 8, 2023 20:15
-
-
Save scmanjarrez/779046531e14987799543164af9ff579 to your computer and use it in GitHub Desktop.
Code snippet for custom gcodes in klipper to start Tuya Smart Plugs using tinytuya without printer connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gcode.register_command('TUYA_ON', self.cmd_TUYA_ON, True, | |
desc=self.cmd_TUYA_ON_help) | |
gcode.register_command('TUYA_OFF', self.cmd_TUYA_OFF, True, | |
desc=self.cmd_TUYA_OFF_help) | |
### ... | |
### ... | |
cmd_TUYA_ON_help = "Turn on Tuya Smart Plug" | |
def cmd_TUYA_ON(self, gcmd): | |
try: | |
proc = subprocess.Popen( | |
["/home/pi/tuya/venv/bin/python3", "-c", "import tinytuya; tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE').turn_on()"], | |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
except Exception: | |
logging.exception("tuya_on: failed") | |
raise gcmd.error("Error running TUYA_ON") | |
cmd_TUYA_OFF_help = "Turn off Tuya Smart Plug" | |
def cmd_TUYA_OFF(self, gcmd): | |
try: | |
proc = subprocess.Popen( | |
["/home/pi/tuya/venv/bin/python3", "-c", "import tinytuya; tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE').turn_off()"], | |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
except Exception: | |
logging.exception("tuya_off: failed") | |
raise gcmd.error("Error running TUYA_OFF") | |
cmd_RESTART_WIFI_help = "Restart WiFi service" | |
def cmd_RESTART_WIFI(self, gcmd): | |
try: | |
proc = subprocess.Popen( | |
["sudo", "ip", "link", "set", "wlan0", "down"], | |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
proc = subprocess.Popen( | |
["sudo", "ip", "link", "set", "wlan0", "up"], | |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
except Exception: | |
logging.exception("RESTART_WIFI: failed") | |
raise gcmd.error("Error running RESTART_WIFI" | |
cmd_LED_CONTROL_args = ('bright_up', 'bright_down', 'off', 'on', 'red', 'green', 'darkblue', 'white', 'orange', 'green-blue', 'blue-violet', 'flash', 'yellow', 'blue-green', 'violet', 'strobe', 'green-yellow', 'aquamarine', 'purple', 'fade', 'lightgreen', 'cyan', 'pink', 'smooth') | |
cmd_LED_CONTROL_help = f"Control LED strip: {', '.join(cmd_LED_CONTROL_args)}" | |
def cmd_LED_CONTROL(self, gcmd): | |
arg = gcmd.get_command_parameters()['CMD'] | |
if arg not in self.cmd_LED_CONTROL_args: | |
gcmd.respond_info(f"Invalid CMD: {', '.join(self.cmd_LED_CONTROL_args)}") | |
else: | |
try: | |
proc = subprocess.Popen( | |
["led_control", arg], | |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
except Exception: | |
logging.exception("LED_CONTROL: failed") | |
raise gcmd.error("Error running LED_CONTROL") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import subprocess | |
import sys | |
btn_map = { | |
'bright_up': 'KEY_A', | |
'bright_down': 'KEY_B', | |
'off': 'KEY_C', | |
'on': 'KEY_D', | |
'red': 'KEY_E', | |
'green': 'KEY_F', | |
'darkblue': 'KEY_G', | |
'white': 'KEY_H', | |
'orange': 'KEY_I', | |
'green-blue': 'KEY_J', | |
'blue-violet': 'KEY_K', | |
'flash': 'KEY_L', | |
'yellow': 'KEY_M', | |
'blue-green': 'KEY_N', | |
'violet': 'KEY_O', | |
'strobe': 'KEY_P', | |
'green-yellow': 'KEY_Q', | |
'aquamarine': 'KEY_R', | |
'purple': 'KEY_S', | |
'fade': 'KEY_T', | |
'lightgreen': 'KEY_U', | |
'cyan': 'KEY_V', | |
'pink': 'KEY_W', | |
'smooth': 'KEY_X' | |
} | |
if __name__ == '__main__': | |
if len(sys.argv) == 1: | |
print("Argument required: {}".format(', '.join(btn_map.keys()))) | |
else: | |
if sys.argv[1] == '-h' or sys.argv[1] == '--help': | |
print("Posible values: {}".format(', '.join(btn_map.keys()))) | |
elif sys.argv[1] in btn_map: | |
subprocess.Popen( | |
["irsend", "SEND_ONCE", "led_rc", btn_map[sys.argv[1]]], | |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
else: | |
print("Argument must be: {}".format(', '.join(btn_map.keys()))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Please take the time to finish this file as described in | |
# https://sourceforge.net/p/lirc-remotes/wiki/Checklist/ | |
# and make it available to others by sending it to | |
# <[email protected]> | |
# | |
# This config file was automatically generated | |
# using lirc-0.10.1(default) on Mon May 23 19:04:29 2022 | |
# Command line used: -d /dev/lirc-rx | |
# Kernel version (uname -r): 5.15.32+ | |
# | |
# Remote name (as of config file): led_rc | |
# Brand of remote device, the thing you hold in your hand: | |
# Remote device model nr: | |
# Remote device info url: | |
# Does remote device has a bundled capture device e. g., a | |
# usb dongle? : | |
# For bundled USB devices: usb vendor id, product id | |
# and device string (use dmesg or lsusb): | |
# Type of device controlled | |
# (TV, VCR, Audio, DVD, Satellite, Cable, HTPC, ...) : | |
# Device(s) controlled by this remote: | |
begin remote | |
name led_rc | |
bits 24 | |
flags SPACE_ENC|CONST_LENGTH | |
eps 30 | |
aeps 100 | |
header 9357 4483 | |
one 653 1615 | |
zero 653 497 | |
ptrail 654 | |
repeat 9353 2197 | |
pre_data_bits 8 | |
pre_data 0x0 | |
gap 135141 | |
toggle_bit_mask 0x0 | |
frequency 38000 | |
begin codes | |
KEY_A 0xF700FF | |
KEY_B 0xF7807F | |
KEY_C 0xF740BF | |
KEY_D 0xF7C03F | |
KEY_E 0xF720DF | |
KEY_F 0xF7A05F | |
KEY_G 0xF7609F | |
KEY_H 0xF7E01F | |
KEY_I 0xF710EF | |
KEY_J 0xF7906F | |
KEY_K 0xF750AF | |
KEY_L 0xF7D02F | |
KEY_M 0xF730CF | |
KEY_N 0xF7B04F | |
KEY_O 0xF7708F | |
KEY_P 0xF7F00F | |
KEY_Q 0xF708F7 | |
KEY_R 0xF78877 | |
KEY_S 0xF748B7 | |
KEY_T 0xF7C837 | |
KEY_U 0xF728D7 | |
KEY_V 0xF7A857 | |
KEY_W 0xF76897 | |
KEY_X 0xF7E817 | |
end codes | |
end remote | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment