Skip to content

Commit

Permalink
Sonoff: fix message handler (GladysAssistant#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato authored and Pierre-Gilles committed Oct 28, 2019
1 parent 58788bb commit 2793376
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const SensorDeviceType = ({ children, ...props }) => (
{props.deviceFeature.category === 'humidity-sensor' && <i class="fe fe-droplet" />}
{props.deviceFeature.category === 'light-sensor' && <i class="fe fe-sun" />}
{props.deviceFeature.category === 'battery-sensor' && <i class="fe fe-percent" />}
{props.deviceFeature.category === 'switch' && props.deviceFeature.type === 'power' && <i class="fe fe-zap" />}
{OPEN_CLOSE_SENSORS.indexOf(props.deviceFeature.category) !== -1 && <i class="fe fe-home" />}
{props.deviceFeature.category === null && <i class="fe fe-bar-chart-2" />}
</td>
Expand Down
18 changes: 13 additions & 5 deletions server/services/sonoff/lib/handleMqttMessage.js
Original file line number Diff line number Diff line change
@@ -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');

/**
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions server/test/services/sonoff/lib/handleMqttMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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,
};

Expand All @@ -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,
};

Expand All @@ -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,
};

Expand All @@ -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,
};

Expand All @@ -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,
};

Expand All @@ -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,
};

Expand All @@ -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,
};

Expand Down

0 comments on commit 2793376

Please sign in to comment.