Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property Response Improvements #138

Merged
merged 2 commits into from
Feb 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
.lambda/
config.js
/nbproject/
.vscode
Expand Down Expand Up @@ -50,4 +51,3 @@ jspm_packages

# Yarn Integrity file
.yarn-integrity

224 changes: 137 additions & 87 deletions USAGE.md

Large diffs are not rendered by default.

28 changes: 13 additions & 15 deletions alexaCapabilities.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
* Copyright (c) 2014-2019 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand Down Expand Up @@ -95,29 +95,33 @@ AlexaCapabilities.prototype.percentageController = function () {
};
};

AlexaCapabilities.prototype.thermostatController = function (targetSetpoint, upperSetpoint, lowerSetpoint, thermostatMode, supportedModes) {
AlexaCapabilities.prototype.thermostatController = function (targetSetpoint, upperSetpoint, lowerSetpoint, thermostatMode) {
var supported = [];
if (targetSetpoint) {
supported.push({
"name": "targetSetpoint"
})
});
}
if (upperSetpoint) {
supported.push({
"name": "upperSetpoint"
})
});
}
if (lowerSetpoint) {
supported.push({
"name": "lowerSetpoint"
})
});
}
if (thermostatMode) {
supported.push({
"name": "thermostatMode"
})
});
}
var configuration = {};
if (typeof thermostatMode.parameters.supportedModes === 'string') {
jsetton marked this conversation as resolved.
Show resolved Hide resolved
configuration.supportedModes = thermostatMode.parameters.supportedModes.split(',').map(mode => mode.trim());
}
var controller = {
return {
capabilities: {
"type": "AlexaInterface",
"interface": "Alexa.ThermostatController",
Expand All @@ -126,19 +130,13 @@ AlexaCapabilities.prototype.thermostatController = function (targetSetpoint, upp
"supported": supported,
"proactivelyReported": false,
"retrievable": true
}
},
"configuration": configuration
},
category: "THERMOSTAT"
};
if(supportedModes){
controller.capabilities.configuration = {
supportedModes : supportedModes.split(',').map(mode => mode.trim())
};
}
return controller;
};


AlexaCapabilities.prototype.temperatureSensor = function () {
return {
capabilities: {
Expand Down
163 changes: 62 additions & 101 deletions alexaContextProperties.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2016 by the respective copyright holders.
* Copyright (c) 2014-2019 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -8,7 +8,7 @@
*/

/**
* Amazon Smart Home Skill Controller Properties for API V3
* Amazon Smart Home Skill Context Properties for API V3
*/
var utils = require('./utils.js');

Expand Down Expand Up @@ -95,12 +95,10 @@ AlexaContextProperties.prototype.colorStateProperty = function (state) {
}

/**
* Returns a property response for color temperature ndpoints
* Returns a property response for color temperature endpoints
* @param {integer} state
* @param {string} type
*/
AlexaContextProperties.prototype.colorTemperatureStateProperty = function (state, type) {
state = utils.normalizeColorTemperature(state, type);
return this.generateProperty('Alexa.ColorTemperatureController', 'colorTemperatureInKelvin', parseInt(state));
}

Expand Down Expand Up @@ -163,8 +161,7 @@ AlexaContextProperties.prototype.temperatureSensorStateProperty = function (stat
* @param {string} state
*/
AlexaContextProperties.prototype.lockStateProperty = function (state) {
var locked = state === "ON" ? "LOCKED" : state === "OFF" ? "UNLOCKED" : "JAMMED";
return this.generateProperty('Alexa.LockController', 'lockState', locked);
return this.generateProperty('Alexa.LockController', 'lockState', state);
}

/**
Expand Down Expand Up @@ -229,135 +226,99 @@ AlexaContextProperties.prototype.generateProperty = function (namespace, name, v
}

/**
* Given an array of items (name,state only) and a Alexa cookies object, generate a property response for all
* endpoints listed in the propertyMap
* Given an array of capability interfaces and Alexa property map object, generate a property response list
*
* @param {array} items
* @param {array} interfaceNames
* @param {object} propertyMap
*/
AlexaContextProperties.prototype.propertiesResponseForItems = function (items, propertyMap) {
AlexaContextProperties.prototype.propertiesResponseForInterfaces = function (interfaceNames, propertyMap) {
var self = this;
var properties = [];
var response = [];

function itemByName(itemName) {
return items.find(item => item.name === itemName);
}

Object.keys(propertyMap).forEach(function (groupName) {
var item;
var group = propertyMap[groupName];
switch (groupName) {
interfaceNames.forEach(function (interfaceName) {
var properties = propertyMap[interfaceName];
switch (interfaceName) {
case "PowerController": //Switch, Dimmer [Switchable]
item = itemByName(group.powerState.itemName);
if (item) {
properties.push(self.powerStateProperty(item.state));
}
var item = properties.powerState.item;
response.push(self.powerStateProperty(item.state));
break;
case "PowerLevelController": //Dimmer or Number, Rollershutter [Lighting]
item = itemByName(group.powerLevel.itemName);
if (item) {
properties.push(self.powerLevelStateProperty(item.state));
}
var item = properties.powerLevel.item;
response.push(self.powerLevelStateProperty(item.state));
break;
case "BrightnessController":
item = itemByName(group.brightness.itemName);
if (item) {
properties.push(self.brightnessStateProperty(item.state));
}
var item = properties.brightness.item;
response.push(self.brightnessStateProperty(item.state));
break;
case "PercentageController":
item = itemByName(group.percentage.itemName);
if (item) {
properties.push(self.percentageStateProperty(item.state));
}
var item = properties.percentage.item;
response.push(self.percentageStateProperty(item.state));
break;
case "ColorController": //Color [Lighting]
item = itemByName(group.color.itemName);
if (item) {
properties.push(self.colorStateProperty(item.state));
}
var item = properties.color.item;
response.push(self.colorStateProperty(item.state));
break;
case "ColorTemperatureController": //Dimmer or Number
item = itemByName(group.colorTemperatureInKelvin.itemName);
if(item){
properties.push(self.colorTemperatureStateProperty(item.state, item.type));
}
var item = properties.colorTemperatureInKelvin.item;
var state = utils.normalizeColorTemperature(item.state, item.type);
response.push(self.colorTemperatureStateProperty(state));
break;
case "ThermostatController": //Group [Thermostat]
if (group.targetSetpoint) {
item = itemByName(group.targetSetpoint.itemName);
if (item) {
var scale = group.targetSetpoint.parameters.scale ?
group.targetSetpoint.parameters.scale.toUpperCase() : "CELSIUS";
properties.push(self.targetSetpointStateProperty(item.state, scale));
}
if (properties.targetSetpoint) {
var item = properties.targetSetpoint.item;
var scale = properties.targetSetpoint.parameters.scale ?
properties.targetSetpoint.parameters.scale.toUpperCase() : "CELSIUS";
response.push(self.targetSetpointStateProperty(item.state, scale));
}
if (group.upperSetpoint) {
item = itemByName(group.upperSetpoint.itemName);
if (item) {
var scale = group.upperSetpoint.parameters.scale ?
group.upperSetpoint.parameters.scale.toUpperCase() : "CELSIUS";
properties.push(self.upperSetpointStateProperty(item.state, scale));
}
if (properties.upperSetpoint) {
var item = properties.upperSetpoint.item;
var scale = properties.upperSetpoint.parameters.scale ?
properties.upperSetpoint.parameters.scale.toUpperCase() : "CELSIUS";
response.push(self.upperSetpointStateProperty(item.state, scale));
}
if (group.lowerSetpoint) {
item = itemByName(group.lowerSetpoint.itemName);
if (item) {
var scale = group.lowerSetpoint.parameters.scale ?
group.lowerSetpoint.parameters.scale.toUpperCase() : "CELSIUS";
properties.push(self.lowerSetpointStateProperty(item.state, scale));
}
if (properties.lowerSetpoint) {
var item = properties.lowerSetpoint.item;
var scale = properties.lowerSetpoint.parameters.scale ?
properties.lowerSetpoint.parameters.scale.toUpperCase() : "CELSIUS";
response.push(self.lowerSetpointStateProperty(item.state, scale));
}
if (group.thermostatMode) {
item = itemByName(group.thermostatMode.itemName);
if (item) {
var state = utils.normalizeThermostatMode(item.state, group.thermostatMode.parameters);
properties.push(self.thermostatModeStateProperty(state));
}
if (properties.thermostatMode) {
var item = properties.thermostatMode.item;
var state = utils.normalizeThermostatMode(item.state, properties.thermostatMode.parameters);
response.push(self.thermostatModeStateProperty(state));
}
break;
case "TemperatureSensor":
item = itemByName(group.temperature.itemName);
if (item) {
var scale = group.temperature.parameters.scale ?
group.temperature.parameters.scale.toUpperCase() : "CELSIUS";
properties.push(self.temperatureSensorStateProperty(item.state, scale));
}
var item = properties.temperature.item;
var scale = properties.temperature.parameters.scale ?
properties.temperature.parameters.scale.toUpperCase() : "CELSIUS";
response.push(self.temperatureSensorStateProperty(item.state, scale));
break;
case "LockController": //Switch [Lock]
item = itemByName(group.lockState.itemName);
if (item) {
properties.push(self.lockStateProperty(item.state));
}
var item = properties.lockState.item;
var state = utils.normalizeLockState(item.state, item.type, properties.lockState.parameters);
response.push(self.lockStateProperty(state));
break;
case "ChannelController": //Number [Alexa@Channel]
item = itemByName(group.channel.itemName);
if (item){
properties.push(self.channelStateProperty(item.state));
}
var item = properties.channel.item;
response.push(self.channelStateProperty(item.state));
break;
case "InputController": //String [Alexa@Input]
item = itemByName(group.input.itemName);
if(item){
properties.push(self.inputStateProperty(item.state));
}
var item = properties.input.item;
response.push(self.inputStateProperty(item.state));
break;
case "PlaybackController": //Player or Group? [Alexa@Player]
break;
case "SceneController": //Switch ? [Scene]
break;
case "Speaker": //Group ? (volume dimmer, mute switch) [Alexa@Speaker]
if (group.muted) {
item = itemByName(group.muted.itemName);
if(item){
properties.push(self.speakerMutedStateProperty(item.state));
}
if (properties.muted) {
var item = properties.muted.item;
response.push(self.speakerMutedStateProperty(item.state));
}
if (group.volume) {
item = itemByName(group.volume.itemName);
if(item){
properties.push(self.speakerVolumeStateProperty(item.state));
}
if (properties.volume) {
var item = properties.volume.item;
response.push(self.speakerVolumeStateProperty(item.state));
}
break;
case "StepSpeaker": //Group ? (steup string, mute, not really sure) [Alexa@StepSpeaker]
Expand All @@ -368,8 +329,8 @@ AlexaContextProperties.prototype.propertiesResponseForItems = function (items, p
}
});

properties.push(this.endpointHealthProperty());
response.push(this.endpointHealthProperty());

return properties;
return response;
}
module.exports = new AlexaContextProperties();
Loading