This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#21 Implemented shutter with Service.GarageDoorOpener. This is more l…
…ike a real shutter than Service.LockMechanism.
- Loading branch information
1 parent
05b1ec5
commit 95740ca
Showing
3 changed files
with
161 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
grunt = require 'grunt' | ||
assert = require 'assert' | ||
Promise = require 'bluebird' | ||
|
||
env = | ||
logger: | ||
debug: (stmt) -> | ||
grunt.log.writeln stmt | ||
error: (stmt) -> | ||
grunt.log.writeln stmt | ||
ShutterAccessory = require("../accessories/shutter")(env) | ||
hap = require 'hap-nodejs' | ||
Service = hap.Service | ||
Characteristic = hap.Characteristic | ||
|
||
class TestShutter extends require('events').EventEmitter | ||
id: "testshutter-id" | ||
name: "testshutter" | ||
config: {} | ||
|
||
_position: null | ||
|
||
getPosition: -> | ||
Promise.resolve(@_position) | ||
|
||
firePositionChange: (position) -> | ||
@_position = position | ||
@emit 'position', position | ||
|
||
moveUp: -> | ||
@_position = "up" | ||
return Promise.resolve() | ||
|
||
moveDown: -> | ||
@_position = "down" | ||
return Promise.resolve() | ||
|
||
stop: -> | ||
@_position = "stopped" | ||
return Promise.resolve() | ||
|
||
describe "shutter", -> | ||
|
||
device = null | ||
accessory = null | ||
|
||
beforeEach -> | ||
device = new TestShutter() | ||
accessory = new ShutterAccessory(device) | ||
|
||
describe "getting Characteristic.CurrentDoorState", -> | ||
|
||
it "should return CLOSED when postion is down", -> | ||
device._position = 'down' | ||
accessory.getService(Service.GarageDoorOpener) | ||
.getCharacteristic(Characteristic.CurrentDoorState) | ||
.getValue((error, value) -> | ||
assert error is null | ||
assert value is Characteristic.CurrentDoorState.CLOSED | ||
) | ||
|
||
it "should return STOPPED when postion is stopped", -> | ||
device._position = 'stopped' | ||
accessory.getService(Service.GarageDoorOpener) | ||
.getCharacteristic(Characteristic.CurrentDoorState) | ||
.getValue((error, value) -> | ||
assert error is null | ||
assert value is Characteristic.CurrentDoorState.STOPPED | ||
) | ||
|
||
describe "changing position", -> | ||
|
||
it "should change Characteristic.CurrentDoorState", -> | ||
device.firePositionChange('down') | ||
accessory.getService(Service.GarageDoorOpener) | ||
.getCharacteristic(Characteristic.CurrentDoorState) | ||
.getValue((error, value) -> | ||
assert error is null | ||
assert value is Characteristic.CurrentDoorState.CLOSED | ||
) | ||
|
||
describe "changing Characteristic.TargetDoorState", -> | ||
|
||
it "should call moveUp() when set to OPEN", -> | ||
accessory.getService(Service.GarageDoorOpener) | ||
.getCharacteristic(Characteristic.TargetDoorState) | ||
.setValue(Characteristic.TargetDoorState.OPEN) | ||
assert device._position is 'up' | ||
|
||
it "should call moveDown() when set to CLOSED", -> | ||
accessory.getService(Service.GarageDoorOpener) | ||
.getCharacteristic(Characteristic.TargetDoorState) | ||
.setValue(Characteristic.TargetDoorState.CLOSED) | ||
assert device._position is 'down' | ||
|
||
it "should call stop() when set to OPEN twice", -> | ||
accessory.getService(Service.GarageDoorOpener) | ||
.getCharacteristic(Characteristic.TargetDoorState) | ||
.setValue(Characteristic.TargetDoorState.OPEN) | ||
accessory.getService(Service.GarageDoorOpener) | ||
.getCharacteristic(Characteristic.TargetDoorState) | ||
.setValue(Characteristic.TargetDoorState.OPEN) | ||
assert device._position is 'stopped' | ||
|
||
describe "getCurrentState", -> | ||
|
||
it "should return correct value", -> | ||
assert accessory.getCurrentState('up') is Characteristic.CurrentDoorState.OPEN | ||
assert accessory.getCurrentState('down') is Characteristic.CurrentDoorState.CLOSED | ||
assert accessory.getCurrentState('stopped') is Characteristic.CurrentDoorState.STOPPED | ||
|
||
it "should trow an error when used with wrong value", -> | ||
fn = () -> accessory.getCurrentState('bla') | ||
require('chai').expect(fn).to.throw(assert.AssertionError) | ||
|
||
describe "getTargetPosition", -> | ||
|
||
it "should return correct value", -> | ||
assert accessory.getTargetPosition(Characteristic.TargetDoorState.OPEN) is 'up' | ||
assert accessory.getTargetPosition(Characteristic.TargetDoorState.CLOSED) is 'down' |