PHP code example of christianjbrown / smartthings-api-sdk

1. Go to this page and download the library: Download christianjbrown/smartthings-api-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

christianjbrown / smartthings-api-sdk example snippets


use ChristianBrown\SmartThings\SmartThings;

$smartThings     = new SmartThings('your-smartthings-personal-access-token');
$deviceApi       = $smartThings->getDeviceApi();  // DeviceApiInterface
$deviceStatusApi = $smartThings->getDeviceStatusApi();  // DeviceStatusApiInterface
$deviceHealthApi = $smartThings->getDeviceHealthApi();  // DeviceHealthApiInterface
$deviceHistoryApi = $smartThings->getDeviceHistoryApi();  // DeviceHistoryApiInterface
$locationApi     = $smartThings->getLocationApi();  // LocationApiInterface
$locationModeApi = $smartThings->getLocationModeApi();  // LocationModeApiInterface
$locationRoomApi = $smartThings->getLocationRoomApi();  // LocationRoomApiInterface
$sceneApi        = $smartThings->getSceneApi();  // SceneApiInterface
$ruleApi         = $smartThings->getRuleApi();  // RuleApiInterface
$capabilityApi   = $smartThings->getCapabilityApi();  // CapabilityApiInterface
$deviceProfileApi = $smartThings->getDeviceProfileApi();  // DeviceProfileApiInterface
$presentationApi = $smartThings->getPresentationApi();  // PresentationApiInterface
$appApi          = $smartThings->getAppApi();  // AppApiInterface
$installedAppApi = $smartThings->getInstalledAppApi();  // InstalledAppApiInterface
$subscriptionApi = $smartThings->getSubscriptionApi();  // SubscriptionApiInterface
$scheduleApi     = $smartThings->getScheduleApi();  // ScheduleApiInterface

$devices = $deviceApi->getMultiple();                 // DeviceInterface[]
foreach ($devices as $device) {
    echo $device->getLabel() ?? $device->getName(), "\n";

    // The room the device is assigned to (skip devices with no room).
    // LocationRoomApi caches rooms by roomId, so devices sharing a room only hit the API once.
    if ($device->getRoomId() !== null) {
        $room = $locationRoomApi->getOneByDevice($device);   // LocationRoomInterface
        printf("  Room: %s\n", $room->getName());         // e.g. "Kitchen"
    }

    $status = $deviceStatusApi->getOneByDevice($device);     // DeviceStatusInterface

    $temp = $status->getTemperatureMeasurement()?->getTemperature();
    if ($temp !== null) {
        printf("  Temperature: %.1f°%s\n", $temp->getValue(), $temp->getUnit());
    }

    $humidity = $status->getRelativeHumidityMeasurement()?->getHumidity();
    if ($humidity !== null) {
        printf("  Humidity: %d%s\n", $humidity->getValue(), $humidity->getUnit());
    }

    $battery = $status->getBattery()?->getBattery();
    if ($battery !== null) {
        printf("  Battery: %d%s\n", $battery->getValue(), $battery->getUnit());
    }

    $health = $deviceHealthApi->getOneByDevice($device);     // DeviceHealthInterface
    printf("  Health: %s\n", $health->getState() ?? 'unknown');   // e.g. "ONLINE"
}

$device = $deviceApi->getOneById('a-device-id');           // DeviceInterface
echo $device->getLabel() ?? $device->getName(), "\n";

$status = $deviceStatusApi->getOneById('a-device-id');     // DeviceStatusInterface

$locations = $locationApi->getMultiple();                       // LocationInterface[]
foreach ($locations as $location) {
    echo $location->getName(), "\n";
}

$location = $locationApi->getOneById('a-location-id');      // LocationInterface
echo $location->getName(), "\n";

$rooms = $locationRoomApi->getMultiple($locations[0]);          // LocationRoomInterface[]
foreach ($rooms as $room) {
    echo $room->getName(), "\n";
}

$room = $locationRoomApi->getOneByLocationAndId($locations[0], 'a-room-id'); // LocationRoomInterface
echo $room->getName(), "\n";

use ChristianBrown\SmartThings\Model\DeviceCommand;

// Run one or more capability commands on a device.
$commandResults = $deviceApi->executeCommands('a-device-id', [
    new DeviceCommand('switch', 'on'),
    (new DeviceCommand('switchLevel', 'setLevel'))->setArguments([80]),
]); // DeviceCommandResultInterface[]
foreach ($commandResults as $commandResult) {
    echo $commandResult->getStatus() ?? 'unknown', "\n"; // e.g. "ACCEPTED"
}

// Switch a location's currently active mode.
$mode = $locationModeApi->changeCurrent($locations[0], 'a-mode-id'); // ModeInterface
echo $mode->getLabel(), "\n";

// Execute a scene.
$sceneResult = $sceneApi->execute('a-scene-id'); // SceneExecutionResultInterface
echo $sceneResult->getStatus() ?? 'unknown', "\n"; // e.g. "success"

// Trigger a rule.
$ruleResult = $ruleApi->execute('a-rule-id'); // RuleExecutionResultInterface
echo $ruleResult->getResult() ?? 'unknown', "\n"; // e.g. "Success"
foreach ($ruleResult->getActions() as $action) { // ActionExecutionResultInterface[]
    echo $action->getActionId(), ' ', $action->getIf()?->getResult() ?? '', "\n";
}

$report = $deviceStatusApi->getReportById('a-device-id');
$switch = $report->getComponents()['main']->getCapabilities()['switch']->getAttributes()['switch'];
echo $switch->getValue()['value'] ?? '', "\n";

use ChristianBrown\ApiClient\Exception\Response\ResponseExceptionInterface;

try {
    $deviceApi->getOneById('a-device-id');
} catch (ResponseExceptionInterface $exception) {
    $error = $smartThings->getErrorResponseTransformer()->transform($exception->getDecodedBody() ?? []);
    echo $error->getError()?->getCode(), ' ', $error->getRequestId(), "\n";
}

use ChristianBrown\SmartThings\Model\DeviceListQuery;

$query = (new DeviceListQuery())
    ->setCapabilities(['switch', 'switchLevel'])
    ->setCapabilitiesMode('or')
    ->setIncludeStatus(true);
$devices = $deviceApi->getMultiple(null, false, $query);

$device = $deviceApi->getOneById('a-device-id', false, true); // 

use ChristianBrown\SmartThings\Exception\ExceptionInterface;

try {
    $devices = $deviceApi->getMultiple();
} catch (ExceptionInterface $exception) {
    // Anything this library throws lands here.
}

use ChristianBrown\SmartThings\Api\ApiHost;
use ChristianBrown\SmartThings\SmartThings;

$smartThings = new SmartThings(
    'your-smartthings-personal-access-token',
    new ApiHost('https://staging.example.com')
);

use ChristianBrown\ApiClient\ApiClient;
use ChristianBrown\SmartThings\Api\SceneApi;
use ChristianBrown\SmartThings\Api\Token;
use ChristianBrown\SmartThings\Transformer\SceneExecutionResultTransformer;
use ChristianBrown\SmartThings\Transformer\ScenesTransformer;
use ChristianBrown\SmartThings\Transformer\SceneTransformer;

$sceneTransformer = new SceneTransformer();

$sceneApi = new SceneApi(
    (new ApiClient())->getJsonApiRequestSender(),
    $sceneTransformer,
    new ScenesTransformer($sceneTransformer),
    new Token($apiToken),
    new SceneExecutionResultTransformer()
);