Skip to content

Commit

Permalink
1.9.0
Browse files Browse the repository at this point in the history
Added widget for alarm control panel.
  • Loading branch information
RonnyWinkler committed Dec 28, 2024
1 parent 7db3c3a commit fb7ce66
Show file tree
Hide file tree
Showing 31 changed files with 578 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .homeychangelog.json
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,10 @@
"1.8.6": {
"en": "Internal optimization.",
"de": "Interne Optimierung."
},
"1.9.0": {
"en": "Added widget for alarm control panel.",
"de": "Widget für Alarmzentrale hinzugefügt."
}

}
2 changes: 1 addition & 1 deletion .homeycompose/app.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "io.home-assistant.community",
"version": "1.8.6",
"version": "1.9.0",
"compatibility": ">=12.2.0",
"sdk": 3,
"brandColor": "#0DA6EA",
Expand Down
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"type": "node",
"request": "attach",
"restart": true,
"name": "Attach HA to Homey23 DEV Docker",
"address": "192.168.1.16",
"name": "Attach HA to Homey 23 DEV Docker",
"address": "192.168.1.42",
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app/"
Expand Down
61 changes: 60 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ class App extends Homey.App {
await this._registerFlowTriggers();
await this._registerFlowConditions();
await this._registerFlowArguments();
// register widgets
await this._registerWidgets();

// App events
this.homey.settings.on("set", async (key) => {
Expand Down Expand Up @@ -1608,7 +1610,64 @@ class App extends Homey.App {
}
});
}

// WIDGETS ==============================================================================
async _registerWidgets(){
this.homey.dashboards.getWidget('alarm_control_panel').registerSettingAutocompleteListener('device', async (query, settings) => {
let devices = [];
let alarm_devices = this.homey.drivers.getDriver('alarm_control_panel').getDevices();
alarm_devices.forEach(device => {
devices.push({
name: device.getName(),
device_id: device.getData().id,
driver_id: 'alarm_control_panel'
})
});
return devices.filter((item) => item.name.toLowerCase().includes(query.toLowerCase()));
});
}

// WIDGET API ============================================================================
async apiWidgetUpdate(driver_id, device_id){
let device = this.homey.drivers.getDriver(driver_id).getDevices().filter(e=>{ return ( e.getData().id == device_id ) })[0];
await device.widgetUpdate( );
}

async apiWidgetPost(driver_id, device_id, body){
if (!device_id || !driver_id){
throw new Error("Device not set in widget settings");
}
try{
let device = this.homey.drivers.getDriver(driver_id).getDevices().filter(e=>{ return ( e.getData().id == device_id ) })[0];
await device.widgetPost( body );
}
catch(error){
try{
let message = JSON.parse(error.message)
throw new Error(message.message);
}
catch(error){
throw new Error(error.message);
}
}
}
async apiWidgetGet(driver_id, device_id, command){
if (!device_id || !driver_id){
throw new Error("Device not set in widget settings");
}
try{
let device = this.homey.drivers.getDriver(driver_id).getDevices().filter(e=>{ return ( e.getData().id == device_id ) })[0];
await device.widgetGet( command );
}
catch(error){
try{
let message = JSON.parse(error.message)
throw new Error(message.message);
}
catch(error){
throw new Error(error.message);
}
}
}
}

module.exports = App;
37 changes: 36 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_comment": "This file is generated. Please edit .homeycompose/app.json instead.",
"id": "io.home-assistant.community",
"version": "1.8.6",
"version": "1.9.0",
"compatibility": ">=12.2.0",
"sdk": 3,
"brandColor": "#0DA6EA",
Expand Down Expand Up @@ -9962,6 +9962,41 @@
]
}
],
"widgets": {
"alarm_control_panel": {
"name": {
"en": "Alarm control panel",
"de": "Alarmzentrale",
"nl": "Alarmbedieningspaneel"
},
"settings": [
{
"id": "device",
"type": "autocomplete",
"title": {
"en": "Gerät",
"de": "Device"
}
}
],
"height": 226,
"api": {
"apiWidgetUpdate": {
"method": "GET",
"path": "/widget_update"
},
"apiWidgetPost": {
"method": "POST",
"path": "/widget_post"
},
"apiWidgetGet": {
"method": "GET",
"path": "/widget_get"
}
},
"id": "alarm_control_panel"
}
},
"capabilities": {
"alarm_control_panel_alarm": {
"type": "boolean",
Expand Down
29 changes: 29 additions & 0 deletions drivers/alarm_control_panel/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class AlarmControlPanelDevice extends BaseDevice {
if ((features & 32) == 32) {
modes.push( this.homey.app.manifest.capabilities.alarm_control_panel_mode.values.find(u => u.id === 'armed_vacation') );
}
modes.push( this.homey.app.manifest.capabilities.alarm_control_panel_mode.values.find(u => u.id === 'disarmed') );
await this.setCapabilityEnumList("alarm_control_panel_mode", modes);

if ((features & 8) != 8 && this.hasCapability('alarm_control_panel_alarm_trigger')){
Expand All @@ -113,6 +114,8 @@ class AlarmControlPanelDevice extends BaseDevice {

try{
await this.setCapabilityValue("alarm_control_panel_mode", data.state);
// Realtime event - Widget update
await this.widgetUpdate();
}
catch(error){
this.log("Error setting alarm_control_panel_mode" + error.message);
Expand All @@ -139,6 +142,9 @@ class AlarmControlPanelDevice extends BaseDevice {
// Capabilities ===========================================================================================
async _onCapabilityAlarmControlPanelMode( value, opts ) {
let code = this.getSetting('code');
if (opts.code){
code = opts.code;
}
let service = '';
switch (value){
case 'disarmed':
Expand Down Expand Up @@ -183,6 +189,29 @@ class AlarmControlPanelDevice extends BaseDevice {
await this._onCapabilityAlarmControlPanelMode( args.mode, {});
}

// Widget Actions
async widgetUpdate(){
// Redefinition: Trigger Widget update by sending a realtime event
let modes = [];
try{
modes = await this.getCapabilityEnumList("alarm_control_panel_mode");
}
catch(error){}
await this.homey.api.realtime("alarm_control_panel_state_changed", {driver_id:'alarm_control_panel', device_id: this.getData().id,
mode: this.getCapabilityValue("alarm_control_panel_mode"),
modes: modes
} );
}

async widgetPost(body){
// Redefinition: Process HTTP POST from Widget
switch (body.command){
case 'set_alarm_control_panel_mode':
await this._onCapabilityAlarmControlPanelMode( body.mode, {code: body.code});
break;
}
}

}

module.exports = AlarmControlPanelDevice;
32 changes: 32 additions & 0 deletions drivers/basedevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,27 @@ class BaseDevice extends Homey.Device {
}catch(error){}
}

async getCapabilityEnumList(capability){
let values = [];
if (this.hasCapability(capability)){
try{
let capabilityOptions = this.getCapabilityOptions(capability);
if (capabilityOptions.values){
values = capabilityOptions.values;
}
}
catch(error){
this.log("getCapabilityEnumList(): Error getting capabilityOptions: "+error.message);
// Read capability definition if no options are set
let capabilityDef = Capability.getCapability(capability);
if (capabilityDef.values){
values = capabilityDef.values;
}
}
}
return values;
}

async onInitDevice(){
// Init device on satrtup with latest data to have initial values before HA sends updates
this.homey.clearTimeout(this.timeoutInitDevice);
Expand Down Expand Up @@ -1348,6 +1369,17 @@ class BaseDevice extends Homey.Device {
// }
// });
// }

// Widget functions ===========================================================================================
async widgetUpdate(){
// Redefine in subclass. Trigger widget update by sending a realtime event
}
async widgetPost(body){
// Redefine in subclass. Process HTTP POST message in the device
}
async widgetGet(command){
// Redefine in subclass. Process HTTP GET message in the device
}
}

module.exports = BaseDevice;
14 changes: 14 additions & 0 deletions widgets/alarm_control_panel/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = {

async apiWidgetUpdate({ homey, query }) {
return await homey.app.apiWidgetUpdate( query.driver_id, query.device_id );
},
async apiWidgetPost({ homey, query, body }) {
return await homey.app.apiWidgetPost( query.driver_id, query.device_id, body );
},
async apiWidgetGet({ homey, query }) {
return await homey.app.apiWidgetGet( query.driver_id, query.device_id, query.command );
}
};
Binary file added widgets/alarm_control_panel/preview-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added widgets/alarm_control_panel/preview-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/0.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/6.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/7.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/9.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/armed_away.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/armed_custom_bypass.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/armed_home.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/armed_night.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/armed_vacation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/clear.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions widgets/alarm_control_panel/public/disarmed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fb7ce66

Please sign in to comment.