Skip to content

Commit 3280331

Browse files
committed
add actions for scenes
1 parent a65da17 commit 3280331

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ $options = [
3131
'secretKey' => 'sf94ryyrfvg3awvg4174m88wjpksytre', // access secret of your app
3232
];
3333

34-
$client = new TuyaCloud($options);
34+
$tuya = new TuyaCloud($options);
3535
try {
3636
// to get the device status
3737
// you must pass the device_id
38-
$response = $client->getDevice('bfa18afnfyre87eb7ne0');
38+
$response = $tuya->getDevice('bfa18afnfyre87eb7ne0');
3939
echo '<pre>';
4040
print_r($response);
4141
echo '</pre>';
@@ -52,9 +52,25 @@ try {
5252
]
5353
]
5454
];
55-
$response = $client->setDevice('bfa18afnfyre87eb7ne0', $commands);
55+
$response = $tuya->setDevice('bfa18afnfyre87eb7ne0', $commands);
56+
57+
// we can retrieve all the scenes (including their id)
58+
$response = $tuya->getScenes();
59+
echo '<pre>';
60+
print_r($response);
61+
echo '</pre>';
62+
63+
// and we can start a scene
64+
$response = $tuya->startScene('the_scene_id');
5665
} catch (Exception $e) {
5766
echo 'Error: ' . $e->getMessage();
5867
}
5968
?>
6069
```
70+
71+
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.
72+
73+
Examples:
74+
- a curtain can have the `command` `open`, `close`, or `stop`
75+
- a light will have `switch_led` with `true` or `false`
76+
- a power strip with 2 outlets with have `switch_1` with `true` or `false`, and the same for `switch_2`

TuyaCloud.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ private function sendRequest($path, $method, $data = "{}") {
129129

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

142141
/**
143142
* Set the device properties (`switch_led`, `bright_value`, `fan_switch`, …)
144-
* Documentation: https://developer.tuya.com/en/docs/cloud/e2512fb901
145143
*
146144
* @param {String} $deviceId The device id
147145
* @param {String|Object|Array} $commands An array of commands (e.g. `[{"code":"switch_led", "value":false}, {"code":"fan_switch", "value":true}`)
@@ -151,5 +149,43 @@ public function setDevice($deviceId, $commands) {
151149
if (func_num_args() != 2) throw "[tuyacloud] You have to pass the `device_id` and the `commands` as arguments to this function 'setDevice'.";
152150
return $this->sendRequest('/v1.0/iot-03/devices/' . $deviceId. '/commands', 'POST', $commands);
153151
}
152+
153+
/**
154+
* Return a list of scenes for the user
155+
*
156+
* @return {Array} An array of {id, name, running_mode, space_id, status, type}
157+
*/
158+
public function getScenes() {
159+
// retrieve the spaces
160+
// https://developer.tuya.com/en/docs/cloud/75a240f09b?id=Kcp2kv5bcvne7
161+
$spaces = $this->sendRequest('/v2.0/cloud/space/child', 'GET');
162+
if ($spaces['success'] != 1) throw "[tuyacloud] An error occured with space/child: ".$spaces['error_msg'];
163+
// for each space, retrieve the related scenes
164+
$spaces = $spaces['result']['data'];
165+
$ret = [];
166+
foreach($spaces as $spaceId) {
167+
// if we need to find the space name:
168+
// $spaceDetails = $this->sendRequest('/v2.0/cloud/space/'.$spaceId, 'GET');
169+
// if ($spaceDetails['success'] != 1) throw "[tuyacloud] An error occured with space/".$spaceId.": ".$spaceDetails['error_msg'];
170+
// $spaceName = $spaceDetails['result']['name'];
171+
$scenesDetails = $this->sendRequest('/v2.0/cloud/scene/rule?space_id='.$spaceId, 'GET');
172+
if ($scenesDetails['success'] != 1) throw "[tuyacloud] An error occured with scene/rule?space_id=".$spaceId.": ".$scenesDetails['error_msg'];
173+
foreach($scenesDetails['result']['list'] as $scenes) {
174+
array_push($ret, $scenes);
175+
}
176+
}
177+
return $ret;
178+
}
179+
180+
/**
181+
* To start a scene
182+
*
183+
* @param {String} $sceneId The scene id
184+
* @return {Object} The result of the action
185+
*/
186+
public function startScene($sceneId) {
187+
if (!isset($sceneId)) throw "[tuyacloud] You have to pass the `scene_id` as an argument to this function 'startScene'.";
188+
return $this->sendRequest('/v2.0/cloud/scene/rule/'.$sceneId.'/actions/trigger', 'POST');
189+
}
154190
}
155191
?>

0 commit comments

Comments
 (0)