def onStart(self): # Debug mode if (Parameters["Mode6"] == "Debug"): Domoticz.Debugging(1) Domoticz.Debug("onStart called") else: Domoticz.Debugging(0) #get parameters self.__address = Parameters["Address"] self.__devID = Parameters["Mode1"] self.__localKey = Parameters["Mode2"] #initialize the defined device in Domoticz if (len(Devices) == 0): Domoticz.Device(Name="Tuya SmartBulb", Unit=1, Type=241, Subtype=2, Switchtype=7).Create() Domoticz.Log("Tuya SmartBulb Device created.") #create the pytuya object self.__device = pytuya.BulbDevice(self.__devID, self.__address, self.__localKey) #start the connection self.__last_payload = 'status' self.__connection = Domoticz.Connection(Name="Tuya", Transport="TCP/IP", Address=self.__address, Port="6668") self.__connection.Connect()
def test_set_white(self): d = pytuya.BulbDevice('ID', 'IP_ADDRESS_HERE', LOCAL_KEY) d._send_receive = MagicMock(side_effect=mock_send_receive_set_white) result = d.set_white(255, 255) result = result.decode(mock_byte_encoding) result = json.loads(result) self.assertEqual(result['test_result'], "SUCCESS")
def setup_platform(hass, config, add_devices, discovery_info=None): """Set up of the Tuya switch.""" import pytuya add_devices([TuyaDevice( pytuya.BulbDevice( config.get(CONF_DEVICE_ID), config.get(CONF_HOST), config.get(CONF_LOCAL_KEY), ), config.get(CONF_NAME), )])
def bulb_update(name, id, ip, localkey): global skip_frame print("Enter") bulb = pytuya.BulbDevice(id, ip, localkey) data = bulb.status(); print('Dictionary %r' % data) bulb.set_status(True) while(True): try: if not skip_frame: bulb.set_colour(result_color.color[2],result_color.color[1],result_color.color[0]) except: print(name, " failed to update.") time.sleep(light_update_wait)
def sendCommand(self, cmd, pluglabel, args=None, tries=1): self._tuyasmartbulb_logger.debug('Sending command: %s to %s' % (cmd, pluglabel)) plug = self.plug_search(self._settings.get(["arrSmartbulbs"]), "label", pluglabel) device = pytuya.BulbDevice(plug['id'], plug['ip'], plug['localKey']) commands = { 'info': ('status', None), 'on': ('set_status', True), 'off': ('set_status', False), 'countdown': ('set_timer', None), } try: command, arg = commands[cmd] func = getattr(device, command, None) if not func: self._tuyasmartbulb_logger.debug("No such command '%s'" % command) return False if args: func(args) elif arg is not None: func(arg, plug['slot']) else: func() time.sleep(0.5) ret = device.status() self._tuyasmartbulb_logger.debug('Status: %s' % str(ret)) return ret except socket.error as e: if e.errno == 104: if tries <= 3: self._tuyasmartbulb_logger.debug( "Connection refused... Trying again soon") time.sleep(1) return self.sendCommand(cmd, pluglabel, args, tries + 1) self._tuyasmartbulb_logger.debug("Too many failed attempts") return False self._tuyasmartbulb_logger.debug("Network error") return False except: self._tuyasmartbulb_logger.debug( 'Something went wrong while running the command') return False
def setup_platform(hass, config, add_devices, discovery_info=None): """Set up of the Tuya light.""" import pytuya devices = config[CONF_DEVICES] lights = [] for name, device_config in devices.items(): bulb_device = pytuya.BulbDevice(device_config.get(CONF_DEVICE_ID), device_config.get(CONF_HOST), device_config.get(CONF_LOCAL_KEY)) #name = device_config.get(CONF_NAME) if not name: name = "tuya_" + device_config.get(CONF_DEVICE_ID) device_id = device_config.get(CONF_ID) if not device_id: device_id = device_config.get(CONF_DEVICE_ID) icon = device_config.get(CONF_ICON) if not icon: icon = "mdi:lightbulb" lights.append(TuyaDevice(bulb_device, name, icon, device_id)) add_devices(lights)
def main(): # host: 192.168.86.88 # local_key: 8e46c6403fb73d29 # device_id: 17607013ecfabc48667b print("Started") # kogan 4 # pytuyadevice = pytuya.BulbDevice('17607013ecfabc48667b','192.168.86.88','8e46c6403fb73d29') # kogan 1 # pytuyadevice = pytuya.BulbDevice('17607013840d8ebb24c8','192.168.86.45','4f1b5d561aa4ec0f') # kogan 2 pytuyadevice = pytuya.BulbDevice('17607013840d8ebb2438','192.168.86.52','d3c391f1abb9dbe1') pytuyadevice.set_version(3.3) bulb_device = TuyaCache(pytuyadevice) bulb_device.turn_on() bulb_device.set_brightness(1000) bulb_device.set_color_temp(1000) print("state " + str(bulb_device.state())) print("brightness " + str(bulb_device.brightness())) print("colour_temp " + str(bulb_device.color_temp())) bulb_device.set_brightness(1000) bulb_device.set_color_temp(0) print("state " + str(bulb_device.state())) print("brightness " + str(bulb_device.brightness())) print("colour_temp " + str(bulb_device.color_temp()))
import pytuya import time d = pytuya.BulbDevice('eb24ee55d15b03d5166pwb', '192.168.0.101', '783cb6fa9e058b5c') d.set_version(3.3) d.turn_on() a = 255 b = 0 c = 0 def rgb_rainbow(): while True: a = 255 b = 0 c = 0 while b < 255: d.set_colour(a,b,c) b += 5 while a !=0: d.set_colour(a,b,c) a -= 5 while c < 255: d.set_colour(a,b,c) c += 5 while b !=0: d.set_colour(a,b,c) b -= 5 while a < 255: d.set_colour(a,b,c)
import discord from discord.ext import commands from itertools import repeat import time import math import pytuya light = pytuya.BulbDevice( 'VIRTUAL-DEVICE-ID', 'INTERNAL-IP', 'LOCAL-KEY') # Refer To Dozens Of Guides Of How To Aquire A Local Key bar = pytuya.OutletDevice( 'VIRTUAL-DEVICE-ID', 'INTERNAL-IP', 'LOCAL-KEY') # Refer To Dozens Of Guides Of How To Aquire A Local Key def blueredblink(data): for x in range(0, 10): data = light.set_colour(255, 0, 0) time.sleep(1) data = light.set_colour(0, 0, 255) time.sleep(1) def redgreenblink(data): for x in range(0, 10): data = light.set_colour(0, 255, 0) time.sleep(0.3) data = light.set_colour(255, 0, 0) time.sleep(0.3)