1
0

2 Коммиты 6634f66820 ... e8ea7533b4

Автор SHA1 Сообщение Дата
  JDierkse e8ea7533b4 Fix PowerSwitch Dimmable bug 9 месяцев назад
  JDierkse d233b5a9cd Add SecuritySystemAccessory 9 месяцев назад
2 измененных файлов с 188 добавлено и 2 удалено
  1. 2 2
      powerSwitchAccessory.ts
  2. 186 0
      securitySystemAccessory.ts

+ 2 - 2
powerSwitchAccessory.ts

@@ -66,7 +66,7 @@ export class powerSwitchAccessory extends accessory {
 			.onSet(this.setOn.bind(this))
 			.onGet(this.getOn.bind(this))
 
-		if (accessory.context.device.dimmable != "false") { // WTF?
+		if (accessory.context.device.dimmable == "true") {
 			this.service.getCharacteristic(this.platform.Characteristic.Brightness)
 				.onSet(this.setBrightness.bind(this))
 				.onGet(this.getBrightness.bind(this))
@@ -110,7 +110,7 @@ export class powerSwitchAccessory extends accessory {
 							}
 							this.service.updateCharacteristic(this.platform.Characteristic.On, this.state.On)
 
-							if (device.dimmable != 'false') { // WTF?
+							if (device.dimmable == "true") {
 								this.state.Brightness = device.dimlevel;
 								this.service.updateCharacteristic(this.platform.Characteristic.Brightness, this.state.Brightness)
 							}

+ 186 - 0
securitySystemAccessory.ts

@@ -0,0 +1,186 @@
+import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge'
+
+import { accessory } from './accessory'
+import { device } from './device'
+import { httpRequest, response } from './httpPromise'
+import { domoticaPlatform } from './platform'
+
+export class securitySystemAccessory extends accessory {
+	private service: Service
+
+	private state = {
+		CurrentState: this.platform.Characteristic.SecuritySystemCurrentState.DISARMED,
+		TargetState: this.platform.Characteristic.SecuritySystemCurrentState.DISARMED
+	}
+
+	static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
+		return new Promise((resolve, reject) => {
+			let securitySystemDevices: device[] = []
+
+			httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/alarmsystem')
+				.then((response) => {
+					let devices = JSON.parse(response.body)
+
+					for (const device of devices) {
+						if (device.enabled == "true") {
+							let object: device = {
+								type: "SecuritySystem",
+								typeId: device.id,
+								uniqueId: "SecuritySystem." + device.id,
+								deviceName: device.name,
+								switchable: "false",
+								dimmable: "false",
+								detailedType: device.type
+							}
+							securitySystemDevices.push(object)
+						}
+					}
+
+					resolve(securitySystemDevices)
+				})
+				.catch((error) => {
+					reject('securitySystemAccessory::discoverDevices Error ->' + error)
+				})
+		})
+	}
+
+	constructor(
+		private readonly platform: domoticaPlatform,
+		private readonly accessory: PlatformAccessory,
+	) {
+		super("SecuritySystem", accessory.context.device.typeId)
+		this.accessory.getService(this.platform.Service.AccessoryInformation)!
+			.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Dierkse')
+			.setCharacteristic(this.platform.Characteristic.Model, 'AlarmSystem')
+			.setCharacteristic(this.platform.Characteristic.SerialNumber, '11000' + accessory.context.device.typeId)
+
+		this.service = this.accessory.getService(this.platform.Service.SecuritySystem) || this.accessory.addService(this.platform.Service.SecuritySystem)
+
+		this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState)
+			.onGet(this.getSecuritySystemCurrentState.bind(this));
+
+		this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemTargetState)
+			.onGet(this.getSecuritySystemTargetState.bind(this))
+			.onSet(this.setSecuritySystemTargetState.bind(this));
+
+		this.setName(accessory.context.device.deviceName)
+		this.update()
+	}
+
+	setName(name) {
+		this.service.setCharacteristic(this.platform.Characteristic.Name, name)
+	}
+
+	setValue(key, value) {
+		if (key == "state" && value != "") {
+			if (value == "Disarmed") {
+				this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.DISARMED
+				this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.DISARM
+			} else if (value == "StayArmed") {
+				this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.STAY_ARM
+				this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM
+			} else if (value == "AwayArmed") {
+				this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.AWAY_ARM
+				this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM
+			} else if (value == "NightArmed") {
+				this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.NIGHT_ARM
+				this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM
+//			} else if (value == "Triggered") {
+//				this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.TRIGGERED
+//				this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.TRIGGERED
+			}
+			this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState, this.state.CurrentState)
+			this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemTargetState, this.state.TargetState)
+		}
+	}
+
+	update() {
+		const data = JSON.stringify([{
+			type: 'alarmsystem',
+			query: 'state'
+		}])
+
+		httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
+			.then((response) => {
+				const devices = JSON.parse(response.body)
+
+				if (devices.hasOwnProperty('alarmsystem')) {
+					for (const device of devices.alarmsystem) {
+						if (device.id == this.accessory.context.device.typeId) {
+							if (device.state == "Disarmed") {
+								this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.DISARMED
+								this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.DISARM
+							} else if (device.state == "StayArmed") {
+								this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.STAY_ARM
+								this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM
+							} else if (device.state == "AwayArmed") {
+								this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.AWAY_ARM
+								this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM
+							} else if (device.state == "NightArmed") {
+								this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.NIGHT_ARM
+								this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM
+//							} else if (device.state == "Triggered") {
+//								this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.TRIGGERED
+//								this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.TRIGGERED
+							}
+
+							this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState, this.state.CurrentState)
+							this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemTargetState, this.state.TargetState)
+
+							break
+						}
+					}
+				}
+			})
+			.catch((error) => {
+				this.platform.log.debug('securitySystemAccessory::update Error ->' + error)
+			})
+
+	}
+
+	getSecuritySystemCurrentState(): CharacteristicValue {
+		return this.state.CurrentState
+	}
+
+	getSecuritySystemTargetState(): CharacteristicValue {
+		return this.state.TargetState
+	}
+
+	
+	async setSecuritySystemTargetState(value: CharacteristicValue) {
+		let targetState: string = "";
+		this.state.TargetState = value as number
+
+		// FIX
+		if (value == this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM) {
+			this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.STAY_ARM
+			targetState = "StayArmed"
+		}
+		else if (value == this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM) {
+			this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.AWAY_ARM
+			targetState = "AwayArmed"
+		}
+		else if (value == this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM) {
+			this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.NIGHT_ARM
+			targetState = "NightArmed"
+		}
+		else if (value == this.platform.Characteristic.SecuritySystemTargetState.DISARM) {
+			this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.DISARMED
+			targetState = "Disarmed"
+		}
+//		else if (value == this.platform.Characteristic.SecuritySystemTargetState.TRIGGERED) {
+//			this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.TRIGGERED
+//			targetState = "Triggered"
+//		}
+
+		this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState, this.state.CurrentState)
+		this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemTargetState, this.state.TargetState)
+
+		// http://$domoticaIP/external/WebUpdate.php?data=AlarmSystem/$id/$state/
+		//httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=AlarmSystem/' + this.accessory.context.device.typeId + '/' + (targetState? '1': '0') + '/')
+		httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=AlarmSystem/' + 1 + '/' + targetState + '/')
+			.catch((error) => {
+				this.platform.log.debug('securitySystemAccessory::setSecuritySystemTargetState Error ->' + error)
+			})
+	}
+}