Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
v3.0.4 Added: Graceful Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Burgess committed Mar 26, 2020
1 parent 6d0dc53 commit c8752da
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 86 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion config.schema.json
Expand Up @@ -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",
Expand Down
142 changes: 60 additions & 82 deletions index.js
Expand Up @@ -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')
}
}

Expand Down Expand Up @@ -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')
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -39,5 +39,5 @@
"type": "git",
"url": "git+https://github.com/nzbullet/homebridge-homeqtt-alarm.git"
},
"version": "3.0.3"
"version": "3.0.4"
}

0 comments on commit c8752da

Please sign in to comment.