From d1cf9943ff3b636c7080a428af2e8a1ecd0ff9fa Mon Sep 17 00:00:00 2001 From: atrovato Date: Sat, 26 Oct 2019 00:51:07 +0200 Subject: [PATCH] Sonoff: fix message handler --- .../device-features/SensorDeviceFeature.jsx | 1 + .../services/sonoff/lib/handleMqttMessage.js | 18 +++++++++++++----- .../sonoff/lib/handleMqttMessage.test.js | 16 ++++++++-------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/front/src/components/boxs/device-in-room/device-features/SensorDeviceFeature.jsx b/front/src/components/boxs/device-in-room/device-features/SensorDeviceFeature.jsx index 2c145862f8..142deaf958 100644 --- a/front/src/components/boxs/device-in-room/device-features/SensorDeviceFeature.jsx +++ b/front/src/components/boxs/device-in-room/device-features/SensorDeviceFeature.jsx @@ -9,6 +9,7 @@ const SensorDeviceType = ({ children, ...props }) => ( {props.deviceFeature.category === 'humidity-sensor' && } {props.deviceFeature.category === 'light-sensor' && } {props.deviceFeature.category === 'battery-sensor' && } + {props.deviceFeature.category === 'switch' && props.deviceFeature.type === 'power' && } {OPEN_CLOSE_SENSORS.indexOf(props.deviceFeature.category) !== -1 && } {props.deviceFeature.category === null && } diff --git a/server/services/sonoff/lib/handleMqttMessage.js b/server/services/sonoff/lib/handleMqttMessage.js index b279f07684..4eca8d5a7d 100644 --- a/server/services/sonoff/lib/handleMqttMessage.js +++ b/server/services/sonoff/lib/handleMqttMessage.js @@ -1,5 +1,5 @@ const logger = require('../../../utils/logger'); -const { EVENTS, DEVICE_FEATURE_TYPES } = require('../../../utils/constants'); +const { EVENTS, DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../../../utils/constants'); const models = require('../models'); /** @@ -19,7 +19,9 @@ function handleMqttMessage(topic, message) { // Power status case 'POWER': { event = { - device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_TYPES.SWITCH.BINARY}`, + device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_CATEGORIES.SWITCH}:${ + DEVICE_FEATURE_TYPES.SWITCH.BINARY + }`, state: message === 'ON' ? 1 : 0, }; break; @@ -29,7 +31,9 @@ function handleMqttMessage(topic, message) { const sensorMsg = JSON.parse(message); event = { - device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_TYPES.SWITCH.POWER}`, + device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_CATEGORIES.SWITCH}:${ + DEVICE_FEATURE_TYPES.SWITCH.POWER + }`, state: sensorMsg.ENERGY.Current, }; break; @@ -53,7 +57,9 @@ function handleMqttMessage(topic, message) { }; event = { - device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_TYPES.SWITCH.BINARY}`, + device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_CATEGORIES.SWITCH}:${ + DEVICE_FEATURE_TYPES.SWITCH.BINARY + }`, state: statusValue, }; } else { @@ -69,7 +75,9 @@ function handleMqttMessage(topic, message) { const stateValue = stateMsg.POWER; event = { - device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_TYPES.SWITCH.BINARY}`, + device_feature_external_id: `sonoff:${deviceExternalId}:${DEVICE_FEATURE_CATEGORIES.SWITCH}:${ + DEVICE_FEATURE_TYPES.SWITCH.BINARY + }`, state: stateValue === 'ON' ? 1 : 0, }; break; diff --git a/server/test/services/sonoff/lib/handleMqttMessage.test.js b/server/test/services/sonoff/lib/handleMqttMessage.test.js index ba2b1e8cf8..73b30d4cda 100644 --- a/server/test/services/sonoff/lib/handleMqttMessage.test.js +++ b/server/test/services/sonoff/lib/handleMqttMessage.test.js @@ -30,7 +30,7 @@ describe('Mqtt handle message', () => { sonoffHandler.handleMqttMessage('stat/my_device/POWER', 'ON'); const expectedEvent = { - device_feature_external_id: `sonoff:my_device:binary`, + device_feature_external_id: `sonoff:my_device:switch:binary`, state: 1, }; @@ -42,7 +42,7 @@ describe('Mqtt handle message', () => { sonoffHandler.handleMqttMessage('stat/my_device/POWER', 'OFF'); const expectedEvent = { - device_feature_external_id: `sonoff:my_device:binary`, + device_feature_external_id: `sonoff:my_device:switch:binary`, state: 0, }; @@ -54,7 +54,7 @@ describe('Mqtt handle message', () => { sonoffHandler.handleMqttMessage('tele/my_device/SENSOR', '{ "ENERGY": { "Current": 125 }}'); const expectedEvent = { - device_feature_external_id: `sonoff:my_device:power`, + device_feature_external_id: `sonoff:my_device:switch:power`, state: 125, }; @@ -76,7 +76,7 @@ describe('Mqtt handle message', () => { ); const expectedEvent = { - device_feature_external_id: `sonoff:my_device:binary`, + device_feature_external_id: `sonoff:my_device:switch:binary`, state: 1, }; @@ -88,7 +88,7 @@ describe('Mqtt handle message', () => { sonoffHandler.handleMqttMessage('stat/my_device/STATE', '{ "POWER": "ON"}'); const expectedEvent = { - device_feature_external_id: `sonoff:my_device:binary`, + device_feature_external_id: `sonoff:my_device:switch:binary`, state: 1, }; @@ -100,7 +100,7 @@ describe('Mqtt handle message', () => { sonoffHandler.handleMqttMessage('stat/my_device/STATE', '{ "POWER": "OFF"}'); const expectedEvent = { - device_feature_external_id: `sonoff:my_device:binary`, + device_feature_external_id: `sonoff:my_device:switch:binary`, state: 0, }; @@ -112,7 +112,7 @@ describe('Mqtt handle message', () => { sonoffHandler.handleMqttMessage('stat/my_device/RESULT', '{ "POWER": "ON"}'); const expectedEvent = { - device_feature_external_id: `sonoff:my_device:binary`, + device_feature_external_id: `sonoff:my_device:switch:binary`, state: 1, }; @@ -124,7 +124,7 @@ describe('Mqtt handle message', () => { sonoffHandler.handleMqttMessage('stat/my_device/RESULT', '{ "POWER": "OFF"}'); const expectedEvent = { - device_feature_external_id: `sonoff:my_device:binary`, + device_feature_external_id: `sonoff:my_device:switch:binary`, state: 0, };