//----------------------------------------------------------------------------------- // // 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 & Someone0nEarth, 31 October 2023 // // // Description: // // Generic menu button with an icon that optionally renders a Home Assistant Template. // //----------------------------------------------------------------------------------- using Toybox.Lang; using Toybox.WatchUi; using Toybox.Graphics; class HomeAssistantMenuItem extends WatchUi.IconMenuItem { private var mTemplate as Lang.String or Null; function initialize( label as Lang.String or Lang.Symbol, template as Lang.String, options as { :alignment as WatchUi.MenuItem.Alignment, :icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol } or Null ) { WatchUi.IconMenuItem.initialize( label, null, null, options.get(:icon), options ); mTemplate = template; } function hasTemplate() as Lang.Boolean { return mTemplate != null; } 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.Number) { var d = data as Lang.Number; setSubLabel(d.format("%d")); } else if(data instanceof Lang.Float) { var f = data as Lang.Float; setSubLabel(f.format("%f")); } else if(data instanceof Lang.Dictionary) { // System.println("HomeAssistantMenuItem 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, Number or Float, or the item cannot be formatted locally without error. setSubLabel(WatchUi.loadResource($.Rez.Strings.TemplateError) as Lang.String); } WatchUi.requestUpdate(); } }