Skip to content

Commit

Permalink
Release 1.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Luligu committed Jan 20, 2025
1 parent 9e5ff8a commit f691e70
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 65 deletions.
152 changes: 91 additions & 61 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,117 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Matterbridge, MatterbridgeDevice, MatterbridgeEndpoint, PlatformConfig } from 'matterbridge';
import { AnsiLogger } from 'matterbridge/logger';
import { AnsiLogger, LogLevel } from 'matterbridge/logger';
import { ShellyPlatform } from './platform.js';
import initializePlugin from './index';
import { jest } from '@jest/globals';
import { wait } from 'matterbridge/utils';

describe('initializePlugin', () => {
let mockMatterbridge: Matterbridge;
let mockLog: AnsiLogger;
let mockConfig: PlatformConfig;
let platform: ShellyPlatform;

const mockLog = {
fatal: jest.fn((message: string, ...parameters: any[]) => {
console.log('mockLog.fatal', message, parameters);
}),
error: jest.fn((message: string, ...parameters: any[]) => {
console.log('mockLog.error', message, parameters);
}),
warn: jest.fn((message: string, ...parameters: any[]) => {
console.log('mockLog.warn', message, parameters);
}),
notice: jest.fn((message: string, ...parameters: any[]) => {
console.log('mockLog.notice', message, parameters);
}),
info: jest.fn((message: string, ...parameters: any[]) => {
console.log('mockLog.info', message, parameters);
}),
debug: jest.fn((message: string, ...parameters: any[]) => {
console.log('mockLog.debug', message, parameters);
}),
} as unknown as AnsiLogger;

const mockMatterbridge = {
matterbridgeDirectory: './jest/matterbridge',
matterbridgePluginDirectory: './jest/plugins',
systemInformation: { ipv4Address: undefined, osRelease: 'xx.xx.xx.xx.xx.xx', nodeVersion: '22.1.10' },
matterbridgeVersion: '1.7.3',
edge: false,
log: mockLog,
getDevices: jest.fn(() => {
// console.log('getDevices called');
return [];
}),
getPlugins: jest.fn(() => {
// console.log('getPlugins called');
return [];
}),
addBridgedDevice: jest.fn(async (pluginName: string, device: MatterbridgeDevice) => {
// console.log('addBridgedDevice called');
}),
addBridgedEndpoint: jest.fn(async (pluginName: string, device: MatterbridgeEndpoint) => {
// console.log('addBridgedEndpoint called');
// await aggregator.add(device);
}),
removeBridgedDevice: jest.fn(async (pluginName: string, device: MatterbridgeDevice) => {
// console.log('removeBridgedDevice called');
}),
removeBridgedEndpoint: jest.fn(async (pluginName: string, device: MatterbridgeEndpoint) => {
// console.log('removeBridgedEndpoint called');
}),
removeAllBridgedDevices: jest.fn(async (pluginName: string) => {
// console.log('removeAllBridgedDevices called');
}),
removeAllBridgedEndpoints: jest.fn(async (pluginName: string) => {
// console.log('removeAllBridgedEndpoints called');
}),
} as unknown as Matterbridge;

const mockConfig = {
'name': 'matterbridge-shelly',
'type': 'DynamicPlatform',
'version': '1.1.2',
'username': 'admin',
'password': 'tango',
'exposeSwitch': 'outlet',
'exposeInput': 'contact',
'exposeInputEvent': 'momentary',
'exposePowerMeter': 'matter13',
'blackList': [],
'whiteList': [],
'enableMdnsDiscover': false,
'enableStorageDiscover': false,
'resetStorageDiscover': false,
'enableConfigDiscover': false,
'enableBleDiscover': false,
'deviceIp': {},
'debug': false,
'unregisterOnShutdown': false,
} as PlatformConfig;

const loggerLogSpy = jest.spyOn(AnsiLogger.prototype, 'log').mockImplementation((level: string, message: string, ...parameters: any[]) => {
console.log(`Mocked AnsiLogger.log: ${level} - ${message}`, ...parameters);
});

beforeEach(() => {
mockMatterbridge = {
matterbridgeDirectory: './jest/matterbridge',
matterbridgePluginDirectory: './jest/plugins',
systemInformation: { ipv4Address: undefined, osRelease: 'xx.xx.xx.xx.xx.xx', nodeVersion: '22.1.10' },
matterbridgeVersion: '1.7.2',
edge: false,
log: mockLog,
getDevices: jest.fn(() => {
// console.log('getDevices called');
return [];
}),
getPlugins: jest.fn(() => {
// console.log('getPlugins called');
return [];
}),
addBridgedDevice: jest.fn(async (pluginName: string, device: MatterbridgeDevice) => {
// console.log('addBridgedDevice called');
}),
addBridgedEndpoint: jest.fn(async (pluginName: string, device: MatterbridgeEndpoint) => {
// console.log('addBridgedEndpoint called');
// await aggregator.add(device);
}),
removeBridgedDevice: jest.fn(async (pluginName: string, device: MatterbridgeDevice) => {
// console.log('removeBridgedDevice called');
}),
removeBridgedEndpoint: jest.fn(async (pluginName: string, device: MatterbridgeEndpoint) => {
// console.log('removeBridgedEndpoint called');
}),
removeAllBridgedDevices: jest.fn(async (pluginName: string) => {
// console.log('removeAllBridgedDevices called');
}),
removeAllBridgedEndpoints: jest.fn(async (pluginName: string) => {
// console.log('removeAllBridgedEndpoints called');
}),
} as unknown as Matterbridge;
mockLog = { fatal: jest.fn(), error: jest.fn(), warn: jest.fn(), notice: jest.fn(), info: jest.fn(), debug: jest.fn() } as unknown as AnsiLogger;
mockConfig = {
'name': 'matterbridge-shelly',
'type': 'DynamicPlatform',
'version': '1.1.2',
'username': 'admin',
'password': 'tango',
'exposeSwitch': 'outlet',
'exposeInput': 'contact',
'exposeInputEvent': 'momentary',
'exposePowerMeter': 'matter13',
'blackList': [],
'whiteList': [],
'enableMdnsDiscover': false,
'enableStorageDiscover': false,
'resetStorageDiscover': false,
'enableConfigDiscover': false,
'enableBleDiscover': false,
'deviceIp': {},
'debug': false,
'unregisterOnShutdown': false,
} as PlatformConfig;
jest.clearAllMocks();
});

it('should return an instance of ShellyPlatform', () => {
platform = initializePlugin(mockMatterbridge, mockLog, mockConfig);
expect(platform).toBeDefined();
expect(platform).toBeInstanceOf(ShellyPlatform);
expect(mockLog.error).not.toHaveBeenCalled();
});

it('should shutdown the instance of ShellyPlatform', async () => {
expect(platform).toBeDefined();
expect(platform).toBeInstanceOf(ShellyPlatform);
await platform.onShutdown();
wait(1000);
expect(mockLog.info).toHaveBeenCalledWith(expect.stringContaining('Shutting down platform'));
expect(mockLog.error).toHaveBeenCalledWith('NodeStorage is not initialized');
(platform as any).shelly.destroy();
});
});
4 changes: 2 additions & 2 deletions src/platform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('ShellyPlatform', () => {
matterbridgeDirectory: './jest/matterbridge',
matterbridgePluginDirectory: './jest/plugins',
systemInformation: { ipv4Address: undefined, osRelease: 'xx.xx.xx.xx.xx.xx', nodeVersion: '22.1.10' },
matterbridgeVersion: '1.7.2',
matterbridgeVersion: '1.7.3',
edge: false,
log: mockLog,
getDevices: jest.fn(() => {
Expand Down Expand Up @@ -352,7 +352,7 @@ describe('ShellyPlatform', () => {
it('should throw because of version', () => {
mockMatterbridge.matterbridgeVersion = '1.5.4';
expect(() => new ShellyPlatform(mockMatterbridge, mockLog, mockConfig)).toThrow();
mockMatterbridge.matterbridgeVersion = '1.7.2';
mockMatterbridge.matterbridgeVersion = '1.7.3';
});

it('should call onStart with reason and start mDNS', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ export class ShellyPlatform extends MatterbridgeDynamicPlatform {
super(matterbridge, log, config);

// Verify that Matterbridge is the correct version
if (this.verifyMatterbridgeVersion === undefined || typeof this.verifyMatterbridgeVersion !== 'function' || !this.verifyMatterbridgeVersion('1.7.2')) {
if (this.verifyMatterbridgeVersion === undefined || typeof this.verifyMatterbridgeVersion !== 'function' || !this.verifyMatterbridgeVersion('1.7.3')) {
throw new Error(
`This plugin requires Matterbridge version >= "1.7.2". Please update Matterbridge from ${this.matterbridge.matterbridgeVersion} to the latest version in the frontend."`,
`This plugin requires Matterbridge version >= "1.7.3". Please update Matterbridge from ${this.matterbridge.matterbridgeVersion} to the latest version in the frontend."`,
);
}

Expand Down

0 comments on commit f691e70

Please sign in to comment.