Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize indentation for code snippets in the README. #184

Merged
merged 2 commits into from
Sep 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 108 additions & 111 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ TinyTuya can also connect to the Tuya Cloud to poll status and issue commands to
![TinyTuya Diagram](https://raw.githubusercontent.com/jasonacox/tinytuya/master/docs/TinyTuya-diagram.svg)

```python
# Example Usage of TinyTuya
import tinytuya
# Example Usage of TinyTuya
import tinytuya

d = tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
d.set_version(3.3)
data = d.status()
print('Device status: %r' % data)
d = tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
d.set_version(3.3)
data = d.status()
print('Device status: %r' % data)
```

NOTE: Devices need to be **activated** by Smart Life App.
Expand All @@ -36,8 +36,8 @@ NOTE: Devices need to be **activated** by Smart Life App.
TinyTuya supports python versions 2.7 and 3.x (recommended).

```bash
# Install TinyTuya
python -m pip install tinytuya
# Install TinyTuya
python -m pip install tinytuya
```

Pip will attempt to install `pycryptodome`, `requests` and `colorama` if not already installed.
Expand Down Expand Up @@ -108,28 +108,28 @@ Notes:
After importing tinytuya, you create a device handle for the device you want to read or control. Here is an example for a Tuya smart switch or plug:

```python
import tinytuya
import tinytuya

# Connect to Device - pytuya Method
d = tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
d.set_version(3.3)
# Connect to Device - pytuya Method
d = tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
d.set_version(3.3)

# And a Alternative Method for TinyTuya v1.7.0+
# d = tinytuya.OutletDevice(
# dev_id='DEVICE_ID_HERE',
# address='IP_ADDRESS_HERE',
# local_key='LOCAL_KEY_HERE',
# version=3.4)
# And a Alternative Method for TinyTuya v1.7.0+
# d = tinytuya.OutletDevice(
# dev_id='DEVICE_ID_HERE',
# address='IP_ADDRESS_HERE',
# local_key='LOCAL_KEY_HERE',
# version=3.4)

# Get Status
data = d.status()
print('set_status() result %r' % data)
# Get Status
data = d.status()
print('set_status() result %r' % data)

# Turn On
d.turn_on()
# Turn On
d.turn_on()

# Turn Off
d.turn_off()
# Turn Off
d.turn_off()
```

### TinyTuya Module Classes and Functions
Expand Down Expand Up @@ -246,68 +246,68 @@ The "Err" number will be one of these:
See the sample python script [test.py](test.py) for an OutletDevice example or look in the [examples](examples) directory for other scripts.

```python
import tinytuya

"""
OUTLET Device
"""
d = tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
d.set_version(3.3)
data = d.status()

# Show status and state of first controlled switch on device
print('Dictionary %r' % data)
print('State (bool, true is ON) %r' % data['dps']['1'])

# Toggle switch state
switch_state = data['dps']['1']
data = d.set_status(not switch_state) # This requires a valid key
if data:
print('set_status() result %r' % data)

# On a switch that has 4 controllable ports, turn the fourth OFF (1 is the first)
data = d.set_status(False, 4)
if data:
print('set_status() result %r' % data)
print('set_status() extra %r' % data[20:-8])

"""
RGB Bulb Device
"""
import time

d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
d.set_version(3.3) # IMPORTANT to set this regardless of version
d.set_socketPersistent(True) # Optional: Keep socket open for multiple commands
data = d.status()

# Show status of first controlled switch on device
print('Dictionary %r' % data)

# Set to RED Color - set_colour(r, g, b):
d.set_colour(255,0,0)

# Cycle through the Rainbow
rainbow = {"red": [255, 0, 0], "orange": [255, 127, 0], "yellow": [255, 200, 0],
"green": [0, 255, 0], "blue": [0, 0, 255], "indigo": [46, 43, 95],
"violet": [139, 0, 255]}
for color in rainbow:
[r, g, b] = rainbow[color]
d.set_colour(r, g, b, nowait=True) # nowait = Go fast don't wait for response
time.sleep(0.25)

# Brightness: Type A devices range = 25-255 and Type B = 10-1000
d.set_brightness(1000)

# Set to White - set_white(brightness, colourtemp):
# colourtemp: Type A devices range = 0-255 and Type B = 0-1000
d.set_white(1000,10)

# Set Bulb to Scene Mode
d.set_mode('scene')

# Scene Example: Set Color Rotation Scene
d.set_value(25, '07464602000003e803e800000000464602007803e803e80000000046460200f003e803e800000000464602003d03e803e80000000046460200ae03e803e800000000464602011303e803e800000000')
import tinytuya

"""
OUTLET Device
"""
d = tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
d.set_version(3.3)
data = d.status()

# Show status and state of first controlled switch on device
print('Dictionary %r' % data)
print('State (bool, true is ON) %r' % data['dps']['1'])

# Toggle switch state
switch_state = data['dps']['1']
data = d.set_status(not switch_state) # This requires a valid key
if data:
print('set_status() result %r' % data)

# On a switch that has 4 controllable ports, turn the fourth OFF (1 is the first)
data = d.set_status(False, 4)
if data:
print('set_status() result %r' % data)
print('set_status() extra %r' % data[20:-8])

"""
RGB Bulb Device
"""
import time

d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
d.set_version(3.3) # IMPORTANT to set this regardless of version
d.set_socketPersistent(True) # Optional: Keep socket open for multiple commands
data = d.status()

# Show status of first controlled switch on device
print('Dictionary %r' % data)

# Set to RED Color - set_colour(r, g, b):
d.set_colour(255,0,0)

# Cycle through the Rainbow
rainbow = {"red": [255, 0, 0], "orange": [255, 127, 0], "yellow": [255, 200, 0],
"green": [0, 255, 0], "blue": [0, 0, 255], "indigo": [46, 43, 95],
"violet": [139, 0, 255]}
for color in rainbow:
[r, g, b] = rainbow[color]
d.set_colour(r, g, b, nowait=True) # nowait = Go fast don't wait for response
time.sleep(0.25)

# Brightness: Type A devices range = 25-255 and Type B = 10-1000
d.set_brightness(1000)

# Set to White - set_white(brightness, colourtemp):
# colourtemp: Type A devices range = 0-255 and Type B = 0-1000
d.set_white(1000,10)

# Set Bulb to Scene Mode
d.set_mode('scene')

# Scene Example: Set Color Rotation Scene
d.set_value(25, '07464602000003e803e800000000464602007803e803e80000000046460200f003e803e800000000464602003d03e803e80000000046460200ae03e803e800000000464602011303e803e800000000')

```
### Example Device Monitor
Expand Down Expand Up @@ -379,13 +379,10 @@ print("Status of device:\n", result)

# Send Command - Turn on switch
commands = {
'commands': [{
'code': 'switch_1',
'value': True
}, {
'code': 'countdown_1',
'value': 0
}]
"commands": [
{"code": "switch_1", "value": True},
{"code": "countdown_1", "value": 0},
]
}
print("Sending command...")
result = c.sendcommand(id,commands)
Expand All @@ -403,17 +400,17 @@ These devices uses AES encryption which is not available in the Python standard
### Command Line

```
python -m tinytuya [command] [<max_retry>] [-nocolor] [-h]

command = scan Scan local network for Tuya devices.
command = wizard Launch Setup Wizard to get Tuya Local KEYs.
command = devices Scan all devices listed in devices.json file.
command = snapshot Scan devices listed in snapshot.json file.
command = json Scan devices listed in snapshot.json file [JSON].
max_retry Maximum number of retries to find Tuya devices [Default=15]
-nocolor Disable color text output.
-force Force network scan for device IP addresses.
-h Show usage.
python -m tinytuya [command] [<max_retry>] [-nocolor] [-h]

command = scan Scan local network for Tuya devices.
command = wizard Launch Setup Wizard to get Tuya Local KEYs.
command = devices Scan all devices listed in devices.json file.
command = snapshot Scan devices listed in snapshot.json file.
command = json Scan devices listed in snapshot.json file [JSON].
max_retry Maximum number of retries to find Tuya devices [Default=15]
-nocolor Disable color text output.
-force Force network scan for device IP addresses.
-h Show usage.
```

### Scan Tool
Expand Down Expand Up @@ -459,11 +456,11 @@ By default, the scan functions will retry 15 times to find new devices. If you a
* Devices running protocol version 3.1 (e.g. below Firmware 1.0.5) do not require a device *Local_Key* to read the status. Both 3.1 and 3.3 devices will require a device *Local_Key* to control the device.
* Some devices with 22 character IDs will require additional setting to poll correctly - here is an example:
```python
a = tinytuya.OutletDevice('here_is_my_key', '192.168.x.x', 'secret_key_here', 'device22')
a.set_version(3.3)
a.set_dpsUsed({"1": None}) # This needs to be a datapoint available on the device
data = a.status()
print(data)
a = tinytuya.OutletDevice('here_is_my_key', '192.168.x.x', 'secret_key_here', 'device22')
a.set_version(3.3)
a.set_dpsUsed({"1": None}) # This needs to be a datapoint available on the device
data = a.status()
print(data)
```
* Windows 10 Users - TinyTuya `wizard` and `scan` interactive tools use ANSI color. This will work correctly in PowerShell but will show cryptic escape codes when run in Windows `CMD`. You can fix this by using the `-nocolor` option on tinytuya, or by changing the Windows `CMD` console registry to process ANSI escape codes by doing something like this:
```
Expand Down Expand Up @@ -629,7 +626,7 @@ Note: Some 3.3 energy management plugs use the DPS values of the 3.1 plug above.
"head": "",
"key1": "[[TO_BE_REPLACED]]",
"type": 0,
"delay": 300
"delay": 300,
}
# Sending the IR command:
payload = d.generate_payload(tinytuya.CONTROL, {"201": json.dumps(command)})
Expand Down