Skip to content

Commit

Permalink
- Format method naming.
Browse files Browse the repository at this point in the history
- Update Open API sign algorithm.
- Update log system to python logging.
- Update example.
- Fix MQTT reconnect.
  • Loading branch information
tsutsuku committed Jun 30, 2021
1 parent cffbd3f commit b3243bf
Show file tree
Hide file tree
Showing 14 changed files with 621 additions and 365 deletions.
81 changes: 67 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,82 @@
# Tuya IoT Python SDK



![PyPI](https://img.shields.io/pypi/v/tuya-iot-py-sdk)

![PyPI - Downloads](https://img.shields.io/pypi/dm/tuya-iot-py-sdk)

![PyPI - Python Version](https://img.shields.io/pypi/pyversions/tuya-iot-py-sdk)

A Python sdk for Tuya Open API, which provides basic IoT capabilities like device management, asset management and industry capabilities, helping you create IoT solutions. With diversified devices and industries, Tuya Cloud Development Platform opens basic IoT capabilities like device management, AI scenarios, and data analytics services, as well as industry capabilities, helping you create IoT solutions.


A Python sdk for Tuya Open API, which provides basic IoT capabilities like device management, asset management and industry capabilities, helping you create IoT solutions.
With diversified devices and industries, Tuya Cloud Development Platform opens basic IoT capabilities like device management, AI scenarios, and data analytics services, as well as industry capabilities, helping you create IoT solutions.



## Features
- OpenAPI
- Get the device list
- Get the device details
- Get device status
- Control devices
- Modify the device name
- Query the device log
- Remove devices
- ...
- Open IoT Hub
- Get device status change
### Base APIs
- TuyaOpenAPI
- login
- is_login
- get
- post
- put
- delete

- TuyaOpenQ
- start
- stop
- add_message_listner
- remove_message_listener

### APIs
- TuyaDeviceListener
- update_device
- add_device
- remove_device

#### Device control
- TuyaDeviceManager
- update_device_list_in_smart_home
- update_device_caches
- update_device_function_cache
- add_device_listener
- remove_device_listener
- get_device_info
- get_device_list_info
- remove_device
- remove_device_list
- get_factory_info
- factory_reset
- get_device_status
- get_device_list_status
- get_device_functions
- get_category_functions
- get_device_specification
- send_commands

#### Home
- TuyaHomeManager
- update_device_cache

#### Assets
- TuyaAssetManager
- get_device_list
- get_asset_info
- get_asset_list


## Possible scenarios



- [HomeAssistant Tuya Plugin](https://github.com/tuya/tuya-home-assistant)

- ...


## Prerequisite

### Registration
Expand All @@ -38,7 +91,7 @@ Please check [Tuya IoT Platform Configuration Guide](https://github.com/tuya/tuy

## Sample code

[OpenAPI Sample](https://github.com/tuya/tuya-iot-python-sdk/blob/master/example/api.py)
[OpenAPI Sample](https://github.com/tuya/tuya-iot-python-sdk/blob/master/example/device.py)

[Open IoT Hub Sample](https://github.com/tuya/tuya-iot-python-sdk/blob/master/example/mq.py)

Expand All @@ -55,4 +108,4 @@ You can provide feedback on your issue via **Github Issue** or [Technical Ticket

## License

tuya-iot-py-sdk is available under the MIT license. Please see the [LICENSE](./LICENSE) file for more info.
tuya-iot-py-sdk is available under the MIT license. Please see the [LICENSE](./LICENSE) file for more info.
4 changes: 3 additions & 1 deletion example/api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from tuya_iot import TuyaOpenAPI
import logging
from tuya_iot import TuyaOpenAPI, tuya_logger

from env import *

tuya_logger.setLevel(logging.DEBUG)
# Init
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY)
openapi.login(USERNAME, PASSWORD)
Expand Down
53 changes: 34 additions & 19 deletions example/device.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from example.env import *
import time
from tuya_iot import *

import logging
from env import ENDPOINT, ACCESS_ID, ACCESS_KEY, USERNAME, PASSWORD
from tuya_iot import (
TuyaOpenAPI,
ProjectType,
TuyaOpenMQ,
TuyaDeviceManager,
TuyaHomeManager,
TuyaDeviceListener,
TuyaDevice,
TuyaTokenInfo,
tuya_logger
)

tuya_logger.setLevel(logging.DEBUG)
# Init
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, ProjectType.INDUSTY_SOLUTIONS)

openapi.login(USERNAME, PASSWORD)
openmq = TuyaOpenMQ(openapi)
openmq.start()

print('device test-> ', openapi.tokenInfo.uid)
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.updateDeviceCache()
homeManager.update_device_cache()
# # deviceManager.updateDeviceCaches(devIds)
# device = deviceManager.deviceMap.get(DEVICE_ID)


class tuyaDeviceListener(TuyaDeviceListener):
def updateDevice(self, device:TuyaDevice):
print("_update-->", device)

def addDevice(self, device:TuyaDevice):
print("_add-->", device)

def removeDevice(self, id:str):
pass
deviceManager.addDeviceListener(tuyaDeviceListener())
def update_device(self, device: TuyaDevice):
print("_update-->", device)

def add_device(self, device: TuyaDevice):
print("_add-->", device)

def remove_device(self, device_id: str):
pass


deviceManager.add_device_listener(tuyaDeviceListener())

# Turn on the light
# deviceManager.sendCommands(device.id, [{'code': 'switch_led', 'value': True}])
Expand All @@ -55,6 +68,8 @@ def removeDevice(self, id:str):
input()
# flag = not flag
# commands = {'commands': [{'code': 'switch_led', 'value': flag}]}
response = openapi.post('/v1.0/iot-03/users/token/{}'.format(openapi.tokenInfo.refreshToken))
openapi.tokenInfo = TuyaTokenInfo(response)
response = openapi.post(
"/v1.0/iot-03/users/token/{}".format(openapi.token_info.refresh_token)
)
openapi.token_info = TuyaTokenInfo(response)
# openapi.post('/v1.0/iot-03/devices/{}/commands'.format(DEVICE_ID), commands)
10 changes: 7 additions & 3 deletions example/mq.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import logging
from tuya_iot import TuyaOpenAPI, TuyaOpenMQ, tuya_logger

from tuya_iot import TuyaOpenAPI, TuyaOpenMQ
from env import ENDPOINT, ACCESS_ID, ACCESS_KEY, USERNAME, PASSWORD

from env import *
tuya_logger.setLevel(logging.DEBUG)

# Init
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY)
openapi.login(USERNAME, PASSWORD)


# Receive device message
def on_message(msg):
print('on_message: %s'%msg)
print("on_message: %s" % msg)


openmq = TuyaOpenMQ(openapi)
openmq.start()
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

# from distutils.core import setup
from setuptools import setup, find_packages

VERSION = '0.2.3'
from tuya_iot import __version__

tests_require = []

Expand Down Expand Up @@ -45,7 +44,7 @@ def requirements():
'Programming Language :: Python :: Implementation :: PyPy'
],

version=VERSION,
version=__version__,
install_requires=requirements(),
tests_require=tests_require,
test_suite='runtests.runtests',
Expand Down
5 changes: 0 additions & 5 deletions test/__init__.py

This file was deleted.

23 changes: 13 additions & 10 deletions tuya_iot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from .openapi import TuyaOpenAPI, TuyaTokenInfo
from .openmq import TuyaOpenMQ
from .asset import TuyaAssetManager
from .device import TuyaDeviceManager, TuyaDevice, TuyaDeviceListener
from .project_type import ProjectType
from .home import TuyaHomeManager
from .logging import tuya_logger
from .version import VERSION

__all__ = [
'TuyaOpenAPI',
'TuyaTokenInfo',
'TuyaOpenMQ',
'TuyaAssetManager',
'TuyaDeviceManager',
'TuyaDevice',
'TuyaDeviceListener',
'ProjectType',
'TuyaHomeManager',
"TuyaOpenAPI",
"TuyaTokenInfo",
"TuyaOpenMQ",
"TuyaAssetManager",
"TuyaDeviceManager",
"TuyaDevice",
"TuyaDeviceListener",
"ProjectType",
"TuyaHomeManager",
"tuya_logger"
]
__version__ = VERSION
Loading

0 comments on commit b3243bf

Please sign in to comment.