@switchbot/homebridge-switchbot 5.2.9-beta.1 → 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/dist/devices/genericDevice.d.ts.map +1 -1
- package/dist/devices/genericDevice.js +528 -118
- package/dist/devices/genericDevice.js.map +1 -1
- package/dist/homebridge-ui/public/js/advanced-settings.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/advanced-settings.js +3 -1
- package/dist/homebridge-ui/public/js/advanced-settings.js.map +1 -1
- package/dist/homebridge-ui/public/js/advanced-settings.ts +3 -1
- 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 +74 -16
- 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 +13 -3
- package/dist/homebridge-ui/public/js/modals.js.map +1 -1
- package/dist/homebridge-ui/public/js/modals.ts +13 -3
- 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/docs/variables/default.html +1 -1
- package/package.json +9 -9
- package/src/devices/genericDevice.ts +529 -129
- package/src/homebridge-ui/public/js/advanced-settings.ts +3 -1
- 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 +13 -3
- package/src/homebridge-ui/public/js/render.ts +67 -17
- package/src/homebridge-ui/public/js/toast.ts +6 -2
- 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 +15 -3
- 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
|
@@ -4,7 +4,14 @@ import { SwitchBotClient } from '../../src/switchbotClient'
|
|
|
4
4
|
|
|
5
5
|
describe('switchBotClient debounce', () => {
|
|
6
6
|
it('should debounce rapid setDeviceState calls to the same device', async () => {
|
|
7
|
-
const client = new SwitchBotClient({
|
|
7
|
+
const client = new SwitchBotClient({
|
|
8
|
+
logger: {
|
|
9
|
+
info: () => {},
|
|
10
|
+
warn: () => {},
|
|
11
|
+
error: () => {},
|
|
12
|
+
debug: () => {},
|
|
13
|
+
},
|
|
14
|
+
} as any)
|
|
8
15
|
const doSetDeviceState = vi.spyOn(client as any, '_doSetDeviceState').mockResolvedValue({ status: 'success' })
|
|
9
16
|
|
|
10
17
|
// Simulate rapid calls
|
|
@@ -20,7 +27,14 @@ describe('switchBotClient debounce', () => {
|
|
|
20
27
|
})
|
|
21
28
|
|
|
22
29
|
it('should not debounce setDeviceState calls to different devices', async () => {
|
|
23
|
-
const client = new SwitchBotClient({
|
|
30
|
+
const client = new SwitchBotClient({
|
|
31
|
+
logger: {
|
|
32
|
+
info: () => {},
|
|
33
|
+
warn: () => {},
|
|
34
|
+
error: () => {},
|
|
35
|
+
debug: () => {},
|
|
36
|
+
},
|
|
37
|
+
} as any)
|
|
24
38
|
const doSetDeviceState = vi.spyOn(client as any, '_doSetDeviceState').mockResolvedValue({ status: 'success' })
|
|
25
39
|
|
|
26
40
|
await Promise.all([
|
|
@@ -36,7 +50,14 @@ describe('switchBotClient debounce', () => {
|
|
|
36
50
|
// HomeKit writes On and Brightness together when a light is turned on at a level. Debouncing by
|
|
37
51
|
// device alone kept only the last body, so turnOn was dropped while both callers saw success.
|
|
38
52
|
it('keeps different operations on the same device', async () => {
|
|
39
|
-
const client = new SwitchBotClient({
|
|
53
|
+
const client = new SwitchBotClient({
|
|
54
|
+
logger: {
|
|
55
|
+
info: () => {},
|
|
56
|
+
warn: () => {},
|
|
57
|
+
error: () => {},
|
|
58
|
+
debug: () => {},
|
|
59
|
+
},
|
|
60
|
+
} as any)
|
|
40
61
|
const doSetDeviceState = vi.spyOn(client as any, '_doSetDeviceState').mockResolvedValue({ status: 'success' })
|
|
41
62
|
|
|
42
63
|
await Promise.all([
|
|
@@ -3,12 +3,25 @@ import { describe, expect, it, vi } from 'vitest'
|
|
|
3
3
|
import { SwitchBotClient } from '../../src/switchbotClient'
|
|
4
4
|
|
|
5
5
|
function client(devices: any[] = []) {
|
|
6
|
-
const c = new SwitchBotClient({
|
|
6
|
+
const c = new SwitchBotClient({
|
|
7
|
+
logger: {
|
|
8
|
+
info: () => {},
|
|
9
|
+
warn: () => {},
|
|
10
|
+
error: () => {},
|
|
11
|
+
debug: () => {},
|
|
12
|
+
},
|
|
13
|
+
} as any)
|
|
7
14
|
const discover = vi.fn(async () => {
|
|
8
15
|
await new Promise(resolve => setTimeout(resolve, 10))
|
|
9
16
|
return devices
|
|
10
17
|
})
|
|
11
|
-
;(c as any).client = {
|
|
18
|
+
;(c as any).client = {
|
|
19
|
+
discover,
|
|
20
|
+
devices: {
|
|
21
|
+
get: () => undefined,
|
|
22
|
+
list: () => devices,
|
|
23
|
+
},
|
|
24
|
+
}
|
|
12
25
|
return { c, discover }
|
|
13
26
|
}
|
|
14
27
|
|
|
@@ -17,13 +30,21 @@ function client(devices: any[] = []) {
|
|
|
17
30
|
// started their own.
|
|
18
31
|
describe('switchBotClient rediscovery for a missing device', () => {
|
|
19
32
|
it('shares one discovery between concurrent lookups', async () => {
|
|
20
|
-
const { c, discover } = client([
|
|
21
|
-
|
|
33
|
+
const { c, discover } = client([
|
|
34
|
+
{ id: 'present' },
|
|
35
|
+
])
|
|
36
|
+
await Promise.all([
|
|
37
|
+
c.getDevice('missing-1'),
|
|
38
|
+
c.getDevice('missing-2'),
|
|
39
|
+
c.getDevice('missing-3'),
|
|
40
|
+
])
|
|
22
41
|
expect(discover).toHaveBeenCalledOnce()
|
|
23
42
|
})
|
|
24
43
|
|
|
25
44
|
it('does not rediscover again within the cache period', async () => {
|
|
26
|
-
const { c, discover } = client([
|
|
45
|
+
const { c, discover } = client([
|
|
46
|
+
{ id: 'present' },
|
|
47
|
+
])
|
|
27
48
|
await c.getDevice('missing')
|
|
28
49
|
await c.getDevice('missing')
|
|
29
50
|
await c.getDevice('missing')
|
|
@@ -2,19 +2,31 @@ import { describe, expect, it, vi } from 'vitest'
|
|
|
2
2
|
|
|
3
3
|
import { SwitchBotClient } from '../../src/switchbotClient'
|
|
4
4
|
|
|
5
|
-
const logger = {
|
|
5
|
+
const logger = {
|
|
6
|
+
info: () => {},
|
|
7
|
+
warn: vi.fn(),
|
|
8
|
+
error: () => {},
|
|
9
|
+
debug: () => {},
|
|
10
|
+
}
|
|
6
11
|
|
|
7
12
|
function client(devices: any[]) {
|
|
8
13
|
const c = new SwitchBotClient({ logger } as any)
|
|
9
14
|
;(c as any).client = {
|
|
10
15
|
discover: vi.fn(async () => devices),
|
|
11
|
-
devices: {
|
|
16
|
+
devices: {
|
|
17
|
+
get: (id: string) => devices.find(d => d.id === id),
|
|
18
|
+
list: () => devices,
|
|
19
|
+
},
|
|
12
20
|
}
|
|
13
21
|
return c
|
|
14
22
|
}
|
|
15
23
|
|
|
16
24
|
function device(id: string, mac?: string) {
|
|
17
|
-
return {
|
|
25
|
+
return {
|
|
26
|
+
id,
|
|
27
|
+
setKey: vi.fn(),
|
|
28
|
+
getInfo: () => ({ id, mac }),
|
|
29
|
+
}
|
|
18
30
|
}
|
|
19
31
|
|
|
20
32
|
// genericDevice used to call client.devices.get(), which the plugin's client wrapper does not have,
|
|
@@ -23,17 +35,26 @@ describe('switchBotClient device keys', () => {
|
|
|
23
35
|
it('applies a key set before the device is discovered', async () => {
|
|
24
36
|
const devices: any[] = []
|
|
25
37
|
const c = client(devices)
|
|
26
|
-
c.setDeviceKey('AA:BB:CC:DD:EE:FF', {
|
|
38
|
+
c.setDeviceKey('AA:BB:CC:DD:EE:FF', {
|
|
39
|
+
encryptionKey: '0'.repeat(32),
|
|
40
|
+
keyId: 'ab',
|
|
41
|
+
})
|
|
27
42
|
const relay = device('aabbccddeeff', 'AA:BB:CC:DD:EE:FF')
|
|
28
43
|
devices.push(relay)
|
|
29
44
|
await c.getDevice('aabbccddeeff')
|
|
30
|
-
expect(relay.setKey).toHaveBeenCalledWith({
|
|
45
|
+
expect(relay.setKey).toHaveBeenCalledWith({
|
|
46
|
+
encryptionKey: '0'.repeat(32),
|
|
47
|
+
keyId: 'ab',
|
|
48
|
+
})
|
|
31
49
|
})
|
|
32
50
|
|
|
33
51
|
it('applies a key immediately when the device already exists, and only once', async () => {
|
|
34
52
|
const relay = device('relay-1')
|
|
35
53
|
const c = client([relay])
|
|
36
|
-
c.setDeviceKey('relay-1', {
|
|
54
|
+
c.setDeviceKey('relay-1', {
|
|
55
|
+
encryptionKey: '1'.repeat(32),
|
|
56
|
+
keyId: '01',
|
|
57
|
+
})
|
|
37
58
|
await c.getDevice('relay-1')
|
|
38
59
|
await c.getDevices()
|
|
39
60
|
expect(relay.setKey).toHaveBeenCalledOnce()
|
|
@@ -53,7 +74,9 @@ describe('switchBotClient device keys', () => {
|
|
|
53
74
|
it('leaves devices without setKey (node-switchbot 5.x) alone', async () => {
|
|
54
75
|
const plain = { id: 'plain' }
|
|
55
76
|
const c = client([plain])
|
|
56
|
-
c.setDeviceKey('plain', {
|
|
77
|
+
c.setDeviceKey('plain', {
|
|
78
|
+
encryptionKey: '2'.repeat(32),
|
|
79
|
+
})
|
|
57
80
|
await expect(c.getDevice('plain')).resolves.toBe(plain)
|
|
58
81
|
})
|
|
59
82
|
})
|
|
@@ -5,7 +5,12 @@ import { SwitchBotClient } from '../../src/switchbotClient'
|
|
|
5
5
|
describe('switchBotClient OpenAPI fallback', () => {
|
|
6
6
|
it('should fallback to OpenAPI if node-switchbot fails to load', async () => {
|
|
7
7
|
// Simulate missing node-switchbot by making import throw
|
|
8
|
-
const logger = {
|
|
8
|
+
const logger = {
|
|
9
|
+
info: vi.fn(),
|
|
10
|
+
warn: vi.fn(),
|
|
11
|
+
error: vi.fn(),
|
|
12
|
+
debug: vi.fn(),
|
|
13
|
+
}
|
|
9
14
|
const cfg = { openApiToken: 'token', openApiSecret: 'secret', logger }
|
|
10
15
|
// Actually, we can't easily mock dynamic import, so just check that client still works
|
|
11
16
|
const client = new SwitchBotClient(cfg as any)
|
|
@@ -8,25 +8,44 @@ describe('switchBotClient', () => {
|
|
|
8
8
|
})
|
|
9
9
|
|
|
10
10
|
it('should initialize with logger and config', () => {
|
|
11
|
-
const logger = {
|
|
11
|
+
const logger = {
|
|
12
|
+
info: () => {},
|
|
13
|
+
warn: () => {},
|
|
14
|
+
error: () => {},
|
|
15
|
+
debug: () => {},
|
|
16
|
+
}
|
|
12
17
|
const cfg = { logger }
|
|
13
18
|
const client = new SwitchBotClient(cfg as any)
|
|
14
19
|
expect(client).toBeDefined()
|
|
15
20
|
})
|
|
16
21
|
|
|
17
22
|
it('should set custom debounce from config', () => {
|
|
18
|
-
const logger = {
|
|
23
|
+
const logger = {
|
|
24
|
+
info: () => {},
|
|
25
|
+
warn: () => {},
|
|
26
|
+
error: () => {},
|
|
27
|
+
debug: () => {},
|
|
28
|
+
}
|
|
19
29
|
const cfg = { logger, writeDebounceMs: 321 }
|
|
20
30
|
const client = new SwitchBotClient(cfg as any)
|
|
21
31
|
expect((client as any).writeDebounceMs).toBe(321)
|
|
22
32
|
})
|
|
23
33
|
|
|
24
34
|
it('should prefer managed devices before discovery', async () => {
|
|
25
|
-
const logger = {
|
|
35
|
+
const logger = {
|
|
36
|
+
info: vi.fn(),
|
|
37
|
+
warn: vi.fn(),
|
|
38
|
+
error: vi.fn(),
|
|
39
|
+
debug: vi.fn(),
|
|
40
|
+
}
|
|
26
41
|
const client = new SwitchBotClient({ logger } as any)
|
|
27
42
|
|
|
28
|
-
const managedDevices = [
|
|
29
|
-
|
|
43
|
+
const managedDevices = [
|
|
44
|
+
{ id: 'managed-1' },
|
|
45
|
+
]
|
|
46
|
+
const discover = vi.fn().mockResolvedValue([
|
|
47
|
+
{ id: 'discovered-1' },
|
|
48
|
+
])
|
|
30
49
|
;(client as any).client = {
|
|
31
50
|
devices: {
|
|
32
51
|
list: () => managedDevices,
|
|
@@ -44,7 +63,12 @@ describe('switchBotClient', () => {
|
|
|
44
63
|
})
|
|
45
64
|
|
|
46
65
|
it('should discover device when manager does not have it', async () => {
|
|
47
|
-
const logger = {
|
|
66
|
+
const logger = {
|
|
67
|
+
info: vi.fn(),
|
|
68
|
+
warn: vi.fn(),
|
|
69
|
+
error: vi.fn(),
|
|
70
|
+
debug: vi.fn(),
|
|
71
|
+
}
|
|
48
72
|
const client = new SwitchBotClient({ logger } as any)
|
|
49
73
|
|
|
50
74
|
const discovered = { id: 'abc123' }
|
|
@@ -5,13 +5,30 @@ import { BotDevice } from '../../src/devices/genericDevice.js'
|
|
|
5
5
|
import { SwitchBotHAPPlatform } from '../../src/SwitchBotHAPPlatform.js'
|
|
6
6
|
|
|
7
7
|
const id = 'bot-door-test'
|
|
8
|
-
const log = {
|
|
8
|
+
const log = {
|
|
9
|
+
info: vi.fn(),
|
|
10
|
+
warn: vi.fn(),
|
|
11
|
+
error: vi.fn(),
|
|
12
|
+
debug: vi.fn(),
|
|
13
|
+
}
|
|
9
14
|
|
|
10
15
|
function bot(config: Record<string, unknown>, result: unknown = { success: true }) {
|
|
11
16
|
const setDeviceState = vi.fn(async () => result)
|
|
12
17
|
const device = new BotDevice(
|
|
13
|
-
{
|
|
14
|
-
|
|
18
|
+
{
|
|
19
|
+
id,
|
|
20
|
+
type: 'Bot',
|
|
21
|
+
name: 'Entrance',
|
|
22
|
+
log,
|
|
23
|
+
blePollingEnabled: false,
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
log,
|
|
27
|
+
devices: [
|
|
28
|
+
{ deviceId: id, configDeviceType: 'Bot', ...config },
|
|
29
|
+
],
|
|
30
|
+
_client: { setDeviceState },
|
|
31
|
+
} as any,
|
|
15
32
|
)
|
|
16
33
|
return { device, setDeviceState }
|
|
17
34
|
}
|
|
@@ -19,14 +36,50 @@ function bot(config: Record<string, unknown>, result: unknown = { success: true
|
|
|
19
36
|
const displays = [
|
|
20
37
|
['switch', 'Switch', 'On', true, false],
|
|
21
38
|
['outlet', 'Outlet', 'On', true, false],
|
|
22
|
-
[
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
39
|
+
[
|
|
40
|
+
'door',
|
|
41
|
+
'Door',
|
|
42
|
+
'TargetPosition',
|
|
43
|
+
100,
|
|
44
|
+
0,
|
|
45
|
+
],
|
|
46
|
+
[
|
|
47
|
+
'window',
|
|
48
|
+
'Window',
|
|
49
|
+
'TargetPosition',
|
|
50
|
+
100,
|
|
51
|
+
0,
|
|
52
|
+
],
|
|
53
|
+
[
|
|
54
|
+
'windowcovering',
|
|
55
|
+
'WindowCovering',
|
|
56
|
+
'TargetPosition',
|
|
57
|
+
100,
|
|
58
|
+
0,
|
|
59
|
+
],
|
|
60
|
+
[
|
|
61
|
+
'garagedoor',
|
|
62
|
+
'GarageDoorOpener',
|
|
63
|
+
'TargetDoorState',
|
|
64
|
+
0,
|
|
65
|
+
1,
|
|
66
|
+
],
|
|
67
|
+
[
|
|
68
|
+
'lock',
|
|
69
|
+
'LockMechanism',
|
|
70
|
+
'LockTargetState',
|
|
71
|
+
0,
|
|
72
|
+
1,
|
|
73
|
+
],
|
|
27
74
|
['faucet', 'Faucet', 'Active', 1, 0],
|
|
28
75
|
['fan', 'Fanv2', 'Active', 1, 0],
|
|
29
|
-
[
|
|
76
|
+
[
|
|
77
|
+
'stateful',
|
|
78
|
+
'StatefulProgrammableSwitch',
|
|
79
|
+
'ProgrammableSwitchOutputState',
|
|
80
|
+
1,
|
|
81
|
+
0,
|
|
82
|
+
],
|
|
30
83
|
] as const
|
|
31
84
|
|
|
32
85
|
describe('bot HomeKit presentation', () => {
|
|
@@ -58,15 +111,32 @@ describe('bot HomeKit presentation', () => {
|
|
|
58
111
|
const { device } = bot({ botDisplay: display, mode: 'press' })
|
|
59
112
|
const accessory = new Accessory('Entrance', uuid.generate(id))
|
|
60
113
|
accessory.addService(Service.Switch)
|
|
61
|
-
const api = {
|
|
114
|
+
const api = {
|
|
115
|
+
hap: { Service, Characteristic, uuid },
|
|
116
|
+
registerPlatformAccessories: vi.fn(),
|
|
117
|
+
}
|
|
62
118
|
const platform = Object.create(SwitchBotHAPPlatform.prototype) as any
|
|
63
119
|
platform.api = api
|
|
64
120
|
platform.log = log
|
|
65
|
-
platform.accessories = new Map([
|
|
121
|
+
platform.accessories = new Map([
|
|
122
|
+
[
|
|
123
|
+
uuid.generate(id),
|
|
124
|
+
accessory,
|
|
125
|
+
],
|
|
126
|
+
])
|
|
66
127
|
platform.hapPushTargets = new Map()
|
|
67
|
-
const created = {
|
|
128
|
+
const created = {
|
|
129
|
+
instance: device,
|
|
130
|
+
createAccessory: () => device.createHAPAccessory(api),
|
|
131
|
+
}
|
|
68
132
|
|
|
69
|
-
await platform.registerHAPAccessories([
|
|
133
|
+
await platform.registerHAPAccessories([
|
|
134
|
+
{
|
|
135
|
+
created,
|
|
136
|
+
d: { id, name: 'Entrance' },
|
|
137
|
+
type: 'Bot',
|
|
138
|
+
},
|
|
139
|
+
])
|
|
70
140
|
|
|
71
141
|
const service = accessory.getService((Service as any)[serviceType])!
|
|
72
142
|
expect(service).toBeDefined()
|
|
@@ -110,7 +180,11 @@ describe('bot HomeKit presentation', () => {
|
|
|
110
180
|
|
|
111
181
|
it('reloads a Bot when its botMode alias changes', async () => {
|
|
112
182
|
const platform = Object.create(SwitchBotHAPPlatform.prototype) as any
|
|
113
|
-
platform.config = {
|
|
183
|
+
platform.config = {
|
|
184
|
+
devices: [
|
|
185
|
+
{ deviceId: id, configDeviceType: 'Bot' },
|
|
186
|
+
],
|
|
187
|
+
}
|
|
114
188
|
platform.lastConfigHash = platform.getConfigHash()
|
|
115
189
|
platform.config.devices[0].botMode = 'press'
|
|
116
190
|
platform.log = log
|
|
@@ -125,7 +199,11 @@ describe('bot HomeKit presentation', () => {
|
|
|
125
199
|
|
|
126
200
|
it('reloads a Bot when its physical mode changes', async () => {
|
|
127
201
|
const platform = Object.create(SwitchBotHAPPlatform.prototype) as any
|
|
128
|
-
platform.config = {
|
|
202
|
+
platform.config = {
|
|
203
|
+
devices: [
|
|
204
|
+
{ deviceId: id, configDeviceType: 'Bot', mode: 'switch' },
|
|
205
|
+
],
|
|
206
|
+
}
|
|
129
207
|
platform.lastConfigHash = platform.getConfigHash()
|
|
130
208
|
platform.config.devices[0].mode = 'press'
|
|
131
209
|
platform.log = log
|
|
@@ -141,14 +219,31 @@ describe('bot HomeKit presentation', () => {
|
|
|
141
219
|
it('emits each Stateful Programmable Switch press as an event', async () => {
|
|
142
220
|
const { device, setDeviceState } = bot({ botDisplay: 'stateful', mode: 'press' })
|
|
143
221
|
const accessory = new Accessory('Entrance', uuid.generate(id))
|
|
144
|
-
const api = {
|
|
222
|
+
const api = {
|
|
223
|
+
hap: { Service, Characteristic, uuid },
|
|
224
|
+
registerPlatformAccessories: vi.fn(),
|
|
225
|
+
}
|
|
145
226
|
const platform = Object.create(SwitchBotHAPPlatform.prototype) as any
|
|
146
227
|
platform.api = api
|
|
147
228
|
platform.log = log
|
|
148
|
-
platform.accessories = new Map([
|
|
229
|
+
platform.accessories = new Map([
|
|
230
|
+
[
|
|
231
|
+
uuid.generate(id),
|
|
232
|
+
accessory,
|
|
233
|
+
],
|
|
234
|
+
])
|
|
149
235
|
platform.hapPushTargets = new Map()
|
|
150
|
-
const created = {
|
|
151
|
-
|
|
236
|
+
const created = {
|
|
237
|
+
instance: device,
|
|
238
|
+
createAccessory: () => device.createHAPAccessory(api),
|
|
239
|
+
}
|
|
240
|
+
await platform.registerHAPAccessories([
|
|
241
|
+
{
|
|
242
|
+
created,
|
|
243
|
+
d: { id, name: 'Entrance' },
|
|
244
|
+
type: 'Bot',
|
|
245
|
+
},
|
|
246
|
+
])
|
|
152
247
|
|
|
153
248
|
const service = accessory.getService(Service.StatefulProgrammableSwitch)!
|
|
154
249
|
const event = service.getCharacteristic(Characteristic.ProgrammableSwitchEvent)
|
|
@@ -199,11 +294,25 @@ describe('bot HomeKit presentation', () => {
|
|
|
199
294
|
const platform = Object.create(SwitchBotHAPPlatform.prototype) as any
|
|
200
295
|
platform.api = api
|
|
201
296
|
platform.log = log
|
|
202
|
-
platform.accessories = new Map([
|
|
297
|
+
platform.accessories = new Map([
|
|
298
|
+
[
|
|
299
|
+
uuid.generate(id),
|
|
300
|
+
accessory,
|
|
301
|
+
],
|
|
302
|
+
])
|
|
203
303
|
platform.hapPushTargets = new Map()
|
|
204
|
-
const created = {
|
|
304
|
+
const created = {
|
|
305
|
+
instance: device,
|
|
306
|
+
createAccessory: () => device.createHAPAccessory(api),
|
|
307
|
+
}
|
|
205
308
|
|
|
206
|
-
await platform.registerHAPAccessories([
|
|
309
|
+
await platform.registerHAPAccessories([
|
|
310
|
+
{
|
|
311
|
+
created,
|
|
312
|
+
d: { id, name: 'Entrance' },
|
|
313
|
+
type: 'Bot',
|
|
314
|
+
},
|
|
315
|
+
])
|
|
207
316
|
|
|
208
317
|
expect(accessory.getService(Service.Switch)).toBeUndefined()
|
|
209
318
|
const door = accessory.getService(Service.Door)!
|
|
@@ -9,9 +9,21 @@ describe('curtain BLE position', () => {
|
|
|
9
9
|
hasBLE: () => true,
|
|
10
10
|
getInfo: () => ({ bleServiceData: advertisement }),
|
|
11
11
|
}
|
|
12
|
-
const client = {
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
const client = {
|
|
13
|
+
getCachedDevice: () => nodeDevice,
|
|
14
|
+
}
|
|
15
|
+
const log = {
|
|
16
|
+
info: vi.fn(),
|
|
17
|
+
warn: vi.fn(),
|
|
18
|
+
error: vi.fn(),
|
|
19
|
+
debug: vi.fn(),
|
|
20
|
+
}
|
|
21
|
+
const curtain = new CurtainDevice({
|
|
22
|
+
id: 'curtain',
|
|
23
|
+
type: 'Curtain',
|
|
24
|
+
log,
|
|
25
|
+
blePollingEnabled: false,
|
|
26
|
+
}, { _client: client, log } as any)
|
|
15
27
|
const getState = vi.spyOn(curtain, 'getState')
|
|
16
28
|
vi.spyOn(curtain, 'setState').mockResolvedValue(true)
|
|
17
29
|
const characteristics = curtain.createHAPAccessory({}).services[0].characteristics
|
|
@@ -3,7 +3,12 @@ import { describe, expect, it } from 'vitest'
|
|
|
3
3
|
import { DeviceBase } from '../../src/devices/deviceBase'
|
|
4
4
|
|
|
5
5
|
describe('deviceBase', () => {
|
|
6
|
-
const mockLogger = {
|
|
6
|
+
const mockLogger = {
|
|
7
|
+
info: () => {},
|
|
8
|
+
warn: () => {},
|
|
9
|
+
error: () => {},
|
|
10
|
+
debug: () => {},
|
|
11
|
+
}
|
|
7
12
|
|
|
8
13
|
// Concrete subclass for testing abstract DeviceBase
|
|
9
14
|
class TestDevice extends DeviceBase {
|
|
@@ -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()
|