├── locales ├── en.json └── nl.json ├── drivers ├── light │ ├── assets │ │ ├── images │ │ │ ├── large.jpg │ │ │ └── small.jpg │ │ ├── icons │ │ │ ├── LST001.svg │ │ │ ├── LLC013.svg │ │ │ ├── LCT001.svg │ │ │ ├── LLC020.svg │ │ │ ├── LLC011.svg │ │ │ ├── LCT003.svg │ │ │ ├── LCT002.svg │ │ │ └── LWB006.svg │ │ └── icon.svg │ ├── driver.js │ └── device.js ├── scene │ ├── assets │ │ ├── images │ │ │ ├── large.jpg │ │ │ └── small.jpg │ │ └── icon.svg │ ├── driver.js │ └── device.js ├── script │ ├── assets │ │ ├── images │ │ │ ├── large.jpg │ │ │ └── small.jpg │ │ └── icon.svg │ ├── driver.js │ └── device.js ├── sensor │ ├── assets │ │ ├── images │ │ │ ├── large.jpg │ │ │ └── small.jpg │ │ ├── icons │ │ │ ├── measure_temperature.svg │ │ │ ├── measure_battery.svg │ │ │ ├── noise.svg │ │ │ ├── measure_humidity.svg │ │ │ ├── meter_power.svg │ │ │ ├── co2.svg │ │ │ ├── measure_luminance.svg │ │ │ ├── measure_pressure.svg │ │ │ └── measure_power.svg │ │ └── icon.svg │ ├── driver.js │ └── device.js ├── switch │ ├── assets │ │ ├── images │ │ │ ├── large.jpg │ │ │ └── small.jpg │ │ ├── icons │ │ │ ├── on.svg │ │ │ └── off.svg │ │ └── icon.svg │ ├── driver.js │ └── device.js ├── compound │ ├── assets │ │ ├── images │ │ │ ├── large.jpg │ │ │ └── small.jpg │ │ └── icons │ │ │ ├── alarm_generic.svg │ │ │ ├── measure_temperature.svg │ │ │ ├── alarm_tamper.svg │ │ │ ├── measure_battery.svg │ │ │ ├── noise.svg │ │ │ ├── measure_humidity.svg │ │ │ ├── meter_power.svg │ │ │ ├── alarm_motion.svg │ │ │ ├── alarm_water.svg │ │ │ ├── co2.svg │ │ │ ├── alarm_smoke.svg │ │ │ ├── measure_luminance.svg │ │ │ ├── alarm_contact.svg │ │ │ ├── measure_pressure.svg │ │ │ ├── measure_power.svg │ │ │ ├── alarm_heat.svg │ │ │ └── alarm_pressure.svg │ ├── driver.js │ └── device.js └── binary_sensor │ ├── assets │ ├── images │ │ ├── large.jpg │ │ └── small.jpg │ ├── icon.svg │ └── icons │ │ ├── alarm_generic.svg │ │ ├── alarm_tamper.svg │ │ ├── alarm_motion.svg │ │ ├── alarm_water.svg │ │ ├── alarm_smoke.svg │ │ ├── alarm_contact.svg │ │ ├── alarm_heat.svg │ │ └── alarm_pressure.svg │ ├── driver.js │ └── device.js ├── custom_components └── homey │ ├── manifest.json │ ├── example.yaml │ └── __init__.py ├── app.js ├── README.md ├── CONTRIBUTING.md ├── settings └── index.html ├── CODE_OF_CONDUCT.md ├── app.json └── lib └── Client.js /locales/en.json: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /locales/nl.json: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /drivers/light/assets/images/large.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/light/assets/images/large.jpg -------------------------------------------------------------------------------- /drivers/light/assets/images/small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/light/assets/images/small.jpg -------------------------------------------------------------------------------- /drivers/scene/assets/images/large.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/scene/assets/images/large.jpg -------------------------------------------------------------------------------- /drivers/scene/assets/images/small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/scene/assets/images/small.jpg -------------------------------------------------------------------------------- /drivers/script/assets/images/large.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/script/assets/images/large.jpg -------------------------------------------------------------------------------- /drivers/script/assets/images/small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/script/assets/images/small.jpg -------------------------------------------------------------------------------- /drivers/sensor/assets/images/large.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/sensor/assets/images/large.jpg -------------------------------------------------------------------------------- /drivers/sensor/assets/images/small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/sensor/assets/images/small.jpg -------------------------------------------------------------------------------- /drivers/switch/assets/images/large.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/switch/assets/images/large.jpg -------------------------------------------------------------------------------- /drivers/switch/assets/images/small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/switch/assets/images/small.jpg -------------------------------------------------------------------------------- /drivers/compound/assets/images/large.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/compound/assets/images/large.jpg -------------------------------------------------------------------------------- /drivers/compound/assets/images/small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/compound/assets/images/small.jpg -------------------------------------------------------------------------------- /drivers/binary_sensor/assets/images/large.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/binary_sensor/assets/images/large.jpg -------------------------------------------------------------------------------- /drivers/binary_sensor/assets/images/small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogro82/io.homeassistant/HEAD/drivers/binary_sensor/assets/images/small.jpg -------------------------------------------------------------------------------- /custom_components/homey/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "domain": "homey", 3 | "name": "homey", 4 | "documentation": "https://github.com/rogro82/io.homeassistant", 5 | "requirements": [], 6 | "dependencies": [], 7 | "codeowners": [ 8 | "@rogro82" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /drivers/light/driver.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class LightDriver extends Homey.Driver { 6 | 7 | onPairListDevices(data, callback) { 8 | 9 | let client = Homey.app.getClient(); 10 | let lights = client.getLights(); 11 | 12 | callback(null, lights); 13 | } 14 | 15 | } 16 | 17 | module.exports = LightDriver; -------------------------------------------------------------------------------- /drivers/scene/driver.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class SceneDriver extends Homey.Driver { 6 | 7 | onPairListDevices(ata, callback) { 8 | 9 | let client = Homey.app.getClient(); 10 | let scenes = client.getScenes(); 11 | 12 | callback(null, scenes); 13 | } 14 | 15 | } 16 | 17 | module.exports = SceneDriver; -------------------------------------------------------------------------------- /drivers/script/driver.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class ScriptDriver extends Homey.Driver { 6 | 7 | onPairListDevices(data, callback) { 8 | 9 | let client = Homey.app.getClient(); 10 | let scripts = client.getScripts(); 11 | 12 | callback(null, scripts); 13 | } 14 | 15 | } 16 | 17 | module.exports = ScriptDriver; -------------------------------------------------------------------------------- /drivers/sensor/driver.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class SensorDriver extends Homey.Driver { 6 | 7 | onPairListDevices(data, callback) { 8 | 9 | let client = Homey.app.getClient(); 10 | let sensors = client.getSensors(); 11 | 12 | callback(null, sensors); 13 | } 14 | 15 | } 16 | 17 | module.exports = SensorDriver; -------------------------------------------------------------------------------- /drivers/switch/driver.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class SwitchDriver extends Homey.Driver { 6 | 7 | onPairListDevices(data, callback) { 8 | 9 | let client = Homey.app.getClient(); 10 | let switches = client.getSwitches(); 11 | 12 | callback(null, switches); 13 | } 14 | 15 | } 16 | 17 | module.exports = SwitchDriver; -------------------------------------------------------------------------------- /drivers/compound/driver.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class CompoundDriver extends Homey.Driver { 6 | 7 | onPairListDevices(data, callback) { 8 | 9 | let client = Homey.app.getClient(); 10 | let sensors = client.getCompounds(); 11 | 12 | callback(null, sensors); 13 | } 14 | 15 | } 16 | 17 | module.exports = CompoundDriver; -------------------------------------------------------------------------------- /drivers/binary_sensor/driver.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class BinarySensorDriver extends Homey.Driver { 6 | 7 | onPairListDevices(data, callback) { 8 | 9 | let client = Homey.app.getClient(); 10 | let sensors = client.getBinarySensors(); 11 | 12 | callback(null, sensors); 13 | } 14 | 15 | } 16 | 17 | module.exports = BinarySensorDriver; -------------------------------------------------------------------------------- /drivers/script/assets/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /custom_components/homey/example.yaml: -------------------------------------------------------------------------------- 1 | homey: 2 | livingroom_sensor: 3 | name: Livingroom environment 4 | capabilities: 5 | measure_temperature: sensor.livingroom_temperature 6 | measure_humidity: sensor.livingroom_humidity 7 | measure_luminance: sensor.livingroom_luminance 8 | alarm_motion: binary_sensor.livingroom_motion 9 | alarm_contact: binary_sensor.toilet_door 10 | button: script.test 11 | onoff: switch.ventilation 12 | dim: input_number.test_slider 13 | capabilitiesConverters: 14 | measure_temperature: 15 | from: (state) => { return parseFloat(state) * 2; } 16 | to: (value) => { return value / 2; } 17 | dim: 18 | from: (state) => { return parseFloat(state) * 0.01; } 19 | to: (value) => { return value * 100; } -------------------------------------------------------------------------------- /drivers/compound/assets/icons/alarm_generic.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /drivers/switch/assets/icons/on.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /drivers/switch/assets/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /drivers/switch/assets/icons/off.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/measure_temperature.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /drivers/sensor/assets/icons/measure_temperature.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /drivers/binary_sensor/assets/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /drivers/binary_sensor/assets/icons/alarm_generic.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /drivers/scene/device.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class SceneDevice extends Homey.Device { 6 | 7 | onInit() { 8 | this._client = Homey.app.getClient(); 9 | 10 | this.entityId = this.getData().id; 11 | 12 | this.log('device init'); 13 | this.log('id:', this.entityId); 14 | this.log('name:', this.getName()); 15 | this.log('class:', this.getClass()); 16 | 17 | this._client.registerDevice(this.entityId, this); 18 | 19 | let entity = this._client.getEntity(this.entityId); 20 | if(entity) { 21 | this.onEntityUpdate(entity); 22 | } 23 | 24 | this.registerCapabilityListener('button', this.onCapabilityButton.bind(this)) 25 | } 26 | 27 | onAdded() { 28 | this.log('device added'); 29 | } 30 | 31 | onDeleted() { 32 | this.log('device deleted'); 33 | this._client.unregisterDevice(this.entityId); 34 | } 35 | 36 | onCapabilityButton( value, opts, callback ) { 37 | this._client.turnOnOff(this.entityId, true); 38 | callback( null ); 39 | } 40 | 41 | onEntityUpdate(data) { 42 | // nothing to update 43 | } 44 | } 45 | 46 | module.exports = SceneDevice; -------------------------------------------------------------------------------- /drivers/script/device.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class ScriptDevice extends Homey.Device { 6 | 7 | onInit() { 8 | this._client = Homey.app.getClient(); 9 | 10 | this.entityId = this.getData().id; 11 | 12 | this.log('device init'); 13 | this.log('id:', this.entityId); 14 | this.log('name:', this.getName()); 15 | this.log('class:', this.getClass()); 16 | 17 | this._client.registerDevice(this.entityId, this); 18 | 19 | let entity = this._client.getEntity(this.entityId); 20 | if(entity) { 21 | this.onEntityUpdate(entity); 22 | } 23 | 24 | this.registerCapabilityListener('button', this.onCapabilityButton.bind(this)) 25 | } 26 | 27 | onAdded() { 28 | this.log('device added'); 29 | } 30 | 31 | onDeleted() { 32 | this.log('device deleted'); 33 | this._client.unregisterDevice(this.entityId); 34 | } 35 | 36 | onCapabilityButton( value, opts, callback ) { 37 | this._client.turnOnOff(this.entityId, true); 38 | callback( null ); 39 | } 40 | 41 | onEntityUpdate(data) { 42 | // nothing to update 43 | } 44 | } 45 | 46 | module.exports = ScriptDevice; -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | const Client = require('./lib/Client.js'); 5 | 6 | class App extends Homey.App { 7 | 8 | onInit() { 9 | super.onInit(); 10 | 11 | this.log('Home-Assistant is running...'); 12 | 13 | let address = Homey.ManagerSettings.get("address"); 14 | let token = Homey.ManagerSettings.get("token"); 15 | 16 | this._client = new Client(address, token) 17 | .on("connection_update", (state) => { 18 | Homey.ManagerApi.realtime('connection_update', state); 19 | }); 20 | 21 | this._onFlowActionCallService = this._onFlowActionCallService.bind(this); 22 | 23 | new Homey.FlowCardAction('callService') 24 | .register() 25 | .registerRunListener( this._onFlowActionCallService ); 26 | 27 | Homey.ManagerSettings.on("set", this._reconnectClient.bind(this)); 28 | } 29 | 30 | getClient() { 31 | return this._client; 32 | } 33 | 34 | _reconnectClient(arg) { 35 | console.log("settings updated.... reconnecting"); 36 | 37 | let address = Homey.ManagerSettings.get("address"); 38 | let token = Homey.ManagerSettings.get("token"); 39 | 40 | this._client.connect(address, token, true); 41 | } 42 | 43 | _onFlowActionCallService(args) { 44 | this._client.callService(args.domain, args.service, args.data); 45 | } 46 | } 47 | 48 | module.exports = App; -------------------------------------------------------------------------------- /drivers/switch/device.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class SwitchDevice extends Homey.Device { 6 | 7 | onInit() { 8 | this._client = Homey.app.getClient(); 9 | 10 | this.entityId = this.getData().id; 11 | 12 | this.log('device init'); 13 | this.log('id:', this.entityId); 14 | this.log('name:', this.getName()); 15 | this.log('class:', this.getClass()); 16 | 17 | this._client.registerDevice(this.entityId, this); 18 | 19 | let entity = this._client.getEntity(this.entityId); 20 | if(entity) { 21 | this.onEntityUpdate(entity); 22 | } 23 | 24 | this.registerCapabilityListener('onoff', this.onCapabilityOnoff.bind(this)) 25 | } 26 | 27 | onAdded() { 28 | this.log('device added'); 29 | } 30 | 31 | onDeleted() { 32 | this.log('device deleted'); 33 | this._client.unregisterDevice(this.entityId); 34 | } 35 | 36 | onCapabilityOnoff( value, opts, callback ) { 37 | this._client.turnOnOff(this.entityId, value); 38 | callback( null ); 39 | } 40 | 41 | onEntityUpdate(data) { 42 | if(data) { 43 | this.setCapabilityValue("onoff", data.state == "on"); 44 | } 45 | } 46 | } 47 | 48 | module.exports = SwitchDevice; -------------------------------------------------------------------------------- /drivers/sensor/device.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class SensorDevice extends Homey.Device { 6 | 7 | onInit() { 8 | this._client = Homey.app.getClient(); 9 | 10 | this.entityId = this.getData().id; 11 | this.capability = this.getCapabilities()[0]; 12 | 13 | this.log('device init'); 14 | this.log('id:', this.entityId); 15 | this.log('name:', this.getName()); 16 | this.log('class:', this.getClass()); 17 | 18 | this._client.registerDevice(this.entityId, this); 19 | 20 | let entity = this._client.getEntity(this.entityId); 21 | if(entity) { 22 | this.onEntityUpdate(entity); 23 | } 24 | } 25 | 26 | onAdded() { 27 | this.log('device added'); 28 | } 29 | 30 | onDeleted() { 31 | this.log('device deleted'); 32 | this._client.unregisterDevice(this.entityId); 33 | } 34 | 35 | onEntityUpdate(data) { 36 | try { 37 | switch(this.capability) { 38 | case "measure_generic": 39 | this.setCapabilityValue(this.capability, data.state); 40 | break; 41 | default: 42 | this.setCapabilityValue(this.capability, parseFloat(data.state)); 43 | } 44 | 45 | } catch(ex) { 46 | this.log("error", ex); 47 | } 48 | } 49 | } 50 | 51 | module.exports = SensorDevice; -------------------------------------------------------------------------------- /drivers/compound/assets/icons/alarm_tamper.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 7 | 9 | 11 | 16 | 17 | -------------------------------------------------------------------------------- /drivers/binary_sensor/assets/icons/alarm_tamper.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 7 | 9 | 11 | 16 | 17 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/measure_battery.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | device_battery_charge_outline_stroke 10 | Created with Sketch. 11 | 12 | 13 | 14 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /drivers/sensor/assets/icons/measure_battery.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | device_battery_charge_outline_stroke 10 | Created with Sketch. 11 | 12 | 13 | 14 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /drivers/binary_sensor/device.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | class BinarySensorDevice extends Homey.Device { 6 | 7 | onInit() { 8 | this._client = Homey.app.getClient(); 9 | 10 | this.entityId = this.getData().id; 11 | this.capabilities = this.getCapabilities(); 12 | 13 | this.log('device init'); 14 | this.log('id:', this.entityId); 15 | this.log('name:', this.getName()); 16 | this.log('class:', this.getClass()); 17 | 18 | this._client.registerDevice(this.entityId, this); 19 | 20 | let entity = this._client.getEntity(this.entityId); 21 | if(entity) { 22 | this.onEntityUpdate(entity); 23 | } 24 | 25 | this.registerCapabilityListener('onoff', this.onCapabilityOnoff.bind(this)) 26 | } 27 | 28 | onAdded() { 29 | this.log('device added'); 30 | } 31 | 32 | onDeleted() { 33 | this.log('device deleted'); 34 | this._client.unregisterDevice(this.entityId); 35 | } 36 | 37 | onEntityUpdate(data) { 38 | try { 39 | 40 | this.capabilities.forEach(capability => { 41 | this.setCapabilityValue(capability, data.state == "on"); 42 | }); 43 | 44 | } catch(ex) { 45 | console.log("error", ex); 46 | } 47 | } 48 | 49 | onCapabilityOnoff( value, opts, callback ) { 50 | let oldValue = this.getCapabilityValue('onoff'); 51 | 52 | callback(null, true); 53 | this.setCapabilityValue("onoff", oldValue); 54 | } 55 | } 56 | 57 | module.exports = BinarySensorDevice; -------------------------------------------------------------------------------- /drivers/light/assets/icons/LST001.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/noise.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 10 | 12 | 14 | 16 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /drivers/sensor/assets/icons/noise.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 10 | 12 | 14 | 16 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /drivers/sensor/assets/icons/measure_humidity.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | 20 | 21 | 23 | 24 | 26 | 27 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/measure_humidity.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | 20 | 21 | 23 | 24 | 26 | 27 | -------------------------------------------------------------------------------- /drivers/sensor/assets/icons/meter_power.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 8 | 9 | 10 | 21 | 22 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/meter_power.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 8 | 9 | 10 | 21 | 22 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/alarm_motion.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 8 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /drivers/binary_sensor/assets/icons/alarm_motion.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 8 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /drivers/light/assets/icons/LLC013.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 8 | 9 | 10 | 15 | 16 | 18 | 19 | 21 | 23 | 25 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/alarm_water.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /drivers/binary_sensor/assets/icons/alarm_water.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/co2.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 12 | 16 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /drivers/sensor/assets/icons/co2.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 12 | 16 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/alarm_smoke.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /drivers/binary_sensor/assets/icons/alarm_smoke.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /drivers/sensor/assets/icons/measure_luminance.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 7 | 9 | 11 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 31 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/measure_luminance.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 7 | 9 | 11 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Home-Assistant integation for Homey by Athom 2 | 3 | This application allows you to add your home-assistant (http://home-assistant.io) devices to your Homey. 4 | 5 | ## Requirements 6 | - Home-assistant 0.78+ 7 | - Homey :) 8 | 9 | ## The list of currently support types: 10 | - sensors 11 | - binary_sensors 12 | - switches 13 | - light 14 | - scenes 15 | - scripts 16 | 17 | ## Getting started 18 | To connect the application to your home-assistant instance you will need to create a `Long Lived Access Token` in home-assistant. You can create a token on your home-assistant account profile page (https://www.home-assistant.io/docs/authentication/). 19 | 20 | You will also need to know the ip-address/hostname of your home-assistant instance. 21 | 22 | Install the application on your Homey and then go into the application settings. 23 | 24 | Fill in the correct address (e.g. http://127.0.0.1:8123) and the access token you have created inside home-assistant and then press "Save changes". 25 | 26 | In case you have entered the correct settings it should show "Successfully connected". 27 | 28 | Now you can add the devices you want to use within Homey 29 | 30 | ## Flow action 31 | Besides the ability to add devices and use them in your Homey flows there is also a flow action available to directly call home-assistant services inside flows. 32 | 33 | ## Compounds 34 | Because Homey and home-assistant are modeled differently when it comes to devices it makes sense to group multiple sensors from home-assistant into one Homey device. 35 | 36 | To do so you will have to install a custom component in your home-assistant installation: 37 | https://github.com/rogro82/io.homeassistant/blob/master/custom_component/homey.py 38 | 39 | After adding the custom component you can now declare the homey platform in your home-assistant configuration and define your devices. 40 | 41 | Example: 42 | ```yaml 43 | homey: 44 | livingroom_sensor: 45 | name: Livingroom environment 46 | icon: alarm_motion 47 | capabilities: 48 | measure_temperature: sensor.livingroom_temperature 49 | measure_humidity: sensor.livingroom_humidity 50 | measure_luminance: sensor.livingroom_luminance 51 | alarm_motion: binary_sensor.livingroom_motion 52 | alarm_contact: binary_sensor.toilet_door 53 | button: script.test 54 | onoff: switch.ventilation 55 | dim: input_number.test_slider 56 | capabilitiesConverters: 57 | measure_temperature: 58 | from: (state) => { return parseFloat(state) * 2; } 59 | to: (value) => { return value / 2; } 60 | dim: 61 | from: (state) => { return parseFloat(state) * 0.01; } 62 | to: (value) => { return value * 100; } 63 | ``` 64 | 65 | After reloading your home-assistant configuration you can add it in Homey by selecting `Compound` in the `Choose a device` dialog. -------------------------------------------------------------------------------- /drivers/sensor/assets/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 27 | 28 | 29 | 30 | 31 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Athom and Homey 2 | 3 | First off all, thank you for taking the time to contribute! 4 | 5 | The following is a set of guidelines for contributing to Athom and its packages, which are hosted in the [Athom Organization](https://github.com/athombv) on GitHub. These are just guidelines, not rules. Use your best judgment, and feel free to contact us if you have any questions. 6 | 7 | Please join our [community slack](https://slack.athom.com), if you have not done so already. 8 | We also have a [community forum](https://community.athom.com) for general discussions. 9 | 10 | 11 | ## Before submitting a bug or feature request 12 | 13 | * **Have you actually read the error message**? 14 | * Have you searched for similar issues? 15 | * Have you updated homey, all apps, and the development tools (if applicable)? 16 | * Have you checked that it's not a problem with one of the apps you're using, rather than Homey itself? 17 | * Have you looked at what's involved in fixing/implementing this? 18 | 19 | Capable programmers should always attempt to investigate and fix problems themselves before asking for others to help. Submit a pull request instead of an issue! 20 | 21 | Regular support is provided through our [support staff](support@athom.com). 22 | 23 | ## A great bug report contains 24 | 25 | * Context – what were you trying to achieve? 26 | * Detailed steps to reproduce the error from scratch. Try isolating the minimal amount of code needed to reproduce the error. 27 | * Any applicable log files or ID's. 28 | * Evidence you've looked into solving the problem and ideally, a theory on the cause and a possible solution. 29 | 30 | ## A great feature request contains 31 | 32 | * The current situation. 33 | * How and why the current situation is problematic. 34 | * A detailed proposal or pull request that demonstrates how the problem could be solved. 35 | * A use case – who needs this feature and why? 36 | * Any caveats. 37 | 38 | ## A great pull request contains 39 | 40 | * Minimal changes. Only submit code relevant to the current issue. Other changes should go in new pull requests. 41 | * Minimal commits. Please squash to a single commit before sending your pull request. 42 | * No conflicts. Please rebase off the latest master before submitting. 43 | * Code conforming to the existing conventions and formats. i.e. Please don't reformat whitespace. 44 | * Passing tests in the test folder (if applicable). Use existing tests as a reference. 45 | * Relevant documentation. 46 | 47 | ## Speeding up your pull request 48 | Merging pull requests takes time. While we always try to merge your pull request as soon as possible, there are certain things you can do to speed up this process. 49 | 50 | * Ask developers to review your code changes and post their feedback. 51 | * Ask users to test your changes and post their feedback. 52 | * Keep your changes to the minimal required amount, and dedicated to one issue/feature only. 53 | * If your PR introduces new features or more than just a small fix, please sign our [Contributor License Agreement](https://go.athom.com/cla). 54 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/alarm_contact.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /drivers/binary_sensor/assets/icons/alarm_contact.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/measure_pressure.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 9 | 10 | 11 | 25 | 26 | 27 | 28 | 29 | 31 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /drivers/sensor/assets/icons/measure_pressure.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 9 | 10 | 11 | 25 | 26 | 27 | 28 | 29 | 31 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /settings/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 15 | 16 | 17 | 18 | 19 |
20 | Connection settings 21 |
22 | 23 | 24 |
25 |
26 | 27 | 28 |
29 |
30 | 31 |
32 | Connection state 33 |
34 |
Failed to connect...
35 |
Successfully connected
36 |
37 |
38 | 39 | 40 | 41 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /custom_components/homey/__init__.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import logging 3 | import json 4 | 5 | import voluptuous as vol 6 | 7 | from homeassistant.const import (CONF_NAME, ATTR_ICON) 8 | from homeassistant.helpers import config_validation as cv 9 | from homeassistant.loader import bind_hass 10 | from homeassistant.helpers.entity import Entity 11 | from homeassistant.helpers.entity_component import EntityComponent 12 | 13 | _LOGGER = logging.getLogger(__name__) 14 | 15 | DOMAIN = 'homey' 16 | ENTITY_ID_FORMAT = DOMAIN + '.{}' 17 | 18 | CONF_ICON = "icon" 19 | CONF_CAPABILITIES = "capabilities" 20 | CONF_CAPABILITIES_CONVERTERS = "capabilitiesConverters" 21 | 22 | CONFIG_SCHEMA = vol.Schema({ 23 | DOMAIN: vol.Schema({ 24 | cv.slug: vol.Any({ 25 | vol.Optional(CONF_NAME): cv.string, 26 | vol.Optional(CONF_ICON): cv.string, 27 | vol.Optional(CONF_CAPABILITIES): dict, 28 | vol.Optional(CONF_CAPABILITIES_CONVERTERS): dict 29 | }, None) 30 | }) 31 | }, extra=vol.ALLOW_EXTRA) 32 | 33 | @asyncio.coroutine 34 | def async_setup(hass, config): 35 | """Set up variables.""" 36 | component = EntityComponent(_LOGGER, DOMAIN, hass) 37 | 38 | devices = [] 39 | 40 | for device_id, device_config in config[DOMAIN].items(): 41 | if not device_config: 42 | device_config = {} 43 | 44 | name = device_config.get(CONF_NAME) 45 | icon = device_config.get(CONF_ICON) 46 | capabilities = device_config.get(CONF_CAPABILITIES) 47 | capabilitiesConverters = device_config.get(CONF_CAPABILITIES_CONVERTERS) 48 | 49 | devices.append(Device(device_id, name, icon, capabilities, capabilitiesConverters)) 50 | 51 | yield from component.async_add_entities(devices) 52 | return True 53 | 54 | class Device(Entity): 55 | """Representation of a homey device.""" 56 | 57 | def __init__(self, device_id, name, icon, capabilities, capabilitiesConverters): 58 | """Initialize a homey device.""" 59 | self.entity_id = ENTITY_ID_FORMAT.format(device_id) 60 | self._name = name 61 | self._icon = icon 62 | self._capabilities = capabilities 63 | self._capabilitiesConverters = capabilitiesConverters 64 | 65 | @asyncio.coroutine 66 | def async_added_to_hass(self): 67 | """Run when entity about to be added.""" 68 | super().async_added_to_hass() 69 | 70 | @property 71 | def should_poll(self): 72 | """If entity should be polled.""" 73 | return False 74 | 75 | @property 76 | def name(self): 77 | """Return the name of the variable.""" 78 | return self._name 79 | 80 | @property 81 | def icon(self): 82 | """Return the icon to be used for this entity.""" 83 | return None 84 | 85 | @property 86 | def state(self): 87 | """Return the state of the component.""" 88 | return "" 89 | 90 | @property 91 | def state_attributes(self): 92 | """Return the state attributes.""" 93 | return { 94 | "icon": self._icon, 95 | "capabilities": self._capabilities, 96 | "capabilitiesConverters": self._capabilitiesConverters 97 | } 98 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | education, socio-economic status, nationality, personal appearance, race, 10 | religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at support@athom.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | -------------------------------------------------------------------------------- /drivers/light/assets/icons/LCT001.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /drivers/light/assets/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /drivers/sensor/assets/icons/measure_power.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | image/svg+xmlPP 78 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/measure_power.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | image/svg+xmlPP 78 | -------------------------------------------------------------------------------- /drivers/compound/assets/icons/alarm_heat.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /drivers/binary_sensor/assets/icons/alarm_heat.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /drivers/light/assets/icons/LLC020.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 9 | 38 | 39 | -------------------------------------------------------------------------------- /drivers/scene/assets/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /drivers/compound/device.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | const defaultValueConverter = { 6 | from: (state) => parseFloat(state), 7 | to: (value) => value 8 | } 9 | 10 | const defaultBooleanConverter = { 11 | from: (state) => (state == "on"), 12 | to: (value) => (value ? "on" : "off") 13 | } 14 | 15 | class CompoundDevice extends Homey.Device { 16 | 17 | onInit() { 18 | this._client = Homey.app.getClient(); 19 | 20 | this.entityId = this.getData().id; 21 | this.capabilities = this.getCapabilities(); 22 | this.compoundCapabilities = this.getData().capabilities; 23 | this.compoundCapabilitiesConverters = this.getData().capabilitiesConverters; 24 | 25 | this.log('device init'); 26 | this.log('id:', this.entityId); 27 | this.log('name:', this.getName()); 28 | this.log('class:', this.getClass()); 29 | 30 | this._client.registerDevice(this.entityId, this); 31 | 32 | if(this.hasCapability("button")) { 33 | this.registerCapabilityListener('button', this.onCapabilityButton.bind(this)) 34 | } 35 | 36 | if(this.hasCapability("onoff")) { 37 | this.registerCapabilityListener('onoff', this.onCapabilityOnoff.bind(this)) 38 | } 39 | 40 | if(this.hasCapability("locked")) { 41 | this.registerCapabilityListener('locked', this.onCapabilityLocked.bind(this)) 42 | } 43 | 44 | if(this.hasCapability("dim")) { 45 | console.log("attach dim listener"); 46 | this.registerCapabilityListener('dim', this.onCapabilityDim.bind(this)) 47 | } 48 | } 49 | 50 | inputConverter(capability) { 51 | let capabilityConverter = this.compoundCapabilitiesConverters[capability]; 52 | 53 | if(capabilityConverter != null) { 54 | if(capabilityConverter.from && typeof capabilityConverter.from === "function") { 55 | return capabilityConverter.from; 56 | } else if(capabilityConverter.from && typeof capabilityConverter.from === "string") { 57 | capabilityConverter.from = eval(capabilityConverter.from); 58 | return capabilityConverter.from; 59 | } 60 | } 61 | 62 | if(capability.startsWith("measure_") || 63 | capability == "dim") { 64 | return defaultValueConverter.from; 65 | } else { 66 | return defaultBooleanConverter.from; 67 | } 68 | } 69 | 70 | outputConverter(capability) { 71 | let capabilityConverter = this.compoundCapabilitiesConverters[capability]; 72 | if(capabilityConverter != null) { 73 | if(capabilityConverter.to && typeof capabilityConverter.to === "function") { 74 | return capabilityConverter.to; 75 | } else if(capabilityConverter.to && typeof capabilityConverter.to === "string") { 76 | capabilityConverter.to = eval(capabilityConverter.to); 77 | return capabilityConverter.to; 78 | } 79 | } 80 | 81 | if(capability.startsWith("measure_" || 82 | capability == "dim")) { 83 | return defaultValueConverter.to; 84 | } else { 85 | return defaultBooleanConverter.to; 86 | } 87 | } 88 | 89 | onAdded() { 90 | this.log('device added'); 91 | } 92 | 93 | onDeleted() { 94 | this.log('device deleted'); 95 | this._client.unregisterDevice(this.entityId); 96 | } 97 | 98 | onEntityUpdate(data) { 99 | let entityId = data.entity_id; 100 | 101 | Object.keys(this.compoundCapabilities).forEach(key => { 102 | if(this.compoundCapabilities[key] == entityId) { 103 | 104 | // console.log("---------------------------------------------------------------"); 105 | // console.log("update compound device:", this.entityId); 106 | // console.log("update compound capability:", key); 107 | // console.log("update compound by entity:", entityId); 108 | 109 | let convert = this.inputConverter(key); 110 | 111 | this.setCapabilityValue(key, convert(data.state)); 112 | } 113 | }); 114 | } 115 | 116 | onCapabilityButton( value, opts, callback ) { 117 | this._client.turnOnOff(this.compoundCapabilities["button"], true); 118 | callback( null ); 119 | } 120 | 121 | 122 | onCapabilityOnoff( value, opts, callback ) { 123 | this._client.turnOnOff(this.compoundCapabilities["onoff"], value); 124 | callback( null ); 125 | } 126 | 127 | onCapabilityLocked( value, opts, callback ) { 128 | console.log("onCapabilityLocked", value); 129 | this._client.turnOnOff(this.compoundCapabilities["locked"], value); 130 | callback( null ); 131 | } 132 | 133 | onCapabilityDim( value, opts, callback ) { 134 | let entityId = this.compoundCapabilities["dim"]; 135 | let outputValue = this.outputConverter("dim")(value); 136 | 137 | // TODO: make service calls configurable to allow other types then just input_number 138 | 139 | this._client.callService("input_number", "set_value", { 140 | "entity_id": entityId, 141 | "value": outputValue 142 | }); 143 | callback( null ); 144 | } 145 | } 146 | 147 | module.exports = CompoundDevice; -------------------------------------------------------------------------------- /drivers/light/device.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | const CAPABILITIES_SET_DEBOUNCE = 100; 6 | 7 | class LightDevice extends Homey.Device { 8 | 9 | onInit() { 10 | this._client = Homey.app.getClient(); 11 | 12 | this.entityId = this.getData().id; 13 | 14 | this._minMireds = 0; 15 | this._maxMireds = 0; 16 | 17 | this.log('device init'); 18 | this.log('id:', this.entityId); 19 | this.log('name:', this.getName()); 20 | this.log('class:', this.getClass()); 21 | 22 | this._client.registerDevice(this.entityId, this); 23 | 24 | let entity = this._client.getEntity(this.entityId); 25 | if(entity) { 26 | this.onEntityUpdate(entity); 27 | } 28 | 29 | this.registerMultipleCapabilityListener(this.getCapabilities(), this._onCapabilitiesSet.bind(this), CAPABILITIES_SET_DEBOUNCE); 30 | } 31 | 32 | onAdded() { 33 | this.log('device added'); 34 | } 35 | 36 | onDeleted() { 37 | this.log('device deleted'); 38 | this._client.unregisterDevice(this.entityId); 39 | } 40 | 41 | onCapabilityOnoff( value, opts, callback ) { 42 | this._client.turnOnOff(this.entityId, value); 43 | callback( null ); 44 | } 45 | 46 | getCapabilityUpdate(valueObj, capability) { 47 | let value = valueObj[capability]; 48 | if(typeof value === 'undefined') value = this.getCapabilityValue(capability) 49 | 50 | return value; 51 | } 52 | 53 | hasCapabilityUpdate(valueObj, capability) { 54 | let value = valueObj[capability]; 55 | return(typeof value !== 'undefined'); 56 | } 57 | 58 | _onCapabilitiesSet(valueObj, optsObj, callback) { 59 | if( typeof valueObj.dim === 'number' ) { 60 | valueObj.onoff = valueObj.dim > 0; 61 | } 62 | 63 | let lightOn = this.getCapabilityUpdate(valueObj, "onoff"); 64 | 65 | let data = { 66 | entity_id: this.entityId 67 | }; 68 | 69 | if(lightOn) { 70 | 71 | if(this.hasCapability("dim")) { 72 | let bri = this.getCapabilityUpdate(valueObj, "dim"); 73 | if(bri != this.getCapabilityValue("dim")) { 74 | data["brightness"] = bri * 250.0; 75 | 76 | this.setCapabilityValue("dim", bri); 77 | } 78 | } 79 | 80 | let lightModeUpdate = null; 81 | 82 | if(this.hasCapabilityUpdate(valueObj, "light_hue") || 83 | this.hasCapabilityUpdate(valueObj, "light_saturation")) { 84 | 85 | lightModeUpdate = "color"; 86 | 87 | let hue = this.getCapabilityUpdate(valueObj, "light_hue"); 88 | let sat = this.getCapabilityUpdate(valueObj, "light_saturation"); 89 | 90 | if(hue != this.getCapabilityValue("light_hue") || 91 | sat != this.getCapabilityValue("light_saturation")) { 92 | 93 | data["hs_color"] = [ 94 | hue * 360.0, 95 | sat * 100.0 96 | ] 97 | 98 | this.setCapabilityValue("light_hue", hue); 99 | this.setCapabilityValue("light_saturation", sat); 100 | } 101 | 102 | } else if(this.hasCapabilityUpdate(valueObj, "light_temperature")) { 103 | lightModeUpdate = "temperature"; 104 | 105 | let tmp = this.getCapabilityUpdate(valueObj, "light_temperature"); 106 | 107 | if(tmp != this.getCapabilityValue("light_temperature")) { 108 | data["color_temp"] = ((this._maxMireds - this._minMireds) * tmp) + this._minMireds; 109 | 110 | this.setCapabilityValue("light_temperature", tmp); 111 | } 112 | } 113 | 114 | if(lightModeUpdate && this.hasCapability("light_mode")) { 115 | this.setCapabilityValue("light_mode", lightModeUpdate); 116 | } 117 | } 118 | 119 | this._client.updateLight(lightOn, data); 120 | 121 | callback(null); 122 | } 123 | 124 | onEntityUpdate(data) { 125 | if(data) { 126 | 127 | this._minMireds = data.attributes["min_mireds"] || 0; 128 | this._maxMireds = data.attributes["max_mireds"] || 0; 129 | 130 | let lightOn = data.state == "on"; 131 | 132 | this.setCapabilityValue("onoff", lightOn); 133 | 134 | if(lightOn) { 135 | 136 | if(this.hasCapability("dim")) { 137 | let brightness = data.attributes["brightness"]; // 0..255 -> 0..1 138 | if(brightness != 0) { 139 | this.setCapabilityValue("dim", 1.0 / 250 * brightness); 140 | } 141 | } 142 | 143 | let hasLightMode = this.hasCapability("light_mode"); 144 | let hs = null; 145 | 146 | if(this.hasCapability("light_hue")) { 147 | hs = data.attributes["hs_color"]; 148 | if(hs) { 149 | let hue = 1.0 / 360.0 * hs[0]; // 0..360 -> 0..1 150 | let sat = 1.0 / 100.0 * hs[1]; // 0..100 -> 0..1 151 | 152 | this.setCapabilityValue("light_hue", hue); 153 | this.setCapabilityValue("light_saturation", sat); 154 | } 155 | } 156 | 157 | if(this.hasCapability("light_temperature")) { 158 | let temperature = data.attributes["color_temp"]; 159 | if(temperature) { 160 | let temp = 1.0 / (this._maxMireds - this._minMireds) * (temperature - this._minMireds); 161 | this.setCapabilityValue("light_temperature", temp); 162 | } 163 | } 164 | 165 | if(hasLightMode) { 166 | let light_mode = hs ? "color" : "temperature"; 167 | 168 | this.setCapabilityValue("light_mode", hs ? "color" : "temperature"); 169 | } 170 | } 171 | } 172 | } 173 | } 174 | 175 | module.exports = LightDevice; -------------------------------------------------------------------------------- /drivers/light/assets/icons/LLC011.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 19 | 33 | 35 | 56 | 68 | 69 | -------------------------------------------------------------------------------- /drivers/light/assets/icons/LCT003.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /drivers/light/assets/icons/LCT002.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 41 | 69 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /drivers/light/assets/icons/LWB006.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 8 | 9 | 11 | 14 | 17 | 19 | 20 | 21 | 22 | 24 | 28 | 35 | 37 | 41 | 42 | 43 | 47 | 54 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "io.home-assistant", 3 | "version": "1.0.0", 4 | "compatibility": ">=1.5.0", 5 | "sdk": 2, 6 | "brandColor": "#038FC7", 7 | "name": { 8 | "en": "Home Assistant" 9 | }, 10 | "description": { 11 | "en": "Connect to Home Assistant", 12 | "nl": "Koppelen met Home Assitant" 13 | }, 14 | "category": [ 15 | "appliances" 16 | ], 17 | "tags": { 18 | "en": [ 19 | "home-assistant", 20 | "hass" 21 | ] 22 | }, 23 | "permissions": [], 24 | "images": { 25 | "large": "/assets/images/large.png", 26 | "small": "/assets/images/small.png" 27 | }, 28 | "author": { 29 | "name": "Rob Groenendijk", 30 | "email": "rogro82@gmail.com" 31 | }, 32 | "capabilities": { 33 | "measure_generic": { 34 | "type": "string", 35 | "title": { 36 | "nl": "Waarde", 37 | "en": "Value" 38 | }, 39 | "getable": true, 40 | "setable": false 41 | }, 42 | "measure_numeric": { 43 | "type": "number", 44 | "title": { 45 | "nl": "Waarde", 46 | "en": "Value" 47 | }, 48 | "getable": true, 49 | "setable": false 50 | } 51 | }, 52 | "drivers": [ 53 | { 54 | "id": "light", 55 | "name": { 56 | "en": "Light", 57 | "nl": "Lamp" 58 | }, 59 | "class": "light", 60 | "capabilities": [ 61 | "onoff", 62 | "dim", 63 | "light_hue", 64 | "light_saturation", 65 | "light_temperature" 66 | ], 67 | "capabilitiesOptions": { 68 | "dim": { 69 | "opts": { 70 | "duration": true 71 | } 72 | }, 73 | "light_hue": { 74 | "opts": { 75 | "duration": true 76 | } 77 | }, 78 | "light_saturation": { 79 | "opts": { 80 | "duration": true 81 | } 82 | }, 83 | "light_temperature": { 84 | "opts": { 85 | "duration": true 86 | } 87 | } 88 | }, 89 | "images": { 90 | "large": "drivers/bulb/assets/images/large.jpg", 91 | "small": "drivers/bulb/assets/images/small.jpg" 92 | }, 93 | "pair": [ 94 | { 95 | "id": "list_devices", 96 | "template": "list_devices", 97 | "navigation": { 98 | "next": "add_devices" 99 | } 100 | }, 101 | { 102 | "id": "add_devices", 103 | "template": "add_devices" 104 | } 105 | ] 106 | }, 107 | { 108 | "id": "scene", 109 | "name": { 110 | "en": "Scene", 111 | "nl": "Scene" 112 | }, 113 | "class": "button", 114 | "capabilities": [ 115 | "button" 116 | ], 117 | "images": { 118 | "large": "drivers/bulb/assets/images/large.jpg", 119 | "small": "drivers/bulb/assets/images/small.jpg" 120 | }, 121 | "pair": [ 122 | { 123 | "id": "list_devices", 124 | "template": "list_devices", 125 | "navigation": { 126 | "next": "add_devices" 127 | } 128 | }, 129 | { 130 | "id": "add_devices", 131 | "template": "add_devices" 132 | } 133 | ] 134 | }, 135 | { 136 | "id": "script", 137 | "name": { 138 | "en": "Script", 139 | "nl": "Script" 140 | }, 141 | "class": "button", 142 | "capabilities": [ 143 | "button" 144 | ], 145 | "images": { 146 | "large": "drivers/bulb/assets/images/large.jpg", 147 | "small": "drivers/bulb/assets/images/small.jpg" 148 | }, 149 | "pair": [ 150 | { 151 | "id": "list_devices", 152 | "template": "list_devices", 153 | "navigation": { 154 | "next": "add_devices" 155 | } 156 | }, 157 | { 158 | "id": "add_devices", 159 | "template": "add_devices" 160 | } 161 | ] 162 | }, 163 | { 164 | "id": "switch", 165 | "name": { 166 | "en": "Switch", 167 | "nl": "Schakelaar" 168 | }, 169 | "class": "socket", 170 | "capabilities": [ 171 | "onoff" 172 | ], 173 | "images": { 174 | "large": "drivers/bulb/assets/images/large.jpg", 175 | "small": "drivers/bulb/assets/images/small.jpg" 176 | }, 177 | "pair": [ 178 | { 179 | "id": "list_devices", 180 | "template": "list_devices", 181 | "navigation": { 182 | "next": "choose_slave" 183 | } 184 | }, 185 | { 186 | "id": "choose_slave", 187 | "template": "choose_slave", 188 | "navigation": { 189 | "next": "add_devices" 190 | } 191 | }, 192 | { 193 | "id": "add_devices", 194 | "template": "add_devices" 195 | } 196 | ] 197 | }, 198 | { 199 | "id": "sensor", 200 | "name": { 201 | "en": "Sensor", 202 | "nl": "Sensor" 203 | }, 204 | "class": "sensor", 205 | "capabilities": [ 206 | "measure_generic", 207 | "measure_numeric", 208 | "measure_battery", 209 | "measure_co", 210 | "measure_co2", 211 | "measure_current", 212 | "measure_gust_angle", 213 | "measure_gust_strength", 214 | "measure_humidity", 215 | "measure_luminance", 216 | "measure_noise", 217 | "measure_pm25", 218 | "measure_power", 219 | "measure_pressure", 220 | "measure_rain", 221 | "measure_temperature", 222 | "measure_ultraviolet", 223 | "measure_voltage", 224 | "measure_water", 225 | "measure_wind_angle", 226 | "measure_wind_strength" 227 | ], 228 | "images": { 229 | "large": "drivers/bulb/assets/images/large.jpg", 230 | "small": "drivers/bulb/assets/images/small.jpg" 231 | }, 232 | "pair": [ 233 | { 234 | "id": "list_devices", 235 | "template": "list_devices", 236 | "navigation": { 237 | "next": "add_devices" 238 | } 239 | }, 240 | { 241 | "id": "add_devices", 242 | "template": "add_devices" 243 | } 244 | ] 245 | }, 246 | { 247 | "id": "binary_sensor", 248 | "name": { 249 | "en": "Binary sensor", 250 | "nl": "Binaire sensor" 251 | }, 252 | "class": "sensor", 253 | "capabilities": [ 254 | "onoff", 255 | "alarm_battery", 256 | "alarm_co", 257 | "alarm_co2", 258 | "alarm_contact", 259 | "alarm_fire", 260 | "alarm_generic", 261 | "alarm_heat", 262 | "alarm_motion", 263 | "alarm_pm25", 264 | "alarm_smoke", 265 | "alarm_tamper", 266 | "alarm_water" 267 | ], 268 | "images": { 269 | "large": "drivers/bulb/assets/images/large.jpg", 270 | "small": "drivers/bulb/assets/images/small.jpg" 271 | }, 272 | "pair": [ 273 | { 274 | "id": "list_devices", 275 | "template": "list_devices", 276 | "navigation": { 277 | "next": "add_devices" 278 | } 279 | }, 280 | { 281 | "id": "add_devices", 282 | "template": "add_devices" 283 | } 284 | ] 285 | }, 286 | { 287 | "id": "compound", 288 | "name": { 289 | "en": "Compound", 290 | "nl": "Samengesteld" 291 | }, 292 | "class": "sensor", 293 | "capabilities": [ 294 | "onoff", 295 | "locked", 296 | "alarm_battery", 297 | "alarm_co", 298 | "alarm_co2", 299 | "alarm_contact", 300 | "alarm_fire", 301 | "alarm_generic", 302 | "alarm_heat", 303 | "alarm_motion", 304 | "alarm_pm25", 305 | "alarm_smoke", 306 | "alarm_tamper", 307 | "alarm_water", 308 | "measure_generic", 309 | "measure_numeric", 310 | "measure_battery", 311 | "measure_co", 312 | "measure_co2", 313 | "measure_current", 314 | "measure_gust_angle", 315 | "measure_gust_strength", 316 | "measure_humidity", 317 | "measure_luminance", 318 | "measure_noise", 319 | "measure_pm25", 320 | "measure_power", 321 | "measure_pressure", 322 | "measure_rain", 323 | "measure_temperature", 324 | "measure_ultraviolet", 325 | "measure_voltage", 326 | "measure_water", 327 | "measure_wind_angle", 328 | "measure_wind_strength" 329 | ], 330 | "images": { 331 | "large": "drivers/bulb/assets/images/large.jpg", 332 | "small": "drivers/bulb/assets/images/small.jpg" 333 | }, 334 | "pair": [ 335 | { 336 | "id": "list_devices", 337 | "template": "list_devices", 338 | "navigation": { 339 | "next": "add_devices" 340 | } 341 | }, 342 | { 343 | "id": "add_devices", 344 | "template": "add_devices" 345 | } 346 | ] 347 | } 348 | ], 349 | "flow": { 350 | "actions": [ 351 | { 352 | "id": "callService", 353 | "title": { 354 | "en": "Call service", 355 | "nl": "Roep service aan" 356 | }, 357 | "args": [ 358 | { 359 | "name": "domain", 360 | "type": "text" 361 | }, 362 | { 363 | "name": "service", 364 | "type": "text" 365 | }, 366 | { 367 | "name": "data", 368 | "type": "text" 369 | } 370 | ] 371 | } 372 | ] 373 | } 374 | } -------------------------------------------------------------------------------- /drivers/compound/assets/icons/alarm_pressure.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 11 | 17 | 23 | 24 | 27 | 31 | 34 | 37 | 40 | 43 | 46 | 49 | 85 | 88 | 90 | 93 | 94 | 97 | 98 | -------------------------------------------------------------------------------- /drivers/binary_sensor/assets/icons/alarm_pressure.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 11 | 17 | 23 | 24 | 27 | 31 | 34 | 37 | 40 | 43 | 46 | 49 | 85 | 88 | 90 | 93 | 94 | 97 | 98 | -------------------------------------------------------------------------------- /lib/Client.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Homey = require('homey'); 4 | 5 | const WebSocket = require('ws'); 6 | global.WebSocket = WebSocket; 7 | 8 | const Hass = require("home-assistant-js-websocket"); 9 | 10 | const sensorIcons = { 11 | 'measure_battery': 'measure_battery', 12 | 'measure_humidity': 'measure_humidity', 13 | 'measure_luminance': 'measure_luminance', 14 | 'measure_temperature': 'measure_temperature', 15 | 'measure_power': 'measure_power', 16 | 'measure_co2': 'co2', 17 | 'measure_noise': 'noise', 18 | 'meter_power': 'meter_power', 19 | 'alarm_contact': 'alarm_contact', 20 | 'alarm_heat': 'alarm_heat', 21 | 'alarm_motion': 'alarm_motion', 22 | 'alarm_pressure': 'alarm_pressure', 23 | 'alarm_smoke': 'alarm_smoke', 24 | 'alarm_tamper': 'alarm_tamper', 25 | 'alarm_water': 'alarm_water', 26 | 'alarm_generic': 'alarm_generic' 27 | } 28 | 29 | class Client extends Homey.SimpleClass { 30 | 31 | constructor(address, token) { 32 | super(); 33 | 34 | this._entities = []; 35 | this._lights = []; 36 | this._scenes = []; 37 | this._scripts = []; 38 | this._switches = []; 39 | this._sensors = []; 40 | this._binary_sensors = []; 41 | this._compounds = []; 42 | 43 | this._devices = {}; 44 | this._connection = null; 45 | 46 | this.connect(address, token); 47 | } 48 | 49 | registerDevice(deviceId, device) { 50 | this._devices[deviceId] = device; 51 | } 52 | 53 | unregisterDevice(deviceId) { 54 | this._devices[deviceId] = null; 55 | } 56 | 57 | getLights() { 58 | return this._lights; 59 | } 60 | 61 | getScenes() { 62 | return this._scenes; 63 | } 64 | 65 | getScripts() { 66 | return this._scripts; 67 | } 68 | 69 | getSwitches() { 70 | return this._switches; 71 | } 72 | 73 | getSensors() { 74 | return this._sensors; 75 | } 76 | 77 | getBinarySensors() { 78 | return this._binary_sensors; 79 | } 80 | 81 | getCompounds() { 82 | return this._compounds; 83 | } 84 | 85 | getEntity(entityId) { 86 | return this._entities[entityId]; 87 | } 88 | 89 | connect(address, token, notify) { 90 | console.log('connecting to home-assistant'); 91 | 92 | if(this._connection != null) { 93 | this._connection.close(); 94 | } 95 | 96 | // clear any previously discovered devices 97 | 98 | this._lights = []; 99 | this._scenes = []; 100 | this._scripts = []; 101 | this._switches = []; 102 | this._sensors = []; 103 | this._compounds = []; 104 | this._binary_sensors = []; 105 | 106 | if(address && address != "" 107 | && token && token != "") { 108 | 109 | let auth = new Hass.Auth({ 110 | hassUrl: address, 111 | access_token: token, 112 | expires: new Date(new Date().getTime() + 1e11) 113 | }); 114 | 115 | Hass.createConnection({ auth }) 116 | .then(conn => { 117 | console.log('succesfully connected... subscribing to entities and events'); 118 | 119 | if(notify) { 120 | this.emit("connection_update", { connected: true }); 121 | } 122 | 123 | this._connection = conn; 124 | Hass.subscribeEntities(conn, this._onEntitiesUpdate.bind(this)); 125 | conn.subscribeEvents(this._onStateChanged.bind(this), "state_changed"); 126 | }) 127 | .catch(err => { 128 | this._connection = null; 129 | this.emit("connection_update", { connected: false }); 130 | this.log("failed to connect:", err); 131 | }); 132 | } 133 | } 134 | 135 | _onStateChanged(event) { 136 | try { 137 | let deviceIds = Object.keys(this._devices); 138 | let data = event.data; 139 | if(data) { 140 | let entityId = data.entity_id; 141 | 142 | deviceIds.forEach(deviceId => { 143 | let device = this._devices[deviceId]; 144 | if(device != null) { 145 | if(deviceId == entityId) { 146 | device.onEntityUpdate(data.new_state); 147 | } 148 | if(deviceId.startsWith("homey.")) { 149 | let capabilities = device.getData().capabilities; 150 | Object.keys(capabilities).forEach(key => { 151 | if(capabilities[key] == entityId) { 152 | device.onEntityUpdate(data.new_state); 153 | } 154 | }); 155 | } 156 | } 157 | }); 158 | } 159 | } catch(e) { 160 | console.error("onStateChanged error:", e); 161 | } 162 | } 163 | 164 | _onEntitiesUpdate(entities) { 165 | if(this._entities.length != entities.length) { 166 | console.log("update entities"); 167 | 168 | let lights = []; 169 | let scenes = []; 170 | let scripts = []; 171 | let switches = []; 172 | let sensors = []; 173 | let binary_sensors = []; 174 | let compounds = []; 175 | 176 | Object.keys(entities).forEach(id => { 177 | 178 | if(id.startsWith("homey.")) { 179 | try { 180 | let entity = entities[id]; 181 | let entityName = entity.attributes["friendly_name"] || id; 182 | 183 | let compoundCapabilities = entity.attributes["capabilities"] || {}; 184 | let compoundCapabilitiesConverters = entity.attributes["capabilitiesConverters"] || {}; 185 | let compoundIcon = entity.attributes["icon"]; 186 | 187 | let compound = { 188 | name: entityName, 189 | data: { 190 | id: id, 191 | capabilities: compoundCapabilities, 192 | capabilitiesConverters: compoundCapabilitiesConverters 193 | }, 194 | capabilities: Object.keys(compoundCapabilities) 195 | }; 196 | 197 | if(typeof sensorIcons[compoundIcon] === 'string' ) { 198 | compound.icon = `/icons/${ sensorIcons[compoundIcon] }.svg`; 199 | } 200 | 201 | compounds.push(compound); 202 | 203 | } catch(ex) { 204 | console.log("failed to load compound:", ex); 205 | } 206 | } 207 | 208 | if(id.startsWith("binary_sensor.")) { 209 | 210 | let entity = entities[id]; 211 | let entityName = entity.attributes["friendly_name"] || id; 212 | let deviceClass = entity.attributes["device_class"]; 213 | 214 | let sensorCapability = null; 215 | 216 | switch(deviceClass) { 217 | case "battery" : sensorCapability = "alarm_battery"; break; 218 | case "gas" : sensorCapability = "alarm_co"; break; 219 | case "opening" : sensorCapability = "alarm_contact"; break; 220 | case "door" : sensorCapability = "alarm_contact"; break; 221 | case "garage_door" : sensorCapability = "alarm_contact"; break; 222 | case "window" : sensorCapability = "alarm_contact"; break; 223 | case "fire" : sensorCapability = "alarm_fire"; break; 224 | case "heat" : sensorCapability = "alarm_heat"; break; 225 | case "motion" : sensorCapability = "alarm_motion"; break; 226 | case "smoke" : sensorCapability = "alarm_smoke"; break; 227 | case "moisture" : sensorCapability = "alarm_water"; break; 228 | default: 229 | sensorCapability = "alarm_generic"; 230 | break; 231 | } 232 | 233 | let binary_sensor = { 234 | name: entityName, 235 | data: { 236 | id: id 237 | }, 238 | capabilities: [ sensorCapability ] 239 | }; 240 | 241 | if(typeof sensorIcons[sensorCapability] === 'string' ) { 242 | binary_sensor.icon = `/icons/${ sensorIcons[sensorCapability] }.svg`; 243 | } 244 | 245 | binary_sensors.push(binary_sensor); 246 | } 247 | 248 | if(id.startsWith("sensor.")) { 249 | 250 | let entity = entities[id]; 251 | let entityName = entity.attributes["friendly_name"] || id; 252 | let deviceClass = entity.attributes["device_class"]; 253 | 254 | let sensorCapability = null; 255 | 256 | switch(deviceClass) { 257 | case "battery" : sensorCapability = "measure_battery"; break; 258 | case "humidity" : sensorCapability = "measure_humidity"; break; 259 | case "illuminance" : sensorCapability = "measure_luminance"; break; 260 | case "temperature" : sensorCapability = "measure_temperature"; break; 261 | case "pressure" : sensorCapability = "measure_pressure"; break; 262 | default: 263 | let unit_of_measurement = entity.attributes["unit_of_measurement"]; 264 | 265 | switch(unit_of_measurement) { 266 | case "kWh" : sensorCapability = "meter_power"; break; 267 | case "A" : sensorCapability = "measure_current"; break; 268 | case "W" : sensorCapability = "measure_power"; break; 269 | case "V" : sensorCapability = "measure_voltage"; break; 270 | case "ppm" : sensorCapability = "measure_co2"; break; 271 | case "dB" : sensorCapability = "measure_noise"; break; 272 | default: { 273 | if(!isNaN(parseFloat(entity.state))) { 274 | sensorCapability = "measure_numeric"; 275 | } else { 276 | sensorCapability = "measure_generic"; 277 | } 278 | break; 279 | } 280 | } 281 | } 282 | 283 | let sensor = { 284 | name: entityName, 285 | data: { 286 | id: id 287 | }, 288 | capabilities: [ sensorCapability ] 289 | }; 290 | 291 | if(typeof sensorIcons[sensorCapability] === 'string' ) { 292 | sensor.icon = `/icons/${ sensorIcons[sensorCapability] }.svg`; 293 | } 294 | 295 | sensors.push(sensor); 296 | } 297 | 298 | if(id.startsWith("switch.")) { 299 | 300 | let entity = entities[id]; 301 | let entityName = entity.attributes["friendly_name"] || id; 302 | 303 | switches.push({ 304 | name: entityName, 305 | data: { 306 | id: id 307 | } 308 | }); 309 | } 310 | 311 | if(id.startsWith("script.")) { 312 | 313 | let entity = entities[id]; 314 | let entityName = entity.attributes["friendly_name"] || id; 315 | 316 | scripts.push({ 317 | name: entityName, 318 | data: { 319 | id: id 320 | } 321 | }); 322 | } 323 | 324 | if(id.startsWith("scene.")) { 325 | 326 | let entity = entities[id]; 327 | let entityName = entity.attributes["friendly_name"] || id; 328 | 329 | scenes.push({ 330 | name: entityName, 331 | data: { 332 | id: id 333 | } 334 | }); 335 | } 336 | 337 | if(id.startsWith("light.")) { 338 | 339 | let entity = entities[id]; 340 | let entityName = entity.attributes["friendly_name"] || id; 341 | let lightCapabilities = [ "onoff" ]; 342 | 343 | /* 344 | SUPPORT_BRIGHTNESS = 1 345 | SUPPORT_COLOR_TEMP = 2 346 | SUPPORT_EFFECT = 4 347 | SUPPORT_FLASH = 8 348 | SUPPORT_COLOR = 16 349 | SUPPORT_TRANSITION = 32 350 | SUPPORT_WHITE_VALUE = 128 351 | */ 352 | 353 | let features = entity.attributes["supported_features"] || 0; 354 | 355 | if((features & 1) == 1) lightCapabilities.push("dim"); 356 | if((features & 2) == 2) lightCapabilities.push("light_temperature"); 357 | if((features & 16) == 16) lightCapabilities.push("light_hue", "light_saturation"); 358 | 359 | if(lightCapabilities.includes("light_temperature") 360 | && lightCapabilities.includes("light_hue")) { 361 | lightCapabilities.push("light_mode") 362 | } 363 | 364 | lights.push({ 365 | name: entityName, 366 | data: { 367 | id: id 368 | }, 369 | capabilities: lightCapabilities 370 | }); 371 | } 372 | }); 373 | 374 | let update = this._entities.length == 0; 375 | 376 | this._lights = lights; 377 | this._scenes = scenes; 378 | this._scripts = scripts; 379 | this._switches = switches; 380 | this._sensors = sensors; 381 | this._binary_sensors = binary_sensors; 382 | this._compounds = compounds; 383 | this._entities = entities; 384 | 385 | if(update) { 386 | setTimeout(() => { 387 | Object.keys(this._entities).forEach(id => { 388 | this._onStateChanged({ 389 | data: { 390 | entity_id: id, 391 | new_state: this._entities[id] 392 | } 393 | }); 394 | }); 395 | }, 5000); 396 | } 397 | } 398 | } 399 | 400 | turnOnOff(entityId, on) { 401 | console.log("turnOnOff:", entityId, on); 402 | if(this._connection) { 403 | Hass.callService(this._connection, "homeassistant", on ? "turn_on" : "turn_off", { 404 | "entity_id": entityId 405 | }).catch(error => { 406 | console.error("turnOnOff error:", error); 407 | }) 408 | } 409 | } 410 | 411 | updateLight(on, data) { 412 | console.log("updateLight:", on, data); 413 | if(this._connection) { 414 | Hass.callService(this._connection, "light", on ? "turn_on" : "turn_off", data) 415 | .catch(error => { 416 | console.error("updateLight error:", error); 417 | }) 418 | } 419 | } 420 | 421 | callService(domain, service, data) { 422 | console.log("callService:", domain, service, data); 423 | try { 424 | if(this._connection) { 425 | let jsonData = (typeof data === "string") ? JSON.parse(data) : data; 426 | 427 | console.log("---- call service ----"); 428 | console.log("domain:", domain); 429 | console.log("service:", service); 430 | console.log("data:", jsonData); 431 | 432 | Hass.callService(this._connection, domain, service, jsonData) 433 | .catch(error => { 434 | console.error("callService error:", error); 435 | }); 436 | } 437 | } catch (ex) { 438 | console.log("callService error:", ex); 439 | } 440 | } 441 | 442 | } 443 | 444 | module.exports = Client --------------------------------------------------------------------------------