Last active
December 29, 2020 15:02
-
-
Save nayelyzarazua-bluetrail/1496bc1a58cdc52a5968ad5e9207deb0 to your computer and use it in GitHub Desktop.
ST_SchemaConnector_CommandResponse
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
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