Skip to content

Commit

Permalink
GitHub #99 - Add transition time on create scene
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmk committed Nov 26, 2015
1 parent 704c0f2 commit 5fdfa9e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
43 changes: 39 additions & 4 deletions library/Phue/Command/CreateScene.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class CreateScene implements CommandInterface
*/
protected $lights = [];

/**
* Transition time
*
* @var mixed
*/
protected $transitionTime = null;

/**
* Constructs a command
*
Expand Down Expand Up @@ -92,6 +99,28 @@ public function lights(array $lights = [])
return $this;
}

/**
* Set transition time
*
* @param double $seconds Time in seconds
*
* @return self This object
*/
public function transitionTime($seconds)
{
// Don't continue if seconds is not valid
if ((double) $seconds < 0) {
throw new \InvalidArgumentException(
'Time must be at least 0'
);
}

// Value is in 1/10 seconds
$this->transitionTime = (int) ($seconds * 10);

return $this;
}

/**
* Send command
*
Expand All @@ -101,13 +130,19 @@ public function lights(array $lights = [])
*/
public function send(Client $client)
{
$body = (object) [
'name' => $this->name,
'lights' => $this->lights
];

if ($this->transitionTime !== null) {
$body->transitiontime = $this->transitionTime;
}

$client->getTransport()->sendRequest(
"/api/{$client->getUsername()}/scenes/{$this->id}",
TransportInterface::METHOD_PUT,
(object) [
'name' => $this->name,
'lights' => $this->lights
]
$body
);

return $this->id;
Expand Down
27 changes: 25 additions & 2 deletions tests/Phue/Test/Command/CreateSceneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ public function testLights()
$this->assertEquals($command, $command->lights([1]));
}

/**
* Test: Set transition time
*
* @covers \Phue\Command\CreateScene::transitionTime
*/
public function testTransitionTime()
{
$command = new CreateScene('phue-test', 'Scene test', [1, 2]);
$command->transitionTime(2);

// Ensure property is set properly
$this->assertAttributeEquals(
20,
'transitionTime',
$command
);

// Ensure self object is returned
$this->assertEquals($command, $command->transitionTime(1));
}

/**
* Test: Send command
*
Expand All @@ -125,6 +146,7 @@ public function testLights()
public function testSend()
{
$command = new CreateScene('phue-test', 'Scene test', [2, 3]);
$command->transitionTime(5);

// Stub transport's sendRequest usage
$this->mockTransport->expects($this->once())
Expand All @@ -134,8 +156,9 @@ public function testSend()
$this->equalTo(TransportInterface::METHOD_PUT),
$this->equalTo(
(object) [
'name' => 'Scene test',
'lights' => [2, 3]
'name' => 'Scene test',
'lights' => [2, 3],
'transitiontime' => 50,
]
)
);
Expand Down

0 comments on commit 5fdfa9e

Please sign in to comment.