@switchbot/homebridge-switchbot 5.2.9-beta.0 → 5.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config.schema.json +29 -0
- package/dist/SwitchBotMatterPlatform.js +1 -1
- package/dist/SwitchBotMatterPlatform.js.map +1 -1
- package/dist/deviceFactory.js +1 -1
- package/dist/deviceFactory.js.map +1 -1
- package/dist/devices/genericDevice.d.ts +2 -0
- package/dist/devices/genericDevice.d.ts.map +1 -1
- package/dist/devices/genericDevice.js +573 -123
- package/dist/devices/genericDevice.js.map +1 -1
- package/dist/homebridge-ui/endpoints/discovery.d.ts.map +1 -1
- package/dist/homebridge-ui/endpoints/discovery.js +2 -0
- package/dist/homebridge-ui/endpoints/discovery.js.map +1 -1
- package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js +2 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js.map +1 -1
- package/dist/homebridge-ui/public/index.html +12 -2
- package/dist/homebridge-ui/public/js/advanced-settings.d.ts +4 -1
- package/dist/homebridge-ui/public/js/advanced-settings.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/advanced-settings.js +21 -11
- package/dist/homebridge-ui/public/js/advanced-settings.js.map +1 -1
- package/dist/homebridge-ui/public/js/advanced-settings.ts +23 -11
- package/dist/homebridge-ui/public/js/api.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/api.js +21 -7
- package/dist/homebridge-ui/public/js/api.js.map +1 -1
- package/dist/homebridge-ui/public/js/api.ts +21 -7
- package/dist/homebridge-ui/public/js/app.js +78 -20
- package/dist/homebridge-ui/public/js/app.js.map +2 -2
- package/dist/homebridge-ui/public/js/credentials.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/credentials.js +9 -3
- package/dist/homebridge-ui/public/js/credentials.js.map +1 -1
- package/dist/homebridge-ui/public/js/credentials.ts +9 -3
- package/dist/homebridge-ui/public/js/devices-delete.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/devices-delete.js +6 -2
- package/dist/homebridge-ui/public/js/devices-delete.js.map +1 -1
- package/dist/homebridge-ui/public/js/devices-delete.ts +6 -2
- package/dist/homebridge-ui/public/js/devices.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/devices.js +3 -1
- package/dist/homebridge-ui/public/js/devices.js.map +1 -1
- package/dist/homebridge-ui/public/js/devices.ts +3 -1
- package/dist/homebridge-ui/public/js/discovery.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/discovery.js +89 -25
- package/dist/homebridge-ui/public/js/discovery.js.map +1 -1
- package/dist/homebridge-ui/public/js/discovery.ts +89 -25
- package/dist/homebridge-ui/public/js/modals.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/modals.js +15 -5
- package/dist/homebridge-ui/public/js/modals.js.map +1 -1
- package/dist/homebridge-ui/public/js/modals.ts +15 -5
- package/dist/homebridge-ui/public/js/render.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/render.js +67 -17
- package/dist/homebridge-ui/public/js/render.js.map +1 -1
- package/dist/homebridge-ui/public/js/render.ts +67 -17
- package/dist/homebridge-ui/public/js/toast.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/toast.js +6 -2
- package/dist/homebridge-ui/public/js/toast.js.map +1 -1
- package/dist/homebridge-ui/public/js/toast.ts +6 -2
- package/dist/homebridge-ui/settings.js.map +1 -1
- package/dist/homebridge-ui/switchbotClient.js +4 -0
- package/dist/homebridge-ui/switchbotClient.js.map +1 -1
- package/dist/settings.d.ts +1 -0
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/dist/switchbotClient.d.ts +1 -0
- package/dist/switchbotClient.d.ts.map +1 -1
- package/dist/switchbotClient.js +4 -0
- package/dist/switchbotClient.js.map +1 -1
- package/docs/variables/default.html +1 -1
- package/package.json +9 -9
- package/src/SwitchBotMatterPlatform.ts +1 -1
- package/src/deviceFactory.ts +1 -1
- package/src/devices/genericDevice.ts +572 -134
- package/src/homebridge-ui/endpoints/discovery.ts +2 -0
- package/src/homebridge-ui/public/index.html +12 -2
- package/src/homebridge-ui/public/js/advanced-settings.ts +23 -11
- package/src/homebridge-ui/public/js/api.ts +21 -7
- package/src/homebridge-ui/public/js/credentials.ts +9 -3
- package/src/homebridge-ui/public/js/devices-delete.ts +6 -2
- package/src/homebridge-ui/public/js/devices.ts +3 -1
- package/src/homebridge-ui/public/js/discovery.ts +89 -25
- package/src/homebridge-ui/public/js/modals.ts +15 -5
- package/src/homebridge-ui/public/js/render.ts +67 -17
- package/src/homebridge-ui/public/js/toast.ts +6 -2
- package/src/settings.ts +1 -0
- package/src/switchbotClient.ts +5 -0
- package/test/client/node-switchbot-commands.spec.ts +44 -8
- package/test/client/switchbot-client-debounce.spec.ts +24 -3
- package/test/client/switchbot-client-discovery.spec.ts +26 -5
- package/test/client/switchbot-client-keys.spec.ts +30 -7
- package/test/client/switchbot-client-openapi.spec.ts +6 -1
- package/test/client/switchbotClient.spec.ts +30 -6
- package/test/device/bot-hap.spec.ts +131 -22
- package/test/device/curtain-ble-position.spec.ts +46 -0
- package/test/device/deviceBase.spec.ts +6 -1
- package/test/device/deviceFactory-edge.spec.ts +12 -2
- package/test/device/deviceFactory.spec.ts +6 -1
- package/test/device/fan-swing.spec.ts +9 -2
- package/test/device/genericDevice-blepoll.spec.ts +17 -3
- package/test/device/getstate-cache.spec.ts +37 -6
- package/test/device/getstate-returns-status.spec.ts +65 -13
- package/test/device/lock-users.spec.ts +24 -7
- package/test/device/matter-descriptors.spec.ts +6 -1
- package/test/device/matter-device-state.spec.ts +6 -1
- package/test/device/meter-battery.spec.ts +86 -14
- package/test/device/meter-co2.spec.ts +65 -10
- package/test/device/model-identities.spec.ts +6 -1
- package/test/device/water-detector-battery.spec.ts +19 -3
- package/test/device/water-detector-leak.spec.ts +6 -1
- package/test/helpers/matter-harness.ts +21 -4
- package/test/platform/accessory-restore.spec.ts +18 -3
- package/test/platform/hap-cache-restart.spec.ts +66 -13
- package/test/platform/matter-childbridge.spec.ts +16 -3
- package/test/platform/matter-integration.spec.ts +6 -1
- package/test/platform/matter-registration.spec.ts +47 -10
- package/test/platform/platform-edge.spec.ts +13 -3
- package/test/platform/platform.integration.spec.ts +16 -3
- package/test/utils/utils.spec.ts +42 -8
- package/.github/npm-version-script-esm.js +0 -103
|
@@ -7,8 +7,18 @@ describe('deviceFactory edge cases', () => {
|
|
|
7
7
|
await expect(createDevice({ id: 'abc', type: 'Bot' }, undefined as any, false)).rejects.toThrow('logger')
|
|
8
8
|
})
|
|
9
9
|
it('should handle unknown protocol', async () => {
|
|
10
|
-
const mockLogger = {
|
|
11
|
-
|
|
10
|
+
const mockLogger = {
|
|
11
|
+
info: () => {},
|
|
12
|
+
warn: () => {},
|
|
13
|
+
error: () => {},
|
|
14
|
+
debug: () => {},
|
|
15
|
+
}
|
|
16
|
+
const result = await createDevice({
|
|
17
|
+
id: 'abc',
|
|
18
|
+
type: 'Bot',
|
|
19
|
+
protocol: 'unknown',
|
|
20
|
+
log: mockLogger,
|
|
21
|
+
}, { logger: mockLogger } as any, false)
|
|
12
22
|
expect(result).toBeDefined()
|
|
13
23
|
})
|
|
14
24
|
// Add more edge case tests as needed
|
|
@@ -6,7 +6,12 @@ const botRegex = /Bot/i
|
|
|
6
6
|
const curtainRegex = /Curtain/i
|
|
7
7
|
|
|
8
8
|
describe('createDevice', () => {
|
|
9
|
-
const mockLogger = {
|
|
9
|
+
const mockLogger = {
|
|
10
|
+
info: () => {},
|
|
11
|
+
warn: () => {},
|
|
12
|
+
error: () => {},
|
|
13
|
+
debug: () => {},
|
|
14
|
+
}
|
|
10
15
|
const dummyConfig = { logger: mockLogger, log: mockLogger }
|
|
11
16
|
|
|
12
17
|
it('should create a Bot device instance', async () => {
|
|
@@ -3,11 +3,18 @@ import { describe, expect, it, vi } from 'vitest'
|
|
|
3
3
|
import { FanDevice } from '../../src/devices/genericDevice'
|
|
4
4
|
|
|
5
5
|
const failRegex = /fail/
|
|
6
|
-
const mockLogger = {
|
|
6
|
+
const mockLogger = {
|
|
7
|
+
info: () => {},
|
|
8
|
+
warn: () => {},
|
|
9
|
+
error: () => {},
|
|
10
|
+
debug: () => {},
|
|
11
|
+
}
|
|
7
12
|
|
|
8
13
|
function makeFan(id: string) {
|
|
9
14
|
const fan = new FanDevice({ id, type: 'Fan' }, { log: mockLogger });
|
|
10
|
-
(fan as any).client = {
|
|
15
|
+
(fan as any).client = {
|
|
16
|
+
setDeviceState: vi.fn().mockResolvedValue({ status: 'success' }),
|
|
17
|
+
}
|
|
11
18
|
return fan
|
|
12
19
|
}
|
|
13
20
|
|
|
@@ -51,9 +51,23 @@ describe('genericDevice BLE polling config', () => {
|
|
|
51
51
|
// API budget and on top of the normal refresh, which can use up the 10,000 calls a day.
|
|
52
52
|
describe('genericDevice BLE polling only polls over BLE', () => {
|
|
53
53
|
function device(cfg: Record<string, unknown>, hasBLE: boolean | undefined) {
|
|
54
|
-
const nodeDevice = hasBLE === undefined
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
const nodeDevice = hasBLE === undefined
|
|
55
|
+
? undefined
|
|
56
|
+
: {
|
|
57
|
+
hasBLE: () => hasBLE,
|
|
58
|
+
}
|
|
59
|
+
const client = {
|
|
60
|
+
devices: {
|
|
61
|
+
get: () => nodeDevice,
|
|
62
|
+
},
|
|
63
|
+
getDevice: vi.fn(async () => nodeDevice),
|
|
64
|
+
}
|
|
65
|
+
const d = new GenericDevice({
|
|
66
|
+
id: 'dev',
|
|
67
|
+
type: 'Unknown',
|
|
68
|
+
log: mockLogger,
|
|
69
|
+
blePollIntervalMs: 60_000,
|
|
70
|
+
}, { log: mockLogger, _client: client, ...cfg } as any)
|
|
57
71
|
const getState = vi.spyOn(d, 'getState').mockResolvedValue({})
|
|
58
72
|
return { d, getState }
|
|
59
73
|
}
|
|
@@ -7,11 +7,27 @@ import { describe, expect, it, vi } from 'vitest'
|
|
|
7
7
|
|
|
8
8
|
import { GenericDevice } from '../../src/devices/genericDevice.js'
|
|
9
9
|
|
|
10
|
-
const log: any = {
|
|
10
|
+
const log: any = {
|
|
11
|
+
info: vi.fn(),
|
|
12
|
+
warn: vi.fn(),
|
|
13
|
+
error: vi.fn(),
|
|
14
|
+
debug: vi.fn(),
|
|
15
|
+
success: vi.fn(),
|
|
16
|
+
}
|
|
11
17
|
|
|
12
18
|
function device() {
|
|
13
|
-
const getDevice = vi.fn(async () => ({
|
|
14
|
-
|
|
19
|
+
const getDevice = vi.fn(async () => ({
|
|
20
|
+
getStatus: async () => ({ temperature: 22.7, humidity: 59 }),
|
|
21
|
+
}))
|
|
22
|
+
const d = new GenericDevice({
|
|
23
|
+
id: 'B0E9FED044E3',
|
|
24
|
+
type: 'meter',
|
|
25
|
+
name: 'M',
|
|
26
|
+
log,
|
|
27
|
+
}, {
|
|
28
|
+
log,
|
|
29
|
+
_client: { getDevice },
|
|
30
|
+
} as any)
|
|
15
31
|
return { d, getDevice }
|
|
16
32
|
}
|
|
17
33
|
|
|
@@ -21,7 +37,12 @@ describe('genericDevice.getState caching', () => {
|
|
|
21
37
|
it('fetches once for a burst of concurrent reads', async () => {
|
|
22
38
|
const { d, getDevice } = device()
|
|
23
39
|
|
|
24
|
-
const states = await Promise.all([
|
|
40
|
+
const states = await Promise.all([
|
|
41
|
+
d.getState(),
|
|
42
|
+
d.getState(),
|
|
43
|
+
d.getState(),
|
|
44
|
+
d.getState(),
|
|
45
|
+
])
|
|
25
46
|
|
|
26
47
|
expect(getDevice).toHaveBeenCalledTimes(1)
|
|
27
48
|
expect(states.every(s => s.temperature === 22.7)).toBe(true)
|
|
@@ -50,8 +71,18 @@ describe('genericDevice.getState caching', () => {
|
|
|
50
71
|
it('marks an unreadable device and does not cache it', async () => {
|
|
51
72
|
const getDevice = vi.fn()
|
|
52
73
|
.mockRejectedValueOnce(new Error('offline'))
|
|
53
|
-
.mockResolvedValueOnce({
|
|
54
|
-
|
|
74
|
+
.mockResolvedValueOnce({
|
|
75
|
+
getStatus: async () => ({ temperature: 21 }),
|
|
76
|
+
})
|
|
77
|
+
const d = new GenericDevice({
|
|
78
|
+
id: 'x',
|
|
79
|
+
type: 'meter',
|
|
80
|
+
name: 'M',
|
|
81
|
+
log,
|
|
82
|
+
}, {
|
|
83
|
+
log,
|
|
84
|
+
_client: { getDevice },
|
|
85
|
+
} as any)
|
|
55
86
|
|
|
56
87
|
const first = await d.getState()
|
|
57
88
|
const second = await d.getState()
|
|
@@ -8,7 +8,13 @@ import { describe, expect, it, vi } from 'vitest'
|
|
|
8
8
|
import { createDevice } from '../../src/deviceFactory.js'
|
|
9
9
|
import { GenericDevice, MeterDevice } from '../../src/devices/genericDevice.js'
|
|
10
10
|
|
|
11
|
-
const log: any = {
|
|
11
|
+
const log: any = {
|
|
12
|
+
info: vi.fn(),
|
|
13
|
+
warn: vi.fn(),
|
|
14
|
+
error: vi.fn(),
|
|
15
|
+
debug: vi.fn(),
|
|
16
|
+
success: vi.fn(),
|
|
17
|
+
}
|
|
12
18
|
|
|
13
19
|
/** A node-switchbot style device: identity on the instance, readings via getStatus(). */
|
|
14
20
|
function switchBotDevice(status: any) {
|
|
@@ -21,15 +27,28 @@ function switchBotDevice(status: any) {
|
|
|
21
27
|
}
|
|
22
28
|
}
|
|
23
29
|
|
|
24
|
-
const STATUS = {
|
|
30
|
+
const STATUS = {
|
|
31
|
+
deviceId: 'B0E9FED044E3',
|
|
32
|
+
temperature: 23.7,
|
|
33
|
+
humidity: 59,
|
|
34
|
+
battery: 100,
|
|
35
|
+
connectionType: 'api',
|
|
36
|
+
}
|
|
25
37
|
|
|
26
38
|
function deviceWith(client: any) {
|
|
27
|
-
return new GenericDevice({
|
|
39
|
+
return new GenericDevice({
|
|
40
|
+
id: 'B0E9FED044E3',
|
|
41
|
+
type: 'meter',
|
|
42
|
+
name: 'Meter',
|
|
43
|
+
log,
|
|
44
|
+
}, { log, _client: client } as any)
|
|
28
45
|
}
|
|
29
46
|
|
|
30
47
|
describe('genericDevice.getState', () => {
|
|
31
48
|
it('returns the status rather than the device instance', async () => {
|
|
32
|
-
const state = await deviceWith({
|
|
49
|
+
const state = await deviceWith({
|
|
50
|
+
getDevice: async () => switchBotDevice(STATUS),
|
|
51
|
+
}).getState()
|
|
33
52
|
|
|
34
53
|
expect(state).toEqual(STATUS)
|
|
35
54
|
expect(state.temperature).toBe(23.7)
|
|
@@ -37,22 +56,39 @@ describe('genericDevice.getState', () => {
|
|
|
37
56
|
})
|
|
38
57
|
|
|
39
58
|
it('passes through a plain status object unchanged', async () => {
|
|
40
|
-
const state = await deviceWith({
|
|
59
|
+
const state = await deviceWith({
|
|
60
|
+
getDevice: async () => ({ temperature: 20, humidity: 40 }),
|
|
61
|
+
}).getState()
|
|
41
62
|
|
|
42
63
|
expect(state).toEqual({ temperature: 20, humidity: 40 })
|
|
43
64
|
})
|
|
44
65
|
|
|
45
66
|
it('unwraps an API-shaped body', async () => {
|
|
46
|
-
const state = await deviceWith({
|
|
67
|
+
const state = await deviceWith({
|
|
68
|
+
getDevice: async () => ({
|
|
69
|
+
body: { temperature: 19 },
|
|
70
|
+
}),
|
|
71
|
+
}).getState()
|
|
47
72
|
|
|
48
73
|
expect(state).toEqual({ temperature: 19 })
|
|
49
74
|
})
|
|
50
75
|
|
|
51
76
|
it.each([
|
|
52
|
-
[
|
|
53
|
-
|
|
77
|
+
[
|
|
78
|
+
'throws',
|
|
79
|
+
async () => { throw new Error('unreachable') },
|
|
80
|
+
],
|
|
81
|
+
[
|
|
82
|
+
'returns null',
|
|
83
|
+
async () => null,
|
|
84
|
+
],
|
|
54
85
|
])('reports minimal info when getStatus() %s', async (_label, getStatus) => {
|
|
55
|
-
const client = {
|
|
86
|
+
const client = {
|
|
87
|
+
getDevice: async () => ({
|
|
88
|
+
...switchBotDevice(STATUS),
|
|
89
|
+
getStatus,
|
|
90
|
+
}),
|
|
91
|
+
}
|
|
56
92
|
|
|
57
93
|
const state = await deviceWith(client).getState()
|
|
58
94
|
|
|
@@ -64,9 +100,17 @@ describe('genericDevice.getState', () => {
|
|
|
64
100
|
|
|
65
101
|
describe('createDevice', () => {
|
|
66
102
|
it('does not replace the device getState with the raw client device', async () => {
|
|
67
|
-
const client = {
|
|
103
|
+
const client = {
|
|
104
|
+
init: async () => {},
|
|
105
|
+
getDevice: async () => switchBotDevice(STATUS),
|
|
106
|
+
}
|
|
68
107
|
const created: any = await createDevice(
|
|
69
|
-
{
|
|
108
|
+
{
|
|
109
|
+
id: 'B0E9FED044E3',
|
|
110
|
+
type: 'meter',
|
|
111
|
+
name: 'Meter',
|
|
112
|
+
log,
|
|
113
|
+
} as any,
|
|
70
114
|
{ log, _client: client } as any,
|
|
71
115
|
false,
|
|
72
116
|
)
|
|
@@ -78,8 +122,16 @@ describe('createDevice', () => {
|
|
|
78
122
|
})
|
|
79
123
|
|
|
80
124
|
it('lets a meter report a real temperature end to end', async () => {
|
|
81
|
-
const client = {
|
|
82
|
-
|
|
125
|
+
const client = {
|
|
126
|
+
init: async () => {},
|
|
127
|
+
getDevice: async () => switchBotDevice(STATUS),
|
|
128
|
+
}
|
|
129
|
+
const device = new MeterDevice({
|
|
130
|
+
id: 'B0E9FED044E3',
|
|
131
|
+
type: 'meter',
|
|
132
|
+
name: 'Meter',
|
|
133
|
+
log,
|
|
134
|
+
} as any, { log, _client: client } as any)
|
|
83
135
|
|
|
84
136
|
const services = device.createHAPAccessory(null).services
|
|
85
137
|
const temperature = services.find((s: any) => s.type === 'TemperatureSensor')
|
|
@@ -2,33 +2,50 @@ import { describe, expect, it, vi } from 'vitest'
|
|
|
2
2
|
|
|
3
3
|
import { LockDevice } from '../../src/devices/genericDevice'
|
|
4
4
|
|
|
5
|
-
const mockLogger = {
|
|
5
|
+
const mockLogger = {
|
|
6
|
+
info: () => {},
|
|
7
|
+
warn: () => {},
|
|
8
|
+
error: () => {},
|
|
9
|
+
debug: () => {},
|
|
10
|
+
}
|
|
6
11
|
|
|
7
12
|
const failRegex = /fail/
|
|
8
13
|
|
|
9
14
|
describe('lockDevice user management', () => {
|
|
10
15
|
function makeLock(id: string) {
|
|
11
16
|
const lock = new LockDevice({ id, type: 'Smart Lock' }, { log: mockLogger });
|
|
12
|
-
(lock as any).client = {
|
|
17
|
+
(lock as any).client = {
|
|
18
|
+
setDeviceState: vi.fn().mockResolvedValue({ status: 'success' }),
|
|
19
|
+
}
|
|
13
20
|
return lock
|
|
14
21
|
}
|
|
15
22
|
|
|
16
23
|
it('should add a user', async () => {
|
|
17
24
|
const lock = makeLock('lock1')
|
|
18
|
-
await lock.setState({
|
|
19
|
-
|
|
25
|
+
await lock.setState({
|
|
26
|
+
addUser: { name: 'Alice', code: '1234' },
|
|
27
|
+
})
|
|
28
|
+
expect((lock as any).client.setDeviceState).toHaveBeenCalledWith('lock1', {
|
|
29
|
+
addUser: { name: 'Alice', code: '1234' },
|
|
30
|
+
})
|
|
20
31
|
})
|
|
21
32
|
|
|
22
33
|
it('should remove a user', async () => {
|
|
23
34
|
const lock = makeLock('lock2')
|
|
24
|
-
await lock.setState({
|
|
25
|
-
|
|
35
|
+
await lock.setState({
|
|
36
|
+
removeUser: { name: 'Bob' },
|
|
37
|
+
})
|
|
38
|
+
expect((lock as any).client.setDeviceState).toHaveBeenCalledWith('lock2', {
|
|
39
|
+
removeUser: { name: 'Bob' },
|
|
40
|
+
})
|
|
26
41
|
})
|
|
27
42
|
|
|
28
43
|
it('should return error if setDeviceState fails', async () => {
|
|
29
44
|
const lock = makeLock('lock3');
|
|
30
45
|
((lock as any).client.setDeviceState as any).mockRejectedValueOnce(new Error('fail'))
|
|
31
|
-
const result = await lock.setState({
|
|
46
|
+
const result = await lock.setState({
|
|
47
|
+
addUser: { name: 'Eve', code: '9999' },
|
|
48
|
+
})
|
|
32
49
|
expect(result.success).toBe(false)
|
|
33
50
|
expect(result.reason).toMatch(failRegex)
|
|
34
51
|
})
|
|
@@ -2,7 +2,12 @@ import { describe, expect, it } from 'vitest'
|
|
|
2
2
|
|
|
3
3
|
import { BotDevice, CurtainDevice } from '../../src/devices/genericDevice'
|
|
4
4
|
|
|
5
|
-
const mockLogger = {
|
|
5
|
+
const mockLogger = {
|
|
6
|
+
info: () => {},
|
|
7
|
+
warn: () => {},
|
|
8
|
+
error: () => {},
|
|
9
|
+
debug: () => {},
|
|
10
|
+
}
|
|
6
11
|
|
|
7
12
|
describe('matter device descriptors', () => {
|
|
8
13
|
it('should expose correct clusters for BotDevice', async () => {
|
|
@@ -2,7 +2,12 @@ import { describe, expect, it } from 'vitest'
|
|
|
2
2
|
|
|
3
3
|
import { BotDevice, CurtainDevice } from '../../src/devices/genericDevice'
|
|
4
4
|
|
|
5
|
-
const mockLogger = {
|
|
5
|
+
const mockLogger = {
|
|
6
|
+
info: () => {},
|
|
7
|
+
warn: () => {},
|
|
8
|
+
error: () => {},
|
|
9
|
+
debug: () => {},
|
|
10
|
+
}
|
|
6
11
|
describe('matter device state translation', () => {
|
|
7
12
|
it('should map Bot state to Matter OnOff', async () => {
|
|
8
13
|
const bot = new BotDevice({ id: 'bot1', type: 'Bot' }, { log: mockLogger })
|
|
@@ -9,25 +9,53 @@ import { describe, expect, it, vi } from 'vitest'
|
|
|
9
9
|
import { createDevice } from '../../src/deviceFactory.js'
|
|
10
10
|
import { MeterDevice } from '../../src/devices/genericDevice.js'
|
|
11
11
|
|
|
12
|
-
const log: any = {
|
|
12
|
+
const log: any = {
|
|
13
|
+
info: vi.fn(),
|
|
14
|
+
warn: vi.fn(),
|
|
15
|
+
error: vi.fn(),
|
|
16
|
+
debug: vi.fn(),
|
|
17
|
+
success: vi.fn(),
|
|
18
|
+
}
|
|
13
19
|
|
|
14
20
|
class HapStatusError extends Error {
|
|
15
21
|
constructor(public hapStatus: number) {
|
|
16
22
|
super(`HAP status ${hapStatus}`)
|
|
17
23
|
}
|
|
18
24
|
}
|
|
19
|
-
const api: any = {
|
|
25
|
+
const api: any = {
|
|
26
|
+
hap: {
|
|
27
|
+
HapStatusError,
|
|
28
|
+
HAPStatus: { SERVICE_COMMUNICATION_FAILURE: -70402 },
|
|
29
|
+
},
|
|
30
|
+
}
|
|
20
31
|
|
|
21
32
|
function battery(status: Record<string, unknown>, deviceType = 'Meter') {
|
|
22
|
-
const device = new MeterDevice({
|
|
33
|
+
const device = new MeterDevice({
|
|
34
|
+
id: 'B0E9FED044E3',
|
|
35
|
+
type: 'meter',
|
|
36
|
+
deviceType,
|
|
37
|
+
name: 'Meter',
|
|
38
|
+
log,
|
|
39
|
+
} as any, { log } as any)
|
|
23
40
|
vi.spyOn(device, 'getState').mockResolvedValue(status)
|
|
24
41
|
const service = device.createHAPAccessory(api).services.find((s: any) => s.type === 'Battery')
|
|
25
42
|
return service.characteristics
|
|
26
43
|
}
|
|
27
44
|
|
|
28
45
|
describe('meterDevice Battery service', () => {
|
|
29
|
-
it.each([
|
|
30
|
-
|
|
46
|
+
it.each([
|
|
47
|
+
'Meter',
|
|
48
|
+
'Meter Plus',
|
|
49
|
+
'MeterPro',
|
|
50
|
+
'MeterPro(CO2)',
|
|
51
|
+
])('is added for %s', (deviceType) => {
|
|
52
|
+
const device = new MeterDevice({
|
|
53
|
+
id: 'x',
|
|
54
|
+
type: 'meter',
|
|
55
|
+
deviceType,
|
|
56
|
+
name: 'M',
|
|
57
|
+
log,
|
|
58
|
+
} as any, { log } as any)
|
|
31
59
|
|
|
32
60
|
expect(device.createHAPAccessory(api).services.map((s: any) => s.type)).toContain('Battery')
|
|
33
61
|
})
|
|
@@ -43,7 +71,12 @@ describe('meterDevice Battery service', () => {
|
|
|
43
71
|
})
|
|
44
72
|
|
|
45
73
|
// Same threshold as the water detector.
|
|
46
|
-
it.each([
|
|
74
|
+
it.each([
|
|
75
|
+
[100, 0],
|
|
76
|
+
[20, 0],
|
|
77
|
+
[19, 1],
|
|
78
|
+
[0, 1],
|
|
79
|
+
])('maps %i%% to low battery %i', async (level, expected) => {
|
|
47
80
|
await expect(battery({ battery: level }).StatusLowBattery.get()).resolves.toBe(expected)
|
|
48
81
|
})
|
|
49
82
|
|
|
@@ -55,9 +88,18 @@ describe('meterDevice Battery service', () => {
|
|
|
55
88
|
// alert, and 100% would hide a real one.
|
|
56
89
|
it.each([
|
|
57
90
|
['missing', {}],
|
|
58
|
-
[
|
|
59
|
-
|
|
60
|
-
|
|
91
|
+
[
|
|
92
|
+
'null',
|
|
93
|
+
{ battery: null },
|
|
94
|
+
],
|
|
95
|
+
[
|
|
96
|
+
'text',
|
|
97
|
+
{ battery: 'full' },
|
|
98
|
+
],
|
|
99
|
+
[
|
|
100
|
+
'unreadable device',
|
|
101
|
+
{ id: 'x', type: 'meter', unreadable: true },
|
|
102
|
+
],
|
|
61
103
|
])('reports Not Available when the level is %s', async (_label, status) => {
|
|
62
104
|
const chars = battery(status)
|
|
63
105
|
|
|
@@ -66,14 +108,44 @@ describe('meterDevice Battery service', () => {
|
|
|
66
108
|
})
|
|
67
109
|
|
|
68
110
|
it('reports the level end to end through node-switchbot', async () => {
|
|
69
|
-
const apiBody = {
|
|
111
|
+
const apiBody = {
|
|
112
|
+
version: 'V1.8',
|
|
113
|
+
temperature: 22.7,
|
|
114
|
+
battery: 100,
|
|
115
|
+
humidity: 55,
|
|
116
|
+
CO2: 483,
|
|
117
|
+
deviceId: 'B0E9FED044E3',
|
|
118
|
+
deviceType: 'MeterPro(CO2)',
|
|
119
|
+
hubDeviceId: '000000000000',
|
|
120
|
+
}
|
|
70
121
|
const switchBotDevice = new WoSensorTHProCO2(
|
|
71
|
-
{
|
|
72
|
-
|
|
122
|
+
{
|
|
123
|
+
id: 'B0E9FED044E3',
|
|
124
|
+
name: 'Meter Pro CO2 Monitor',
|
|
125
|
+
deviceType: 'MeterPro(CO2)',
|
|
126
|
+
connectionTypes: ['api'],
|
|
127
|
+
} as any,
|
|
128
|
+
{
|
|
129
|
+
apiClient: {
|
|
130
|
+
getStatus: async () => apiBody,
|
|
131
|
+
},
|
|
132
|
+
} as any,
|
|
73
133
|
)
|
|
74
134
|
const created: any = await createDevice(
|
|
75
|
-
{
|
|
76
|
-
|
|
135
|
+
{
|
|
136
|
+
id: 'B0E9FED044E3',
|
|
137
|
+
type: 'meter',
|
|
138
|
+
deviceType: 'MeterPro(CO2)',
|
|
139
|
+
name: 'Meter Pro CO2 Monitor',
|
|
140
|
+
log,
|
|
141
|
+
} as any,
|
|
142
|
+
{
|
|
143
|
+
log,
|
|
144
|
+
_client: {
|
|
145
|
+
init: async () => {},
|
|
146
|
+
getDevice: async () => switchBotDevice,
|
|
147
|
+
},
|
|
148
|
+
} as any,
|
|
77
149
|
false,
|
|
78
150
|
)
|
|
79
151
|
const service = created.createAccessory(api).services.find((s: any) => s.type === 'Battery')
|
|
@@ -9,10 +9,22 @@ import { describe, expect, it, vi } from 'vitest'
|
|
|
9
9
|
import { createDevice } from '../../src/deviceFactory.js'
|
|
10
10
|
import { MeterDevice } from '../../src/devices/genericDevice.js'
|
|
11
11
|
|
|
12
|
-
const log: any = {
|
|
12
|
+
const log: any = {
|
|
13
|
+
info: vi.fn(),
|
|
14
|
+
warn: vi.fn(),
|
|
15
|
+
error: vi.fn(),
|
|
16
|
+
debug: vi.fn(),
|
|
17
|
+
success: vi.fn(),
|
|
18
|
+
}
|
|
13
19
|
|
|
14
20
|
function meter(deviceType: string | undefined, status: Record<string, unknown>, cfg: Record<string, unknown> = {}) {
|
|
15
|
-
const device = new MeterDevice({
|
|
21
|
+
const device = new MeterDevice({
|
|
22
|
+
id: 'B0E9FED044E3',
|
|
23
|
+
type: 'meter',
|
|
24
|
+
deviceType,
|
|
25
|
+
name: 'Meter',
|
|
26
|
+
log,
|
|
27
|
+
} as any, { log, ...cfg } as any)
|
|
16
28
|
vi.spyOn(device, 'getState').mockResolvedValue(status)
|
|
17
29
|
return device
|
|
18
30
|
}
|
|
@@ -30,10 +42,21 @@ const READING = { temperature: 22.7, humidity: 55, co2: 483 }
|
|
|
30
42
|
|
|
31
43
|
describe('meterDevice CO2 service', () => {
|
|
32
44
|
it.each(['MeterPro(CO2)', 'Meter Pro (CO2)'])('is added for %s', (deviceType) => {
|
|
33
|
-
expect(serviceTypes(meter(deviceType, READING))).toEqual([
|
|
45
|
+
expect(serviceTypes(meter(deviceType, READING))).toEqual([
|
|
46
|
+
'TemperatureSensor',
|
|
47
|
+
'HumiditySensor',
|
|
48
|
+
'CarbonDioxideSensor',
|
|
49
|
+
'Battery',
|
|
50
|
+
])
|
|
34
51
|
})
|
|
35
52
|
|
|
36
|
-
it.each([
|
|
53
|
+
it.each([
|
|
54
|
+
'Meter',
|
|
55
|
+
'Meter Plus',
|
|
56
|
+
'MeterPro',
|
|
57
|
+
'Outdoor Meter',
|
|
58
|
+
undefined,
|
|
59
|
+
])('is not added for %s', (deviceType) => {
|
|
37
60
|
expect(serviceTypes(meter(deviceType, READING))).toEqual(['TemperatureSensor', 'HumiditySensor', 'Battery'])
|
|
38
61
|
})
|
|
39
62
|
|
|
@@ -63,7 +86,12 @@ describe('meterDevice CO2 detected threshold', () => {
|
|
|
63
86
|
await expect(detected(800, { co2AbnormalThreshold: 800 })).resolves.toBe(1)
|
|
64
87
|
})
|
|
65
88
|
|
|
66
|
-
it.each([
|
|
89
|
+
it.each([
|
|
90
|
+
['zero', 0],
|
|
91
|
+
['negative', -1],
|
|
92
|
+
['text', 'high'],
|
|
93
|
+
['missing', undefined],
|
|
94
|
+
])(
|
|
67
95
|
'falls back to 1000 when the setting is %s',
|
|
68
96
|
async (_label, value) => {
|
|
69
97
|
await expect(detected(999, { co2AbnormalThreshold: value })).resolves.toBe(0)
|
|
@@ -76,15 +104,42 @@ describe('meterDevice CO2 detected threshold', () => {
|
|
|
76
104
|
// fed the status body a real Meter Pro (CO2) returned from the OpenAPI.
|
|
77
105
|
describe('a Meter Pro (CO2) end to end', () => {
|
|
78
106
|
it('reports CO2 to HomeKit through node-switchbot', async () => {
|
|
79
|
-
const apiBody = {
|
|
107
|
+
const apiBody = {
|
|
108
|
+
version: 'V1.8',
|
|
109
|
+
temperature: 22.7,
|
|
110
|
+
battery: 100,
|
|
111
|
+
humidity: 55,
|
|
112
|
+
CO2: 483,
|
|
113
|
+
deviceId: 'B0E9FED044E3',
|
|
114
|
+
deviceType: 'MeterPro(CO2)',
|
|
115
|
+
hubDeviceId: '000000000000',
|
|
116
|
+
}
|
|
80
117
|
const switchBotDevice = new WoSensorTHProCO2(
|
|
81
|
-
{
|
|
82
|
-
|
|
118
|
+
{
|
|
119
|
+
id: 'B0E9FED044E3',
|
|
120
|
+
name: 'Meter Pro CO2 Monitor',
|
|
121
|
+
deviceType: 'MeterPro(CO2)',
|
|
122
|
+
connectionTypes: ['api'],
|
|
123
|
+
} as any,
|
|
124
|
+
{
|
|
125
|
+
apiClient: {
|
|
126
|
+
getStatus: async () => apiBody,
|
|
127
|
+
},
|
|
128
|
+
} as any,
|
|
83
129
|
)
|
|
84
|
-
const client = {
|
|
130
|
+
const client = {
|
|
131
|
+
init: async () => {},
|
|
132
|
+
getDevice: async () => switchBotDevice,
|
|
133
|
+
}
|
|
85
134
|
|
|
86
135
|
const created: any = await createDevice(
|
|
87
|
-
{
|
|
136
|
+
{
|
|
137
|
+
id: 'B0E9FED044E3',
|
|
138
|
+
type: 'meter',
|
|
139
|
+
deviceType: 'MeterPro(CO2)',
|
|
140
|
+
name: 'Meter Pro CO2 Monitor',
|
|
141
|
+
log,
|
|
142
|
+
} as any,
|
|
88
143
|
{ log, _client: client } as any,
|
|
89
144
|
false,
|
|
90
145
|
)
|
|
@@ -6,7 +6,12 @@ import { createDevice } from '../../src/deviceFactory'
|
|
|
6
6
|
// node-switchbot 5 reports product-specific identities (pySwitchbot 3.0.0 parity).
|
|
7
7
|
// Every identity must reach the same accessory class as its canonical display name.
|
|
8
8
|
describe('product-specific model identities', () => {
|
|
9
|
-
const mockLogger = {
|
|
9
|
+
const mockLogger = {
|
|
10
|
+
info: () => {},
|
|
11
|
+
warn: () => {},
|
|
12
|
+
error: () => {},
|
|
13
|
+
debug: () => {},
|
|
14
|
+
}
|
|
10
15
|
const dummyConfig = { logger: mockLogger, log: mockLogger }
|
|
11
16
|
|
|
12
17
|
it.each([
|
|
@@ -17,7 +17,12 @@ vi.mock('node-switchbot', () => ({
|
|
|
17
17
|
OpenAPIClient: openApiMocks.OpenAPIClient,
|
|
18
18
|
}))
|
|
19
19
|
|
|
20
|
-
const mockLogger = {
|
|
20
|
+
const mockLogger = {
|
|
21
|
+
info: vi.fn(),
|
|
22
|
+
warn: vi.fn(),
|
|
23
|
+
error: vi.fn(),
|
|
24
|
+
debug: vi.fn(),
|
|
25
|
+
}
|
|
21
26
|
|
|
22
27
|
const mockApi = {
|
|
23
28
|
hap: {
|
|
@@ -34,8 +39,19 @@ const mockApi = {
|
|
|
34
39
|
|
|
35
40
|
function makeDevice(config: Record<string, unknown> = {}) {
|
|
36
41
|
return new WaterDetectorDevice(
|
|
37
|
-
{
|
|
38
|
-
|
|
42
|
+
{
|
|
43
|
+
id: 'wd1',
|
|
44
|
+
type: 'waterdetector',
|
|
45
|
+
name: 'Fridge',
|
|
46
|
+
log: mockLogger,
|
|
47
|
+
blePollingEnabled: false,
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
log: mockLogger,
|
|
51
|
+
openApiToken: 'token',
|
|
52
|
+
openApiSecret: 'secret',
|
|
53
|
+
...config,
|
|
54
|
+
} as any,
|
|
39
55
|
)
|
|
40
56
|
}
|
|
41
57
|
|