예제 #1
0
def setup(hass, config):
    """Set up Tuya Component."""
    from tuyaha 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
예제 #2
0
    def _try_connect(self):
        """Try to connect and check auth."""
        tuya = TuyaApi()
        try:
            tuya.init(self._username, self._password, self._country_code,
                      self._platform)
        except (TuyaNetException, TuyaServerException):
            return RESULT_CONN_ERROR
        except TuyaAPIException:
            return RESULT_AUTH_FAILED

        return RESULT_SUCCESS
예제 #3
0
 def __init__(self, device_name):
     username = credentials.login['username']
     password = credentials.login['password']
     country_code = 'us'
     api = TuyaApi()  # https://github.com/PaulAnnekov/tuyaha
     devices = api.init(username=username,
                        password=password,
                        countryCode=country_code)
     for dev in devices:
         dev_name = dev.obj_name
         if dev_name == device_name:
             self.device = dev
예제 #4
0
async def async_setup_entry(hass, entry):
    """Set up config entry"""

    tuya = TuyaApi()

    username = entry.data[CONF_USERNAME]
    password = entry.data[CONF_PASSWORD]
    country_code = entry.data[CONF_COUNTRYCODE]
    platform = entry.data[CONF_PLATFORM]

    hass.data[DATA_TUYA] = tuya
    tuya.init(username, password, country_code, platform)

    hass.data[DOMAIN] = {"entities": {}}

    for component in ("climate", "cover", "fan", "light", "switch"):
        hass.async_add_job(
            hass.config_entries.async_forward_entry_setup(entry, component))

    # 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}, entry.data)
    #
    # _LOGGER.info("debug4")
    #
    # 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)
    #
    # _LOGGER.info("debug5")
    #
    # # track_time_interval(hass, poll_devices_update, timedelta(minutes=5))
    # _LOGGER.info("debug5.5")
    #
    # hass.services.register(DOMAIN, SERVICE_PULL_DEVICES, poll_devices_update)

    # _LOGGER.info("debug6")
    #     #
    #     # def force_update(call):
    #     #     """Force all devices to pull data."""
    #     #     dispatcher_send(hass, SIGNAL_UPDATE_ENTITY)
    #     #
    #     # _LOGGER.info("debug6.5")
    #     #
    #     # hass.services.register(DOMAIN, SERVICE_FORCE_UPDATE, force_update)
    #     #
    #     # _LOGGER.info("debug7")

    return True
for opt, arg in opts:
    if opt == '-d':
        DEBUG = True
    elif opt == '-c':
        confFile = arg

conf = ConfigurationReader.Read(confFile)

username = conf["username"]
password = conf["password"]
countryCode = conf["country_code"]
deviceId = conf["device_id"]

api = TuyaApi()

api.init(username, password, countryCode)

device = api.get_device_by_id(deviceId)

device.set_brightness(10)
device.set_color_temp(2700)

delay = 5
step = 1

if DEBUG == True:
    delay = 1
    step = 20

for brightness in range(11, 255, step):
    device.set_brightness(brightness)