@switchbot/homebridge-switchbot 5.2.9-beta.1 → 5.2.10-beta.0

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.
Files changed (85) hide show
  1. package/dist/devices/genericDevice.d.ts.map +1 -1
  2. package/dist/devices/genericDevice.js +528 -118
  3. package/dist/devices/genericDevice.js.map +1 -1
  4. package/dist/homebridge-ui/public/js/advanced-settings.d.ts.map +1 -1
  5. package/dist/homebridge-ui/public/js/advanced-settings.js +3 -1
  6. package/dist/homebridge-ui/public/js/advanced-settings.js.map +1 -1
  7. package/dist/homebridge-ui/public/js/advanced-settings.ts +3 -1
  8. package/dist/homebridge-ui/public/js/api.d.ts.map +1 -1
  9. package/dist/homebridge-ui/public/js/api.js +21 -7
  10. package/dist/homebridge-ui/public/js/api.js.map +1 -1
  11. package/dist/homebridge-ui/public/js/api.ts +21 -7
  12. package/dist/homebridge-ui/public/js/app.js +74 -16
  13. package/dist/homebridge-ui/public/js/app.js.map +2 -2
  14. package/dist/homebridge-ui/public/js/credentials.d.ts.map +1 -1
  15. package/dist/homebridge-ui/public/js/credentials.js +9 -3
  16. package/dist/homebridge-ui/public/js/credentials.js.map +1 -1
  17. package/dist/homebridge-ui/public/js/credentials.ts +9 -3
  18. package/dist/homebridge-ui/public/js/devices-delete.d.ts.map +1 -1
  19. package/dist/homebridge-ui/public/js/devices-delete.js +6 -2
  20. package/dist/homebridge-ui/public/js/devices-delete.js.map +1 -1
  21. package/dist/homebridge-ui/public/js/devices-delete.ts +6 -2
  22. package/dist/homebridge-ui/public/js/devices.d.ts.map +1 -1
  23. package/dist/homebridge-ui/public/js/devices.js +3 -1
  24. package/dist/homebridge-ui/public/js/devices.js.map +1 -1
  25. package/dist/homebridge-ui/public/js/devices.ts +3 -1
  26. package/dist/homebridge-ui/public/js/discovery.d.ts.map +1 -1
  27. package/dist/homebridge-ui/public/js/discovery.js +89 -25
  28. package/dist/homebridge-ui/public/js/discovery.js.map +1 -1
  29. package/dist/homebridge-ui/public/js/discovery.ts +89 -25
  30. package/dist/homebridge-ui/public/js/modals.d.ts.map +1 -1
  31. package/dist/homebridge-ui/public/js/modals.js +13 -3
  32. package/dist/homebridge-ui/public/js/modals.js.map +1 -1
  33. package/dist/homebridge-ui/public/js/modals.ts +13 -3
  34. package/dist/homebridge-ui/public/js/render.d.ts.map +1 -1
  35. package/dist/homebridge-ui/public/js/render.js +67 -17
  36. package/dist/homebridge-ui/public/js/render.js.map +1 -1
  37. package/dist/homebridge-ui/public/js/render.ts +67 -17
  38. package/dist/homebridge-ui/public/js/toast.d.ts.map +1 -1
  39. package/dist/homebridge-ui/public/js/toast.js +6 -2
  40. package/dist/homebridge-ui/public/js/toast.js.map +1 -1
  41. package/dist/homebridge-ui/public/js/toast.ts +6 -2
  42. package/docs/variables/default.html +1 -1
  43. package/package.json +9 -9
  44. package/src/devices/genericDevice.ts +529 -129
  45. package/src/homebridge-ui/public/js/advanced-settings.ts +3 -1
  46. package/src/homebridge-ui/public/js/api.ts +21 -7
  47. package/src/homebridge-ui/public/js/credentials.ts +9 -3
  48. package/src/homebridge-ui/public/js/devices-delete.ts +6 -2
  49. package/src/homebridge-ui/public/js/devices.ts +3 -1
  50. package/src/homebridge-ui/public/js/discovery.ts +89 -25
  51. package/src/homebridge-ui/public/js/modals.ts +13 -3
  52. package/src/homebridge-ui/public/js/render.ts +67 -17
  53. package/src/homebridge-ui/public/js/toast.ts +6 -2
  54. package/test/client/node-switchbot-commands.spec.ts +44 -8
  55. package/test/client/switchbot-client-debounce.spec.ts +24 -3
  56. package/test/client/switchbot-client-discovery.spec.ts +26 -5
  57. package/test/client/switchbot-client-keys.spec.ts +30 -7
  58. package/test/client/switchbot-client-openapi.spec.ts +6 -1
  59. package/test/client/switchbotClient.spec.ts +30 -6
  60. package/test/device/bot-hap.spec.ts +131 -22
  61. package/test/device/curtain-ble-position.spec.ts +15 -3
  62. package/test/device/deviceBase.spec.ts +6 -1
  63. package/test/device/deviceFactory-edge.spec.ts +12 -2
  64. package/test/device/deviceFactory.spec.ts +6 -1
  65. package/test/device/fan-swing.spec.ts +9 -2
  66. package/test/device/genericDevice-blepoll.spec.ts +17 -3
  67. package/test/device/getstate-cache.spec.ts +37 -6
  68. package/test/device/getstate-returns-status.spec.ts +65 -13
  69. package/test/device/lock-users.spec.ts +24 -7
  70. package/test/device/matter-descriptors.spec.ts +6 -1
  71. package/test/device/matter-device-state.spec.ts +6 -1
  72. package/test/device/meter-battery.spec.ts +86 -14
  73. package/test/device/meter-co2.spec.ts +65 -10
  74. package/test/device/model-identities.spec.ts +6 -1
  75. package/test/device/water-detector-battery.spec.ts +19 -3
  76. package/test/device/water-detector-leak.spec.ts +6 -1
  77. package/test/helpers/matter-harness.ts +21 -4
  78. package/test/platform/accessory-restore.spec.ts +18 -3
  79. package/test/platform/hap-cache-restart.spec.ts +66 -13
  80. package/test/platform/matter-childbridge.spec.ts +16 -3
  81. package/test/platform/matter-integration.spec.ts +6 -1
  82. package/test/platform/matter-registration.spec.ts +47 -10
  83. package/test/platform/platform-edge.spec.ts +13 -3
  84. package/test/platform/platform.integration.spec.ts +16 -3
  85. package/test/utils/utils.spec.ts +42 -8
@@ -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({ logger: { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} } } as any)
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({ logger: { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} } } as any)
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({ logger: { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} } } as any)
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({ logger: { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} } } as any)
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 = { discover, devices: { get: () => undefined, list: () => devices } }
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([{ id: 'present' }])
21
- await Promise.all([c.getDevice('missing-1'), c.getDevice('missing-2'), c.getDevice('missing-3')])
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([{ id: 'present' }])
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 = { info: () => {}, warn: vi.fn(), error: () => {}, debug: () => {} }
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: { get: (id: string) => devices.find(d => d.id === id), list: () => 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 { id, setKey: vi.fn(), getInfo: () => ({ id, mac }) }
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', { encryptionKey: '0'.repeat(32), keyId: 'ab' })
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({ encryptionKey: '0'.repeat(32), keyId: 'ab' })
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', { encryptionKey: '1'.repeat(32), keyId: '01' })
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', { encryptionKey: '2'.repeat(32) })
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 = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }
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 = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }
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 = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }
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 = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }
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 = [{ id: 'managed-1' }]
29
- const discover = vi.fn().mockResolvedValue([{ id: 'discovered-1' }])
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 = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }
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 = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }
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
- { id, type: 'Bot', name: 'Entrance', log, blePollingEnabled: false },
14
- { log, devices: [{ deviceId: id, configDeviceType: 'Bot', ...config }], _client: { setDeviceState } } as any,
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
- ['door', 'Door', 'TargetPosition', 100, 0],
23
- ['window', 'Window', 'TargetPosition', 100, 0],
24
- ['windowcovering', 'WindowCovering', 'TargetPosition', 100, 0],
25
- ['garagedoor', 'GarageDoorOpener', 'TargetDoorState', 0, 1],
26
- ['lock', 'LockMechanism', 'LockTargetState', 0, 1],
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
- ['stateful', 'StatefulProgrammableSwitch', 'ProgrammableSwitchOutputState', 1, 0],
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 = { hap: { Service, Characteristic, uuid }, registerPlatformAccessories: vi.fn() }
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([[uuid.generate(id), accessory]])
121
+ platform.accessories = new Map([
122
+ [
123
+ uuid.generate(id),
124
+ accessory,
125
+ ],
126
+ ])
66
127
  platform.hapPushTargets = new Map()
67
- const created = { instance: device, createAccessory: () => device.createHAPAccessory(api) }
128
+ const created = {
129
+ instance: device,
130
+ createAccessory: () => device.createHAPAccessory(api),
131
+ }
68
132
 
69
- await platform.registerHAPAccessories([{ created, d: { id, name: 'Entrance' }, type: 'Bot' }])
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 = { devices: [{ deviceId: id, configDeviceType: 'Bot' }] }
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 = { devices: [{ deviceId: id, configDeviceType: 'Bot', mode: 'switch' }] }
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 = { hap: { Service, Characteristic, uuid }, registerPlatformAccessories: vi.fn() }
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([[uuid.generate(id), accessory]])
229
+ platform.accessories = new Map([
230
+ [
231
+ uuid.generate(id),
232
+ accessory,
233
+ ],
234
+ ])
149
235
  platform.hapPushTargets = new Map()
150
- const created = { instance: device, createAccessory: () => device.createHAPAccessory(api) }
151
- await platform.registerHAPAccessories([{ created, d: { id, name: 'Entrance' }, type: 'Bot' }])
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([[uuid.generate(id), accessory]])
297
+ platform.accessories = new Map([
298
+ [
299
+ uuid.generate(id),
300
+ accessory,
301
+ ],
302
+ ])
203
303
  platform.hapPushTargets = new Map()
204
- const created = { instance: device, createAccessory: () => device.createHAPAccessory(api) }
304
+ const created = {
305
+ instance: device,
306
+ createAccessory: () => device.createHAPAccessory(api),
307
+ }
205
308
 
206
- await platform.registerHAPAccessories([{ created, d: { id, name: 'Entrance' }, type: 'Bot' }])
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 = { getCachedDevice: () => nodeDevice }
13
- const log = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }
14
- const curtain = new CurtainDevice({ id: 'curtain', type: 'Curtain', log, blePollingEnabled: false }, { _client: client, log } as any)
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 = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }
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 = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }
11
- const result = await createDevice({ id: 'abc', type: 'Bot', protocol: 'unknown', log: mockLogger }, { logger: mockLogger } as any, false)
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 = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }
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 = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }
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 = { setDeviceState: vi.fn().mockResolvedValue({ status: 'success' }) }
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 ? undefined : { hasBLE: () => hasBLE }
55
- const client = { devices: { get: () => nodeDevice }, getDevice: vi.fn(async () => nodeDevice) }
56
- const d = new GenericDevice({ id: 'dev', type: 'Unknown', log: mockLogger, blePollIntervalMs: 60_000 }, { log: mockLogger, _client: client, ...cfg } as any)
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 = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), success: vi.fn() }
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 () => ({ getStatus: async () => ({ temperature: 22.7, humidity: 59 }) }))
14
- const d = new GenericDevice({ id: 'B0E9FED044E3', type: 'meter', name: 'M', log }, { log, _client: { getDevice } } as any)
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([d.getState(), d.getState(), d.getState(), d.getState()])
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({ getStatus: async () => ({ temperature: 21 }) })
54
- const d = new GenericDevice({ id: 'x', type: 'meter', name: 'M', log }, { log, _client: { getDevice } } as any)
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()