Added two new options to menu items

1. The ability to disable a menu item without deleting it.
2. The option to quit the application on item selection.
This commit is contained in:
Philip Abbey
2025-07-08 22:48:42 +01:00
parent 659a060c76
commit ec044c5408
4 changed files with 50 additions and 24 deletions

View File

@ -77,15 +77,17 @@ class HomeAssistantMenuItemFactory {
label as Lang.String or Lang.Symbol, label as Lang.String or Lang.Symbol,
entity_id as Lang.String or Null, entity_id as Lang.String or Null,
template as Lang.String or Null, template as Lang.String or Null,
exit as Lang.Boolean,
confirm as Lang.Boolean, confirm as Lang.Boolean,
pin as Lang.Boolean pin as Lang.Boolean
) as WatchUi.MenuItem { ) as WatchUi.MenuItem {
return new HomeAssistantToggleMenuItem( return new HomeAssistantToggleMenuItem(
label, label,
template, template,
{ "entity_id" => entity_id },
exit,
confirm, confirm,
pin, pin,
{ "entity_id" => entity_id },
mMenuItemOptions mMenuItemOptions
); );
} }
@ -105,9 +107,10 @@ class HomeAssistantMenuItemFactory {
entity_id as Lang.String or Null, entity_id 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,
data as Lang.Dictionary or Null,
exit as Lang.Boolean,
confirm as Lang.Boolean, confirm as Lang.Boolean,
pin as Lang.Boolean, pin as Lang.Boolean
data as Lang.Dictionary or Null
) as WatchUi.MenuItem { ) as WatchUi.MenuItem {
if (entity_id != null) { if (entity_id != null) {
if (data == null) { if (data == null) {
@ -121,9 +124,10 @@ class HomeAssistantMenuItemFactory {
label, label,
template, template,
service, service,
data,
exit,
confirm, confirm,
pin, pin,
data,
mTapTypeIcon, mTapTypeIcon,
mMenuItemOptions, mMenuItemOptions,
mHomeAssistantService mHomeAssistantService
@ -133,9 +137,10 @@ class HomeAssistantMenuItemFactory {
label, label,
template, template,
service, service,
data,
exit,
confirm, confirm,
pin, pin,
data,
mInfoTypeIcon, mInfoTypeIcon,
mMenuItemOptions, mMenuItemOptions,
mHomeAssistantService mHomeAssistantService

View File

@ -23,6 +23,7 @@ class HomeAssistantTapMenuItem extends HomeAssistantMenuItem {
private var mHomeAssistantService as HomeAssistantService; private var mHomeAssistantService as HomeAssistantService;
private var mService as Lang.String or Null; private var mService as Lang.String or Null;
private var mConfirm as Lang.Boolean; private var mConfirm as Lang.Boolean;
private var mExit as Lang.Boolean;
private var mPin as Lang.Boolean; private var mPin as Lang.Boolean;
private var mData as Lang.Dictionary or Null; private var mData as Lang.Dictionary or Null;
@ -43,9 +44,10 @@ class HomeAssistantTapMenuItem extends HomeAssistantMenuItem {
label as Lang.String or Lang.Symbol, label as Lang.String or Lang.Symbol,
template as Lang.String, template as Lang.String,
service as Lang.String or Null, service as Lang.String or Null,
data as Lang.Dictionary or Null,
exit as Lang.Boolean,
confirm as Lang.Boolean, confirm as Lang.Boolean,
pin as Lang.Boolean, pin as Lang.Boolean,
data as Lang.Dictionary or Null,
icon as Graphics.BitmapType or WatchUi.Drawable, icon as Graphics.BitmapType or WatchUi.Drawable,
options as { options as {
:alignment as WatchUi.MenuItem.Alignment, :alignment as WatchUi.MenuItem.Alignment,
@ -67,9 +69,10 @@ class HomeAssistantTapMenuItem extends HomeAssistantMenuItem {
mHomeAssistantService = haService; mHomeAssistantService = haService;
mService = service; mService = service;
mData = data;
mExit = exit;
mConfirm = confirm; mConfirm = confirm;
mPin = pin; mPin = pin;
mData = data;
} }
//! Call a Home Assistant service only after checks have been done for confirmation or PIN entry. //! Call a Home Assistant service only after checks have been done for confirmation or PIN entry.
@ -105,6 +108,9 @@ class HomeAssistantTapMenuItem extends HomeAssistantMenuItem {
if (mService != null) { if (mService != null) {
mHomeAssistantService.call(mService, mData); mHomeAssistantService.call(mService, mData);
} }
if (mExit) {
System.exit();
}
} }
} }

View File

@ -22,10 +22,11 @@ using Toybox.Timer;
//! Light or switch toggle menu button that calls the API to maintain the up to date state. //! Light or switch toggle menu button that calls the API to maintain the up to date state.
// //
class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
private var mConfirm as Lang.Boolean;
private var mPin as Lang.Boolean;
private var mData as Lang.Dictionary; private var mData as Lang.Dictionary;
private var mTemplate as Lang.String; private var mTemplate as Lang.String;
private var mExit as Lang.Boolean;
private var mConfirm as Lang.Boolean;
private var mPin as Lang.Boolean;
private var mHasVibrate as Lang.Boolean = false; private var mHasVibrate as Lang.Boolean = false;
//! Class Constructor //! Class Constructor
@ -40,9 +41,10 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
function initialize( function initialize(
label as Lang.String or Lang.Symbol, label as Lang.String or Lang.Symbol,
template as Lang.String, template as Lang.String,
data as Lang.Dictionary or Null,
exit as Lang.Boolean,
confirm as Lang.Boolean, confirm as Lang.Boolean,
pin as Lang.Boolean, pin as Lang.Boolean,
data as Lang.Dictionary or Null,
options as { options as {
:alignment as WatchUi.MenuItem.Alignment, :alignment as WatchUi.MenuItem.Alignment,
:icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol :icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol
@ -58,10 +60,11 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
if (Attention has :vibrate) { if (Attention has :vibrate) {
mHasVibrate = true; mHasVibrate = true;
} }
mConfirm = confirm;
mPin = pin;
mData = data; mData = data;
mTemplate = template; mTemplate = template;
mExit = exit;
mConfirm = confirm;
mPin = pin;
} }
//! Set the state of a toggle menu item. //! Set the state of a toggle menu item.
@ -294,6 +297,9 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
// //
function onConfirm(b as Lang.Boolean) as Void { function onConfirm(b as Lang.Boolean) as Void {
setState(b); setState(b);
if (mExit) {
System.exit();
}
} }
} }

View File

@ -51,21 +51,30 @@ class HomeAssistantView extends WatchUi.Menu2 {
var confirm = false as Lang.Boolean or Null; var confirm = false as Lang.Boolean or Null;
var pin = false as Lang.Boolean or Null; var pin = false as Lang.Boolean or Null;
var data = null as Lang.Dictionary or Null; var data = null as Lang.Dictionary or Null;
var enable = true as Lang.Boolean or Null;
var exit = false as Lang.Boolean or Null;
if (items[i].get("enable") != null) {
enable = items[i].get("enable"); // Optional
}
if (items[i].get("exit") != null) {
exit = items[i].get("exit"); // Optional
}
if (tap_action != null) { if (tap_action != null) {
service = tap_action.get("service"); service = tap_action.get("service");
confirm = tap_action.get("confirm"); // Optional
pin = tap_action.get("pin"); // Optional
data = tap_action.get("data"); // Optional data = tap_action.get("data"); // Optional
if (confirm == null) { if (tap_action.get("confirm") != null) {
confirm = false; confirm = tap_action.get("confirm"); // Optional
}
if (tap_action.get("pin") != null) {
pin = tap_action.get("pin"); // Optional
} }
} }
if (type != null && name != null) { if (type != null && name != null && enable) {
if (type.equals("toggle") && entity != null) { if (type.equals("toggle") && entity != null) {
addItem(HomeAssistantMenuItemFactory.create().toggle(name, entity, content, confirm, pin)); addItem(HomeAssistantMenuItemFactory.create().toggle(name, entity, content, exit, confirm, pin));
} else if ((type.equals("tap") && service != null) || (type.equals("info") && content != null) || (type.equals("template") && content != null)) { } else if ((type.equals("tap") && service != null) || (type.equals("info") && content != null) || (type.equals("template") && content != null)) {
// NB. "template" is deprecated in the schema and remains only for backward compatibility. All menu items can now use templates, so the replacement is "info". // NB. "template" is deprecated in the schema and remains only for backward compatibility. All menu items can now use templates, so the replacement is "info".
addItem(HomeAssistantMenuItemFactory.create().tap(name, entity, content, service, confirm, pin, data)); addItem(HomeAssistantMenuItemFactory.create().tap(name, entity, content, service, data, exit, confirm, pin));
} else if (type.equals("group")) { } else if (type.equals("group")) {
addItem(HomeAssistantMenuItemFactory.create().group(items[i], content)); addItem(HomeAssistantMenuItemFactory.create().group(items[i], content));
} }