indexzer0/ha-rest-api-client
Composer 安装命令:
composer require indexzer0/ha-rest-api-client
包简介
Simple client for accessing home assistant rest api and webhooks.
关键字:
README 文档
README
Clients for accessing HomeAssistant Rest API & Webhooks.
Requirements
- PHP Version >= 8.2
- A PSR-18 compatible http client.
- A PSR-17 compatible factory.
Installation
You can install the package via composer:
composer require indexzer0/ha-rest-api-client
This library does not have a dependency on Guzzle or any other library that sends HTTP requests. This packages uses the awesome HTTPlug to achieve the decoupling. We want you to choose what library to use for sending HTTP requests. Consult this list of packages that support php-http/client-implementation to find clients to use. For more information about virtual packages please refer to HTTPlug.
Install your choice of http client.
Example:
composer require guzzlehttp/guzzle composer require guzzlehttp/psr7
Prerequisites
- Check out the Home Assistant Rest Api docs.
- Add the API integration to your
configuration.yaml. - Create a
Long-Lived Access Tokenin your profile.
- Add the API integration to your
Usage
This package provides two clients. HaRestApiClient and HaWebhookClient.
Basic (Http Client Auto Discovery)
/* * --------------------------- * HaRestApiClient * --------------------------- */ $restApiClient = new \IndexZer0\HaRestApiClient\HaRestApiClient( 'token', 'http://localhost:8123/api/' ); $restApiClient->status(); // ['message' => 'API running.'] /* * --------------------------- * HaWebhookClient * --------------------------- */ $webhookClient = new \IndexZer0\HaRestApiClient\HaWebhookClient( 'http://localhost:8123/api/' ); $webhookClient->send('GET', 'webhook_id'); // ['response' => '']
Custom Http Client (optional)
The client needs to know what library you are using to send HTTP messages. You could provide an instance of a PSR-18 compatible http client and PSR-17 compatible factory, or you could fallback on auto discovery (basic example above). Below is an example on where you provide a Guzzle7 instance.
/* * --------------------------- * HaRestApiClient * --------------------------- */ $restApiClient = new \IndexZer0\HaRestApiClient\HaRestApiClient( 'token', 'http://localhost:8123/api/', new \IndexZer0\HaRestApiClient\HttpClient\Builder( new \GuzzleHttp\Client(), new \GuzzleHttp\Psr7\HttpFactory(), new \GuzzleHttp\Psr7\HttpFactory(), new \GuzzleHttp\Psr7\HttpFactory(), ) ); $restApiClient->status(); // ['message' => 'API running.'] /* * --------------------------- * HaWebhookClient * --------------------------- */ $webhookClient = new \IndexZer0\HaRestApiClient\HaWebhookClient( 'http://localhost:8123/api/', new \IndexZer0\HaRestApiClient\HttpClient\Builder( new \GuzzleHttp\Client(), new \GuzzleHttp\Psr7\HttpFactory(), new \GuzzleHttp\Psr7\HttpFactory(), new \GuzzleHttp\Psr7\HttpFactory(), ) ); $webhookClient->send('GET', 'webhook_id'); // ['response' => '']
HaRestApiClient - Available Methods
$restApiClient = new \IndexZer0\HaRestApiClient\HaRestApiClient( 'token', 'http://localhost:8123/api/' ); $restApiClient->status(); $restApiClient->config(); $restApiClient->events(); $restApiClient->services(); $restApiClient->history(['light.bedroom_ceiling']); $restApiClient->logbook(); $restApiClient->states(); $restApiClient->state('light.bedroom_ceiling'); $restApiClient->errorLog(); $restApiClient->calendars(); $restApiClient->calendarEvents('calendar.birthdays'); $restApiClient->updateState('light.bedroom_ceiling', 'on'); $restApiClient->fireEvent('script_started', [ 'name' => 'Turn All Lights Off', 'entity_id' => 'script.turn_all_lights_off' ]); $restApiClient->callService('light', 'turn_on', [ 'entity_id' => 'light.bedroom_ceiling' ]); $restApiClient->renderTemplate("The bedroom ceiling light is {{ states('light.bedroom_ceiling') }}."); $restApiClient->checkConfig(); $restApiClient->handleIntent([ 'name' => 'SetTimer', 'data' => [ 'seconds' => '30', ] ]);
HaWebhookClient - Available Methods
$webhookClient = new \IndexZer0\HaRestApiClient\HaWebhookClient( 'http://localhost:8123/api/' ); /* * --------------------------- * GET request example * --------------------------- */ $webhookClient->send( method: 'GET', webhookId: 'webhook_id', queryParams: ['query' => 'param'], ); /* * --------------------------- * POST request example - with json body * --------------------------- */ $webhookClient->send( method: 'POST', webhookId: 'webhook_id', payloadType: 'json', data: ['json' => 'data'] ); /* * --------------------------- * POST request example - with form params body * --------------------------- */ $webhookClient->send( method: 'POST', webhookId: 'webhook_id', payloadType: 'form_params', data: ['form' => 'param'] );
Error Handling
All exceptions thrown by the package implement \IndexZer0\HaRestApiClient\Exception\HaExceptionInterface.
How-ever it doesn't harm to also catch \Throwable.
try { $webhookClient = new \IndexZer0\HaRestApiClient\HaWebhookClient( 'http://localhost:8123/api/' ); $response = $webhookClient->send('GET', 'webhook_id'); } catch (\IndexZer0\HaRestApiClient\Exception\HaExceptionInterface $haException) { } catch (\Throwable $t) { // Shouldn't happen - but failsafe. }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
- Currently accepting PR for
$client->camera();as I don't have a camera entity to develop against.
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
indexzer0/ha-rest-api-client 适用场景与选型建议
indexzer0/ha-rest-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 460 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 02 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「webhook」 「REST API」 「psr-7」 「httplug」 「psr-17」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 indexzer0/ha-rest-api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 indexzer0/ha-rest-api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 indexzer0/ha-rest-api-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Inbox pattern process implementation for your Laravel Applications
GitHub Webhook Listener with plugin-based API for creating your own triggerered actions
Alfabank REST API integration
统计信息
- 总下载量: 460
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-21