Skip to content

Instantly share code, notes, and snippets.

@nayelyzarazua-bluetrail
Last active December 29, 2020 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nayelyzarazua-bluetrail/1496bc1a58cdc52a5968ad5e9207deb0 to your computer and use it in GitHub Desktop.
Save nayelyzarazua-bluetrail/1496bc1a58cdc52a5968ad5e9207deb0 to your computer and use it in GitHub Desktop.
ST_SchemaConnector_CommandResponse
const {SchemaConnector, DeviceErrorTypes} = require('st-schema');
const deviceStates = { switch: 'off', presence: 'not present'}//1-10
const connector = new SchemaConnector()
.enableEventLogging(2)
.discoveryHandler((accessToken, response) => {
const d = response.addDevice('virtualPresenceSensor', 'Virtual Presence Sensor', 'device-profile-id')
//Device manufacturer
d.manufacturerName('xxxx');
//Device model
d.modelName('xxxxx');
})
.stateRefreshHandler((accessToken, response) => {
response.addDevice('virtualPresenceSensor', [
{
component: 'main',
capability: 'st.switch',
attribute: 'switch',
value: deviceStates.switch
},
{
component: 'main',
capability: 'st.presenceSensor',
attribute: 'presence',
value: deviceStates.presence
}
])
})
.commandHandler((accessToken, response, devices) => {
for (const device of devices) {
const deviceResponse = response.addDevice(device.externalDeviceId);
for (const cmd of device.commands) {
const state = {
component: cmd.component,
capability: cmd.capability
};
const state2 = {
component: cmd.component
};
if (cmd.capability === 'st.switch') {
state.attribute = 'switch';
state.value = deviceStates.switch = cmd.command === 'on' ? 'on' : 'off';
deviceResponse.addState(state);
state2.capability = 'st.presenceSensor'
state2.attribute = 'presence';
state2.value = deviceStates.presence = cmd.command === 'on' ? 'present' : 'not present';
deviceResponse.addState(state2);
} else {
deviceResponse.setError(
`Command '${cmd.command} of capability '${cmd.capability}' not supported`,
DeviceErrorTypes.CAPABILITY_NOT_SUPPORTED)
}
}
}
});
module.exports = connector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment