- Updated `shelly` class to support POST request handling (`sendPostRequest`) and server URL validation. - Adjusted `shelly_device_state` to handle object-based `status` and `settings`, adding a `populate` method for data initialization. - Added `shelly_device_switch` helper to manage switching operations via Shelly API. - Removed unused `api_url` property in `shelly` class. - Modified `shelly_i` interface with definitions for `requireValidServerUrl` and `sendPostRequest`.
51 lines
1.3 KiB
PHP
51 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace modules\shelly\helpers;
|
|
|
|
use classes\shelly;
|
|
use Exception;
|
|
|
|
/**
|
|
* @class shelly_device_switch
|
|
* @description
|
|
* @docs https://shelly-api-docs.shelly.cloud/cloud-control-api/communication-v2#control-a-switch
|
|
*/
|
|
class shelly_device_switch extends shelly_device_state
|
|
{
|
|
/**
|
|
* @var boolean $on
|
|
* @description The output state
|
|
* @values true | false
|
|
*/
|
|
public bool $on;
|
|
/**
|
|
* @var int $channel
|
|
* @description The channel number
|
|
* @values 0 | 1 | 2 | 3
|
|
* @default 0
|
|
*/
|
|
public int $channel = 0;
|
|
/**
|
|
* @var int $toggle_after
|
|
* @description After how many seconds, the state should be set to opposite the value of "on"
|
|
* @values 1
|
|
*/
|
|
public int $toggle_after = 1;
|
|
|
|
/**
|
|
* Send a request to the shelly API to switch the output
|
|
* @param bool $on
|
|
* @return array|object|null
|
|
* @throws Exception
|
|
*/
|
|
public function switch(bool $on): array|object|null
|
|
{
|
|
$parameters = [
|
|
'id' => (string)$this->id,
|
|
'channel' => (int)$this->channel,
|
|
'on' => $on,
|
|
'toggle_after' => (int)$this->toggle_after,
|
|
];
|
|
return (new shelly())->sendPostRequest('/v2/devices/api/set/switch', $parameters);
|
|
}
|
|
} |