Skip to content

Commit

Permalink
Use Tuya Cloud API
Browse files Browse the repository at this point in the history
Switch from the old HomeAssistant API to the new Tuya Cloud API
  • Loading branch information
Aymkdn committed Sep 23, 2024
1 parent 4540e8f commit a65da17
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 230 deletions.
95 changes: 46 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,60 @@
# tuyacloud-php

PHP Library to access to the SmartLife / Tuya objects using the Cloud of Home Assistant API.

It's based on https://github.com/unparagoned/cloudtuya and https://github.com/tuya/tuya-smart-life
PHP Library to access to the SmartLife / Tuya objects using the Tuya Cloud API.

Note: you need a Smartlife Account created with an email, not with Google Connection or similar third party. You could also create a new account from email and then invite this account to the "family" of your main Smartlife Account.

## Usage
## Required

A developer account must be created on the [Tuya platform](https://eu.platform.tuya.com/). I'm not sure how the details, but from the menu, access to [`Cloud` > `Development`](https://eu.platform.tuya.com/cloud/) and create a cloud project.

Once the project is created, it will show you the **access key** and **access secret**.

Under the tab `devices`, you can tie the project with your Tuya/SmartLife app by scanning a QR Code. If it worked, you will see all your devices listed on this page (with their `device_id`).

See [TuyaCloud.php](https://github.com/Aymkdn/tuyacloud-php/blob/master/TuyaCloud.php) for more details.
You need to know which region/server you're using, and then you have to use the correct URL:
- China Data Center: https://openapi.tuyacn.com
- Western America Data Center: https://openapi.tuyaus.com
- Central Europe Data Center: https://openapi.tuyaeu.com
- India Data Center: https://openapi.tuyain.com


## Usage

```php
<?php
require 'TuyaCloud.php';

// ATTENTION: it seems you have to keep "33" for the countryCode and "eu" for the region, wherever you are…
// -> see this comment: https://github.com/Aymkdn/tuyacloud-php/issues/9#issuecomment-1144097955
$tuya = new TuyaCloud([
"userName" => "[email protected]", // username/email to access to SmartLife/Tuya app
"password" => "MyPassword", // password to access to SmartLife/Tuya app
"bizType" => "smart_life", // type ('tuya' or 'smart_life')
"countryCode" => "33", // Country code (International dialing number), e.g. "33" for France or "1" for USA
"region" => "eu" // region (az=Americas, ay=Asia, eu=Europe)
]);

// to get a list of your devices
$devices = $tuya->getDevices();
foreach($devices as $device) {
echo "Name: ".$device->name."<br>";
echo "ID: ".$device->id."<br>";
echo "Type: ".$device->dev_type."<br>";
if ($device->dev_type !== "scene") {
echo "State: ".$device->data->state."<br>";
echo "Online: ".$device->data->online."<br>";
}
$options = [
'baseUrl' => 'https://openapi.tuyaeu.com', // URL API of Tuya
'accessKey' => 'nhepe4mrrtz8wju45mk3', // access key of your app
'secretKey' => 'sf94ryyrfvg3awvg4174m88wjpksytre', // access secret of your app
];

$client = new TuyaCloud($options);
try {
// to get the device status
// you must pass the device_id
$response = $client->getDevice('bfa18afnfyre87eb7ne0');
echo '<pre>';
print_r($response);
echo '</pre>';

// to send a command
// you can pass a JSON string: '{"commands":[{"code":"switch_led","value":true}]}'
// or a strClass object
// or an array like the below one:
$commands = [
"commands" => [
[
"code" => "switch_led",
"value" => true
]
]
];
$response = $client->setDevice('bfa18afnfyre87eb7ne0', $commands);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}

// to switch on a device called "switch 1"
$tuya->setState([
"name" => "switch 1",
"value" => "on"
]);

// to switch off a device with ID "123456"
$tuya->setState([
"id" => "123456",
"value" => "off"
]);

// to stop a cover
$tuya->setState([
"name" => "cover living room",
"command" => "startStop",
"value" => 0
]);

// to get the state of a device
echo "State Switch 1 => ".$tuya->getState([
"name" => "switch 1"
]);
?>
```
Loading

0 comments on commit a65da17

Please sign in to comment.