From 44ec2e2eb12f1a94ebd9154e2a30d63a43dbf905 Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Fri, 19 Jan 2024 19:26:02 +0000 Subject: [PATCH] Initial Solution --- config.schema.json | 122 +++++++++--------------- source/Alert.mc | 25 ++--- source/ErrorView.mc | 10 +- source/HomeAssistantApp.mc | 4 +- source/HomeAssistantConfirmation.mc | 12 ++- source/HomeAssistantMenuItemFactory.mc | 29 +++--- source/HomeAssistantTapMenuItem.mc | 7 +- source/HomeAssistantTemplateMenuItem.mc | 11 +-- source/HomeAssistantToggleMenuItem.mc | 29 +++++- source/HomeAssistantView.mc | 4 +- source/WebhookManager.mc | 4 +- 11 files changed, 123 insertions(+), 134 deletions(-) diff --git a/config.schema.json b/config.schema.json index 1b105f1..c8d273e 100644 --- a/config.schema.json +++ b/config.schema.json @@ -2,12 +2,8 @@ "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { - "title": { - "type": "string" - }, - "items": { - "$ref": "#/$defs/items" - }, + "title": { "type": "string" }, + "items": { "$ref": "#/$defs/items" }, "required": ["title", "items"], "additionalProperties": false }, @@ -15,17 +11,19 @@ "toggle": { "type": "object", "properties": { - "entity": { - "$ref": "#/$defs/entity" - }, - "name": { - "title": "Your familiar name", - "type": "string" - }, + "entity": { "$ref": "#/$defs/entity" }, + "name": { "title": "Your familiar name", "type": "string" }, "type": { "title": "Menu item type", "description": "One of 'tap', 'template', 'toggle' or 'group'.", "const": "toggle" + }, + "tap_action": { + "type": "object", + "properties": { + "confirm": { "$ref": "#/$defs/confirm" } + }, + "additionalProperties": false } }, "required": ["entity", "name", "type"], @@ -34,39 +32,24 @@ "template": { "type": "object", "properties": { - "entity": { - "$ref": "#/$defs/entity" - }, - "name": { - "title": "Your familiar name", - "type": "string" - }, - "content": { - "title": "What to display (template)", - "type": "string" - }, + "entity": { "$ref": "#/$defs/entity" }, + "name": { "title": "Your familiar name", "type": "string" }, + "content": { "title": "What to display (template)", "type": "string" }, "type": { "title": "Menu item type", "description": "One of 'tap', 'template', 'toggle' or 'group'.", "const": "template" }, - "tap_action": { - "$ref": "#/$defs/action" - } + "tap_action": { "$ref": "#/$defs/tap_action" } }, - "required": ["name", "entity", "content", "type"], + "required": ["name", "content", "type"], "additionalProperties": false }, "tap": { "type": "object", "properties": { - "entity": { - "$ref": "#/$defs/entity" - }, - "name": { - "title": "Your familiar name", - "type": "string" - }, + "entity": { "$ref": "#/$defs/entity" }, + "name": { "title": "Your familiar name", "type": "string" }, "type": { "title": "Menu item type", "description": "One of 'tap', 'template', 'toggle' or 'group'.", @@ -78,61 +61,43 @@ "title": "Schema change:", "description": "Use 'tap_action' instead to mirror Home Assistant." }, - "tap_action": { - "$ref": "#/$defs/action" - } + "tap_action": { "$ref": "#/$defs/tap_action" } }, "oneOf": [ - { - "required": ["entity", "name", "type", "service"] - }, - { - "required": ["entity", "name", "type", "tap_action"] - } + { "required": ["name", "type", "service"] }, + { "required": ["name", "type", "tap_action"] } ], "additionalProperties": false }, - "menu": { + "group": { "type": "object", "properties": { "entity": { - "$ref": "#/$defs/entity" - }, - "name": { - "title": "Your familiar name", - "type": "string" - }, - "title": { - "type": "string" + "$ref": "#/$defs/entity", + "deprecated": true, + "title": "Schema change:", + "description": "'entity' is no longer necessary and should be removed." }, + "name": { "title": "Your familiar name", "type": "string" }, + "title": { "type": "string" }, "type": { "title": "Menu item type", "description": "One of 'tap', 'template', 'toggle' or 'group'.", "const": "group" }, - "items": { - "$ref": "#/$defs/items" - } + "items": { "$ref": "#/$defs/items" } }, - "required": ["entity", "name", "title", "type", "items"], + "required": ["name", "title", "type", "items"], "additionalProperties": false }, "items": { "type": "array", "items": { "oneOf": [ - { - "$ref": "#/$defs/toggle" - }, - { - "$ref": "#/$defs/template" - }, - { - "$ref": "#/$defs/tap" - }, - { - "$ref": "#/$defs/menu" - } + { "$ref": "#/$defs/toggle" }, + { "$ref": "#/$defs/template" }, + { "$ref": "#/$defs/tap" }, + { "$ref": "#/$defs/group" } ] } }, @@ -141,21 +106,24 @@ "type": "string", "pattern": "^[^.]+\\.[^.]+$" }, - "action": { + "tap_action": { "title": "Action", "description": "'confirm' field is optional.", "type": "object", "properties": { - "service": { - "$ref": "#/$defs/entity" - }, - "confirm": { - "type": "boolean", - "default": false, - "description": "Confirm the action before execution as a precaution." + "service": { "$ref": "#/$defs/entity" }, + "confirm": { "$ref": "#/$defs/confirm" }, + "data": { + "type": "object", + "description": "The object containing the parameters and their values to be passed to the entity. No schema checking can be done here, you are on your own! On application crash, remove the parameters." } }, "required": ["service"] + }, + "confirm": { + "type": "boolean", + "default": false, + "description": "Confirm the action before execution as a precaution." } } } diff --git a/source/Alert.mc b/source/Alert.mc index 51ca1dc..4ef730c 100644 --- a/source/Alert.mc +++ b/source/Alert.mc @@ -28,14 +28,13 @@ using Toybox.WatchUi; using Toybox.Timer; class Alert extends WatchUi.View { - private static const bRadius = 10; - private var mTimer as Timer.Timer; - private var mTimeout as Lang.Number; - private var mText as Lang.String; - private var mFont as Graphics.FontType; - private var mFgcolor as Graphics.ColorType; - private var mBgcolor as Graphics.ColorType; - private var mAntiAlias as Lang.Boolean = false; + private static const scRadius = 10; + private var mTimer as Timer.Timer; + private var mTimeout as Lang.Number; + private var mText as Lang.String; + private var mFont as Graphics.FontType; + private var mFgcolor as Graphics.ColorType; + private var mBgcolor as Graphics.ColorType; function initialize(params as Lang.Dictionary) { View.initialize(); @@ -65,10 +64,6 @@ class Alert extends WatchUi.View { mTimeout = 2000; } - if (Graphics.Dc has :setAntiAlias) { - mAntiAlias = true; - } - mTimer = new Timer.Timer(); } @@ -88,7 +83,7 @@ class Alert extends WatchUi.View { var bX = (dc.getWidth() - bWidth) / 2; var bY = (dc.getHeight() - bHeight) / 2; - if (mAntiAlias) { + if (Graphics.Dc has :setAntiAlias) { dc.setAntiAlias(true); } @@ -98,7 +93,7 @@ class Alert extends WatchUi.View { ); dc.clear(); dc.setColor(mBgcolor, mBgcolor); - dc.fillRoundedRectangle(bX, bY, bWidth, bHeight, bRadius); + dc.fillRoundedRectangle(bX, bY, bWidth, bHeight, scRadius); dc.setColor(mFgcolor, mBgcolor); for (var i = 0; i < 3; ++i) { @@ -106,7 +101,7 @@ class Alert extends WatchUi.View { bY += i; bWidth -= (2 * i); bHeight -= (2 * i); - dc.drawRoundedRectangle(bX, bY, bWidth, bHeight, bRadius); + dc.drawRoundedRectangle(bX, bY, bWidth, bHeight, scRadius); } var tX = dc.getWidth() / 2; diff --git a/source/ErrorView.mc b/source/ErrorView.mc index 114a763..f4a99da 100644 --- a/source/ErrorView.mc +++ b/source/ErrorView.mc @@ -36,16 +36,14 @@ using Toybox.Communications; using Toybox.Timer; class ErrorView extends ScalableView { - private var mText as Lang.String = ""; + private static const scErrorIconMargin as Lang.Float = 7f; + private var mText as Lang.String = ""; private var mDelegate as ErrorDelegate; - private const cSettings as Lang.Dictionary = { - :errorIconMargin => 7f - }; // Vertical spacing between the top of the face and the error icon private var mErrorIconMargin as Lang.Number; private var mErrorIcon; private var mTextArea as WatchUi.TextArea or Null; - private var mAntiAlias as Lang.Boolean = false; + private var mAntiAlias as Lang.Boolean = false; private static var instance; private static var mShown as Lang.Boolean = false; @@ -54,7 +52,7 @@ class ErrorView extends ScalableView { ScalableView.initialize(); mDelegate = new ErrorDelegate(self); // Convert the settings from % of screen size to pixels - mErrorIconMargin = pixelsForScreen(cSettings.get(:errorIconMargin) as Lang.Float); + mErrorIconMargin = pixelsForScreen(scErrorIconMargin); mErrorIcon = Application.loadResource(Rez.Drawables.ErrorIcon) as Graphics.BitmapResource; if (Graphics.Dc has :setAntiAlias) { mAntiAlias = true; diff --git a/source/HomeAssistantApp.mc b/source/HomeAssistantApp.mc index 902ffb3..460e7f4 100644 --- a/source/HomeAssistantApp.mc +++ b/source/HomeAssistantApp.mc @@ -86,8 +86,8 @@ class HomeAssistantApp extends Application.AppBase { // Return the initial view of your application here function getInitialView() as Lang.Array? { - mIsApp = true; - mQuitTimer = new QuitTimer(); + mIsApp = true; + mQuitTimer = new QuitTimer(); RezStrings.update(); mApiStatus = RezStrings.getChecking(); mMenuStatus = RezStrings.getChecking(); diff --git a/source/HomeAssistantConfirmation.mc b/source/HomeAssistantConfirmation.mc index ce3abe7..c42918f 100644 --- a/source/HomeAssistantConfirmation.mc +++ b/source/HomeAssistantConfirmation.mc @@ -34,12 +34,14 @@ class HomeAssistantConfirmation extends WatchUi.Confirmation { } class HomeAssistantConfirmationDelegate extends WatchUi.ConfirmationDelegate { - private var mConfirmMethod; - private var mTimer; + private var mConfirmMethod as Method(state as Lang.Boolean) as Void; + private var mTimer as Timer.Timer or Null; + private var mState as Lang.Boolean; - function initialize(callback as Method() as Void) { + function initialize(callback as Method(state as Lang.Boolean) as Void, state as Lang.Boolean) { WatchUi.ConfirmationDelegate.initialize(); mConfirmMethod = callback; + mState = state; var timeout = Settings.getConfirmTimeout(); // ms if (timeout > 0) { mTimer = new Timer.Timer(); @@ -49,11 +51,11 @@ class HomeAssistantConfirmationDelegate extends WatchUi.ConfirmationDelegate { function onResponse(response) as Lang.Boolean { getApp().getQuitTimer().reset(); - if (mTimer) { + if (mTimer != null) { mTimer.stop(); } if (response == WatchUi.CONFIRM_YES) { - mConfirmMethod.invoke(); + mConfirmMethod.invoke(mState); } return true; } diff --git a/source/HomeAssistantMenuItemFactory.mc b/source/HomeAssistantMenuItemFactory.mc index 60cb49c..67003ec 100644 --- a/source/HomeAssistantMenuItemFactory.mc +++ b/source/HomeAssistantMenuItemFactory.mc @@ -64,21 +64,26 @@ class HomeAssistantMenuItemFactory { return instance; } - function toggle(label as Lang.String or Lang.Symbol, entity_id as Lang.String or Null) as WatchUi.MenuItem { + function toggle( + label as Lang.String or Lang.Symbol, + entity_id as Lang.String or Null, + confirm as Lang.Boolean + ) as WatchUi.MenuItem { return new HomeAssistantToggleMenuItem( label, + confirm, { "entity_id" => entity_id }, mMenuItemOptions ); } function template_tap( - label as Lang.String or Lang.Symbol, - entity as Lang.String or Null, - template as Lang.String or Null, - service as Lang.String or Null, - confirm as Lang.Boolean, - data as Lang.Dictionary or Null + label as Lang.String or Lang.Symbol, + entity as Lang.String or Null, + template as Lang.String or Null, + service as Lang.String or Null, + confirm as Lang.Boolean, + data as Lang.Dictionary or Null ) as WatchUi.MenuItem { if (entity != null) { if (data == null) { @@ -116,11 +121,11 @@ class HomeAssistantMenuItemFactory { } function tap( - label as Lang.String or Lang.Symbol, - entity as Lang.String or Null, - service as Lang.String or Null, - confirm as Lang.Boolean, - data as Lang.Dictionary or Null + label as Lang.String or Lang.Symbol, + entity as Lang.String or Null, + service as Lang.String or Null, + confirm as Lang.Boolean, + data as Lang.Dictionary or Null ) as WatchUi.MenuItem { if (entity != null) { if (data == null) { diff --git a/source/HomeAssistantTapMenuItem.mc b/source/HomeAssistantTapMenuItem.mc index 238cce5..9623a63 100644 --- a/source/HomeAssistantTapMenuItem.mc +++ b/source/HomeAssistantTapMenuItem.mc @@ -57,15 +57,16 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem { if (mConfirm) { WatchUi.pushView( new HomeAssistantConfirmation(), - new HomeAssistantConfirmationDelegate(method(:onConfirm)), + new HomeAssistantConfirmationDelegate(method(:onConfirm), false), WatchUi.SLIDE_IMMEDIATE ); } else { - onConfirm(); + mHomeAssistantService.call(mService, mData); } } - function onConfirm() as Void { + // NB. Parameter 'b' is ignored + function onConfirm(b as Lang.Boolean) as Void { mHomeAssistantService.call(mService, mData); } diff --git a/source/HomeAssistantTemplateMenuItem.mc b/source/HomeAssistantTemplateMenuItem.mc index e617237..2d60a1c 100644 --- a/source/HomeAssistantTemplateMenuItem.mc +++ b/source/HomeAssistantTemplateMenuItem.mc @@ -64,15 +64,16 @@ class HomeAssistantTemplateMenuItem extends WatchUi.IconMenuItem { if (mConfirm) { WatchUi.pushView( new HomeAssistantConfirmation(), - new HomeAssistantConfirmationDelegate(method(:onConfirm)), + new HomeAssistantConfirmationDelegate(method(:onConfirm), false), WatchUi.SLIDE_IMMEDIATE ); } else { - onConfirm(); + onConfirm(false); } } - function onConfirm() as Void { + // NB. Parameter 'b' is ignored + function onConfirm(b as Lang.Boolean) as Void { if (mService != null) { mHomeAssistantService.call(mService, mData); } @@ -181,9 +182,7 @@ class HomeAssistantTemplateMenuItem extends WatchUi.IconMenuItem { } Communications.makeWebRequest( url, - { - "template" => mTemplate - }, + { "template" => mTemplate }, { :method => Communications.HTTP_REQUEST_METHOD_POST, :headers => { diff --git a/source/HomeAssistantToggleMenuItem.mc b/source/HomeAssistantToggleMenuItem.mc index 9dd1d10..793d011 100644 --- a/source/HomeAssistantToggleMenuItem.mc +++ b/source/HomeAssistantToggleMenuItem.mc @@ -25,18 +25,21 @@ using Toybox.Application.Properties; using Toybox.Timer; class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { - private var mData as Lang.Dictionary; + private var mConfirm as Lang.Boolean; + private var mData as Lang.Dictionary; function initialize( - label as Lang.String or Lang.Symbol, - data as Lang.Dictionary or Null, - options as { + label as Lang.String or Lang.Symbol, + confirm as Lang.Boolean, + data as Lang.Dictionary or Null, + options as { :alignment as WatchUi.MenuItem.Alignment, :icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol } or Null ) { WatchUi.ToggleMenuItem.initialize(label, null, null, false, options); - mData = data; + mConfirm = confirm; + mData = data; } private function setUiToggle(state as Null or Lang.String) as Void { @@ -301,4 +304,20 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { } } + function callService(b as Lang.Boolean) as Void { + if (mConfirm) { + WatchUi.pushView( + new HomeAssistantConfirmation(), + new HomeAssistantConfirmationDelegate(method(:onConfirm), b), + WatchUi.SLIDE_IMMEDIATE + ); + } else { + setState(b); + } + } + + function onConfirm(b as Lang.Boolean) as Void { + setState(b); + } + } diff --git a/source/HomeAssistantView.mc b/source/HomeAssistantView.mc index 12b1e5e..c689111 100644 --- a/source/HomeAssistantView.mc +++ b/source/HomeAssistantView.mc @@ -64,7 +64,7 @@ class HomeAssistantView extends WatchUi.Menu2 { } if (type != null && name != null) { if (type.equals("toggle") && entity != null) { - addItem(HomeAssistantMenuItemFactory.create().toggle(name, entity)); + addItem(HomeAssistantMenuItemFactory.create().toggle(name, entity, confirm)); } else if (type.equals("template") && content != null) { if (service == null) { addItem(HomeAssistantMenuItemFactory.create().template_notap(name, content)); @@ -149,7 +149,7 @@ class HomeAssistantViewDelegate extends WatchUi.Menu2InputDelegate { if (Globals.scDebug) { System.println(haToggleItem.getLabel() + " " + haToggleItem.getId() + " " + haToggleItem.isEnabled()); } - haToggleItem.setState(haToggleItem.isEnabled()); + haToggleItem.callService(haToggleItem.isEnabled()); } else if (item instanceof HomeAssistantTapMenuItem) { var haItem = item as HomeAssistantTapMenuItem; if (Globals.scDebug) { diff --git a/source/WebhookManager.mc b/source/WebhookManager.mc index 5fc2f3f..9d1cf70 100644 --- a/source/WebhookManager.mc +++ b/source/WebhookManager.mc @@ -27,6 +27,7 @@ using Toybox.System; // Can use push view so must never be run in a glance context class WebhookManager { + function onReturnRequestWebhookId(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void { switch (responseCode) { case Communications.BLE_HOST_TIMEOUT: @@ -236,11 +237,12 @@ class WebhookManager { { :method => Communications.HTTP_REQUEST_METHOD_POST, :headers => { - "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON + "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON }, :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON }, method(:onReturnRegisterWebhookSensor) ); } + }