├── requirements.txt ├── lightbulb.png ├── config.json ├── README.md └── tuya-tray.py /requirements.txt: -------------------------------------------------------------------------------- 1 | PyQt6==6.4.0 2 | tuyapy==0.1.4 3 | requests==2.28.1 4 | -------------------------------------------------------------------------------- /lightbulb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dev-est/tuya_tray/HEAD/lightbulb.png -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | {"username":"example@domain.com","password": "YOURPASS","country_code":"44","application":"tuya"} 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tuya Tray 2 | 3 | Tuya Tray is an application utilising the Tuya API to automatically create a taskbar application to control SmartLife/Tuya lights. 4 | 5 | 6 | ### Installation 7 | 8 | Tuya Tray requires [Python 3](https://www.python.org/downloads/) to run. 9 | 10 | Install the requirements and run the script. 11 | 12 | ```sh 13 | $ pip install -r requirements.txt 14 | $ python tuya-tray.py 15 | ``` 16 | 17 | You will also need to alter the config.json file with the following: 18 | 1. Your Tuya/SmartLife login and password so it looks like this {"username":"example@domain.com","password":"YOURPASS"...} 19 | 2. Your country code ("44" for UK users, "1" for US/Canadian users, etc) 20 | 3. The application you're using ('tuya' for tuya users and 'smart_life' for smart life users) 21 | 22 | ``` 23 | {"username":"example@domain.com","password":"YOURPASS","country_code":"44","application":"tuya"} 24 | ``` 25 | ### In Action! 26 | 27 | ![Gif](https://i.imgur.com/YdeAgh4.gif) 28 | 29 | 30 | 31 | 32 | 33 | ### Todos 34 | 35 | - Implement colour wheel to lights 36 | - Light percentages (25,50,75) 37 | -------------------------------------------------------------------------------- /tuya-tray.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from PyQt6.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QColorDialog 3 | from PyQt6.QtGui import QIcon 4 | from PyQt6.QtCore import QCoreApplication 5 | from tuyapy import TuyaApi 6 | import os 7 | import time 8 | from functools import partial 9 | import json 10 | 11 | api = TuyaApi() 12 | 13 | class SystemTrayIcon(QSystemTrayIcon): 14 | 15 | def __init__(self,parent=None): 16 | super().__init__() 17 | QSystemTrayIcon.__init__(self,parent) 18 | self.setIcon(QIcon('lightbulb.png')) 19 | self.setToolTip('Tray - Lights') 20 | self.menu = QMenu() 21 | self.initUI() 22 | 23 | def turn_off(self,device): 24 | is_list = isinstance(device,list) 25 | if is_list == False: 26 | return device.turn_off() 27 | else: 28 | return [i.turn_off() for i in device] 29 | 30 | def turn_on(self,device): 31 | is_list = isinstance(device,list) 32 | if is_list == False: 33 | return device.turn_on() 34 | else: 35 | return [i.turn_on() for i in device] 36 | 37 | def change_colour(self,device): 38 | colors = QColorDialog.getColor() 39 | h,s,v,t = colors.getHsv() 40 | s = int((s/255 * 100)) 41 | if s < 60: 42 | s = 60 43 | is_list = isinstance(device,list) 44 | if is_list == False: 45 | return device.set_color([h,s,100]) 46 | else: 47 | return [i.set_color([h,s,100]) for i in device] 48 | 49 | def initUI(self): 50 | 51 | with open('config.json') as config: 52 | data = json.load(config) 53 | 54 | username,password,country_code,application = data['username'],data['password'],data['country_code'],data['application'] 55 | api.init(username,password,country_code,application) 56 | self.device_ids = api.get_all_devices() 57 | self.switch = dict(sorted(dict((i.name(),i) for i in self.device_ids if i.obj_type == 'switch').items())) 58 | self.switch['All Switches'] = list(self.switch.values()) 59 | self.lights = dict(sorted(dict((i.name(),i) for i in self.device_ids if i.obj_type == 'light').items())) 60 | self.lights['All Lights'] = list(self.lights.values()) 61 | self.devices = {**self.switch,**self.lights} 62 | self.menus = dict() 63 | self.counter = 0 64 | 65 | for j in self.devices.keys(): 66 | if isinstance(self.devices[j],list) == False and self.devices[j].obj_type == 'light': 67 | if self.counter == 0: 68 | self.menu.addSeparator() 69 | self.counter += 1 70 | self.menus[f"{j}_Action"] = self.menu.addMenu(j) 71 | if j in self.lights.keys(): 72 | on_menu = self.menus[f"{j}_Action"].addMenu('On') 73 | on = on_menu.addAction('On') 74 | colour_wheel = on_menu.addAction('Light Colour') 75 | colour_wheel.triggered.connect(partial(self.change_colour,self.devices[j])) 76 | else: 77 | on = self.menus[f"{j}_Action"].addAction('On') 78 | 79 | off = self.menus[f"{j}_Action"].addAction('Off') 80 | on.triggered.connect(partial(self.turn_on,self.devices[j])) 81 | off.triggered.connect(partial(self.turn_off,self.devices[j])) 82 | 83 | exitaction = self.menu.addAction('Exit') 84 | exitaction.triggered.connect(QCoreApplication.quit) 85 | self.setContextMenu(self.menu) 86 | self.show() 87 | 88 | if __name__ == "__main__": 89 | app= QApplication.instance() # checks if QApplication already exists 90 | if not app: # create QApplication if it doesnt exist 91 | app = QApplication(sys.argv) 92 | app.setQuitOnLastWindowClosed(False) 93 | tray = SystemTrayIcon() 94 | sys.exit(app.exec()) 95 | --------------------------------------------------------------------------------