From 5fdfa9e01b2d9d28c2f8eb97b9f80f47edc3f1bb Mon Sep 17 00:00:00 2001 From: "Michael K. Squires" Date: Thu, 26 Nov 2015 10:14:15 -0600 Subject: [PATCH] GitHub #99 - Add transition time on create scene --- library/Phue/Command/CreateScene.php | 43 +++++++++++++++++++-- tests/Phue/Test/Command/CreateSceneTest.php | 27 ++++++++++++- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/library/Phue/Command/CreateScene.php b/library/Phue/Command/CreateScene.php index bf56ad6..9d1feff 100644 --- a/library/Phue/Command/CreateScene.php +++ b/library/Phue/Command/CreateScene.php @@ -31,6 +31,13 @@ class CreateScene implements CommandInterface */ protected $lights = []; + /** + * Transition time + * + * @var mixed + */ + protected $transitionTime = null; + /** * Constructs a command * @@ -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 * @@ -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; diff --git a/tests/Phue/Test/Command/CreateSceneTest.php b/tests/Phue/Test/Command/CreateSceneTest.php index c0ab4a0..9449714 100644 --- a/tests/Phue/Test/Command/CreateSceneTest.php +++ b/tests/Phue/Test/Command/CreateSceneTest.php @@ -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 * @@ -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()) @@ -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, ] ) );