Exemplo n.º 1
0
def setup(hass, config):
    """Set up Tuya Component."""
    from tuyapy import TuyaApi

    tuya = TuyaApi()
    username = config[DOMAIN][CONF_USERNAME]
    password = config[DOMAIN][CONF_PASSWORD]
    country_code = config[DOMAIN][CONF_COUNTRYCODE]
    platform = config[DOMAIN][CONF_PLATFORM]

    hass.data[DATA_TUYA] = tuya
    tuya.init(username, password, country_code, platform)
    hass.data[DOMAIN] = {'entities': {}}

    def load_devices(device_list):
        """Load new devices by device_list."""
        device_type_list = {}
        for device in device_list:
            dev_type = device.device_type()
            if (dev_type in TUYA_TYPE_TO_HA and device.object_id()
                    not in hass.data[DOMAIN]['entities']):
                ha_type = TUYA_TYPE_TO_HA[dev_type]
                if ha_type not in device_type_list:
                    device_type_list[ha_type] = []
                device_type_list[ha_type].append(device.object_id())
                hass.data[DOMAIN]['entities'][device.object_id()] = None
        for ha_type, dev_ids in device_type_list.items():
            discovery.load_platform(hass, ha_type, DOMAIN,
                                    {'dev_ids': dev_ids}, config)

    device_list = tuya.get_all_devices()
    load_devices(device_list)

    def poll_devices_update(event_time):
        """Check if accesstoken is expired and pull device list from server."""
        _LOGGER.debug("Pull devices from Tuya.")
        tuya.poll_devices_update()
        # Add new discover device.
        device_list = tuya.get_all_devices()
        load_devices(device_list)
        # Delete not exist device.
        newlist_ids = []
        for device in device_list:
            newlist_ids.append(device.object_id())
        for dev_id in list(hass.data[DOMAIN]['entities']):
            if dev_id not in newlist_ids:
                dispatcher_send(hass, SIGNAL_DELETE_ENTITY, dev_id)
                hass.data[DOMAIN]['entities'].pop(dev_id)

    track_time_interval(hass, poll_devices_update, timedelta(minutes=5))

    hass.services.register(DOMAIN, SERVICE_PULL_DEVICES, poll_devices_update)

    def force_update(call):
        """Force all devices to pull data."""
        dispatcher_send(hass, SIGNAL_UPDATE_ENTITY)

    hass.services.register(DOMAIN, SERVICE_FORCE_UPDATE, force_update)

    return True
Exemplo n.º 2
0
 def __init__(self):
     creds = get_credentials()
     tapi = TuyaApi()
     tapi.init(
         username=creds.get("TUYA_USERNAME"),
         password=creds.get("TUYA_PASSWORD"),
         countryCode=creds.get("TUYA_LOCATION")
     )
     self.device = tapi.get_device_by_id(creds.get("TUYA_DEVICE_ID"))
Exemplo n.º 3
0
    async def discover(self):
        print("tuya scan...")
        device_list = []

        # https://github.com/dev-est/tuya_tray/blob/master/tuya-tray.py
        api = TuyaApi()
        api.init("USER_NAME", "PASSWORD", "61", "smart_life")
        devices2 = api.discover_devices()
        print(devices2)

        #devices = tinytuya.deviceScan(True, 1)
        #for ip in devices:
        #    dev_conf = devices[ip]
        #    print(devices[ip])
        #    dev = self.create_device(dev_conf)
        #    print("we found a tuya device!")
        #    device_list.append(dev)

        print("tuya scan done")
        return device_list
Exemplo n.º 4
0
def main():
    username = input("Please enter username: "******"How many times would you like your lights to flash? Max (10): "))
    if repeat > 10:
        repeat = 10
    elif repeat < 0:
        repeat = 1
    api = TuyaApi()
    api.init(os.getenv('TUYA_LOGIN'),os.getenv('TUYA_PASSWORD'),"44","tuya")
    device_id = api.get_all_devices()
    devices = dict((i.name(),i) for i in device_id if i.obj_type == 'light')
    current_followers = get_followers(username)

    while True:
        followers_now = get_followers(username)
        if current_followers != followers_now:
            gained_followers = (int(followers_now) - int(current_followers))
            print(f"Change by {gained_followers}, New Follower count is {followers_now} " )
            tiktok_notif(devices,repeat)
            time.sleep(10)
            current_followers = followers_now
        else:
            print(f"Currently {current_followers} followers")
            time.sleep(100)
Exemplo n.º 5
0
import sys
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QColorDialog
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QCoreApplication
from tuyapy import TuyaApi
import os
import time
from functools import partial
import json

api = TuyaApi()


class SystemTrayIcon(QSystemTrayIcon):
    def __init__(self, parent=None):
        super().__init__()
        QSystemTrayIcon.__init__(self, parent)
        self.setIcon(QIcon('lightbulb.png'))
        self.setToolTip('Tray - Lights')
        self.menu = QMenu()
        self.initUI()

    def turn_off(self, device):
        is_list = isinstance(device, list)
        if is_list == False:
            return device.turn_off()
        else:
            return [i.turn_off() for i in device]

    def turn_on(self, device):
        is_list = isinstance(device, list)
Exemplo n.º 6
0
 def __init__(self, username, password, location, device):
     tuyaapi = TuyaApi()
     tuyaapi.init(username=username,
                  password=password,
                  countryCode=location)
     self.device = tuyaapi.get_device_by_id(device)
Exemplo n.º 7
0
def setup(hass, config):
    """Set up Tuya Component."""
    from tuyapy import TuyaApi

    tuya = TuyaApi()
    username = config[DOMAIN][CONF_USERNAME]
    password = config[DOMAIN][CONF_PASSWORD]
    country_code = config[DOMAIN][CONF_COUNTRYCODE]

    hass.data[DATA_TUYA] = tuya
    tuya.init(username, password, country_code)
    hass.data[DOMAIN] = {
        'entities': {}
    }

    def load_devices(device_list):
        """Load new devices by device_list."""
        device_type_list = {}
        for device in device_list:
            dev_type = device.device_type()
            if (dev_type in TUYA_TYPE_TO_HA and
                    device.object_id() not in hass.data[DOMAIN]['entities']):
                ha_type = TUYA_TYPE_TO_HA[dev_type]
                if ha_type not in device_type_list:
                    device_type_list[ha_type] = []
                device_type_list[ha_type].append(device.object_id())
                hass.data[DOMAIN]['entities'][device.object_id()] = None
        for ha_type, dev_ids in device_type_list.items():
            discovery.load_platform(
                hass, ha_type, DOMAIN, {'dev_ids': dev_ids}, config)

    device_list = tuya.get_all_devices()
    load_devices(device_list)

    def poll_devices_update(event_time):
        """Check if accesstoken is expired and pull device list from server."""
        _LOGGER.debug("Pull devices from Tuya.")
        tuya.poll_devices_update()
        # Add new discover device.
        device_list = tuya.get_all_devices()
        load_devices(device_list)
        # Delete not exist device.
        newlist_ids = []
        for device in device_list:
            newlist_ids.append(device.object_id())
        for dev_id in list(hass.data[DOMAIN]['entities']):
            if dev_id not in newlist_ids:
                dispatcher_send(hass, SIGNAL_DELETE_ENTITY, dev_id)
                hass.data[DOMAIN]['entities'].pop(dev_id)

    track_time_interval(hass, poll_devices_update, timedelta(minutes=5))

    hass.services.register(DOMAIN, SERVICE_PULL_DEVICES, poll_devices_update)

    def force_update(call):
        """Force all devices to pull data."""
        dispatcher_send(hass, SIGNAL_UPDATE_ENTITY)

    hass.services.register(DOMAIN, SERVICE_FORCE_UPDATE, force_update)

    return True