Skip to content
This repository was archived by the owner on Jun 25, 2026. It is now read-only.

Commit eda398d

Browse files
Merge pull request #12 from robertoschwald/feature/fix_validateConfig
Fix validateConfig, add tests
2 parents 764d300 + 1aae92b commit eda398d

5 files changed

Lines changed: 28 additions & 2 deletions

File tree

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ jobs:
2929
- name: Build the project
3030
run: npm run build
3131

32+
- name: Test the project
33+
run: npm test
34+
3235
- name: List, audit, fix outdated dependencies and build again
3336
run: |
3437
npm list --outdated

.github/workflows/npm-publish.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ jobs:
5757
- name: Build
5858
run: npm run build
5959

60+
- name: Test the project
61+
run: npm test
62+
6063
- name: Show npm pack file list (dry-run)
6164
run: npm pack --dry-run
6265

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "homebridge-obis-powermeter",
33
"displayName": "Homebridge OBIS Powermeter",
4-
"version": "1.0.1",
4+
"version": "1.0.2",
55
"description": "Plugin for OBIS smart meter devices (SML/D0) to read power, energy, and voltages.",
66
"license": "Apache-2.0",
77
"author": "Robert Oschwald <robertoschwald@users.noreply.github.com>",

src/Platform.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ export class HomebridgeObisPowerConsumption implements DynamicPlatformPlugin {
112112
}
113113

114114
private validateConfig(): boolean {
115-
return this.config.serialPort.length > 0;
115+
const sp = (this.config as Partial<PluginConfig> | undefined)?.serialPort;
116+
return typeof sp === 'string' && sp.trim().length > 0;
116117
}
117118

118119
private obisOptions: ObisSerialOptions = {

src/__tests__/Platform.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ describe('HomebridgeSmlPowerConsumption', () => {
5252
expect((platform as any).validateConfig()).toBe(false);
5353
});
5454

55+
it('validateConfig returns false when serialPort is missing', () => {
56+
const cfg = { ...config } as any;
57+
delete cfg.serialPort;
58+
const p = new HomebridgeObisPowerConsumption(log, cfg, api);
59+
expect((p as any).validateConfig()).toBe(false);
60+
});
61+
62+
it('validateConfig returns false for whitespace-only serialPort', () => {
63+
const cfg = { ...config, serialPort: ' \t ' } as any;
64+
const p = new HomebridgeObisPowerConsumption(log, cfg, api);
65+
expect((p as any).validateConfig()).toBe(false);
66+
});
67+
68+
it('validateConfig returns false for non-string serialPort', () => {
69+
const cfg = { ...config, serialPort: 12345 as unknown as string } as any;
70+
const p = new HomebridgeObisPowerConsumption(log, cfg, api);
71+
expect((p as any).validateConfig()).toBe(false);
72+
});
73+
5574
it('handles invalid serial port during validation (init throws)', async () => {
5675
jest.spyOn(SmartMeterObis as any, 'init').mockImplementation(() => {
5776
throw new Error('open failed');

0 commit comments

Comments
 (0)