Skip to content

Commit

Permalink
Use custom exception and improved README
Browse files Browse the repository at this point in the history
  • Loading branch information
Gared committed Jan 25, 2021
1 parent 19e4ea5 commit a7209aa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# shelly-php-client

You can use this library to access an API of a Shelly device.
You can use this library to access the HTTP API of a Shelly device.
You can find the official API documentation [here](https://shelly-api-docs.shelly.cloud).

## Installation
Expand All @@ -23,7 +23,7 @@ $client = new \ShellyClient\HTTP\Client('http://192.168.1.10', 'ShellyDeviceLigh
```php
$meter = $client->getMeter();
$power = $meter->getPower();
echo "Watt usage: " . $power;
echo "Current power usage: " . $power;
```

### Power shelly plug "on"
Expand All @@ -32,6 +32,41 @@ echo "Watt usage: " . $power;
$client->getRelay(0, \ShellyClient\Request\RelayRequest::TURN_ON);
```


### Full example

```php
// Create new client for one device
$clientLightA = new \ShellyClient\HTTP\Client('http://192.168.1.10', 'ShellyDeviceLightA');
$clientLightB = new \ShellyClient\HTTP\Client('http://192.168.1.20', 'ShellyDeviceLightB');
// Switch device on and get relay data
$relay = $clientLightA->getRelay(0, \ShellyClient\Request\RelayRequest::TURN_ON);
// check if timer has been set
if ($relay->hasTimer()) {
// do something
}

$meter = $clientLightA->getMeter();
// Get total power consumption in kW/h
$kilowattHours = $meter->getTotalInKilowattHours();
// Get last three watt per minute power consumption values
$counters = $meter->getCounters();

// Parallel call informations from device B
$power = $clientLightB->getMeter()->getPower();
// Get device name set above in construct
$deviceNameDeviceB = $clientLightB->getDeviceName();
```

## Supported Platforms

* You need at least PHP 7.4

## Supported APIs
| API | Code |
| -------------------|-------------------------|
| /settings | $client->getSettings(); |
| /settings/actions | $client->getActions(); |
| /status | $client->getStatus(); |
| /meter | $client->getMeter(); |
| /relay | $client->getRelay(); |
10 changes: 10 additions & 0 deletions lib/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace ShellyClient\Exception;

use Exception;

class InvalidArgumentException extends Exception
{
}
3 changes: 2 additions & 1 deletion lib/Request/RelayRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace ShellyClient\Request;

use ShellyClient\Exception\InvalidArgumentException;
use ShellyClient\Model\RelayResponse;

class RelayRequest extends RequestAbstract
Expand All @@ -16,7 +17,7 @@ class RelayRequest extends RequestAbstract
public function doRequest(int $relayIndex = 0, string $turn = null): void
{
if ($turn !== null && !in_array($turn, [self::TURN_ON, self::TURN_OFF, self::TURN_TOGGLE], true)) {
throw new \Exception('Invalid value for turn parameter: ' . $turn);
throw new InvalidArgumentException('Invalid value for turn parameter: ' . $turn);
}

$this->response = $this->client->request('GET', self::METER_ENDPOINT . '/' . $relayIndex, [
Expand Down

0 comments on commit a7209aa

Please sign in to comment.