diff --git a/source/HomeAssistantMenuItemFactory.mc b/source/HomeAssistantMenuItemFactory.mc index a05e716..2a881ae 100644 --- a/source/HomeAssistantMenuItemFactory.mc +++ b/source/HomeAssistantMenuItemFactory.mc @@ -70,6 +70,7 @@ class HomeAssistantMenuItemFactory { //! @param label Menu item label. //! @param entity_id Home Assistant Entity ID (optional) //! @param template Template for Home Assistant to render (optional) + //! @param options A list of Boolean options //! @param exit Should the service call complete and then exit? //! @param confirm Should this menu item selection be confirmed? //! @param pin Should this menu item selection request the security PIN? @@ -78,18 +79,21 @@ 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 + options as { + :exit as Lang.Boolean, + :confirm as Lang.Boolean, + :pin as Lang.Boolean + } ) as WatchUi.MenuItem { + var keys = mMenuItemOptions.keys(); + for (var i = 0; i < keys.size(); i++) { + options.put(keys[i], mMenuItemOptions.get(keys[i])); + } return new HomeAssistantToggleMenuItem( label, template, { "entity_id" => entity_id }, - exit, - confirm, - pin, - mMenuItemOptions + options ); } @@ -100,9 +104,7 @@ class HomeAssistantMenuItemFactory { //! @param template Template for Home Assistant to render (optional) //! @param service Template for Home Assistant to render (optional) //! @param data Sourced from the menu JSON, this is the `data` field from the `tap_action` field. - //! @param exit Should the service call complete and then exit? - //! @param confirm Should this menu item selection be confirmed? - //! @param pin Should this menu item selection request the security PIN? + //! @param options Menu item options to be passed on, including both SDK and menu options, e.g. exit, confirm & pin. // function tap( label as Lang.String or Lang.Symbol, @@ -110,9 +112,11 @@ class HomeAssistantMenuItemFactory { 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 + options as { + :exit as Lang.Boolean, + :confirm as Lang.Boolean, + :pin as Lang.Boolean + } ) as WatchUi.MenuItem { if (entity_id != null) { if (data == null) { @@ -121,30 +125,28 @@ class HomeAssistantMenuItemFactory { data.put("entity_id", entity_id); } } + var keys = mMenuItemOptions.keys(); + for (var i = 0; i < keys.size(); i++) { + options.put(keys[i], mMenuItemOptions.get(keys[i])); + } if (service != null) { + options.put(:icon, mTapTypeIcon); return new HomeAssistantTapMenuItem( label, template, service, data, - exit, - confirm, - pin, - mTapTypeIcon, - mMenuItemOptions, + options, mHomeAssistantService ); } else { + options.put(:icon, mInfoTypeIcon); return new HomeAssistantTapMenuItem( label, template, - service, + null, data, - exit, - confirm, - pin, - mInfoTypeIcon, - mMenuItemOptions, + options, mHomeAssistantService ); } diff --git a/source/HomeAssistantTapMenuItem.mc b/source/HomeAssistantTapMenuItem.mc index 7a59750..79a61ea 100644 --- a/source/HomeAssistantTapMenuItem.mc +++ b/source/HomeAssistantTapMenuItem.mc @@ -21,7 +21,7 @@ using Toybox.Graphics; // class HomeAssistantTapMenuItem extends HomeAssistantMenuItem { 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 mExit as Lang.Boolean; private var mPin as Lang.Boolean; @@ -37,43 +37,39 @@ class HomeAssistantTapMenuItem extends HomeAssistantMenuItem { //! @param confirm Should the service call be confirmed to avoid accidental invocation? //! @param pin Should the service call be protected with a PIN for some low level of security? //! @param icon Icon to use for the menu item. - //! @param options Menu item options to be passed on. + //! @param options Menu item options to be passed on, including both SDK and menu options, e.g. exit, confirm & pin. //! @param haService Shared Home Assistant service object that will perform the required call. Only //! one of these objects is created for all menu items to re-use. // 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, data as Lang.Dictionary or Null, - exit as Lang.Boolean, - confirm as Lang.Boolean, - pin as Lang.Boolean, - icon as Graphics.BitmapType or WatchUi.Drawable, options as { :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, + :exit as Lang.Boolean, + :confirm as Lang.Boolean, + :pin as Lang.Boolean } or Null, haService as HomeAssistantService ) { - if (options != null) { - options.put(:icon, icon); - } else { - options = { :icon => icon }; - } - HomeAssistantMenuItem.initialize( label, template, - options + { + :alignment => options.get(:alignment), + :icon => options.get(:icon) + } ); mHomeAssistantService = haService; mService = service; mData = data; - mExit = exit; - mConfirm = confirm; - mPin = pin; + mExit = options.get(:exit); + mConfirm = options.get(:confirm); + mPin = options.get(:acospin); } //! Call a Home Assistant service only after checks have been done for confirmation or PIN entry. diff --git a/source/HomeAssistantToggleMenuItem.mc b/source/HomeAssistantToggleMenuItem.mc index 7f55aff..e902916 100644 --- a/source/HomeAssistantToggleMenuItem.mc +++ b/source/HomeAssistantToggleMenuItem.mc @@ -34,21 +34,18 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { //! @param label Menu item label. //! @param template Menu item template. //! @param data Data to supply to the service call. - //! @param exit Should the service call complete and then exit? - //! @param confirm Should the service call be confirmed to avoid accidental invocation? - //! @param pin Should the service call be protected with a PIN for some low level of security? - //! @param options Menu item options to be passed on. + //! @param options Menu item options to be passed on, including both SDK and menu options, e.g. exit, confirm & pin. // 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, options as { :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, + :exit as Lang.Boolean, + :confirm as Lang.Boolean, + :pin as Lang.Boolean } or Null ) { WatchUi.ToggleMenuItem.initialize( @@ -56,16 +53,19 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { null, null, false, - options + { + :alignment => options.get(:alignment), + :icon => options.get(:icon) + } ); if (Attention has :vibrate) { mHasVibrate = true; } mData = data; mTemplate = template; - mExit = exit; - mConfirm = confirm; - mPin = pin; + mExit = options.get(:exit); + mConfirm = options.get(:confirm); + mPin = options.get(:pin); } //! Set the state of a toggle menu item. diff --git a/source/HomeAssistantView.mc b/source/HomeAssistantView.mc index 7dd2e0d..e4e9cfd 100644 --- a/source/HomeAssistantView.mc +++ b/source/HomeAssistantView.mc @@ -71,22 +71,75 @@ class HomeAssistantView extends WatchUi.Menu2 { } if (type != null && name != null && enabled) { if (type.equals("toggle") && entity != null) { - addItem(HomeAssistantMenuItemFactory.create().toggle(name, entity, content, exit, confirm, pin)); + addItem(HomeAssistantMenuItemFactory.create().toggle( + name, + entity, + content, + { + :exit => exit, + :confirm => confirm, + :pin => pin + } + )); } else if (type.equals("tap") && service != null) { - addItem(HomeAssistantMenuItemFactory.create().tap(name, entity, content, service, data, exit, confirm, pin)); + addItem(HomeAssistantMenuItemFactory.create().tap( + name, + entity, + content, + service, + data, + { + :exit => exit, + :confirm => confirm, + :pin => pin + } + )); } else if (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". // The exit option is dependent on the type of template. if (tap_action == null) { // No exit from an information only item - addItem(HomeAssistantMenuItemFactory.create().tap(name, entity, content, service, data, false, confirm, pin)); + addItem(HomeAssistantMenuItemFactory.create().tap( + name, + entity, + content, + service, + data, + { + :exit => false, + :confirm => confirm, + :pin => pin + } + )); } else { // You may exit from template item with a 'tap_action'. - addItem(HomeAssistantMenuItemFactory.create().tap(name, entity, content, service, data, exit, confirm, pin)); + addItem(HomeAssistantMenuItemFactory.create().tap( + name, + entity, + content, + service, + data, + { + :exit => exit, + :confirm => confirm, + :pin => pin + } + )); } } else if (type.equals("info") && content != null) { // Cannot exit from a non-actionable information only menu item. - addItem(HomeAssistantMenuItemFactory.create().tap(name, entity, content, service, data, false, confirm, pin)); + addItem(HomeAssistantMenuItemFactory.create().tap( + name, + entity, + content, + service, + data, + { + :exit => false, + :confirm => confirm, + :pin => pin + } + )); } else if (type.equals("group")) { addItem(HomeAssistantMenuItemFactory.create().group(items[i], content)); }