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

View File

@ -23,6 +23,7 @@ class HomeAssistantTapMenuItem extends HomeAssistantMenuItem {
private var mHomeAssistantService as HomeAssistantService;
private var mService as Lang.String or Null;
private var mConfirm as Lang.Boolean;
private var mExit as Lang.Boolean;
private var mPin as Lang.Boolean;
private var mData as Lang.Dictionary or Null;
@ -43,9 +44,10 @@ class HomeAssistantTapMenuItem extends HomeAssistantMenuItem {
label as Lang.String or Lang.Symbol,
template as Lang.String,
service as Lang.String or Null,
data as Lang.Dictionary or Null,
exit as Lang.Boolean,
confirm as Lang.Boolean,
pin as Lang.Boolean,
data as Lang.Dictionary or Null,
icon as Graphics.BitmapType or WatchUi.Drawable,
options as {
:alignment as WatchUi.MenuItem.Alignment,
@ -67,9 +69,10 @@ class HomeAssistantTapMenuItem extends HomeAssistantMenuItem {
mHomeAssistantService = haService;
mService = service;
mData = data;
mExit = exit;
mConfirm = confirm;
mPin = pin;
mData = data;
}
//! 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) {
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.
//
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 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;
//! Class Constructor
@ -40,9 +41,10 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
function initialize(
label as Lang.String or Lang.Symbol,
template as Lang.String,
data as Lang.Dictionary or Null,
exit as Lang.Boolean,
confirm as Lang.Boolean,
pin 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
@ -58,10 +60,11 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
if (Attention has :vibrate) {
mHasVibrate = true;
}
mConfirm = confirm;
mPin = pin;
mData = data;
mTemplate = template;
mExit = exit;
mConfirm = confirm;
mPin = pin;
}
//! 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 {
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 pin = false as Lang.Boolean 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) {
service = tap_action.get("service");
confirm = tap_action.get("confirm"); // Optional
pin = tap_action.get("pin"); // Optional
data = tap_action.get("data"); // Optional
if (confirm == null) {
confirm = false;
if (tap_action.get("confirm") != null) {
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) {
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)) {
// 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")) {
addItem(HomeAssistantMenuItemFactory.create().group(items[i], content));
}