revolution/laravel-switchbot

Laravel 的 SwitchBot API

1.2.0 2024-02-20 04:53 UTC

This package is auto-updated.

Last update: 2024-08-29 09:04:46 UTC


README

tests Maintainability Test Coverage

https://github.com/OpenWonderLabs/SwitchBotAPI

要求

  • PHP >= 8.1
  • Laravel >= 10.0

版本控制

  • 基本:semver
  • 丢弃旧的 PHP 或 Laravel 版本:+0.1
  • 仅支持最新主要版本(main 分支)

安装

composer require revolution/laravel-switchbot

配置

从 SwitchBot 应用获取令牌。

.env

SWITCHBOT_TOKEN=
SWITCHBOT_SECRET=

使用

作为 Laravel HTTP 客户端宏构建。

use Illuminate\Support\Facades\Http;

$response = Http::switchbot()->get('devices');

dump($response->json());

$deviceId = $response->json('body.deviceList.0.deviceId');
if (filled($deviceId)) {
    $response = Http::switchbot()->get("devices/$deviceId/status");
    dump($response->json());
}
use Illuminate\Support\Facades\Http;

$response = Http::switchbot()->get('scenes');
dump($response->json());

测试

use Illuminate\Support\Facades\Http;

Http::fake([
    '*' => Http::response([
        "statusCode" => 100,
        "body" => [
            "deviceList" => [],
            "infraredRemoteList" => [],
        ],
        "message" => "success",
   ]),
]);

$response = Http::switchbot()->get('devices');

$this->assertSame(100, $response->json('statusCode'));

许可证

MIT