Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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