From 5d3a2d15d1b2aa2be523b72d349bf0525793650e Mon Sep 17 00:00:00 2001 From: Matt Schmitt Date: Thu, 26 Oct 2017 13:43:00 +0800 Subject: [PATCH] Add support for scripts (#235) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update multiple files Add support for scripts (on or on/off, depending on script contents) * Update README.md Fix “supported_types” array --- README.md | 9 ++++++++- accessories/switch.js | 11 +++++++++-- index.js | 4 +++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8624cad..7c2758a 100755 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Here's a list of the devices that are currently exposed: * **Media Players** - exposed as an on/off switch * **Remotes** - exposed as an on/off switch * **Scenes** - exposed as an on/off switch +* **Scripts** - exposed as an on/off switch * **Sensors** - air quality, carbon dioxide (CO2), humidity, light, temperature sensors * **Switches** - on/off @@ -104,6 +105,12 @@ cannot be deleted. Simply add your Home Assistant scene to them and set the state you would like them to be when executed. That's most like the ON state. The switch will automatically turn off shortly after turning on. +### Script Support + +Scripts containing only one service call will function exactly like scenes (see above). + +Scripts containing more than one service call will be able to be turned off as well. + ### Sensor Support Air quality, carbon dioxide (CO2), humidity, light and temperature sensors are currently supported. @@ -141,7 +148,7 @@ To avoid too much information in your log, just set `logging` to `false` as soon "name": "HomeAssistant", "host": "http://127.0.0.1:8123", "password": "yourapipassword", - "supported_types": ["automation", "binary_sensor", "climate", "cover", "device_tracker", "fan", "group", "input_boolean", "light", "lock", "media_player", "remote", "scene", "sensor", "switch"], + "supported_types": ["automation", "binary_sensor", "climate", "cover", "device_tracker", "fan", "group", "input_boolean", "light", "lock", "media_player", "remote", "scene", "script", "sensor", "switch"], "default_visibility": "hidden", "logging": true, "verify_ssl": true diff --git a/accessories/switch.js b/accessories/switch.js index 0ce3468..ee33636 100644 --- a/accessories/switch.js +++ b/accessories/switch.js @@ -59,7 +59,7 @@ HomeAssistantSwitch.prototype = { this.log(`Setting power state on the '${this.name}' to on`); this.client.callService(callDomain, 'turn_on', serviceData, (data) => { - if (this.domain === 'scene') { + if (this.domain === 'scene' || (this.domain === 'script' && !(this.data.attributes.can_cancel))) { setTimeout(() => { this.service.getCharacteristic(Characteristic.On) .setValue(false, null, 'internal'); @@ -138,6 +138,13 @@ HomeAssistantSwitch.prototype = { model = 'Automation'; } break; + case 'script': + if (this.data.attributes && this.data.attributes.homebridge_model) { + model = String(this.data.attributes.homebridge_model); + } else { + model = 'Script'; + } + break; default: model = 'Switch'; } @@ -161,7 +168,7 @@ HomeAssistantSwitch.prototype = { .setCharacteristic(Characteristic.Model, model) .setCharacteristic(Characteristic.SerialNumber, this.serial); - if (this.domain === 'remote' || this.domain === 'switch' || this.domain === 'input_boolean' || this.domain === 'group' || this.domain === 'automation') { + if (this.domain === 'remote' || this.domain === 'switch' || this.domain === 'input_boolean' || this.domain === 'group' || this.domain === 'automation' || (this.domain === 'script' && this.data.attributes.can_cancel)) { this.service .getCharacteristic(Characteristic.On) .on('get', this.getPowerState.bind(this)) diff --git a/index.js b/index.js index 7a4e0b7..91c5fef 100644 --- a/index.js +++ b/index.js @@ -24,7 +24,7 @@ function HomeAssistantPlatform(log, config, api) { // auth info this.host = config.host; this.password = config.password; - this.supportedTypes = config.supported_types || ['alarm_control_panel', 'automation', 'binary_sensor', 'climate', 'cover', 'device_tracker', 'fan', 'group', 'input_boolean', 'light', 'lock', 'media_player', 'remote', 'scene', 'sensor', 'switch']; + this.supportedTypes = config.supported_types || ['alarm_control_panel', 'automation', 'binary_sensor', 'climate', 'cover', 'device_tracker', 'fan', 'group', 'input_boolean', 'light', 'lock', 'media_player', 'remote', 'scene', 'script', 'sensor', 'switch']; this.foundAccessories = []; this.logging = config.logging !== undefined ? config.logging : true; this.verify_ssl = config.verify_ssl !== undefined ? config.verify_ssl : true; @@ -194,6 +194,8 @@ HomeAssistantPlatform.prototype = { accessory = new HomeAssistantSwitch(that.log, entity, that, 'remote'); } else if (entityType === 'automation') { accessory = new HomeAssistantSwitch(that.log, entity, that, 'automation'); + } else if (entityType === 'script') { + accessory = new HomeAssistantSwitch(that.log, entity, that, 'script'); } }