def _try_login( user_input: dict[str, Any]) -> tuple[dict[Any, Any], dict[str, Any]]: """Try login.""" response = {} country = [ country for country in TUYA_COUNTRIES if country.name == user_input[CONF_COUNTRY_CODE] ][0] data = { CONF_ENDPOINT: country.endpoint, CONF_AUTH_TYPE: AuthType.CUSTOM, CONF_ACCESS_ID: user_input[CONF_ACCESS_ID], CONF_ACCESS_SECRET: user_input[CONF_ACCESS_SECRET], CONF_USERNAME: user_input[CONF_USERNAME], CONF_PASSWORD: user_input[CONF_PASSWORD], CONF_COUNTRY_CODE: country.country_code, } for app_type in ("", TUYA_SMART_APP, SMARTLIFE_APP): data[CONF_APP_TYPE] = app_type if data[CONF_APP_TYPE] == "": data[CONF_AUTH_TYPE] = AuthType.CUSTOM else: data[CONF_AUTH_TYPE] = AuthType.SMART_HOME api = TuyaOpenAPI( endpoint=data[CONF_ENDPOINT], access_id=data[CONF_ACCESS_ID], access_secret=data[CONF_ACCESS_SECRET], auth_type=data[CONF_AUTH_TYPE], ) api.set_dev_channel("hass") response = api.connect( username=data[CONF_USERNAME], password=data[CONF_PASSWORD], country_code=data[CONF_COUNTRY_CODE], schema=data[CONF_APP_TYPE], ) LOGGER.debug("Response %s", response) if response.get(TUYA_RESPONSE_SUCCESS, False): break return response, data
import logging from tuya_iot import TuyaOpenAPI, TuyaOpenMQ, TUYA_LOGGER from env import ENDPOINT, ACCESS_ID, ACCESS_KEY, USERNAME, PASSWORD TUYA_LOGGER.setLevel(logging.DEBUG) # Init openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY) openapi.connect(USERNAME, PASSWORD, "86", 'tuyaSmart') # Receive device message def on_message(msg): print("on_message: %s" % msg) openapi.token_info.expire_time = 0 openmq = TuyaOpenMQ(openapi) openmq.start() openmq.add_message_listener(on_message)
import logging from env import ENDPOINT, ACCESS_ID, ACCESS_KEY, USERNAME, PASSWORD from tuya_iot import (TuyaOpenAPI, AuthType, TuyaOpenMQ, TuyaDeviceManager, TuyaHomeManager, TuyaDeviceListener, TuyaDevice, TuyaTokenInfo, TUYA_LOGGER) TUYA_LOGGER.setLevel(logging.DEBUG) # Init openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, AuthType.CUSTOM) openapi.connect(USERNAME, PASSWORD) openmq = TuyaOpenMQ(openapi) openmq.start() print("device test-> ", openapi.token_info.uid) # Get device list # assetManager = TuyaAssetManager(openapi) # devIds = assetManager.getDeviceList(ASSET_ID) # Update device status deviceManager = TuyaDeviceManager(openapi, openmq) homeManager = TuyaHomeManager(openapi, openmq, deviceManager) homeManager.update_device_cache() # # deviceManager.updateDeviceCaches(devIds) # device = deviceManager.deviceMap.get(DEVICE_ID) class tuyaDeviceListener(TuyaDeviceListener): def update_device(self, device: TuyaDevice): print("_update-->", device)