Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge devices, keep name and room #1575

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ function handleMessage(topic, message) {
}
// Device secondary features
case 'STATUS8': {
let device = this.pendingDevices[deviceExternalId];
const device = this.pendingDevices[deviceExternalId];
if (device) {
this.subStatus(device, message);
device = this.tasmotaHandler.mergeWithExistingDevice(device);

this.discoveredDevices[deviceExternalId] = device;
delete this.pendingDevices[deviceExternalId];
Expand Down
14 changes: 2 additions & 12 deletions server/services/tasmota/lib/tasmota.mergeWithExistingDevice.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { hasDeviceChanged } = require('../../../utils/device');
const { mergeDevices } = require('../../../utils/device');

/**
* @description Get all discovered devices, and if device already created, the Gladys device.
Expand All @@ -9,17 +9,7 @@ const { hasDeviceChanged } = require('../../../utils/device');
*/
function mergeWithExistingDevice(tasmotaDevice) {
const existing = this.gladys.stateManager.get('deviceByExternalId', tasmotaDevice.external_id);
if (existing) {
const device = { ...existing, ...tasmotaDevice };
const updatable = hasDeviceChanged(device, existing);
if (updatable) {
device.updatable = updatable;
}

return device;
}

return tasmotaDevice;
return mergeDevices(tasmotaDevice, existing);
}

module.exports = {
Expand Down
3 changes: 2 additions & 1 deletion server/services/tasmota/lib/tasmota.notifyNewDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const { EVENTS } = require('../../../utils/constants');
* notifyNewDevice(discorveredDevice)
*/
function notifyNewDevice(device, event) {
const payload = this.mergeWithExistingDevice(device);
this.gladys.event.emit(EVENTS.WEBSOCKET.SEND_ALL, {
type: event,
payload: device,
payload,
});
}

Expand Down
10 changes: 3 additions & 7 deletions server/services/zigbee2mqtt/lib/getDiscoveredDevices.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { hasDeviceChanged } = require('../../../utils/device');
const { mergeDevices } = require('../../../utils/device');
const { convertDevice } = require('../utils/convertDevice');

/**
Expand All @@ -13,13 +13,9 @@ function getDiscoveredDevices() {
// Convert to Gladys device
.map((d) => convertDevice(d, this.serviceId))
.map((d) => {
// Check if updatable
const existingDevice = this.gladys.stateManager.get('deviceByExternalId', d.external_id);
const device = { ...(existingDevice || {}), ...d };
if (existingDevice) {
device.updatable = hasDeviceChanged(device, existingDevice);
}
return device;
// Merge with existing device.
return mergeDevices(d, existingDevice);
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const gladys = {
event: {
emit: fake.returns(null),
},
stateManager: {
get: fake.returns(null),
},
};
const serviceId = 'service-uuid-random';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const gladys = {
event: {
emit: fake.returns(null),
},
stateManager: {
get: fake.returns(null),
},
};
const serviceId = 'service-uuid-random';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const existingDevice = {
external_id: 'alreadyExists',
name: 'alreadyExists',
model: 'sonoff-basic',
room_id: 'room_id',
features: [
{
name: 'feature 1',
Expand Down Expand Up @@ -57,13 +58,10 @@ describe('Tasmota - MQTT - getDiscoveredDevices', () => {
});

it('discovered already in Gladys', () => {
protocolHandler.discoveredDevices.alreadyExists = {
external_id: 'alreadyExists',
model: 'sonoff-basic',
};
protocolHandler.discoveredDevices.alreadyExists = existingDevice;
const result = tasmotaHandler.getDiscoveredDevices(protocol);
expect(result).to.be.lengthOf(1);
expect(result).deep.eq([existingDevice]);
expect(result).deep.eq([{ ...existingDevice, updatable: false }]);
});

it('discovered already in Gladys, but updated (basic to pow)', () => {
Expand All @@ -82,6 +80,7 @@ describe('Tasmota - MQTT - getDiscoveredDevices', () => {
type: 'type 2',
category: 'category 2',
external_id: 'external_id:2',
min: 10,
},
{
name: 'feature 3',
Expand All @@ -98,6 +97,7 @@ describe('Tasmota - MQTT - getDiscoveredDevices', () => {
external_id: 'alreadyExists',
model: 'sonoff-pow',
name: 'alreadyExists',
room_id: 'room_id',
features: [
{
name: 'feature 1',
Expand All @@ -106,10 +106,11 @@ describe('Tasmota - MQTT - getDiscoveredDevices', () => {
external_id: 'external_id:1',
},
{
name: 'feature 2 bis',
name: 'feature 2',
type: 'type 2',
category: 'category 2',
external_id: 'external_id:2',
min: 10,
},
{
name: 'feature 3',
Expand All @@ -118,10 +119,10 @@ describe('Tasmota - MQTT - getDiscoveredDevices', () => {
external_id: 'external_id:3',
},
],
updatable: true,
params: [],
};
expectedDevice.updatable = true;
expectedDevice.name = 'alreadyExists';

expect(result).deep.eq([expectedDevice]);
});

Expand Down Expand Up @@ -152,13 +153,10 @@ describe('Tasmota - HTTP - getDiscoveredDevices', () => {
});

it('discovered already in Gladys', () => {
protocolHandler.discoveredDevices.alreadyExists = {
external_id: 'alreadyExists',
model: 'sonoff-basic',
};
protocolHandler.discoveredDevices.alreadyExists = existingDevice;
const result = tasmotaHandler.getDiscoveredDevices(protocol);
expect(result).to.be.lengthOf(1);
expect(result).deep.eq([existingDevice]);
expect(result).deep.eq([{ ...existingDevice, updatable: false }]);
});

it('discovered already in Gladys, but updated (basic to pow)', () => {
Expand All @@ -177,6 +175,7 @@ describe('Tasmota - HTTP - getDiscoveredDevices', () => {
type: 'type 2',
category: 'category 2',
external_id: 'external_id:2',
min: 0,
},
{
name: 'feature 3',
Expand All @@ -193,6 +192,8 @@ describe('Tasmota - HTTP - getDiscoveredDevices', () => {
external_id: 'alreadyExists',
model: 'sonoff-pow',
name: 'alreadyExists',
updatable: true,
room_id: 'room_id',
features: [
{
name: 'feature 1',
Expand All @@ -201,10 +202,11 @@ describe('Tasmota - HTTP - getDiscoveredDevices', () => {
external_id: 'external_id:1',
},
{
name: 'feature 2 bis',
name: 'feature 2',
type: 'type 2',
category: 'category 2',
external_id: 'external_id:2',
min: 0,
},
{
name: 'feature 3',
Expand All @@ -215,8 +217,7 @@ describe('Tasmota - HTTP - getDiscoveredDevices', () => {
],
params: [],
};
expectedDevice.updatable = true;
expectedDevice.name = 'alreadyExists';

expect(result).deep.eq([expectedDevice]);
});

Expand Down Expand Up @@ -253,6 +254,8 @@ describe('Tasmota - HTTP - getDiscoveredDevices', () => {
external_id: 'alreadyExists',
name: 'alreadyExists',
model: 'sonoff-basic',
updatable: true,
room_id: 'room_id',
features: [
{
name: 'feature 1',
Expand All @@ -274,8 +277,7 @@ describe('Tasmota - HTTP - getDiscoveredDevices', () => {
},
],
};
expectedDevice.updatable = true;
expectedDevice.name = 'alreadyExists';

expect(result).deep.eq([expectedDevice]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('zigbee2mqtt getDiscoveredDevices', () => {
// PREPARE
gladys.stateManager.get
.onFirstCall()
.returns(true)
.returns({ room_id: 'room_id', name: 'device-name' })
.onSecondCall()
.returns(false)
.onThirdCall()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ describe('zigbee2mqtt handleMqttMessage', () => {
stateManagerGetStub = sinon.stub();
stateManagerGetStub
.onFirstCall()
.returns(true)
.returns({ room_id: 'room_id', name: 'device-name' })
.onSecondCall()
.returns(false)
.returns(null)
.onThirdCall()
.returns(false);
.returns(null);
zigbee2mqttManager.gladys.stateManager.get = stateManagerGetStub;
// EXECUTE
await zigbee2mqttManager.handleMqttMessage('zigbee2mqtt/bridge/devices', JSON.stringify(zigbeeDevices));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
}
],
"model": "WXKG01LM",
"name": "0x00158d00033e88d5",
"name": "device-name",
"room_id": "room_id",
"service_id": "f87b7af2-ca8e-44fc-b754-444354b42fee",
"should_poll": false,
"updatable": true
Expand Down
Loading