Developers - PJanisio/ewelinkApiPhp GitHub Wiki
π Intro
Official eWeLink API documentation
Possible API endpoints for the standard role:
π Project statement
- MIT license (use it however you like, but keep attribution)
- The
main
branch is intended to be operational - Set
DEBUG = 1
inConstants.php
to log every API request (with parameters and output) todebug.log
π§ Project structure
ewelinkApiPhp/
βββ .github/
β βββ workflows/
β βββ ci.yml # GitHub Actions matrix CI: PHP 7.4β8.5, Composer install, tests & QA checks
βββ src/
β βββ Config.php # Loads & merges config (Constants β’ optional JSON_LOG_DIR/config.json β’ runtime overrides)
β βββ Constants.php # Defaults & switches (APP IDs, secrets, SAVE_CONFIG_JSON, JSON_LOG_DIR, DEBUG, error codes)
β βββ HttpClient.php # Signs requests; helpers/factories (Token, Devices, Home)
β βββ Token.php # OAuth flow: get/refresh/validate tokens; persists to token.json; security redirects
β βββ Utils.php # Helpers: nonce/signature, config & JSON validation, debug logger, security warnings
β βββ Home.php # Thin faΓ§ade over /v2/family endpoints; caches family/room lists; exposes current family ID
β βββ Devices.php # High-level device API: discovery, live status, multi-channel helpers, search & control
βββ tests/
β βββ ConstantsTest.php # PHPUnit smoke tests for constant values (autoload-dev excluded from classmap)
β βββ config/
β βββ phpunit.xml.dist # PHPUnit bootstrap, coverage & colors settings (run via `composer test`)
β βββ phpstan.neon.dist # PHPStan rules for static analysis (run via `composer stan`)
βββ gateway.php # Demo gateway: composer autoload β OAuth login β list devices in the browser
βββ README.md
βββ composer.json # Package metadata, PSR-4 map, QA scripts (`test`, `stan`, `cs`, `cs-fix`)
βββ LICENSE (MIT) # MIT license text β do what you want, keep attribution
βββ .gitignore # Ignores vendor/, debug.log, *.json, IDE files, etc.
βββ .gitattributes # Normalizes line endings & export settings for archives
All classes are located in the src
directory.
[!TIP] By default, JSON outputs (including
config.json
,token.json
,devices.json
, anddebug.log
) are saved in the project root.
You can change the storage directory viaJSON_LOG_DIR
inConstants.php
or at runtime with overrides.
βοΈ Configuration options
Configuration can be provided at three levels. Precedence is:
runtime overrides β config.json
(when enabled) β Constants.php
-
Constants.php
(defaults & switches)
Define your baseline values:APPID
,APP_SECRET
,REDIRECT_URL
,EMAIL
,PASSWORD
,REGION
DEBUG
(0/1) β enables verbose logging todebug.log
JSON_LOG_DIR
β where all JSON/log files live (default: project root)SAVE_CONFIG_JSON
(true/false)true
(default): read & writeJSON_LOG_DIR/config.json
false
: ignoreconfig.json
entirely and never write it
If a staleconfig.json
exists, the library will display a notice recommending deletion.
-
config.json
(optional)
Used only whenSAVE_CONFIG_JSON === true
.
After the first OAuth, the effective configuration is written toconfig.json
. On subsequent runs it is merged on top ofConstants.php
.
Path:JSON_LOG_DIR/config.json
. -
Runtime overrides (best for multi-site / multi-user)
Pass an associative array tonew HttpClient($overrides)
(or callConfig::setOverrides([...])
before use).
Overrides are merged last, so they win over bothconfig.json
andConstants.php
.
Typical use: set a per-siteJSON_LOG_DIR
, different credentials, region, or debug level without touching files.
β Supported runtime override keys
You can override any of the following at runtime:
APPID
APP_SECRET
REDIRECT_URL
EMAIL
PASSWORD
REGION
(cn
,us
,eu
,as
)DEBUG
(0/1)JSON_LOG_DIR
(controls whereconfig.json
,token.json
,debug.log
, etc. are stored)
Note:
SAVE_CONFIG_JSON
is a project-level constant (read at runtime) and not designed to be overridden at runtime. Set it inConstants.php
.
gateway.php
)
π§© Example (<?php
use pjanisio\ewelinkapiphp\HttpClient;
/**
* A) Default (backward compatible)
* - Constants + (if enabled) JSON_LOG_DIR/config.json
* - SAVE_CONFIG_JSON=true (default) β read/write config.json
*/
$http = new HttpClient();
/**
* B) Disable config.json completely
* - Set SAVE_CONFIG_JSON=false in Constants.php
* - Library ignores config.json even if present and will not write it
*/
$http = new HttpClient();
/**
* C) Per-run overrides (win over both constants and config.json)
* - Great for multi-site isolation (custom JSON_LOG_DIR, creds, region)
*/
$overrides = [
'APPID' => 'your_app_id',
'APP_SECRET' => 'your_app_secret',
'REDIRECT_URL' => 'https://yourdomain.com/ewelinkApiPhp/gateway.php',
'EMAIL' => '[email protected]',
'PASSWORD' => 'your_password',
'REGION' => 'eu',
'JSON_LOG_DIR' => __DIR__ . '/json_logs', // must be writable
'DEBUG' => 0,
];
$http = new HttpClient($overrides);
[!NOTE] If
SAVE_CONFIG_JSON=false
andJSON_LOG_DIR/config.json
still exists, a notice will be shown suggesting that you delete the file to fully disable file-based config.
A security warning is also shown ifconfig.json
resides under your web root.
π€ Contribution guidelines
Thank you for your interest in contributing! Please follow these steps:
1) Fork the repository
- Click Fork at the top-right of this page to create your copy of the repo.
2) Work on the code
- Clone your fork locally.
- Implement your changes and test with Composer.
Use Composer scripts to check code:
composer install # install dependencies (skip if already installed)
composer cs # run code sniffer (warnings allowed)
composer cs-fix # auto-fix style issues (phpcbf)
3) Create a pull request
- Add clear labels
- Request a reviewer
π§ͺ CI tests
Currently we test:
- eWeLink API gateways
- Compatibility across PHP versions
- Composer installation, code style, and PHPStan (level 3)
Before drafting a PR or committing to the repo, run:
composer install
composer cs
composer cs-fix
All tests are managed by .github/workflows/ci.yml
.
π§Ύ Versioning
We follow Semantic Versioning.