Skip to content

Commit

Permalink
add actions for scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aymkdn committed Sep 28, 2024
1 parent a65da17 commit 3280331
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ $options = [
'secretKey' => 'sf94ryyrfvg3awvg4174m88wjpksytre', // access secret of your app
];

$client = new TuyaCloud($options);
$tuya = new TuyaCloud($options);
try {
// to get the device status
// you must pass the device_id
$response = $client->getDevice('bfa18afnfyre87eb7ne0');
$response = $tuya->getDevice('bfa18afnfyre87eb7ne0');
echo '<pre>';
print_r($response);
echo '</pre>';
Expand All @@ -52,9 +52,25 @@ try {
]
]
];
$response = $client->setDevice('bfa18afnfyre87eb7ne0', $commands);
$response = $tuya->setDevice('bfa18afnfyre87eb7ne0', $commands);

// we can retrieve all the scenes (including their id)
$response = $tuya->getScenes();
echo '<pre>';
print_r($response);
echo '</pre>';

// and we can start a scene
$response = $tuya->startScene('the_scene_id');
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
```

The different commands can be foundby going to your project in the [Tuya Cloud Development platform](https://eu.platform.tuya.com/cloud/), then click on the device and you can find the options.

Examples:
- a curtain can have the `command` `open`, `close`, or `stop`
- a light will have `switch_led` with `true` or `false`
- a power strip with 2 outlets with have `switch_1` with `true` or `false`, and the same for `switch_2`
40 changes: 38 additions & 2 deletions TuyaCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ private function sendRequest($path, $method, $data = "{}") {

/**
* Get the device status/properties (`switch_led`, `bright_value`, `fan_switch`, …)
* Documentation: https://developer.tuya.com/en/docs/cloud/1ef1a3044b
*
* @param {String} $deviceId The device id
* @return {Object} {success:(boolean), result:[{code, value}]}
Expand All @@ -141,7 +140,6 @@ public function getDevice($deviceId) {

/**
* Set the device properties (`switch_led`, `bright_value`, `fan_switch`, …)
* Documentation: https://developer.tuya.com/en/docs/cloud/e2512fb901
*
* @param {String} $deviceId The device id
* @param {String|Object|Array} $commands An array of commands (e.g. `[{"code":"switch_led", "value":false}, {"code":"fan_switch", "value":true}`)
Expand All @@ -151,5 +149,43 @@ public function setDevice($deviceId, $commands) {
if (func_num_args() != 2) throw "[tuyacloud] You have to pass the `device_id` and the `commands` as arguments to this function 'setDevice'.";
return $this->sendRequest('/v1.0/iot-03/devices/' . $deviceId. '/commands', 'POST', $commands);
}

/**
* Return a list of scenes for the user
*
* @return {Array} An array of {id, name, running_mode, space_id, status, type}
*/
public function getScenes() {
// retrieve the spaces
// https://developer.tuya.com/en/docs/cloud/75a240f09b?id=Kcp2kv5bcvne7
$spaces = $this->sendRequest('/v2.0/cloud/space/child', 'GET');
if ($spaces['success'] != 1) throw "[tuyacloud] An error occured with space/child: ".$spaces['error_msg'];
// for each space, retrieve the related scenes
$spaces = $spaces['result']['data'];
$ret = [];
foreach($spaces as $spaceId) {
// if we need to find the space name:
// $spaceDetails = $this->sendRequest('/v2.0/cloud/space/'.$spaceId, 'GET');
// if ($spaceDetails['success'] != 1) throw "[tuyacloud] An error occured with space/".$spaceId.": ".$spaceDetails['error_msg'];
// $spaceName = $spaceDetails['result']['name'];
$scenesDetails = $this->sendRequest('/v2.0/cloud/scene/rule?space_id='.$spaceId, 'GET');
if ($scenesDetails['success'] != 1) throw "[tuyacloud] An error occured with scene/rule?space_id=".$spaceId.": ".$scenesDetails['error_msg'];
foreach($scenesDetails['result']['list'] as $scenes) {
array_push($ret, $scenes);
}
}
return $ret;
}

/**
* To start a scene
*
* @param {String} $sceneId The scene id
* @return {Object} The result of the action
*/
public function startScene($sceneId) {
if (!isset($sceneId)) throw "[tuyacloud] You have to pass the `scene_id` as an argument to this function 'startScene'.";
return $this->sendRequest('/v2.0/cloud/scene/rule/'.$sceneId.'/actions/trigger', 'POST');
}
}
?>

0 comments on commit 3280331

Please sign in to comment.