@switchbot/homebridge-switchbot 4.2.0 → 4.3.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.
package/src/platform.ts CHANGED
@@ -11,13 +11,12 @@ import type { MqttClient } from 'mqtt'
11
11
  * import type { blindTilt, curtain, curtain3, device, irdevice } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
12
12
  * import { LogLevel, SwitchBotBLE, SwitchBotModel, SwitchBotOpenAPI } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
13
13
  */
14
- import type { blindTilt, bodyChange, curtain, curtain3, device, deviceStatus, deviceStatusRequest, irdevice } from 'node-switchbot'
14
+ import type { blindTilt, bodyChange, curtain, curtain3, device, deviceStatusRequest, irdevice } from 'node-switchbot'
15
15
 
16
16
  import type { blindTiltConfig, curtainConfig, devicesConfig, irDevicesConfig, options, SwitchBotPlatformConfig } from './settings.js'
17
17
 
18
18
  import { readFileSync } from 'node:fs'
19
- import { hostname } from 'node:os'
20
- import process, { argv } from 'node:process'
19
+ import { argv } from 'node:process'
21
20
 
22
21
  import asyncmqtt from 'async-mqtt'
23
22
  import fakegato from 'fakegato-history'
@@ -42,6 +41,7 @@ import { MeterPlus } from './device/meterplus.js'
42
41
  import { MeterPro } from './device/meterpro.js'
43
42
  import { Motion } from './device/motion.js'
44
43
  import { Plug } from './device/plug.js'
44
+ import { RelaySwitch } from './device/relayswitch.js'
45
45
  import { RobotVacuumCleaner } from './device/robotvacuumcleaner.js'
46
46
  import { WaterDetector } from './device/waterdetector.js'
47
47
  import { AirConditioner } from './irdevice/airconditioner.js'
@@ -586,6 +586,8 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
586
586
  'Humidifier2': this.createHumidifier.bind(this),
587
587
  'Hub 2': this.createHub2.bind(this),
588
588
  'Bot': this.createBot.bind(this),
589
+ 'Relay Switch 1': this.createRelaySwitch.bind(this),
590
+ 'Relay Switch 1PM': this.createRelaySwitch.bind(this),
589
591
  'Meter': this.createMeter.bind(this),
590
592
  'MeterPlus': this.createMeterPlus.bind(this),
591
593
  'Meter Plus (JP)': this.createMeterPlus.bind(this),
@@ -796,6 +798,69 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
796
798
  }
797
799
  }
798
800
 
801
+ private async createRelaySwitch(device: device & devicesConfig) {
802
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
803
+
804
+ // see if an accessory with the same uuid has already been registered and restored from
805
+ // the cached devices we stored in the `configureAccessory` method above
806
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
807
+
808
+ if (existingAccessory) {
809
+ // the accessory already exists
810
+ if (await this.registerDevice(device)) {
811
+ // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
812
+ existingAccessory.context.device = device
813
+ existingAccessory.context.deviceId = device.deviceId
814
+ existingAccessory.context.deviceType = device.deviceType
815
+ existingAccessory.context.model = device.deviceType === 'Relay Switch 1' ? SwitchBotModel.RelaySwitch1 : SwitchBotModel.RelaySwitch1PM
816
+ existingAccessory.displayName = device.configDeviceName
817
+ ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
818
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
819
+ existingAccessory.context.connectionType = await this.connectionType(device)
820
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
821
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
822
+ this.api.updatePlatformAccessories([existingAccessory])
823
+ // create the accessory handler for the restored accessory
824
+ // this is imported from `platformAccessory.ts`
825
+ new RelaySwitch(this, existingAccessory, device)
826
+ this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
827
+ } else {
828
+ this.unregisterPlatformAccessories(existingAccessory)
829
+ }
830
+ } else if (await this.registerDevice(device)) {
831
+ // create a new accessory
832
+ const accessory = new this.api.platformAccessory(device.configDeviceName
833
+ ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
834
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
835
+
836
+ // store a copy of the device object in the `accessory.context`
837
+ // the `context` property can be used to store any data about the accessory you may need
838
+ accessory.context.device = device
839
+ accessory.context.deviceId = device.deviceId
840
+ accessory.context.deviceType = device.deviceType
841
+ accessory.context.model = device.deviceType === 'Relay Switch 1' ? SwitchBotModel.RelaySwitch1 : SwitchBotModel.RelaySwitch1PM
842
+ accessory.displayName = device.configDeviceName
843
+ ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
844
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
845
+ accessory.context.connectionType = await this.connectionType(device)
846
+ accessory.context.connectionType = await this.connectionType(device)
847
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
848
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
849
+ this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
850
+ // accessory.context.version = findaccessories.accessoryAttribute.softwareRevision;
851
+ // create the accessory handler for the newly create accessory
852
+ // this is imported from `platformAccessory.ts`
853
+ new RelaySwitch(this, accessory, device)
854
+ this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
855
+
856
+ // publish device externally or link the accessory to your platform
857
+ this.externalOrPlatform(device, accessory)
858
+ this.accessories.push(accessory)
859
+ } else {
860
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
861
+ }
862
+ }
863
+
799
864
  private async createMeter(device: device & devicesConfig) {
800
865
  const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
801
866
 
package/src/settings.ts CHANGED
@@ -53,7 +53,7 @@ export interface options {
53
53
  logging?: string
54
54
  };
55
55
 
56
- export type devicesConfig = botConfig | meterConfig | meterProConfig | indoorOutdoorSensorConfig | humidifierConfig | curtainConfig | blindTiltConfig | contactConfig | motionConfig | waterDetectorConfig | plugConfig | colorBulbConfig | stripLightConfig | ceilingLightConfig | lockConfig | hubConfig
56
+ export type devicesConfig = botConfig | relaySwitch1Config | relaySwitch1PMConfig | meterConfig | meterProConfig | indoorOutdoorSensorConfig | humidifierConfig | curtainConfig | blindTiltConfig | contactConfig | motionConfig | waterDetectorConfig | plugConfig | colorBulbConfig | stripLightConfig | ceilingLightConfig | lockConfig | hubConfig
57
57
 
58
58
  export interface BaseDeviceConfig extends device {
59
59
  bleMac?: string
@@ -97,6 +97,18 @@ export interface botConfig extends BaseDeviceConfig {
97
97
  multiPress?: boolean
98
98
  };
99
99
 
100
+ export interface relaySwitch1Config extends BaseDeviceConfig {
101
+ configDeviceType: 'Relay Switch 1'
102
+ type: string
103
+ allowPush?: boolean
104
+ };
105
+
106
+ export interface relaySwitch1PMConfig extends BaseDeviceConfig {
107
+ configDeviceType: 'Relay Switch 1PM'
108
+ type: string
109
+ allowPush?: boolean
110
+ };
111
+
100
112
  export interface meterConfig extends BaseDeviceConfig {
101
113
  configDeviceType: 'Meter' | 'MeterPlus'
102
114
  hide_temperature?: boolean