Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
337 additions
and
87 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const { setLightValue } = require('./light.setValue'); | ||
const { setSwitchValue } = require('./switch.setValue'); | ||
|
||
module.exports = { | ||
setLightValue, | ||
setSwitchValue, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { BadParameters } = require('../../../../utils/coreErrors'); | ||
const { DEVICE_FEATURE_TYPES } = require('../../../../utils/constants'); | ||
|
||
/** | ||
* @description Set value for light device. | ||
* @param {Object} feature - Feature. | ||
* @param {string|number} value - New value. | ||
* @returns {Object} A pair object containing command topic and value to emit. | ||
* @example | ||
* setLightValue(feature); | ||
*/ | ||
function setLightValue(feature, value) { | ||
switch (feature.type) { | ||
case DEVICE_FEATURE_TYPES.LIGHT.BINARY: { | ||
return { topic: 'power', value: value ? 'ON' : 'OFF' }; | ||
} | ||
case DEVICE_FEATURE_TYPES.LIGHT.COLOR: { | ||
return { | ||
topic: 'color', | ||
value: `#${Number(value) | ||
.toString(16) | ||
.padStart(6, '0')}`, | ||
}; | ||
} | ||
case DEVICE_FEATURE_TYPES.LIGHT.BRIGHTNESS: { | ||
return { | ||
topic: 'dimmer', | ||
value, | ||
}; | ||
} | ||
default: | ||
throw new BadParameters(`Sonoff device type not managed to set value on "${feature.external_id}"`); | ||
} | ||
} | ||
|
||
module.exports = { | ||
setLightValue, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const { BadParameters } = require('../../../../utils/coreErrors'); | ||
const { DEVICE_FEATURE_TYPES } = require('../../../../utils/constants'); | ||
|
||
/** | ||
* @description Set value for switch device. | ||
* @param {Object} feature - Feature. | ||
* @param {string|number} value - New value. | ||
* @returns {Object} A pair object containing command topic and value to emit. | ||
* @example | ||
* setSwitchValue(feature); | ||
*/ | ||
function setSwitchValue(feature, value) { | ||
switch (feature.type) { | ||
case DEVICE_FEATURE_TYPES.SWITCH.BINARY: { | ||
return { topic: 'power', value: value ? 'ON' : 'OFF' }; | ||
} | ||
default: | ||
throw new BadParameters(`Sonoff device type not managed to set value on "${feature.external_id}"`); | ||
} | ||
} | ||
|
||
module.exports = { | ||
setSwitchValue, | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { status } = require('./status'); | ||
const { state } = require('./state'); | ||
const { sensor } = require('./sensor'); | ||
const { power } = require('./power'); | ||
|
||
module.exports = { | ||
status, | ||
state, | ||
sensor, | ||
power, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../../../../utils/constants'); | ||
|
||
/** | ||
* @description Handle Tasmota 'stat/+/POWER' topics. | ||
* @param {string} deviceExternalId - Device external id. | ||
* @param {string} message - MQTT message. | ||
* @param {string} eventType - MQTT topic. | ||
* @param {Array} events - Resulting events. | ||
* @example | ||
* power('sonoff:sonoff-plug', '{"key": "value"}', 'POWER3', []); | ||
*/ | ||
function power(deviceExternalId, message, eventType, events) { | ||
let switchNo = eventType.replace('POWER', ''); | ||
if (switchNo.length > 0) { | ||
switchNo = `:${switchNo}`; | ||
} | ||
|
||
events.push({ | ||
device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_CATEGORIES.SWITCH}:${DEVICE_FEATURE_TYPES.SWITCH.BINARY}${switchNo}`, | ||
state: message === 'ON' ? 1 : 0, | ||
}); | ||
} | ||
|
||
module.exports = { | ||
power, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../../../../utils/constants'); | ||
|
||
/** | ||
* @description Handle Tasmota 'stat/+/SENSOR' topics. | ||
* @param {string} deviceExternalId - Device external id. | ||
* @param {string} message - MQTT message. | ||
* @param {Array} events - Resulting events. | ||
* @example | ||
* sensor('sonoff:sonoff-plug', '{"key": "value"}', []); | ||
*/ | ||
function sensor(deviceExternalId, message, events) { | ||
const sensorMsg = JSON.parse(message); | ||
|
||
const energyMsg = sensorMsg.ENERGY; | ||
if (energyMsg) { | ||
if (energyMsg.Current) { | ||
events.push({ | ||
device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_CATEGORIES.SWITCH}:${DEVICE_FEATURE_TYPES.SWITCH.ENERGY}`, | ||
state: energyMsg.Current, | ||
}); | ||
} | ||
|
||
if (energyMsg.Power) { | ||
events.push({ | ||
device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_CATEGORIES.SWITCH}:${DEVICE_FEATURE_TYPES.SWITCH.POWER}`, | ||
state: energyMsg.Power / 1000, | ||
}); | ||
} | ||
|
||
if (energyMsg.Voltage) { | ||
events.push({ | ||
device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_CATEGORIES.SWITCH}:${DEVICE_FEATURE_TYPES.SWITCH.VOLTAGE}`, | ||
state: energyMsg.Voltage, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
sensor, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../../../../utils/constants'); | ||
|
||
/** | ||
* @description Handle Tasmota 'stat/+/STATE' or 'stat/+/RESULT' topics. | ||
* @param {string} deviceExternalId - Device external id. | ||
* @param {string} message - MQTT message. | ||
* @param {Array} events - Resulting events. | ||
* @example | ||
* state('sonoff:sonoff-plug', '{"key": "value"}', []); | ||
*/ | ||
function state(deviceExternalId, message, events) { | ||
const stateMsg = JSON.parse(message); | ||
const stateValue = stateMsg.POWER; | ||
|
||
events.push({ | ||
device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_CATEGORIES.SWITCH}:${DEVICE_FEATURE_TYPES.SWITCH.BINARY}`, | ||
state: stateValue === 'ON' ? 1 : 0, | ||
}); | ||
} | ||
|
||
module.exports = { | ||
state, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const logger = require('../../../../utils/logger'); | ||
const { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../../../../utils/constants'); | ||
const models = require('../../models'); | ||
|
||
/** | ||
* @description Handle Tasmota 'stat/+/STATUS' topics. | ||
* @param {string} deviceExternalId - Device external id. | ||
* @param {string} message - MQTT message. | ||
* @param {Array} events - Resulting events. | ||
* @param {Object} sonoffHandler - Sonoff handler. | ||
* @example | ||
* status('sonoff:sonoff-plug', '{"key": "value"}', [], {}); | ||
*/ | ||
function status(deviceExternalId, message, events, sonoffHandler) { | ||
const statusMsg = JSON.parse(message); | ||
const statusValue = statusMsg.Status.Power; | ||
const friendlyName = statusMsg.Status.FriendlyName[0]; | ||
const moduleId = statusMsg.Status.Module; | ||
|
||
const model = models[moduleId]; | ||
if (model) { | ||
sonoffHandler[deviceExternalId] = { | ||
name: friendlyName, | ||
external_id: `sonoff:${deviceExternalId}`, | ||
features: model.getFeatures(), | ||
model: model.getModel(), | ||
service_id: this.serviceId, | ||
should_poll: false, | ||
}; | ||
|
||
events.push({ | ||
device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_CATEGORIES.SWITCH}:${DEVICE_FEATURE_TYPES.SWITCH.BINARY}`, | ||
state: statusValue, | ||
}); | ||
} else { | ||
logger.warn(`MQTT : Sonoff model ${moduleId} (${friendlyName}) not managed`); | ||
} | ||
} | ||
|
||
module.exports = { | ||
status, | ||
}; |
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
Oops, something went wrong.