Skip to content

Commit

Permalink
GitHub #88 - Add commands to update sensor config and state
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmk committed Jan 4, 2015
1 parent 5f04559 commit 331bbbd
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/update-sensor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@
(new \Phue\Command\UpdateSensor($sensor))
->name('Test sensor new name')
);

$client->sendCommand(
(new \Phue\Command\UpdateSensorState($sensor))
->stateAttribute('flag', false)
);

$client->sendCommand(
(new \Phue\Command\UpdateSensorConfig($sensor))
->configAttribute('battery', 99)
);
72 changes: 72 additions & 0 deletions library/Phue/Command/UpdateSensorConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Phue: Philips Hue PHP Client
*
* @author Michael Squires <sqmk@php.net>
* @copyright Copyright (c) 2012 Michael K. Squires
* @license http://github.com/sqmk/Phue/wiki/License
*/

namespace Phue\Command;

use Phue\Client;
use Phue\Transport\TransportInterface;

/**
* Update sensor config command
*/
class UpdateSensorConfig extends CreateSensor
{
/**
* Sensor Id
*
* @var string
*/
protected $sensorId;

/**
* Sensor config
*
* @var array
*/
protected $config = [];

/**
* Constructs a command
*
* @param mixed $sensor Sensor Id or Sensor object
*/
public function __construct($sensor)
{
$this->sensorId = (string) $sensor;
}

/**
* Config attribute
*
* @param string $key Key
* @param mixed $value Value
*
* @return self This object
*/
public function configAttribute($key, $value)
{
$this->config[$key] = $value;

return $this;
}

/**
* Send command
*
* @param Client $client Phue Client
*/
public function send(Client $client)
{
$client->getTransport()->sendRequest(
"/api/{$client->getUsername()}/sensors/{$this->sensorId}/config",
TransportInterface::METHOD_PUT,
(object) $this->config
);
}
}
72 changes: 72 additions & 0 deletions library/Phue/Command/UpdateSensorState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Phue: Philips Hue PHP Client
*
* @author Michael Squires <sqmk@php.net>
* @copyright Copyright (c) 2012 Michael K. Squires
* @license http://github.com/sqmk/Phue/wiki/License
*/

namespace Phue\Command;

use Phue\Client;
use Phue\Transport\TransportInterface;

/**
* Update sensor state command
*/
class UpdateSensorState extends CreateSensor
{
/**
* Sensor Id
*
* @var string
*/
protected $sensorId;

/**
* Sensor state
*
* @var array
*/
protected $state = [];

/**
* Constructs a command
*
* @param mixed $sensor Sensor Id or Sensor object
*/
public function __construct($sensor)
{
$this->sensorId = (string) $sensor;
}

/**
* State attribute
*
* @param string $key Key
* @param mixed $value Value
*
* @return self This object
*/
public function stateAttribute($key, $value)
{
$this->state[$key] = $value;

return $this;
}

/**
* Send command
*
* @param Client $client Phue Client
*/
public function send(Client $client)
{
$client->getTransport()->sendRequest(
"/api/{$client->getUsername()}/sensors/{$this->sensorId}/state",
TransportInterface::METHOD_PUT,
(object) $this->state
);
}
}

0 comments on commit 331bbbd

Please sign in to comment.