From c8752da660aebe4c5a563cbb35d694a4e1d78e16 Mon Sep 17 00:00:00 2001 From: Daniel Burgess Date: Thu, 26 Mar 2020 20:20:57 +1300 Subject: [PATCH] v3.0.4 Added: Graceful Error Handling --- CHANGELOG.md | 2 +- README.md | 2 +- config.schema.json | 2 +- index.js | 142 +++++++++++++++++++-------------------------- package.json | 2 +- 5 files changed, 64 insertions(+), 86 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e2b0b4..b4efe3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Enable / disable by zone - Option to override topic per sensor (would default to main sensorTopic). This allows you to get sensor activity from multiple topics. -## v3.0.3 - 2020-03-26 +## v3.0.4 - 2020-03-26 ### Added - Errors throw friendly log messages and the process stops gracefully. - Added a minimum count of 1 to sensors. diff --git a/README.md b/README.md index 2a57cba..528b929 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ Once the plugin is installed you will be presented with the settings page to pop | Settings Option | config.json Value | Required | Description | Type | Default / Example Value | |-----------------------------|----------------------------------|:--------:|---------------------------------------------------------------------------|:--------:|--------------------------| -| Name | name | Yes | Name of the alarm in HomeKit | String | Homeqtt | +| Name | name | Yes | Name of the alarm in HomeKit | String | Homeqtt | | N/A (auto set by settings) | accessory | Yes | Homebridge plugin accessory identifier | String | homebridge-homeqtt-alarm | | MQTT Broker URL:Port | mqttConfig/url | Yes | URL and Port of your MQTT broker | URL | mqtt://URL:PORT | | MQTT Username | mqttConfig/username | No | Your MQTT Broker username (optional) | String | username | diff --git a/config.schema.json b/config.schema.json index 54686be..eb48390 100644 --- a/config.schema.json +++ b/config.schema.json @@ -3,7 +3,7 @@ "pluginType": "accessory", "singular": true, "headerDisplay": "MQTT must be installed on your system", - "footerDisplay": "For help please see the [Readme](https://github.com/nzbullet/homebridge-homeqtt-alarm/blob/master/README.md).", + "footerDisplay": "For help please see the [README](https://github.com/nzbullet/homebridge-homeqtt-alarm/blob/master/README.md).", "schema": { "name": { "title": "Name", diff --git a/index.js b/index.js index fe037c9..cba2fe1 100644 --- a/index.js +++ b/index.js @@ -201,51 +201,40 @@ function homeqttAlarmAccessory(log, config) { // Set HomeKit Alarm based on Keyfob Button presses if (that.keyfob === true && topic === that.alarmTopics.messageTopic) { for (let keyfob in that.keyfobs) { - if (that.keyfobs[keyfob].enabled === true) { for (let button in that.keyfobs[keyfob].buttons) { - if (that.keyfobs[keyfob].buttons[button].MQTTCode) { - if (that.keyfobs[keyfob].buttons[button].enabled === true && message.indexOf(that.keyfobs[keyfob].buttons[button].MQTTCode) !== -1) { - if (that.keyfobs[keyfob].buttons[button].alarmState) { - var stateName = that.keyfobs[keyfob].buttons[button].alarmState - switch (stateName) { - case that.commandStayArm: - stateName = 'STAY_ARM' - break; - case that.commandAwayArm: - stateName = 'AWAY_ARM' - break; - case that.commandNightArm: - stateName = 'NIGHT_ARM' - break; - case that.commandDisarmed: - stateName = 'DISARMED' - break; - case that.commandTriggered: - stateName = 'ALARM_TRIGGERED' - break; - default: - stateName = null; - break; - } - if (that.debug) { - log("Keyfob Button Pressed:", stateName, "(", that.keyfobs[keyfob].buttons[button].alarmState, ")") - } - if (stateName === 'ALARM_TRIGGERED') { - that.securityService.setCharacteristic(Characteristic.SecuritySystemCurrentState, that.commandTriggered); - that.client.publish(that.alarmTopics.getCurrentStateTopic, that.stateTriggered); - } - if (stateName !== 'ALARM_TRIGGERED') { - that.securityService.setCharacteristic(Characteristic.SecuritySystemTargetState, that.keyfobs[keyfob].buttons[button].alarmState); - } - } else { - if (that.debug) { - log('Button Alarm State has not been set in config.json') - } - } + if (that.keyfobs[keyfob].buttons[button].enabled === true && message.indexOf(that.keyfobs[keyfob].buttons[button].MQTTCode) !== -1) { + var stateName = that.keyfobs[keyfob].buttons[button].alarmState + switch (stateName) { + case that.commandStayArm: + stateName = 'STAY_ARM' + break; + case that.commandAwayArm: + stateName = 'AWAY_ARM' + break; + case that.commandNightArm: + stateName = 'NIGHT_ARM' + break; + case that.commandDisarmed: + stateName = 'DISARMED' + break; + case that.commandTriggered: + stateName = 'ALARM_TRIGGERED' + break; + default: + stateName = null; + break; + } + if (that.debug) { + log("Keyfob Button Pressed:", stateName, "(", that.keyfobs[keyfob].buttons[button].alarmState, ")") + } + if (stateName === 'ALARM_TRIGGERED') { + that.securityService.setCharacteristic(Characteristic.SecuritySystemCurrentState, that.commandTriggered); + that.client.publish(that.alarmTopics.getCurrentStateTopic, that.stateTriggered); + } + if (stateName !== 'ALARM_TRIGGERED') { + that.securityService.setCharacteristic(Characteristic.SecuritySystemTargetState, that.keyfobs[keyfob].buttons[button].alarmState); } - } else { - log('Button MQTT Code has not been provided in config.json') } } @@ -319,53 +308,42 @@ function homeqttAlarmAccessory(log, config) { if (topic == that.alarmTopics.messageTopic) { for (let sensor in that.sensor) { // Sensor is in MQTT message - if (that.sensor[sensor].MQTTCode) { - if (message.indexOf(that.sensor[sensor].MQTTCode) !== -1) { - // Sensor is enabled and alarm is not Disarmed - if (that.sensor[sensor].enabled === true && that.readstate != 3) { - // If sensor is allowed in the enabled state (and alarm is triggered) - if (that.sensor[sensor].allowStay === true && that.readstate === 0) { - triggerAlarm.call() - } - if (that.sensor[sensor].allowAway === true && that.readstate === 1) { - triggerAlarm.call() - } - if (that.sensor[sensor].allowNight === true && that.readstate === 2) { - triggerAlarm.call() - } - // Trigger Alarm Function - function triggerAlarm() { - log('Sensor Triggered:', that.sensor[sensor].location, '(', that.sensor[sensor].MQTTCode, ')') - // MQTT Publish Triggered - that.client.publish(that.alarmTopics.getTargetStateTopic, that.stateTriggered); - // Trigger Alarm in HomeKit - that.securityService.setCharacteristic(Characteristic.SecuritySystemCurrentState, that.commandTriggered); - // Trigger Siren if enabled - if (that.keyfob === true && that.siren.enabled === true) { - for (let keyfob in that.keyfobs) { - if (that.keyfobs[keyfob].enabled === true) { - for (let button in that.keyfobs[keyfob].buttons) { - if (that.keyfobs[keyfob].buttons[button].alarmState) { - if (that.keyfobs[keyfob].buttons[button].enabled === true && that.keyfobs[keyfob].buttons[button].alarmState === that.commandTriggered) { - if (that.keyfobs[keyfob].buttons[button].MQTTCode) { - that.client.publish(that.keyfobs[keyfob].buttons[button].rfkeyTopic, that.keyfobs[keyfob].buttons[button].MQTTCode); - } else { - log('Button MQTT Code has not been provided in config.json') - } - } - } else { - log('Button alarm state has not been set in config.json') - } + + if (message.indexOf(that.sensor[sensor].MQTTCode) !== -1) { + // Sensor is enabled and alarm is not Disarmed + if (that.sensor[sensor].enabled === true && that.readstate != 3) { + // If sensor is allowed in the enabled state (and alarm is triggered) + if (that.sensor[sensor].allowStay === true && that.readstate === 0) { + triggerAlarm.call() + } + if (that.sensor[sensor].allowAway === true && that.readstate === 1) { + triggerAlarm.call() + } + if (that.sensor[sensor].allowNight === true && that.readstate === 2) { + triggerAlarm.call() + } + // Trigger Alarm Function + function triggerAlarm() { + log('Sensor Triggered:', that.sensor[sensor].location, '(', that.sensor[sensor].MQTTCode, ')') + // MQTT Publish Triggered + that.client.publish(that.alarmTopics.getTargetStateTopic, that.stateTriggered); + // Trigger Alarm in HomeKit + that.securityService.setCharacteristic(Characteristic.SecuritySystemCurrentState, that.commandTriggered); + // Trigger Siren if enabled + if (that.keyfob === true && that.siren.enabled === true) { + for (let keyfob in that.keyfobs) { + if (that.keyfobs[keyfob].enabled === true) { + for (let button in that.keyfobs[keyfob].buttons) { + if (that.keyfobs[keyfob].buttons[button].enabled === true && that.keyfobs[keyfob].buttons[button].alarmState === that.commandTriggered) { + that.client.publish(that.keyfobs[keyfob].buttons[button].rfkeyTopic, that.keyfobs[keyfob].buttons[button].MQTTCode); } } } - log("Siren Triggered") } + log("Siren Triggered") } } } - } else { - log('Sensor MQTT Code has not been provided in config.json') } } } diff --git a/package.json b/package.json index cca4211..6e04497 100644 --- a/package.json +++ b/package.json @@ -39,5 +39,5 @@ "type": "git", "url": "git+https://github.com/nzbullet/homebridge-homeqtt-alarm.git" }, - "version": "3.0.3" + "version": "3.0.4" }