Deprecate template type

This commit is contained in:
Joseph Abbey
2024-08-30 13:25:16 +01:00
parent df3be94bf9
commit 1c182dd615
5 changed files with 50 additions and 179 deletions

View File

@ -64,7 +64,10 @@
}, },
"type": { "type": {
"$ref": "#/$defs/type", "$ref": "#/$defs/type",
"const": "template" "const": "template",
"deprecated": true,
"title": "Schema change:",
"description": "Use 'tap' instead."
} }
}, },
"required": ["name", "content", "type"], "required": ["name", "content", "type"],
@ -84,7 +87,10 @@
}, },
"type": { "type": {
"$ref": "#/$defs/type", "$ref": "#/$defs/type",
"const": "template" "const": "template",
"deprecated": true,
"title": "Schema change:",
"description": "Use 'tap' instead."
}, },
"tap_action": { "tap_action": {
"$ref": "#/$defs/tap_action" "$ref": "#/$defs/tap_action"
@ -108,6 +114,10 @@
"$ref": "#/$defs/type", "$ref": "#/$defs/type",
"const": "tap" "const": "tap"
}, },
"content": {
"$ref": "#/$defs/content",
"description": "Optional in a tap."
},
"service": { "service": {
"$ref": "#/$defs/entity", "$ref": "#/$defs/entity",
"deprecated": true, "deprecated": true,
@ -118,14 +128,7 @@
"$ref": "#/$defs/tap_action" "$ref": "#/$defs/tap_action"
} }
}, },
"oneOf": [ "required": ["name", "type"],
{
"required": ["name", "type", "service"]
},
{
"required": ["name", "type", "tap_action"]
}
],
"additionalProperties": false "additionalProperties": false
}, },
"group": { "group": {

View File

@ -79,55 +79,13 @@ class HomeAssistantMenuItemFactory {
); );
} }
function template_tap( function tap(
label as Lang.String or Lang.Symbol, label as Lang.String or Lang.Symbol,
entity as Lang.String or Null, entity as Lang.String or Null,
template as Lang.String or Null, template as Lang.String or Null,
service as Lang.String or Null, service as Lang.String or Null,
confirm as Lang.Boolean, confirm as Lang.Boolean,
data as Lang.Dictionary or Null 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 { ) as WatchUi.MenuItem {
if (entity != null) { if (entity != null) {
if (data == null) { if (data == null) {
@ -138,6 +96,7 @@ class HomeAssistantMenuItemFactory {
} }
return new HomeAssistantTapMenuItem( return new HomeAssistantTapMenuItem(
label, label,
template,
service, service,
confirm, confirm,
data, data,

View File

@ -24,12 +24,14 @@ using Toybox.Graphics;
class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem { class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem {
private var mHomeAssistantService as HomeAssistantService; 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 mConfirm as Lang.Boolean;
private var mData as Lang.Dictionary or Null; private var mData as Lang.Dictionary or Null;
function initialize( function initialize(
label as Lang.String or Lang.Symbol, label as Lang.String or Lang.Symbol,
template as Lang.String,
service as Lang.String or Null, service as Lang.String or Null,
confirm as Lang.Boolean, confirm as Lang.Boolean,
data as Lang.Dictionary or Null, data as Lang.Dictionary or Null,
@ -48,16 +50,33 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem {
); );
mHomeAssistantService = haService; mHomeAssistantService = haService;
mTemplate = template;
mService = service; mService = service;
mConfirm = confirm; mConfirm = confirm;
mData = data; mData = data;
} }
function buildTemplate() as Lang.String or Null { 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 { function callService() as Void {
@ -68,13 +87,15 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem {
WatchUi.SLIDE_IMMEDIATE WatchUi.SLIDE_IMMEDIATE
); );
} else { } else {
mHomeAssistantService.call(mService, mData); onConfirm(false);
} }
} }
// NB. Parameter 'b' is ignored // NB. Parameter 'b' is ignored
function onConfirm(b as Lang.Boolean) as Void { function onConfirm(b as Lang.Boolean) as Void {
if (mService != null) {
mHomeAssistantService.call(mService, mData); mHomeAssistantService.call(mService, mData);
} }
}
} }

View File

@ -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);
}
}
}

View File

@ -63,15 +63,8 @@ class HomeAssistantView extends WatchUi.Menu2 {
if (type != null && name != null) { if (type != null && name != null) {
if (type.equals("toggle") && entity != null) { if (type.equals("toggle") && entity != null) {
addItem(HomeAssistantMenuItemFactory.create().toggle(name, entity, content, confirm)); addItem(HomeAssistantMenuItemFactory.create().toggle(name, entity, content, confirm));
} else if (type.equals("template") && content != null) { } else if ((type.equals("tap") && service != null) || (type.equals("template") && content != null)) {
if (service == null) { addItem(HomeAssistantMenuItemFactory.create().tap(name, entity, content, service, confirm, data));
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("group")) { } else if (type.equals("group")) {
addItem(HomeAssistantMenuItemFactory.create().group(items[i], content)); addItem(HomeAssistantMenuItemFactory.create().group(items[i], content));
} }