-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tasmota: Manage distance sensor (#1019)
Fixes #1017
- Loading branch information
Showing
12 changed files
with
432 additions
and
5 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
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,30 @@ | ||
const { | ||
DEVICE_FEATURE_CATEGORIES, | ||
DEVICE_FEATURE_TYPES, | ||
DEVICE_FEATURE_UNITS, | ||
} = require('../../../../utils/constants'); | ||
|
||
module.exports = { | ||
// Tasmota matcher | ||
keyMatcher: /^(StatusSNS|Gladys)\.(SR04|VL53L0X)\.Distance$/, | ||
// Gladys feature | ||
generateFeature: (device, key, value, fullKey) => { | ||
const keyParts = fullKey.split('.'); | ||
const unit = keyParts[1] === 'VL53L0X' ? DEVICE_FEATURE_UNITS.MM : DEVICE_FEATURE_UNITS.CM; | ||
|
||
return { | ||
category: DEVICE_FEATURE_CATEGORIES.DISTANCE_SENSOR, | ||
type: DEVICE_FEATURE_TYPES.SENSOR.DECIMAL, | ||
name: 'Distance', | ||
read_only: true, | ||
has_feedback: false, | ||
min: 0, | ||
max: 10000, | ||
unit, | ||
}; | ||
}, | ||
generateExternalId: (key, fullKey) => { | ||
const keyParts = fullKey.split('.'); | ||
return `${keyParts[1]}:Distance`; | ||
}, | ||
}; |
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
43 changes: 43 additions & 0 deletions
43
server/test/services/tasmota/lib/device-creation/SR04.json
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,43 @@ | ||
{ | ||
"STATUS": { | ||
"Status": { | ||
"ButtonRetain": 0, | ||
"ButtonTopic": "0", | ||
"FriendlyName": ["Tasmota"], | ||
"LedMask": "FFFF", | ||
"LedState": 1, | ||
"Module": 18, | ||
"Power": 0, | ||
"PowerOnState": 1, | ||
"PowerRetain": 0, | ||
"SaveData": 1, | ||
"SaveState": 1, | ||
"SensorRetain": 0, | ||
"SwitchMode": [0, 0, 0, 0, 0, 0, 0, 0], | ||
"SwitchRetain": 0, | ||
"SwitchTopic": "0", | ||
"Topic": "tasmota" | ||
} | ||
}, | ||
"STATUS11": { | ||
"StatusSTS": { | ||
"MqttCount": 1, | ||
"Sleep": 50, | ||
"SleepMode": "Dynamic", | ||
"Wifi": { | ||
"AP": 1, | ||
"BSSId": "EC:BE:DD:85:1F:E0", | ||
"Channel": 1, | ||
"LinkCount": 1, | ||
"SSId": "ALEX-NETWORK" | ||
} | ||
} | ||
}, | ||
"STATUS8": { | ||
"StatusSNS": { | ||
"SR04": { | ||
"Distance": 3.65 | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
server/test/services/tasmota/lib/device-creation/VL53L0X.json
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,43 @@ | ||
{ | ||
"STATUS": { | ||
"Status": { | ||
"ButtonRetain": 0, | ||
"ButtonTopic": "0", | ||
"FriendlyName": ["Tasmota"], | ||
"LedMask": "FFFF", | ||
"LedState": 1, | ||
"Module": 18, | ||
"Power": 0, | ||
"PowerOnState": 1, | ||
"PowerRetain": 0, | ||
"SaveData": 1, | ||
"SaveState": 1, | ||
"SensorRetain": 0, | ||
"SwitchMode": [0, 0, 0, 0, 0, 0, 0, 0], | ||
"SwitchRetain": 0, | ||
"SwitchTopic": "0", | ||
"Topic": "tasmota" | ||
} | ||
}, | ||
"STATUS11": { | ||
"StatusSTS": { | ||
"MqttCount": 1, | ||
"Sleep": 50, | ||
"SleepMode": "Dynamic", | ||
"Wifi": { | ||
"AP": 1, | ||
"BSSId": "EC:BE:DD:85:1F:E0", | ||
"Channel": 1, | ||
"LinkCount": 1, | ||
"SSId": "ALEX-NETWORK" | ||
} | ||
} | ||
}, | ||
"STATUS8": { | ||
"StatusSNS": { | ||
"VL53L0X": { | ||
"Distance": 3.65 | ||
} | ||
} | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
...e-message/device-creation/tasmota.mqtt.handleMessage.deviceCreation-distance_SR04.test.js
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,143 @@ | ||
const sinon = require('sinon'); | ||
const { expect } = require('chai'); | ||
|
||
const { fake, assert } = sinon; | ||
const TasmotaHandler = require('../../../../../../../services/tasmota/lib'); | ||
const { | ||
DEVICE_FEATURE_CATEGORIES, | ||
DEVICE_FEATURE_TYPES, | ||
DEVICE_FEATURE_UNITS, | ||
EVENTS, | ||
WEBSOCKET_MESSAGE_TYPES, | ||
} = require('../../../../../../../utils/constants'); | ||
|
||
const messages = require('../../../device-creation/SR04.json'); | ||
|
||
const mqttService = { | ||
device: { | ||
publish: fake.returns(null), | ||
}, | ||
}; | ||
const gladys = { | ||
event: { | ||
emit: fake.returns(null), | ||
}, | ||
stateManager: { | ||
get: fake.returns(null), | ||
}, | ||
}; | ||
const serviceId = 'service-uuid-random'; | ||
|
||
describe('Tasmota - MQTT - create device with SR04 distance features', () => { | ||
const tasmota = new TasmotaHandler(gladys, serviceId); | ||
const tasmotaHandler = tasmota.protocols.mqtt; | ||
tasmotaHandler.mqttService = mqttService; | ||
|
||
beforeEach(() => { | ||
sinon.reset(); | ||
}); | ||
|
||
it('decode STATUS message', () => { | ||
tasmotaHandler.handleMessage('stat/tasmota-device-topic/STATUS', JSON.stringify(messages.STATUS)); | ||
|
||
expect(tasmotaHandler.discoveredDevices).to.deep.eq({}); | ||
expect(tasmotaHandler.pendingDevices).to.deep.eq({ | ||
'tasmota-device-topic': { | ||
name: 'Tasmota', | ||
params: [ | ||
{ | ||
name: 'protocol', | ||
value: 'mqtt', | ||
}, | ||
], | ||
model: 18, | ||
external_id: 'tasmota:tasmota-device-topic', | ||
selector: 'tasmota-tasmota-device-topic', | ||
service_id: serviceId, | ||
should_poll: false, | ||
features: [], | ||
}, | ||
}); | ||
|
||
assert.notCalled(gladys.event.emit); | ||
assert.notCalled(gladys.stateManager.get); | ||
assert.calledWith(mqttService.device.publish, 'cmnd/tasmota-device-topic/STATUS', '11'); | ||
}); | ||
|
||
it('decode STATUS11 message', () => { | ||
tasmotaHandler.handleMessage('stat/tasmota-device-topic/STATUS11', JSON.stringify(messages.STATUS11)); | ||
|
||
expect(tasmotaHandler.discoveredDevices).to.deep.eq({}); | ||
expect(tasmotaHandler.pendingDevices).to.deep.eq({ | ||
'tasmota-device-topic': { | ||
name: 'Tasmota', | ||
params: [ | ||
{ | ||
name: 'protocol', | ||
value: 'mqtt', | ||
}, | ||
], | ||
model: 18, | ||
external_id: 'tasmota:tasmota-device-topic', | ||
selector: 'tasmota-tasmota-device-topic', | ||
service_id: serviceId, | ||
should_poll: false, | ||
features: [], | ||
}, | ||
}); | ||
|
||
assert.notCalled(gladys.event.emit); | ||
assert.notCalled(gladys.stateManager.get); | ||
assert.calledWith(mqttService.device.publish, 'cmnd/tasmota-device-topic/STATUS', '8'); | ||
}); | ||
|
||
it('decode STATUS8 message', () => { | ||
tasmotaHandler.handleMessage('stat/tasmota-device-topic/STATUS8', JSON.stringify(messages.STATUS8)); | ||
|
||
const expectedDevice = { | ||
name: 'Tasmota', | ||
params: [ | ||
{ | ||
name: 'protocol', | ||
value: 'mqtt', | ||
}, | ||
], | ||
model: 18, | ||
external_id: 'tasmota:tasmota-device-topic', | ||
selector: 'tasmota-tasmota-device-topic', | ||
service_id: serviceId, | ||
should_poll: false, | ||
features: [ | ||
{ | ||
category: DEVICE_FEATURE_CATEGORIES.DISTANCE_SENSOR, | ||
type: DEVICE_FEATURE_TYPES.SENSOR.DECIMAL, | ||
unit: DEVICE_FEATURE_UNITS.CM, | ||
external_id: 'tasmota:tasmota-device-topic:SR04:Distance', | ||
selector: 'tasmota-tasmota-device-topic-sr04-distance', | ||
name: 'Distance', | ||
read_only: true, | ||
has_feedback: false, | ||
min: 0, | ||
max: 10000, | ||
last_value: 3.65, | ||
}, | ||
], | ||
}; | ||
expect(tasmotaHandler.discoveredDevices).to.deep.eq({ | ||
'tasmota-device-topic': expectedDevice, | ||
}); | ||
expect(tasmotaHandler.pendingDevices).to.deep.eq({}); | ||
|
||
assert.notCalled(mqttService.device.publish); | ||
assert.calledWith(gladys.stateManager.get, 'deviceByExternalId', 'tasmota:tasmota-device-topic'); | ||
|
||
assert.calledWith(gladys.event.emit, EVENTS.DEVICE.NEW_STATE, { | ||
device_feature_external_id: 'tasmota:tasmota-device-topic:SR04:Distance', | ||
state: 3.65, | ||
}); | ||
assert.calledWith(gladys.event.emit, EVENTS.WEBSOCKET.SEND_ALL, { | ||
type: WEBSOCKET_MESSAGE_TYPES.TASMOTA.NEW_MQTT_DEVICE, | ||
payload: expectedDevice, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.