@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.
Files changed (115) hide show
  1. package/config.schema.json +29 -0
  2. package/dist/SwitchBotMatterPlatform.js +1 -1
  3. package/dist/SwitchBotMatterPlatform.js.map +1 -1
  4. package/dist/deviceFactory.js +1 -1
  5. package/dist/deviceFactory.js.map +1 -1
  6. package/dist/devices/genericDevice.d.ts +2 -0
  7. package/dist/devices/genericDevice.d.ts.map +1 -1
  8. package/dist/devices/genericDevice.js +573 -123
  9. package/dist/devices/genericDevice.js.map +1 -1
  10. package/dist/homebridge-ui/endpoints/discovery.d.ts.map +1 -1
  11. package/dist/homebridge-ui/endpoints/discovery.js +2 -0
  12. package/dist/homebridge-ui/endpoints/discovery.js.map +1 -1
  13. package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js +2 -0
  14. package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js.map +1 -1
  15. package/dist/homebridge-ui/public/index.html +12 -2
  16. package/dist/homebridge-ui/public/js/advanced-settings.d.ts +4 -1
  17. package/dist/homebridge-ui/public/js/advanced-settings.d.ts.map +1 -1
  18. package/dist/homebridge-ui/public/js/advanced-settings.js +21 -11
  19. package/dist/homebridge-ui/public/js/advanced-settings.js.map +1 -1
  20. package/dist/homebridge-ui/public/js/advanced-settings.ts +23 -11
  21. package/dist/homebridge-ui/public/js/api.d.ts.map +1 -1
  22. package/dist/homebridge-ui/public/js/api.js +21 -7
  23. package/dist/homebridge-ui/public/js/api.js.map +1 -1
  24. package/dist/homebridge-ui/public/js/api.ts +21 -7
  25. package/dist/homebridge-ui/public/js/app.js +78 -20
  26. package/dist/homebridge-ui/public/js/app.js.map +2 -2
  27. package/dist/homebridge-ui/public/js/credentials.d.ts.map +1 -1
  28. package/dist/homebridge-ui/public/js/credentials.js +9 -3
  29. package/dist/homebridge-ui/public/js/credentials.js.map +1 -1
  30. package/dist/homebridge-ui/public/js/credentials.ts +9 -3
  31. package/dist/homebridge-ui/public/js/devices-delete.d.ts.map +1 -1
  32. package/dist/homebridge-ui/public/js/devices-delete.js +6 -2
  33. package/dist/homebridge-ui/public/js/devices-delete.js.map +1 -1
  34. package/dist/homebridge-ui/public/js/devices-delete.ts +6 -2
  35. package/dist/homebridge-ui/public/js/devices.d.ts.map +1 -1
  36. package/dist/homebridge-ui/public/js/devices.js +3 -1
  37. package/dist/homebridge-ui/public/js/devices.js.map +1 -1
  38. package/dist/homebridge-ui/public/js/devices.ts +3 -1
  39. package/dist/homebridge-ui/public/js/discovery.d.ts.map +1 -1
  40. package/dist/homebridge-ui/public/js/discovery.js +89 -25
  41. package/dist/homebridge-ui/public/js/discovery.js.map +1 -1
  42. package/dist/homebridge-ui/public/js/discovery.ts +89 -25
  43. package/dist/homebridge-ui/public/js/modals.d.ts.map +1 -1
  44. package/dist/homebridge-ui/public/js/modals.js +15 -5
  45. package/dist/homebridge-ui/public/js/modals.js.map +1 -1
  46. package/dist/homebridge-ui/public/js/modals.ts +15 -5
  47. package/dist/homebridge-ui/public/js/render.d.ts.map +1 -1
  48. package/dist/homebridge-ui/public/js/render.js +67 -17
  49. package/dist/homebridge-ui/public/js/render.js.map +1 -1
  50. package/dist/homebridge-ui/public/js/render.ts +67 -17
  51. package/dist/homebridge-ui/public/js/toast.d.ts.map +1 -1
  52. package/dist/homebridge-ui/public/js/toast.js +6 -2
  53. package/dist/homebridge-ui/public/js/toast.js.map +1 -1
  54. package/dist/homebridge-ui/public/js/toast.ts +6 -2
  55. package/dist/homebridge-ui/settings.js.map +1 -1
  56. package/dist/homebridge-ui/switchbotClient.js +4 -0
  57. package/dist/homebridge-ui/switchbotClient.js.map +1 -1
  58. package/dist/settings.d.ts +1 -0
  59. package/dist/settings.d.ts.map +1 -1
  60. package/dist/settings.js.map +1 -1
  61. package/dist/switchbotClient.d.ts +1 -0
  62. package/dist/switchbotClient.d.ts.map +1 -1
  63. package/dist/switchbotClient.js +4 -0
  64. package/dist/switchbotClient.js.map +1 -1
  65. package/docs/variables/default.html +1 -1
  66. package/package.json +9 -9
  67. package/src/SwitchBotMatterPlatform.ts +1 -1
  68. package/src/deviceFactory.ts +1 -1
  69. package/src/devices/genericDevice.ts +572 -134
  70. package/src/homebridge-ui/endpoints/discovery.ts +2 -0
  71. package/src/homebridge-ui/public/index.html +12 -2
  72. package/src/homebridge-ui/public/js/advanced-settings.ts +23 -11
  73. package/src/homebridge-ui/public/js/api.ts +21 -7
  74. package/src/homebridge-ui/public/js/credentials.ts +9 -3
  75. package/src/homebridge-ui/public/js/devices-delete.ts +6 -2
  76. package/src/homebridge-ui/public/js/devices.ts +3 -1
  77. package/src/homebridge-ui/public/js/discovery.ts +89 -25
  78. package/src/homebridge-ui/public/js/modals.ts +15 -5
  79. package/src/homebridge-ui/public/js/render.ts +67 -17
  80. package/src/homebridge-ui/public/js/toast.ts +6 -2
  81. package/src/settings.ts +1 -0
  82. package/src/switchbotClient.ts +5 -0
  83. package/test/client/node-switchbot-commands.spec.ts +44 -8
  84. package/test/client/switchbot-client-debounce.spec.ts +24 -3
  85. package/test/client/switchbot-client-discovery.spec.ts +26 -5
  86. package/test/client/switchbot-client-keys.spec.ts +30 -7
  87. package/test/client/switchbot-client-openapi.spec.ts +6 -1
  88. package/test/client/switchbotClient.spec.ts +30 -6
  89. package/test/device/bot-hap.spec.ts +131 -22
  90. package/test/device/curtain-ble-position.spec.ts +46 -0
  91. package/test/device/deviceBase.spec.ts +6 -1
  92. package/test/device/deviceFactory-edge.spec.ts +12 -2
  93. package/test/device/deviceFactory.spec.ts +6 -1
  94. package/test/device/fan-swing.spec.ts +9 -2
  95. package/test/device/genericDevice-blepoll.spec.ts +17 -3
  96. package/test/device/getstate-cache.spec.ts +37 -6
  97. package/test/device/getstate-returns-status.spec.ts +65 -13
  98. package/test/device/lock-users.spec.ts +24 -7
  99. package/test/device/matter-descriptors.spec.ts +6 -1
  100. package/test/device/matter-device-state.spec.ts +6 -1
  101. package/test/device/meter-battery.spec.ts +86 -14
  102. package/test/device/meter-co2.spec.ts +65 -10
  103. package/test/device/model-identities.spec.ts +6 -1
  104. package/test/device/water-detector-battery.spec.ts +19 -3
  105. package/test/device/water-detector-leak.spec.ts +6 -1
  106. package/test/helpers/matter-harness.ts +21 -4
  107. package/test/platform/accessory-restore.spec.ts +18 -3
  108. package/test/platform/hap-cache-restart.spec.ts +66 -13
  109. package/test/platform/matter-childbridge.spec.ts +16 -3
  110. package/test/platform/matter-integration.spec.ts +6 -1
  111. package/test/platform/matter-registration.spec.ts +47 -10
  112. package/test/platform/platform-edge.spec.ts +13 -3
  113. package/test/platform/platform.integration.spec.ts +16 -3
  114. package/test/utils/utils.spec.ts +42 -8
  115. package/.github/npm-version-script-esm.js +0 -103
@@ -8,8 +8,12 @@ function showToast(
8
8
  ): void {
9
9
  try {
10
10
  // Defensive: check for window and homebridge existence
11
- const hb = typeof window !== 'undefined' ? (window as any).homebridge : undefined
12
- const toast = hb && typeof hb.toast === 'object' ? hb.toast : undefined
11
+ const hb = typeof window !== 'undefined'
12
+ ? (window as any).homebridge
13
+ : undefined
14
+ const toast = hb && typeof hb.toast === 'object'
15
+ ? hb.toast
16
+ : undefined
13
17
  if (toast && typeof toast[method] === 'function') {
14
18
  try {
15
19
  toast[method](message, title)
package/src/settings.ts CHANGED
@@ -7,6 +7,7 @@ export interface SwitchBotPluginConfig {
7
7
  preferMatter?: boolean
8
8
  enableMatter?: boolean
9
9
  enableBLE?: boolean // Enable or disable BLE support
10
+ nodeSwitchBotLogLevel?: 0 | 1 | 2 | 3 | 4
10
11
  // other plugin-specific configuration
11
12
  [key: string]: any
12
13
  }
@@ -81,6 +81,7 @@ export class SwitchBotClient implements ISwitchBotClient {
81
81
  enableBLE: this.cfg.enableBLE !== false, // Use config value, default true
82
82
  scanTimeout,
83
83
  ...rawNodeClientConfig,
84
+ logLevel: this.cfg.nodeSwitchBotLogLevel ?? rawNodeClientConfig.logLevel,
84
85
  })
85
86
  this.lastDiscoveryAt = 0
86
87
  this.logger?.info?.('SwitchBot client initialized with native resilience features')
@@ -159,6 +160,10 @@ export class SwitchBotClient implements ISwitchBotClient {
159
160
  throw new SwitchbotOperationError('No SwitchBot client available', 'no_client')
160
161
  }
161
162
 
163
+ getCachedDevice(id: string): any {
164
+ return this.getManagedDevice(id)
165
+ }
166
+
162
167
  async getDevices(): Promise<any[]> {
163
168
  if (this.client) {
164
169
  try {
@@ -4,16 +4,34 @@ import { describe, expect, it, vi } from 'vitest'
4
4
  import { hsToRgb, toKelvin, toRgb } from '../../src/deviceCommandMapper'
5
5
  import { SwitchBotClient } from '../../src/switchbotClient'
6
6
 
7
- const logger = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }
7
+ const logger = {
8
+ info: () => {},
9
+ warn: () => {},
10
+ error: () => {},
11
+ debug: () => {},
12
+ }
8
13
 
9
14
  // Drive real node-switchbot devices (OpenAPI only) through the plugin client and record what
10
15
  // reaches the OpenAPI. Commands used to be looked up by device type string, and most real types
11
16
  // ("Plug Mini (US)", "Strip Light", "Lock Pro", ...) had no entry, so they failed as unsupported.
12
17
  async function send(deviceType: string, ...bodies: any[]) {
13
- const switchbot = new SwitchBot({ enableBLE: false, token: 'token', secret: 'secret', enableRetry: false })
18
+ const switchbot = new SwitchBot({
19
+ enableBLE: false,
20
+ token: 'token',
21
+ secret: 'secret',
22
+ enableRetry: false,
23
+ })
14
24
  const api = switchbot.getAPIClient()!
15
25
  vi.spyOn(api, 'getDevices').mockResolvedValue({
16
- deviceList: [{ deviceId: 'DEV', deviceName: 'Test', deviceType, enableCloudService: true, hubDeviceId: 'HUB' }],
26
+ deviceList: [
27
+ {
28
+ deviceId: 'DEV',
29
+ deviceName: 'Test',
30
+ deviceType,
31
+ enableCloudService: true,
32
+ hubDeviceId: 'HUB',
33
+ },
34
+ ],
17
35
  infraredRemoteList: [],
18
36
  } as any)
19
37
  const sendCommand = vi.spyOn(api, 'sendCommand').mockResolvedValue({ statusCode: 100, message: 'success', body: {} } as any)
@@ -26,8 +44,19 @@ async function send(deviceType: string, ...bodies: any[]) {
26
44
  }
27
45
 
28
46
  describe('commands reach node-switchbot devices', () => {
29
- it.each(['Plug Mini (US)', 'Plug Mini (JP)', 'Plug', 'Relay Switch 1', 'Strip Light', 'Color Bulb', 'Ceiling Light'])('%s turns on and off', async (type) => {
30
- expect(await send(type, { command: 'turnOn' }, { command: 'turnOff' })).toEqual([['turnOn', undefined], ['turnOff', undefined]])
47
+ it.each([
48
+ 'Plug Mini (US)',
49
+ 'Plug Mini (JP)',
50
+ 'Plug',
51
+ 'Relay Switch 1',
52
+ 'Strip Light',
53
+ 'Color Bulb',
54
+ 'Ceiling Light',
55
+ ])('%s turns on and off', async (type) => {
56
+ expect(await send(type, { command: 'turnOn' }, { command: 'turnOff' })).toEqual([
57
+ ['turnOn', undefined],
58
+ ['turnOff', undefined],
59
+ ])
31
60
  })
32
61
 
33
62
  it('lock Pro locks and unlocks', async () => {
@@ -51,12 +80,19 @@ describe('commands reach node-switchbot devices', () => {
51
80
  })
52
81
 
53
82
  it('curtain and Blind Tilt set their position', async () => {
54
- expect(await send('Curtain3', { command: 'setPosition', parameter: '30' })).toEqual([['setPosition', '0,ff,30']])
55
- expect(await send('Blind Tilt', { command: 'setPosition', parameter: '50' })).toEqual([['setPosition', 'down;100']])
83
+ expect(await send('Curtain3', { command: 'setPosition', parameter: '30' })).toEqual([
84
+ ['setPosition', '0,ff,30'],
85
+ ])
86
+ expect(await send('Blind Tilt', { command: 'setPosition', parameter: '50' })).toEqual([
87
+ ['setPosition', 'down;100'],
88
+ ])
56
89
  })
57
90
 
58
91
  it('a vacuum starts and docks', async () => {
59
- expect(await send('K10+', { command: 'start' }, { command: 'dock' })).toEqual([['start', undefined], ['dock', undefined]])
92
+ expect(await send('K10+', { command: 'start' }, { command: 'dock' })).toEqual([
93
+ ['start', undefined],
94
+ ['dock', undefined],
95
+ ])
60
96
  })
61
97
 
62
98
  it('an unsupported command is rejected', async () => {
@@ -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)!
@@ -0,0 +1,46 @@
1
+ import { describe, expect, it, vi } from 'vitest'
2
+
3
+ import { CurtainDevice } from '../../src/devices/genericDevice'
4
+
5
+ describe('curtain BLE position', () => {
6
+ it('keeps a commanded position when an old advertisement reappears', async () => {
7
+ let advertisement = { position: 39 }
8
+ const nodeDevice = {
9
+ hasBLE: () => true,
10
+ getInfo: () => ({ bleServiceData: advertisement }),
11
+ }
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)
27
+ const getState = vi.spyOn(curtain, 'getState')
28
+ vi.spyOn(curtain, 'setState').mockResolvedValue(true)
29
+ const characteristics = curtain.createHAPAccessory({}).services[0].characteristics
30
+
31
+ expect(await characteristics.CurrentPosition.get()).toBe(61)
32
+ await characteristics.TargetPosition.set(69)
33
+ expect(await characteristics.CurrentPosition.get()).toBe(69)
34
+
35
+ advertisement = { position: 39 }
36
+ expect(await characteristics.CurrentPosition.get()).toBe(69)
37
+ expect(await characteristics.TargetPosition.get()).toBe(69)
38
+
39
+ advertisement = { position: 31 }
40
+ expect(await characteristics.CurrentPosition.get()).toBe(69)
41
+
42
+ advertisement = { position: 39 }
43
+ expect(await characteristics.CurrentPosition.get()).toBe(69)
44
+ expect(getState).not.toHaveBeenCalled()
45
+ })
46
+ })
@@ -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 {