This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0eb417c
Showing
9 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor/ | ||
/composer.lock | ||
/.idea/ | ||
/phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# API Client for Tuya | ||
|
||
An API Client build for [Tuya][1] heavily inspired by [tuyapy][0]. | ||
|
||
[0]: https://pypi.org/project/tuyapy | ||
[1]: https://www.tuya.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "inverse/tuya-client", | ||
"description": "API client for Tuya", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Malachi Soord", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.1", | ||
"guzzlehttp/guzzle": "^6.3", | ||
"webmozart/assert": "^1.3", | ||
"phpunit/phpunit": "^7.2" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Inverse\\TuyaClient\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Tests\\Inverse\\TuyaClient\\": "tests/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html --> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd" | ||
backupGlobals="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
syntaxCheck="true" | ||
bootstrap="./tests/bootstrap.php" | ||
> | ||
<php> | ||
<env name="TUYA_USERNAME" value=""/> | ||
<env name="TUYA_PASSWORD" value="" /> | ||
<env name="TUYA_COUNTRYCODE" value="" /> | ||
</php> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Inverse\TuyaClient; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Psr7\Uri; | ||
use GuzzleHttp\Psr7\UriResolver; | ||
use Psr\Http\Message\UriInterface; | ||
|
||
class ApiClient | ||
{ | ||
private const BASE_URL_FORMAT = 'https://px1.tuya%s.com'; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var Session | ||
*/ | ||
private $session; | ||
|
||
public function __construct(Session $session, Client $client = null) | ||
{ | ||
$this->session = $session; | ||
$this->client = $client ?? new Client(); | ||
} | ||
|
||
public function getAccessToken() | ||
{ | ||
$baseUrl = $this->getBaseUrl($this->session); | ||
|
||
$uri = UriResolver::resolve($baseUrl, new Uri('homeassistant/auth.do')); | ||
|
||
$response = $this->client->request('POST', $uri, [ | ||
'form_params' => [ | ||
'userName' => $this->session->getUsername(), | ||
'password' => $this->session->getPassword(), | ||
'countryCode' => $this->session->getCountryCode(), | ||
'from' => 'tuya' | ||
] | ||
]); | ||
|
||
$response = json_decode((string)$response->getBody(), true); | ||
|
||
return $response; | ||
} | ||
|
||
private function getBaseUrl(Session $session): UriInterface | ||
{ | ||
return new Uri(sprintf(self::BASE_URL_FORMAT, $session->getRegion()->getValue())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Inverse\TuyaClient; | ||
|
||
use Webmozart\Assert\Assert; | ||
|
||
class Region | ||
{ | ||
const CN = 'cn'; | ||
const EU = 'eu'; | ||
const US = 'us'; | ||
|
||
const VALID_REGIONS = [ | ||
self::CN, | ||
self::EU, | ||
self::US, | ||
]; | ||
|
||
private $value; | ||
|
||
public function __construct(string $value) | ||
{ | ||
Assert::oneOf($value, self::VALID_REGIONS, '%s is not a valid region'); | ||
$this->value = $value; | ||
} | ||
|
||
public function getValue(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Inverse\TuyaClient; | ||
|
||
class Session | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $username; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $password; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $countryCode; | ||
|
||
/** | ||
* @var Region | ||
*/ | ||
private $region; | ||
|
||
public function __construct(string $username, string $password, int $countryCode) | ||
{ | ||
$this->username = $username; | ||
$this->password = $password; | ||
$this->countryCode = $countryCode; | ||
$this->region = new Region(Region::US); | ||
} | ||
|
||
public function getUsername(): string | ||
{ | ||
return $this->username; | ||
} | ||
|
||
public function getPassword(): string | ||
{ | ||
return $this->password; | ||
} | ||
|
||
public function getCountryCode(): int | ||
{ | ||
return $this->countryCode; | ||
} | ||
|
||
public function getRegion(): Region | ||
{ | ||
return $this->region; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Tests\Inverse\TuyaClient; | ||
|
||
use Inverse\TuyaClient\Session; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ApiClientTest extends TestCase | ||
{ | ||
private function getSessionFromEnv(): Session | ||
{ | ||
$username = getenv('TUYA_USERNAME'); | ||
$password = getenv('TUYA_PASSWORD'); | ||
$countryCode = getenv('TUYA_COUNTRYCODE'); | ||
return new Session($username, $password, $countryCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|