From 1c182dd615fa71333cb19119e9315ec13f4c643b Mon Sep 17 00:00:00 2001 From: Joseph Abbey Date: Fri, 30 Aug 2024 13:25:16 +0100 Subject: [PATCH] Deprecate `template` type --- config.schema.json | 23 +++--- source/HomeAssistantMenuItemFactory.mc | 45 +--------- source/HomeAssistantTapMenuItem.mc | 45 +++++++--- source/HomeAssistantTemplateMenuItem.mc | 105 ------------------------ source/HomeAssistantView.mc | 11 +-- 5 files changed, 50 insertions(+), 179 deletions(-) delete mode 100644 source/HomeAssistantTemplateMenuItem.mc diff --git a/config.schema.json b/config.schema.json index 02e58dd..0f4d6ec 100644 --- a/config.schema.json +++ b/config.schema.json @@ -64,7 +64,10 @@ }, "type": { "$ref": "#/$defs/type", - "const": "template" + "const": "template", + "deprecated": true, + "title": "Schema change:", + "description": "Use 'tap' instead." } }, "required": ["name", "content", "type"], @@ -84,7 +87,10 @@ }, "type": { "$ref": "#/$defs/type", - "const": "template" + "const": "template", + "deprecated": true, + "title": "Schema change:", + "description": "Use 'tap' instead." }, "tap_action": { "$ref": "#/$defs/tap_action" @@ -108,6 +114,10 @@ "$ref": "#/$defs/type", "const": "tap" }, + "content": { + "$ref": "#/$defs/content", + "description": "Optional in a tap." + }, "service": { "$ref": "#/$defs/entity", "deprecated": true, @@ -118,14 +128,7 @@ "$ref": "#/$defs/tap_action" } }, - "oneOf": [ - { - "required": ["name", "type", "service"] - }, - { - "required": ["name", "type", "tap_action"] - } - ], + "required": ["name", "type"], "additionalProperties": false }, "group": { diff --git a/source/HomeAssistantMenuItemFactory.mc b/source/HomeAssistantMenuItemFactory.mc index d19cd34..d9313ea 100644 --- a/source/HomeAssistantMenuItemFactory.mc +++ b/source/HomeAssistantMenuItemFactory.mc @@ -79,55 +79,13 @@ class HomeAssistantMenuItemFactory { ); } - function template_tap( + function 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 - ) as WatchUi.MenuItem { - if (entity != null) { - if (data == null) { - data = { "entity_id" => entity }; - } else { - data.put("entity_id", entity); - } - } - return new HomeAssistantTemplateMenuItem( - label, - template, - service, - confirm, - data, - mTapTypeIcon, - mMenuItemOptions, - mHomeAssistantService - ); - } - - function template_notap( - label as Lang.String or Lang.Symbol, - template as Lang.String or Null - ) as WatchUi.MenuItem { - return new HomeAssistantTemplateMenuItem( - label, - template, - null, - false, - null, - mInfoTypeIcon, - mMenuItemOptions, - mHomeAssistantService - ); - } - - 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 ) as WatchUi.MenuItem { if (entity != null) { if (data == null) { @@ -138,6 +96,7 @@ class HomeAssistantMenuItemFactory { } return new HomeAssistantTapMenuItem( label, + template, service, confirm, data, diff --git a/source/HomeAssistantTapMenuItem.mc b/source/HomeAssistantTapMenuItem.mc index 5fee882..8dffbba 100644 --- a/source/HomeAssistantTapMenuItem.mc +++ b/source/HomeAssistantTapMenuItem.mc @@ -24,20 +24,22 @@ using Toybox.Graphics; class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem { private var mHomeAssistantService as HomeAssistantService; - private var mService as Lang.String; + private var mTemplate as Lang.String; + private var mService as Lang.String or Null; private var mConfirm as Lang.Boolean; private var mData as Lang.Dictionary or Null; function initialize( - label as Lang.String or Lang.Symbol, - service as Lang.String or Null, - confirm as Lang.Boolean, - data as Lang.Dictionary or Null, - icon as Graphics.BitmapType or WatchUi.Drawable, - options as { + label as Lang.String or Lang.Symbol, + template as Lang.String, + service as Lang.String or Null, + confirm as Lang.Boolean, + data as Lang.Dictionary or Null, + icon as Graphics.BitmapType or WatchUi.Drawable, + options as { :alignment as WatchUi.MenuItem.Alignment } or Null, - haService as HomeAssistantService + haService as HomeAssistantService ) { WatchUi.IconMenuItem.initialize( label, @@ -48,16 +50,33 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem { ); mHomeAssistantService = haService; + mTemplate = template; mService = service; mConfirm = confirm; mData = data; } function buildTemplate() as Lang.String or Null { - return null; + return mTemplate; } - function updateState(data as Lang.String or Null) as Void { + function updateState(data as Lang.String or Lang.Dictionary or Null) as Void { + if (data == null) { + setSubLabel($.Rez.Strings.Empty); + } else if(data instanceof Lang.String) { + setSubLabel(data); + } else if(data instanceof Lang.Dictionary) { + // System.println("HomeAsistantTemplateMenuItem updateState() data = " + data); + if (data.get("error") != null) { + setSubLabel($.Rez.Strings.TemplateError); + } else { + setSubLabel($.Rez.Strings.PotentialError); + } + } else { + // The template must return a Lang.String, a number can be either integer or float and hence cannot be formatted locally without error. + setSubLabel(WatchUi.loadResource($.Rez.Strings.TemplateError) as Lang.String); + } + WatchUi.requestUpdate(); } function callService() as Void { @@ -68,13 +87,15 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem { WatchUi.SLIDE_IMMEDIATE ); } else { - mHomeAssistantService.call(mService, mData); + onConfirm(false); } } // NB. Parameter 'b' is ignored function onConfirm(b as Lang.Boolean) as Void { - mHomeAssistantService.call(mService, mData); + if (mService != null) { + mHomeAssistantService.call(mService, mData); + } } } diff --git a/source/HomeAssistantTemplateMenuItem.mc b/source/HomeAssistantTemplateMenuItem.mc deleted file mode 100644 index adfa0d8..0000000 --- a/source/HomeAssistantTemplateMenuItem.mc +++ /dev/null @@ -1,105 +0,0 @@ -//----------------------------------------------------------------------------------- -// -// Distributed under MIT Licence -// See https://github.com/house-of-abbey/GarminHomeAssistant/blob/main/LICENSE. -// -//----------------------------------------------------------------------------------- -// -// GarminHomeAssistant is a Garmin IQ application written in Monkey C and routinely -// tested on a Venu 2 device. The source code is provided at: -// https://github.com/house-of-abbey/GarminHomeAssistant. -// -// P A Abbey & J D Abbey, 12 January 2024 -// -// -// Description: -// -// Menu button that renders a Home Assistant Template, and optionally triggers a service. -// -// Reference: -// * https://developers.home-assistant.io/docs/api/rest/ -// * https://www.home-assistant.io/docs/configuration/templating -// -//----------------------------------------------------------------------------------- - -using Toybox.Lang; -using Toybox.WatchUi; -using Toybox.Graphics; - -class HomeAssistantTemplateMenuItem extends WatchUi.IconMenuItem { - private var mHomeAssistantService as HomeAssistantService; - private var mTemplate as Lang.String; - private var mService as Lang.String or Null; - private var mConfirm as Lang.Boolean; - private var mData as Lang.Dictionary or Null; - - function initialize( - label as Lang.String or Lang.Symbol, - template as Lang.String, - service as Lang.String or Null, - confirm as Lang.Boolean, - data as Lang.Dictionary or Null, - icon as Graphics.BitmapType or WatchUi.Drawable, - options as { - :alignment as WatchUi.MenuItem.Alignment - } or Null, - haService as HomeAssistantService - ) { - WatchUi.IconMenuItem.initialize( - label, - null, - null, - icon, - options - ); - - mHomeAssistantService = haService; - mTemplate = template; - mService = service; - mConfirm = confirm; - mData = data; - } - - function buildTemplate() as Lang.String or Null { - return mTemplate; - } - - function updateState(data as Lang.String or Lang.Dictionary or Null) as Void { - if (data == null) { - setSubLabel($.Rez.Strings.Empty); - } else if(data instanceof Lang.String) { - setSubLabel(data); - } else if(data instanceof Lang.Dictionary) { - // System.println("HomeAsistantTemplateMenuItem updateState() data = " + data); - if (data.get("error") != null) { - setSubLabel($.Rez.Strings.TemplateError); - } else { - setSubLabel($.Rez.Strings.PotentialError); - } - } else { - // The template must return a Lang.String, a number can be either integer or float and hence cannot be formatted locally without error. - setSubLabel(WatchUi.loadResource($.Rez.Strings.TemplateError) as Lang.String); - } - WatchUi.requestUpdate(); - } - - function callService() as Void { - if (mConfirm) { - WatchUi.pushView( - new HomeAssistantConfirmation(), - new HomeAssistantConfirmationDelegate(method(:onConfirm), false), - WatchUi.SLIDE_IMMEDIATE - ); - } else { - onConfirm(false); - } - } - - // NB. Parameter 'b' is ignored - function onConfirm(b as Lang.Boolean) as Void { - if (mService != null) { - mHomeAssistantService.call(mService, mData); - } - } - -} diff --git a/source/HomeAssistantView.mc b/source/HomeAssistantView.mc index 1bb431e..e19049e 100644 --- a/source/HomeAssistantView.mc +++ b/source/HomeAssistantView.mc @@ -63,15 +63,8 @@ class HomeAssistantView extends WatchUi.Menu2 { if (type != null && name != null) { if (type.equals("toggle") && entity != null) { addItem(HomeAssistantMenuItemFactory.create().toggle(name, entity, content, confirm)); - } else if (type.equals("template") && content != null) { - if (service == null) { - addItem(HomeAssistantMenuItemFactory.create().template_notap(name, content)); - } else { - addItem(HomeAssistantMenuItemFactory.create().template_tap(name, entity, content, service, confirm, data)); - } - - } else if (type.equals("tap") && service != null) { - addItem(HomeAssistantMenuItemFactory.create().tap(name, entity, service, confirm, data)); + } else if ((type.equals("tap") && service != null) || (type.equals("template") && content != null)) { + addItem(HomeAssistantMenuItemFactory.create().tap(name, entity, content, service, confirm, data)); } else if (type.equals("group")) { addItem(HomeAssistantMenuItemFactory.create().group(items[i], content)); }