Skip to content
Permalink
Browse files

Fix toggle action for RGB and Dimmer channels

  • Loading branch information
fracz committed Jan 28, 2019
1 parent e18c167 commit 33c5ab353ebd611950361f939e495c12e5d89ebd
@@ -15,9 +15,11 @@ public function getState(IODeviceChannel $channel): array {
if (in_array($channel->getFunction(), [ChannelFunction::RGBLIGHTING(), ChannelFunction::DIMMERANDRGBLIGHTING()])) {
$result['color'] = $value['color'];
$result['color_brightness'] = $value['color_brightness'];
$result['on'] = $value['color_brightness'] > 0;
}
if (in_array($channel->getFunction(), [ChannelFunction::DIMMER(), ChannelFunction::DIMMERANDRGBLIGHTING()])) {
$result['brightness'] = $value['brightness'];
$result['on'] = ($result['on'] ?? false) || $value['brightness'] > 0;
}
}
return $result;
@@ -25,6 +25,8 @@
class SuplaServerMock extends SuplaServer {
private static $nextResponse;
public static $executedCommands = [];
/** @var SuplaServerMockCommandsCollector */
private $commandsCollector;
@@ -43,6 +45,7 @@ protected function disconnect() {
protected function command($command) {
$this->commandsCollector->addCommand($command);
self::$executedCommands[] = $command;
return $this->tryToHandleCommand($command);
}
@@ -0,0 +1,101 @@
<?php
/*
Copyright (C) AC SOFTWARE SP. Z O.O.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
namespace SuplaBundle\Tests\Integration\Model\ChannelActionExecutor;
use SuplaBundle\Entity\IODevice;
use SuplaBundle\Enums\ChannelFunction;
use SuplaBundle\Enums\ChannelFunctionAction;
use SuplaBundle\Enums\ChannelType;
use SuplaBundle\Model\ChannelActionExecutor\ChannelActionExecutor;
use SuplaBundle\Supla\SuplaServerMock;
use SuplaBundle\Tests\Integration\IntegrationTestCase;
use SuplaBundle\Tests\Integration\Traits\SuplaApiHelper;
class ToggleChannelActionExecutorIntegrationTest extends IntegrationTestCase {
use SuplaApiHelper;
/** @var IODevice */
private $device;
/** @var ChannelActionExecutor */
private $channelActionExecutor;
/** @before */
public function createDeviceForTests() {
$user = $this->createConfirmedUser();
$location = $this->createLocation($user);
$this->device = $this->createDevice($location, [
[ChannelType::RELAY, ChannelFunction::POWERSWITCH],
[ChannelType::RELAY, ChannelFunction::DIMMER],
[ChannelType::RELAY, ChannelFunction::RGBLIGHTING],
]);
$this->channelActionExecutor = $this->container->get(ChannelActionExecutor::class);
}
/** @after */
public function clearCommands() {
SuplaServerMock::$executedCommands = [];
}
public function testTogglePowerSwitchOnOff() {
SuplaServerMock::mockTheNextResponse("VALUE:1\n");
$this->channelActionExecutor->executeAction($this->device->getChannels()[0], ChannelFunctionAction::TOGGLE());
$this->assertCount(2, SuplaServerMock::$executedCommands);
$setCommand = SuplaServerMock::$executedCommands[1];
$this->assertEquals('SET-CHAR-VALUE:1,1,1,0', $setCommand);
}
public function testTogglePowerSwitchOffOn() {
SuplaServerMock::mockTheNextResponse("VALUE:0\n");
$this->channelActionExecutor->executeAction($this->device->getChannels()[0], ChannelFunctionAction::TOGGLE());
$this->assertCount(2, SuplaServerMock::$executedCommands);
$setCommand = SuplaServerMock::$executedCommands[1];
$this->assertEquals('SET-CHAR-VALUE:1,1,1,1', $setCommand);
}
public function testToggleDimmerOnOff() {
SuplaServerMock::mockTheNextResponse("VALUE:0,0,10\n");
$this->channelActionExecutor->executeAction($this->device->getChannels()[1], ChannelFunctionAction::TOGGLE());
$this->assertCount(2, SuplaServerMock::$executedCommands);
$setCommand = SuplaServerMock::$executedCommands[1];
$this->assertEquals('SET-CHAR-VALUE:1,1,2,0', $setCommand);
}
public function testToggleDimmerOffOn() {
SuplaServerMock::mockTheNextResponse("VALUE:0,0,0\n");
$this->channelActionExecutor->executeAction($this->device->getChannels()[1], ChannelFunctionAction::TOGGLE());
$this->assertCount(2, SuplaServerMock::$executedCommands);
$setCommand = SuplaServerMock::$executedCommands[1];
$this->assertEquals('SET-CHAR-VALUE:1,1,2,1', $setCommand);
}
public function testToggleRgbOnOff() {
SuplaServerMock::mockTheNextResponse("VALUE:0,10,0\n");
$this->channelActionExecutor->executeAction($this->device->getChannels()[2], ChannelFunctionAction::TOGGLE());
$this->assertCount(2, SuplaServerMock::$executedCommands);
$setCommand = SuplaServerMock::$executedCommands[1];
$this->assertEquals('SET-CHAR-VALUE:1,1,3,0', $setCommand);
}
public function testToggleRgbOfOn() {
SuplaServerMock::mockTheNextResponse("VALUE:0,10,0\n");
$this->channelActionExecutor->executeAction($this->device->getChannels()[2], ChannelFunctionAction::TOGGLE());
$this->assertCount(2, SuplaServerMock::$executedCommands);
$setCommand = SuplaServerMock::$executedCommands[1];
$this->assertEquals('SET-CHAR-VALUE:1,1,3,1', $setCommand);
}
}
@@ -0,0 +1,111 @@
<?php
/*
Copyright (C) AC SOFTWARE SP. Z O.O.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
namespace SuplaBundle\Tests\Integration\Model\ChannelParamsUpdater;
use SuplaBundle\Entity\IODevice;
use SuplaBundle\Enums\ChannelFunction;
use SuplaBundle\Enums\ChannelType;
use SuplaBundle\Model\ChannelStateGetter\ChannelStateGetter;
use SuplaBundle\Supla\SuplaServerMock;
use SuplaBundle\Tests\Integration\IntegrationTestCase;
use SuplaBundle\Tests\Integration\Traits\SuplaApiHelper;
class OnOffChannelStateGetterIntegrationTest extends IntegrationTestCase {
use SuplaApiHelper;
/** @var IODevice */
private $device;
/** @var ChannelStateGetter */
private $channelStateGetter;
/** @before */
public function createDeviceForTests() {
$user = $this->createConfirmedUser();
$location = $this->createLocation($user);
$this->device = $this->createDevice($location, [
[ChannelType::RELAY, ChannelFunction::POWERSWITCH],
[ChannelType::RELAY, ChannelFunction::DIMMER],
[ChannelType::RELAY, ChannelFunction::RGBLIGHTING],
[ChannelType::RELAY, ChannelFunction::DIMMERANDRGBLIGHTING],
]);
$this->channelStateGetter = $this->container->get(ChannelStateGetter::class);
}
public function testGettingOnromPowerSwitch() {
SuplaServerMock::mockTheNextResponse("VALUE:1\n");
$state = $this->channelStateGetter->getState($this->device->getChannels()[0]);
$this->assertArrayHasKey('on', $state);
$this->assertTrue($state['on']);
}
public function testGettingOffFromPowerSwitch() {
SuplaServerMock::mockTheNextResponse("VALUE:0\n");
$state = $this->channelStateGetter->getState($this->device->getChannels()[0]);
$this->assertArrayHasKey('on', $state);
$this->assertFalse($state['on']);
}
public function testGettingOnFromDimmer() {
SuplaServerMock::mockTheNextResponse("VALUE:0,0,10\n");
$state = $this->channelStateGetter->getState($this->device->getChannels()[1]);
$this->assertArrayHasKey('on', $state);
$this->assertTrue($state['on']);
}
public function testGettingOffFromDimmer() {
SuplaServerMock::mockTheNextResponse("VALUE:0,0,0\n");
$state = $this->channelStateGetter->getState($this->device->getChannels()[1]);
$this->assertArrayHasKey('on', $state);
$this->assertFalse($state['on']);
}
public function testGettingOnFromRgb() {
SuplaServerMock::mockTheNextResponse("VALUE:0,10,0\n");
$state = $this->channelStateGetter->getState($this->device->getChannels()[2]);
$this->assertArrayHasKey('on', $state);
$this->assertTrue($state['on']);
}
public function testGettingOnFromDimmerRgbWhenBothOn() {
SuplaServerMock::mockTheNextResponse("VALUE:0,10,10\n");
$state = $this->channelStateGetter->getState($this->device->getChannels()[3]);
$this->assertArrayHasKey('on', $state);
$this->assertTrue($state['on']);
}
public function testGettingOnFromDimmerRgbWhenColorOn() {
SuplaServerMock::mockTheNextResponse("VALUE:0,10,0\n");
$state = $this->channelStateGetter->getState($this->device->getChannels()[3]);
$this->assertArrayHasKey('on', $state);
$this->assertTrue($state['on']);
}
public function testGettingOnFromDimmerRgbWhenDimOn() {
SuplaServerMock::mockTheNextResponse("VALUE:0,0,10\n");
$state = $this->channelStateGetter->getState($this->device->getChannels()[3]);
$this->assertArrayHasKey('on', $state);
$this->assertTrue($state['on']);
}
public function testGettingOffFromDimmerRgbWhen() {
SuplaServerMock::mockTheNextResponse("VALUE:0,0,0\n");
$state = $this->channelStateGetter->getState($this->device->getChannels()[3]);
$this->assertArrayHasKey('on', $state);
$this->assertFalse($state['on']);
}
}

0 comments on commit 33c5ab3

Please sign in to comment.
You can’t perform that action at this time.