Skip to content

Commit

Permalink
Restrict generic semantic mappings support
Browse files Browse the repository at this point in the history
Signed-off-by: jsetton <jeremy.setton@gmail.com>
  • Loading branch information
jsetton committed Feb 15, 2023
1 parent c1a6698 commit 387e764
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 26 deletions.
24 changes: 1 addition & 23 deletions lambda/alexa/smarthome/capabilities/capability.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,16 @@ export default class AlexaCapability {
getCapabilityInterface() {
// Define capability properties
const properties = this.getCapabilityProperties();
// Define capability resources
const capabilityResources = this.getCapabilityResources();
// Define capability configuration
const configuration = this.getCapabilityConfiguration();
// Define capability semantics
const semantics = this.getCapabilitySemantics();

return {
type: 'AlexaInterface',
interface: this.interface,
...(this.instance && { instance: this.instance }),
version: this.version,
...(properties.supported.length > 0 && { properties }),
...(Object.keys(capabilityResources).length > 0 && { capabilityResources }),
...(Object.keys(configuration).length > 0 && { configuration }),
...(Object.keys(semantics).length > 0 && { semantics })
...(Object.keys(configuration).length > 0 && { configuration })
};
}

Expand All @@ -195,14 +189,6 @@ export default class AlexaCapability {
};
}

/**
* Returns capability resources
* @return {Object}
*/
getCapabilityResources() {
return {};
}

/**
* Returns capability configuration
* @return {Object}
Expand All @@ -211,14 +197,6 @@ export default class AlexaCapability {
return {};
}

/**
* Returns capability semantics
* @return {Object}
*/
getCapabilitySemantics() {
return {};
}

/**
* Returns relationship
* @return {Object}
Expand Down
28 changes: 28 additions & 0 deletions lambda/alexa/smarthome/capabilities/genericController.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ export default class GenericController extends AlexaCapability {
return this.properties.every((property) => property.isReportable && property.isNonControllable);
}

/**
* Returns capability interface
* @return {Object}
*/
getCapabilityInterface() {
// Get capability configuration and interface from parent method
const { configuration = {}, ...capabilityInterface } = super.getCapabilityInterface();
// Define capability resources
const capabilityResources = this.getCapabilityResources();
// Define capability semantics
const semantics = this.getCapabilitySemantics();

return {
...capabilityInterface,
...(Object.keys(capabilityResources).length > 0 && { capabilityResources }),
...(Object.keys(configuration).length > 0 && { configuration }),
...(Object.keys(semantics).length > 0 && { semantics })
};
}

/**
* Returns capability properties
* @return {Object}
Expand All @@ -70,4 +90,12 @@ export default class GenericController extends AlexaCapability {
// Return capability resources object based on labels and language
return AlexaCapabilityResources.getResources(labels, language);
}

/**
* Returns capability semantics
* @return {Object}
*/
getCapabilitySemantics() {
return {};
}
}
6 changes: 4 additions & 2 deletions lambda/alexa/smarthome/properties/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,15 @@ export default class Generic extends DecoupleState {
// Upate non-controllable parameter using item state description read only property if not defined
parameters[Parameter.NON_CONTROLLABLE] = nonControllable ?? item.stateDescription?.readOnly;

const actionMappings = parameters[Parameter.ACTION_MAPPINGS] || {};
// Define action mappings using parameter values if property is controllable
const actionMappings = (!this.isNonControllable && parameters[Parameter.ACTION_MAPPINGS]) || {};
// Update action mappings parameter removing unsupported action semantics
parameters[Parameter.ACTION_MAPPINGS] = Object.entries(actionMappings)
.filter(([action]) => this.supportedActionSemantics.includes(action))
.reduce((actions, [action, mapping]) => ({ ...actions, [action]: mapping }), undefined);

const stateMappings = parameters[Parameter.STATE_MAPPINGS] || {};
// Define state mappings using parameter values if property is retrievable
const stateMappings = (this.isRetrievable && parameters[Parameter.STATE_MAPPINGS]) || {};
// Update state mappings parameter removing unsupported state semantics
parameters[Parameter.STATE_MAPPINGS] = Object.entries(stateMappings)
.filter(([state]) => this.supportedStateSemantics.includes(state))
Expand Down
55 changes: 54 additions & 1 deletion lambda/test/alexa/cases/discovery/other.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ export default {
}
}
},
{
type: 'Switch',
name: 'mode4',
label: 'Mode 4',
metadata: {
alexa: {
value: 'Mode',
config: {
supportedModes: 'OFF=@Value.Close,ON=@Value.Open',
retrievable: false,
// No state mappings support if not retrievable
stateMappings: 'Closed=OFF,Open=ON'
}
}
}
},
{
type: 'String',
name: 'mode99',
Expand Down Expand Up @@ -155,7 +171,9 @@ export default {
alexa: {
value: 'RangeValue',
config: {
nonControllable: true
nonControllable: true,
// No action mappings support if non controllable
actionMappings: 'Close=0'
}
}
}
Expand Down Expand Up @@ -511,6 +529,40 @@ export default {
}
]
},
mode4: {
capabilities: ['Alexa.ModeController:mode4', 'Alexa'],
displayCategories: ['OTHER'],
friendlyName: 'Mode 4',
propertyFlags: {}, // no property flags expected
resources: {
'Alexa.ModeController:mode4': {
friendlyNames: ['asset:Alexa.Setting.Mode']
}
},
configuration: {
'Alexa.ModeController:mode4': {
ordered: false,
supportedModes: {
OFF: { friendlyNames: ['asset:Alexa.Value.Close'] },
ON: { friendlyNames: ['asset:Alexa.Value.Open'] }
}
}
},
semantics: {}, // no semantics expected
cookie: [
{
name: 'ModeController',
instance: 'Mode:mode4',
property: 'mode',
parameters: {
capabilityNames: ['@Setting.Mode'],
supportedModes: { OFF: ['@Value.Close'], ON: ['@Value.Open'] },
retrievable: false
},
item: { name: 'mode4', type: 'Switch' }
}
]
},
range1: {
capabilities: [
'Alexa.ModeController:range1',
Expand Down Expand Up @@ -804,6 +856,7 @@ export default {
unitOfMeasure: 'Alexa.Unit.Angle.Degrees'
}
},
semantics: {}, // no semantics expected
cookie: [
{
name: 'RangeController',
Expand Down

0 comments on commit 387e764

Please sign in to comment.