Skip to content

Commit

Permalink
Refactor contructor to accept DeviceTypeDefinition | AtLeastOne<Devic…
Browse files Browse the repository at this point in the history
…eTypeDefinition>
  • Loading branch information
Luligu committed Jul 5, 2024
1 parent 1310d9f commit 1ef3202
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

If you like this project and find it useful, please consider giving it a star on GitHub at https://github.com/Luligu/matterbridge and sponsoring it.

## [1.3.11] - 2024-07-08

### Changed

- [device]: Refactor contructor to accept DeviceTypeDefinition | AtLeastOne<DeviceTypeDefinition>.

## [1.3.10] - 2024-07-05

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/matterbridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Matterbridge', () => {
});

test('hasParameter("debug") should return true', async () => {
expect(hasParameter('debug')).toBeTruthy();
expect(hasParameter('debug')).toBeFalsy();
});

test('Sanitize fabrics', () => {
Expand Down
46 changes: 46 additions & 0 deletions src/matterbridgeDevice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

/* eslint-disable @typescript-eslint/no-explicit-any */

import { jest } from '@jest/globals';

import { AnsiLogger, LogLevel } from 'node-ansi-logger';
import { bridgedNode, MatterbridgeDevice, powerSource } from './matterbridgeDevice.js';

describe('Matterbridge platform', () => {
beforeAll(async () => {
// Mock the AnsiLogger.log method
jest.spyOn(AnsiLogger.prototype, 'log').mockImplementation((level: string, message: string, ...parameters: any[]) => {
// console.log(`Mocked log: ${level} - ${message}`, ...parameters);
});
jest.spyOn(AnsiLogger.prototype, 'debug').mockImplementation((message: string, ...parameters: any[]) => {
// console.log(`Mocked debug: ${message}`, ...parameters);
});
jest.spyOn(AnsiLogger.prototype, 'info').mockImplementation((message: string, ...parameters: any[]) => {
// console.log(`Mocked info: ${message}`, ...parameters);
});
jest.spyOn(AnsiLogger.prototype, 'warn').mockImplementation((message: string, ...parameters: any[]) => {
// console.log(`Mocked warn: ${message}`, ...parameters);
});
jest.spyOn(AnsiLogger.prototype, 'error').mockImplementation((message: string, ...parameters: any[]) => {
// console.log(`Mocked error: ${message}`, ...parameters);
});
});

afterAll(async () => {
// Restore the mocked AnsiLogger.log method
(AnsiLogger.prototype.log as jest.Mock).mockRestore();
}, 60000);

test('create a device syncronously', async () => {
const device = new MatterbridgeDevice(bridgedNode);
expect(device.getDeviceTypes().length).toBeGreaterThan(0);
expect(device.getDeviceTypes()[0].name).toBe('MA-bridgedNode');
});

test('create a device asyncronously', async () => {
const device = await MatterbridgeDevice.loadInstance(powerSource);
expect(device.getDeviceTypes().length).toBeGreaterThan(0);
expect(device.getDeviceTypes()[0].name).toBe('MA-powerSource');
});
});
17 changes: 13 additions & 4 deletions src/matterbridgeDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,22 @@ export class MatterbridgeDevice extends extendPublicHandlerMethods<typeof Device
/**
* Create a Matterbridge device.
* @constructor
* @param {DeviceTypeDefinition} definition - The definition of the device.
* @param {DeviceTypeDefinition | AtLeastOne<DeviceTypeDefinition>} definition - The definition of the device.
* @param {EndpointOptions} [options={}] - The options for the device.
*/
constructor(definition: DeviceTypeDefinition, options: EndpointOptions = {}, debug = false) {
super(definition, options);
constructor(definition: DeviceTypeDefinition | AtLeastOne<DeviceTypeDefinition>, options: EndpointOptions = {}, debug = false) {
let firstDefinition: DeviceTypeDefinition;
if (Array.isArray(definition)) firstDefinition = definition[0];
else firstDefinition = definition;
super(firstDefinition, options);
if (Array.isArray(definition)) {
definition.forEach((deviceType) => {
this.addDeviceType(deviceType);
});
}
this.addDeviceType(firstDefinition);
this.log = new AnsiLogger({ logName: 'MatterbridgeDevice', logTimestampFormat: TimestampFormat.TIME_MILLIS, logDebug: debug });
this.log.debug(`MatterbridgeDevice with deviceType: ${zb}${definition.code}${db}-${zb}${definition.name}${db}`);
this.log.debug(`MatterbridgeDevice with deviceType: ${zb}${firstDefinition.code}${db}-${zb}${firstDefinition.name}${db}`);
}

/**
Expand Down

0 comments on commit 1ef3202

Please sign in to comment.