Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elmodo7/tuya4ahk
An AHK class for wrapping Tuya OpenAPI calls for IoT device automation over Wi-Fi.
https://github.com/elmodo7/tuya4ahk
ahk autohotkey iot openapi tuya tuya-api tuya-cloud
Last synced: 6 days ago
JSON representation
An AHK class for wrapping Tuya OpenAPI calls for IoT device automation over Wi-Fi.
- Host: GitHub
- URL: https://github.com/elmodo7/tuya4ahk
- Owner: elModo7
- License: mit
- Created: 2024-02-15T13:39:41.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-02-15T17:22:39.000Z (10 months ago)
- Last Synced: 2024-02-16T16:53:38.950Z (10 months ago)
- Topics: ahk, autohotkey, iot, openapi, tuya, tuya-api, tuya-cloud
- Language: AutoHotkey
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Tuya IoT Cloud OpenAPI wrapper for AutoHotkey
I had some wifi bulbs that I was given like 5 years ago or so and I wanted to automate them with **[AutoHotkey](https://www.autohotkey.com/)**.
I noticed they were using **port 6668**, so chances were they could use **[Tuya Cloud API](https://developer.tuya.com/en/docs/cloud)** and since there was no ahk library for dealing with them I made my own.Now I've read Tuya is known for changing their API a lot and __making it hard on purpose__ for users to deal directly with their IoT devices (which was my idea in the first place). However, ***this is working as of 15/Feb/2024.***
## Connection Flow:
```mermaid
graph LR
A[AutoHotkey
Build cmd] -.- B((Bulb))
A --> C1{Sign Request}
C1 --> C(HTTP Query)
D[(Tuya Cloud)] --> B
C --> DclassDef ahk fill:#586,stroke:#333;
classDef bulb fill:#990,stroke:#333;
classDef sign fill:#888,stroke:#333;
classDef tuya fill:#F50,stroke:#333;
class A ahk
class C1 sign
class B bulb
class D tuya
```## Class Usage
### Initializing the class and obtaining a *Token*
```AutoHotkey
tuyaApi := new TuyaApi()
tuyaApi.cfg.debug := 1 ; Get notified of errors (default off)
tuyaApi.getToken()
```
> **Quick Note:** getToken is not always necessary since this lib is able to detect invalid or deprecated tokens and **recreate a valid token** on its own when **performing an action**.#### Performing actions on the devices
```AutoHotkey
; Turn on light
tuyaApi.toggleOn(tuyaApi.devices[1].id, "true") ; Returns status 200 if successful; Turn off light
tuyaApi.toggleOn(tuyaApi.devices[1].id, "false"); Turns on light, sets luminosity to 1000 (max [10-1000]) and warmness to 0 (warmest [0-1000])
tuyaApi.setNormalLightStatus(tuyaApi.devices[1].id, "true", "1000", "0"); Turns on light, sets HSV values to: h:240 -> blue, saturation -> 1000, value -> 1000
tuyaApi.setHSVLightStatus(tuyaApi.devices[1].id, "true", "240", "1000", "1000")
```### Getting device information
```AutoHotkey
bulb1_info := tuyaApi.getDeviceInfo(tuyaApi.devices[1].id) ; Get info of a specific device
devices_info := tuyaApi.getDevicesInfo(tuyaApi.devices) ; Get info of each registered device
bulb1_stats := tuyaApi.getDeviceStats(tuyaApi.devices[1].id) ; Get stats of a scpecific device
devices_stats := tuyaApi.getDevicesStats(tuyaApi.devices) ; Get stats of each registered device
```## Configuration Example
### common_data.json
```JSON
{
"debug": false,
"url": "https://openapi.tuyaeu.com",
"client_id": "9647axf8vtyuqtwugjnd",
"secret": "aa75b183b4v757aba8db85u8945c7acf",
"project_code": "p1807878438912hout6r"
}
```### devices.json
```JSON
[
{
"type": "bulb",
"name": "Smart Bulb",
"id": "07468567f4cfa678542d",
"product_id": "1urlnfjaiklk4dly"
},
{
"type": "bulb",
"name": "Smart Bulb 2",
"id": "03740273f4cfa2289311",
"product_id": "irsteipfgt0kt7rco"
}
]
```## Detailed connection flow:
```mermaid
sequenceDiagram
AutoHotkey ->> Tuya API: GetToken()
Tuya API-->>AutoHotkey: TokenData
AutoHotkey ->> Tuya API: RunAction(token, body)
Tuya API->> Bulb: Turn On at 50%
Bulb ->> Tuya API: ACK
Tuya API->> AutoHotkey: Command completed successfully
Note right of Bulb: Bulbs have a reverse
connection towards
Tuya IoT services so
we need Tuya as a
Middleman to establish
a connection.
```## Libraries used
- [Cryptography API: Next Generation (CNG)](https://github.com/jNizM/AHK_CNG)
- [cJSON](https://github.com/G33kDude/cJson.ahk)## Notes
I have implemented but not yet used **refresh tokens**, feel free to improve this library by doing a pull request.I **do not have nor automate IoT stuff myself** nor I tend to create AutoHotkey libs for others (I'm more and more leaning towards doing so though lately because I'm planning to **make Udemy Courses or video content** on AutoHotkey development), so any suggestion is greatly appreciated.
This project was written with AutoHotkey v1 in mind, if you want to port it to AHK v2 or want some advise on it, you can find me on Discord.