gared / shelly-php-client
访问 Shelly 设备 HTTP API 的客户端
2.1.0
2024-09-05 19:07 UTC
Requires
- php: >=8.1
- ext-json: *
- guzzlehttp/guzzle: ^7.2
- phpdocumentor/reflection-docblock: ^5.2
- symfony/property-access: ^5.2|^6.0|^7.0
- symfony/property-info: ^5.2|^6.0|^7.0
- symfony/serializer: ^5.2|^6.0|^7.0
Requires (Dev)
- phpstan/phpstan: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^10.5.0
This package is auto-updated.
Last update: 2024-09-05 19:12:04 UTC
README
您可以使用这个库来访问 Shelly 设备的 HTTP API。您可以在这里找到官方 API 文档。
安装
使用 composer
composer require gared/shelly-php-client
入门
配置客户端
$client = new \ShellyClient\HTTP\Client('http://192.168.1.10');
如果您已经配置了登录凭证,您必须像这样创建客户端
$client = new \ShellyClient\HTTP\Client('http://shellyuser:secret@192.168.1.10');
获取当前电力使用情况
$meter = $client->getMeter(new \ShellyClient\Model\Request\MeterRequest()); $power = $meter->getPower(); echo "Current power usage: " . $power;
开启 Shelly 插座
$client->getRelay(0, \ShellyClient\Model\Request\RelayRequest::TURN_ON);
完整示例
// Create new client for one device $clientLightA = new \ShellyClient\HTTP\Client('http://192.168.1.10', 'ShellyDeviceLightA'); $clientLightB = new \ShellyClient\HTTP\Client('http://192.168.1.20', 'ShellyDeviceLightB'); // Switch device on and get relay data $relayRequest = new \ShellyClient\Model\Request\RelayRequest(); $relayRequest->setRelayIndex(0); $relayRequest->setTurn(\ShellyClient\Model\Request\RelayRequest::TURN_ON); $relay = $clientLightA->getRelay($relayRequest); // check if timer has been set if ($relay->hasTimer()) { // do something } $meter = $clientLightA->getMeter(new \ShellyClient\Model\Request\MeterRequest()); // Get total power consumption in kW/h $kilowattHours = $meter->getTotalInKilowattHours(); // Get last three watt per minute power consumption values $counters = $meter->getCounters(); // Parallel call informations from device B $power = $clientLightB->getMeter(new \ShellyClient\Model\Request\MeterRequest())->getPower(); // Get device name set above in construct $deviceNameDeviceB = $clientLightB->getDeviceName(); // or use device name set in shelly device $deviceNameShelly = $clientLightB->getSettings(new \ShellyClient\Model\Request\SettingsRequest())->getName();
支持的平台
- 您至少需要 PHP 8.1