Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Improvements (#9)
Browse files Browse the repository at this point in the history
* Fix for no devices

* Improve error

* Align
  • Loading branch information
inverse authored Jun 29, 2020
1 parent 62636bf commit c52045e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
16 changes: 6 additions & 10 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ private function getAccessToken(): Token

$this->validate($response, 'An error occurred while fetching access token');

$token = Token::fromArray($response);

return $token;
return Token::fromArray($response);
}

private function refreshAccessToken(): Token
Expand All @@ -151,9 +149,7 @@ private function refreshAccessToken(): Token

$this->validate($response, 'Failed to refresh access token');

$token = Token::fromArray($response);

return $token;
return Token::fromArray($response);
}

private function getBaseUrl(Session $session): UriInterface
Expand All @@ -162,15 +158,15 @@ private function getBaseUrl(Session $session): UriInterface
}

/**
* @param array $response
* @param string|null $message
* @param array $response
* @param string $message
*
* @throws TuyaClientException
*/
private function validate(array $response, string $message = null)
private function validate(array $response, string $message)
{
if (isset($response['responseStatus']) && $response['responseStatus'] === 'error') {
$message = $message ?? $response['responseMsg'];
$message = sprintf('%s - %s', $message, $response['errorMsg']);

throw new TuyaClientException($message);
}
Expand Down
35 changes: 24 additions & 11 deletions tests/Functional/ApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Functional\Inverse\TuyaClient;

use Inverse\TuyaClient\ApiClient;
use Inverse\TuyaClient\Device\AbstractDevice;
use Inverse\TuyaClient\Device\SwitchDevice;

class ApiClientTest extends BaseTestCase
Expand All @@ -11,25 +12,37 @@ public function testDiscoverDevices()
{
$apiClient = $this->getApiClient();

$switch = $this->getDevice($apiClient);
$device = $this->getDevice($apiClient);

if (!$switch->isOn()) {
$apiClient->sendEvent($switch->getOnEvent());

$switch = $this->getDevice($apiClient);
$this->assertTrue($switch->isOn());
if ($device === null) {
return $this->doesNotPerformAssertions();
}

$apiClient->sendEvent($switch->getOffEvent());
$switch = $this->getDevice($apiClient);
if ($device instanceof SwitchDevice) {
if (!$device->isOn()) {
$apiClient->sendEvent($device->getOnEvent());

$device = $this->getDevice($apiClient);
$this->assertTrue($device->isOn());
}

$this->assertFalse($switch->isOn());
$apiClient->sendEvent($device->getOffEvent());
$device = $this->getDevice($apiClient);

$this->assertFalse($device->isOn());
}
}

private function getDevice(ApiClient $apiClient): SwitchDevice
private function getDevice(ApiClient $apiClient): ?AbstractDevice
{
$devices = $apiClient->discoverDevices();

return $devices[1];
$device = null;

if (!empty($devices)) {
$device = $devices[0];
}

return $device;
}
}

0 comments on commit c52045e

Please sign in to comment.