mirror of
				https://github.com/house-of-abbey/GarminHomeAssistant.git
				synced 2025-11-04 00:48:14 +00:00 
			
		
		
		
	Renamed class variables
There's a Monkey C convention to have class variable names start with 'm', then be camel case. 'm' for 'member' according to https://developer.garmin.com/connect-iq/reference-guides/monkey-c-reference/.
This commit is contained in:
		@@ -30,55 +30,55 @@ using Toybox.Timer;
 | 
			
		||||
const bRadius = 10;
 | 
			
		||||
 | 
			
		||||
class Alert extends WatchUi.View {
 | 
			
		||||
    hidden var timer;
 | 
			
		||||
    hidden var timeout;
 | 
			
		||||
    hidden var text;
 | 
			
		||||
    hidden var font;
 | 
			
		||||
    hidden var fgcolor;
 | 
			
		||||
    hidden var bgcolor;
 | 
			
		||||
    hidden var mTimer;
 | 
			
		||||
    hidden var mTimeout;
 | 
			
		||||
    hidden var mText;
 | 
			
		||||
    hidden var mFont;
 | 
			
		||||
    hidden var mFgcolor;
 | 
			
		||||
    hidden var mBgcolor;
 | 
			
		||||
 | 
			
		||||
    function initialize(params as Lang.Dictionary) {
 | 
			
		||||
        View.initialize();
 | 
			
		||||
 | 
			
		||||
        text = params.get(:text);
 | 
			
		||||
        if (text == null) {
 | 
			
		||||
            text = "Alert";
 | 
			
		||||
        mText = params.get(:text);
 | 
			
		||||
        if (mText == null) {
 | 
			
		||||
            mText = "Alert";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        font = params.get(:font);
 | 
			
		||||
        if (font == null) {
 | 
			
		||||
            font = Graphics.FONT_MEDIUM;
 | 
			
		||||
        mFont = params.get(:font);
 | 
			
		||||
        if (mFont == null) {
 | 
			
		||||
            mFont = Graphics.FONT_MEDIUM;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        fgcolor = params.get(:fgcolor);
 | 
			
		||||
        if (fgcolor == null) {
 | 
			
		||||
            fgcolor = Graphics.COLOR_BLACK;
 | 
			
		||||
        mFgcolor = params.get(:fgcolor);
 | 
			
		||||
        if (mFgcolor == null) {
 | 
			
		||||
            mFgcolor = Graphics.COLOR_BLACK;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        bgcolor = params.get(:bgcolor);
 | 
			
		||||
        if (bgcolor == null) {
 | 
			
		||||
            bgcolor = Graphics.COLOR_WHITE;
 | 
			
		||||
        mBgcolor = params.get(:bgcolor);
 | 
			
		||||
        if (mBgcolor == null) {
 | 
			
		||||
            mBgcolor = Graphics.COLOR_WHITE;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        timeout = params.get(:timeout);
 | 
			
		||||
        if (timeout == null) {
 | 
			
		||||
            timeout = 2000;
 | 
			
		||||
        mTimeout = params.get(:timeout);
 | 
			
		||||
        if (mTimeout == null) {
 | 
			
		||||
            mTimeout = 2000;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        timer = new Timer.Timer();
 | 
			
		||||
        mTimer = new Timer.Timer();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function onShow() {
 | 
			
		||||
        timer.start(method(:dismiss), timeout, false);
 | 
			
		||||
        mTimer.start(method(:dismiss), mTimeout, false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function onHide() {
 | 
			
		||||
        timer.stop();
 | 
			
		||||
        mTimer.stop();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function onUpdate(dc) {
 | 
			
		||||
        var tWidth  = dc.getTextWidthInPixels(text, font);
 | 
			
		||||
        var tHeight = dc.getFontHeight(font);
 | 
			
		||||
        var tWidth  = dc.getTextWidthInPixels(mText, mFont);
 | 
			
		||||
        var tHeight = dc.getFontHeight(mFont);
 | 
			
		||||
        var bWidth  = tWidth  + 20;
 | 
			
		||||
        var bHeight = tHeight + 15;
 | 
			
		||||
        var bX      = (dc.getWidth()  - bWidth)  / 2;
 | 
			
		||||
@@ -93,10 +93,10 @@ class Alert extends WatchUi.View {
 | 
			
		||||
            Graphics.COLOR_TRANSPARENT
 | 
			
		||||
        );
 | 
			
		||||
        dc.clear();
 | 
			
		||||
        dc.setColor(bgcolor, bgcolor);
 | 
			
		||||
        dc.setColor(mBgcolor, mBgcolor);
 | 
			
		||||
        dc.fillRoundedRectangle(bX, bY, bWidth, bHeight, bRadius);
 | 
			
		||||
 | 
			
		||||
        dc.setColor(fgcolor, bgcolor);
 | 
			
		||||
        dc.setColor(mFgcolor, mBgcolor);
 | 
			
		||||
        for (var i = 0; i < 3; ++i) {
 | 
			
		||||
            bX      += i;
 | 
			
		||||
            bY      += i;
 | 
			
		||||
@@ -107,8 +107,8 @@ class Alert extends WatchUi.View {
 | 
			
		||||
 | 
			
		||||
        var tX = dc.getWidth() / 2;
 | 
			
		||||
        var tY = bY + bHeight  / 2;
 | 
			
		||||
        dc.setColor(fgcolor, bgcolor);
 | 
			
		||||
        dc.drawText(tX, tY, font, text, Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER);
 | 
			
		||||
        dc.setColor(mFgcolor, mBgcolor);
 | 
			
		||||
        dc.drawText(tX, tY, mFont, mText, Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Remove the alert from view, usually on user input, but that is defined by the calling function.
 | 
			
		||||
 
 | 
			
		||||
@@ -26,31 +26,31 @@ using Toybox.WatchUi;
 | 
			
		||||
using Toybox.Communications;
 | 
			
		||||
 | 
			
		||||
class ErrorView extends ScalableView {
 | 
			
		||||
    hidden const settings as Lang.Dictionary = {
 | 
			
		||||
    hidden const cSettings as Lang.Dictionary = {
 | 
			
		||||
        :errorIconMargin => 7f
 | 
			
		||||
    };
 | 
			
		||||
    // Vertical spacing between the top of the face and the error icon
 | 
			
		||||
    hidden var errorIconMargin;
 | 
			
		||||
    hidden var text as Lang.String;
 | 
			
		||||
    hidden var errorIcon;
 | 
			
		||||
    hidden var textArea;
 | 
			
		||||
    hidden var mErrorIconMargin;
 | 
			
		||||
    hidden var mText as Lang.String;
 | 
			
		||||
    hidden var mErrorIcon;
 | 
			
		||||
    hidden var mTextArea;
 | 
			
		||||
 | 
			
		||||
    function initialize(text as Lang.String) {
 | 
			
		||||
        ScalableView.initialize();
 | 
			
		||||
        self.text = text;
 | 
			
		||||
        mText = text;
 | 
			
		||||
        // Convert the settings from % of screen size to pixels
 | 
			
		||||
        errorIconMargin = pixelsForScreen(settings.get(:errorIconMargin) as Lang.Float);
 | 
			
		||||
        mErrorIconMargin = pixelsForScreen(cSettings.get(:errorIconMargin) as Lang.Float);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Load your resources here
 | 
			
		||||
    function onLayout(dc as Graphics.Dc) as Void {
 | 
			
		||||
        errorIcon = Application.loadResource(Rez.Drawables.ErrorIcon) as Graphics.BitmapResource;
 | 
			
		||||
        mErrorIcon = Application.loadResource(Rez.Drawables.ErrorIcon) as Graphics.BitmapResource;
 | 
			
		||||
 | 
			
		||||
        var w = dc.getWidth();
 | 
			
		||||
        var h = dc.getHeight();
 | 
			
		||||
 | 
			
		||||
        textArea = new WatchUi.TextArea({
 | 
			
		||||
            :text          => text,
 | 
			
		||||
        mTextArea = new WatchUi.TextArea({
 | 
			
		||||
            :text          => mText,
 | 
			
		||||
            :color         => Graphics.COLOR_WHITE,
 | 
			
		||||
            :font          => Graphics.FONT_XTINY,
 | 
			
		||||
            :justification => Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER,
 | 
			
		||||
@@ -71,8 +71,8 @@ class ErrorView extends ScalableView {
 | 
			
		||||
        }
 | 
			
		||||
        dc.setColor(Graphics.COLOR_WHITE, bg);
 | 
			
		||||
        dc.clear();
 | 
			
		||||
        dc.drawBitmap(hw - errorIcon.getWidth()/2, errorIconMargin, errorIcon);
 | 
			
		||||
        textArea.draw(dc);
 | 
			
		||||
        dc.drawBitmap(hw - mErrorIcon.getWidth()/2, mErrorIconMargin, mErrorIcon);
 | 
			
		||||
        mTextArea.draw(dc);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -23,9 +23,9 @@ using Toybox.Lang;
 | 
			
		||||
class Globals {
 | 
			
		||||
    // Enable printing of messages to the debug console (don't make this a Property
 | 
			
		||||
    // as the messages can't be read from a watch!)
 | 
			
		||||
    static const debug                  = false;
 | 
			
		||||
    static const scDebug                  = false;
 | 
			
		||||
    // There's a danger this time is device sensitive.
 | 
			
		||||
    static const menuItemUpdateInterval =  100; // ms, 100 ms seems okay for Venu2
 | 
			
		||||
    static const alertTimeout           = 2000; // ms
 | 
			
		||||
    static const tapTimeout             = 1000; // ms
 | 
			
		||||
    static const scMenuItemUpdateInterval =  100; // ms, 100 ms seems okay for Venu2
 | 
			
		||||
    static const scAlertTimeout           = 2000; // ms
 | 
			
		||||
    static const scTapTimeout             = 1000; // ms
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,7 @@
 | 
			
		||||
//
 | 
			
		||||
// Description:
 | 
			
		||||
//
 | 
			
		||||
// Application root for GarminHomeAssistant.
 | 
			
		||||
// Application root for GarminHomeAssistant
 | 
			
		||||
//
 | 
			
		||||
//-----------------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
@@ -25,16 +25,16 @@ using Toybox.Application.Properties;
 | 
			
		||||
using Toybox.Timer;
 | 
			
		||||
 | 
			
		||||
class HomeAssistantApp extends Application.AppBase {
 | 
			
		||||
    hidden var haMenu;
 | 
			
		||||
    hidden var mHaMenu;
 | 
			
		||||
    hidden var strNoApiKey    as Lang.String;
 | 
			
		||||
    hidden var strNoApiUrl    as Lang.String;
 | 
			
		||||
    hidden var strNoConfigUrl as Lang.String;
 | 
			
		||||
    hidden var strNoInternet  as Lang.String;
 | 
			
		||||
    hidden var strNoMenu      as Lang.String;
 | 
			
		||||
    hidden var strApiFlood    as Lang.String;
 | 
			
		||||
    hidden var timer          as Timer.Timer;
 | 
			
		||||
    hidden var itemsToUpdate;        // Array initialised by onReturnFetchMenuConfig()
 | 
			
		||||
    hidden var nextItemToUpdate = 0; // Index into the above array
 | 
			
		||||
    hidden var mTimer         as Timer.Timer;
 | 
			
		||||
    hidden var mItemsToUpdate;        // Array initialised by onReturnFetchMenuConfig()
 | 
			
		||||
    hidden var mNextItemToUpdate = 0; // Index into the above array
 | 
			
		||||
 | 
			
		||||
    function initialize() {
 | 
			
		||||
        AppBase.initialize();
 | 
			
		||||
@@ -44,7 +44,7 @@ class HomeAssistantApp extends Application.AppBase {
 | 
			
		||||
        strNoInternet  = WatchUi.loadResource($.Rez.Strings.NoInternet);
 | 
			
		||||
        strNoMenu      = WatchUi.loadResource($.Rez.Strings.NoMenu);
 | 
			
		||||
        strApiFlood    = WatchUi.loadResource($.Rez.Strings.ApiFlood);
 | 
			
		||||
        timer          = new Timer.Timer();
 | 
			
		||||
        mTimer          = new Timer.Timer();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // onStart() is called on application start up
 | 
			
		||||
@@ -53,25 +53,25 @@ class HomeAssistantApp extends Application.AppBase {
 | 
			
		||||
 | 
			
		||||
    // onStop() is called when your application is exiting
 | 
			
		||||
    function onStop(state as Lang.Dictionary?) as Void {
 | 
			
		||||
        if (timer != null) {
 | 
			
		||||
            timer.stop();
 | 
			
		||||
        if (mTimer != null) {
 | 
			
		||||
            mTimer.stop();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Return the initial view of your application here
 | 
			
		||||
    function getInitialView() as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>? {
 | 
			
		||||
        if ((Properties.getValue("api_key") as Lang.String).length() == 0) {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantMenuItem Note - execScript(): No API key in the application settings.");
 | 
			
		||||
            }
 | 
			
		||||
            return [new ErrorView(strNoApiKey + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>;
 | 
			
		||||
        } else if ((Properties.getValue("api_url") as Lang.String).length() == 0) {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantMenuItem Note - execScript(): No API URL in the application settings.");
 | 
			
		||||
            }
 | 
			
		||||
            return [new ErrorView(strNoApiUrl + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>;
 | 
			
		||||
        } else if ((Properties.getValue("config_url") as Lang.String).length() == 0) {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantMenuItem Note - execScript(): No configuration URL in the application settings.");
 | 
			
		||||
            }
 | 
			
		||||
            return [new ErrorView(strNoConfigUrl + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>;
 | 
			
		||||
@@ -79,7 +79,7 @@ class HomeAssistantApp extends Application.AppBase {
 | 
			
		||||
            fetchMenuConfig();
 | 
			
		||||
            return [new WatchUi.View(), new WatchUi.BehaviorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>;
 | 
			
		||||
        } else {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantApp Note - fetchMenuConfig(): No Internet connection, skipping API call.");
 | 
			
		||||
            }
 | 
			
		||||
            return [new ErrorView(strNoInternet + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>;
 | 
			
		||||
@@ -89,12 +89,12 @@ class HomeAssistantApp extends Application.AppBase {
 | 
			
		||||
    // Callback function after completing the GET request to fetch the configuration menu.
 | 
			
		||||
    //
 | 
			
		||||
    function onReturnFetchMenuConfig(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
 | 
			
		||||
        if (Globals.debug) {
 | 
			
		||||
        if (Globals.scDebug) {
 | 
			
		||||
            System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: " + responseCode);
 | 
			
		||||
            System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Data: " + data);
 | 
			
		||||
        }
 | 
			
		||||
        if (responseCode == Communications.BLE_QUEUE_FULL) {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
 | 
			
		||||
            }
 | 
			
		||||
            var cw = WatchUi.getCurrentView();
 | 
			
		||||
@@ -103,21 +103,21 @@ class HomeAssistantApp extends Application.AppBase {
 | 
			
		||||
                WatchUi.pushView(new ErrorView(strApiFlood), new ErrorDelegate(), WatchUi.SLIDE_UP);
 | 
			
		||||
            }
 | 
			
		||||
        } else if (responseCode == 200) {
 | 
			
		||||
            haMenu = new HomeAssistantView(data, null);
 | 
			
		||||
            WatchUi.switchToView(haMenu, new HomeAssistantViewDelegate(), WatchUi.SLIDE_IMMEDIATE);
 | 
			
		||||
            itemsToUpdate = haMenu.getItemsToUpdate();
 | 
			
		||||
            timer.start(
 | 
			
		||||
            mHaMenu = new HomeAssistantView(data, null);
 | 
			
		||||
            WatchUi.switchToView(mHaMenu, new HomeAssistantViewDelegate(), WatchUi.SLIDE_IMMEDIATE);
 | 
			
		||||
            mItemsToUpdate = mHaMenu.getItemsToUpdate();
 | 
			
		||||
            mTimer.start(
 | 
			
		||||
                method(:updateNextMenuItem),
 | 
			
		||||
                Globals.menuItemUpdateInterval,
 | 
			
		||||
                Globals.scMenuItemUpdateInterval,
 | 
			
		||||
                true
 | 
			
		||||
            );
 | 
			
		||||
        } else if (responseCode == -300) {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantApp Note - onReturnFetchMenuConfig(): Network request timeout.");
 | 
			
		||||
            }
 | 
			
		||||
            WatchUi.pushView(new ErrorView(strNoMenu + ". " + strNoInternet + "?"), new ErrorDelegate(), WatchUi.SLIDE_UP);
 | 
			
		||||
        } else {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantApp Note - onReturnFetchMenuConfig(): Configuration not found or potential validation issue.");
 | 
			
		||||
            }
 | 
			
		||||
            WatchUi.pushView(new ErrorView(strNoMenu + " code=" + responseCode ), new ErrorDelegate(), WatchUi.SLIDE_UP);
 | 
			
		||||
@@ -140,9 +140,9 @@ class HomeAssistantApp extends Application.AppBase {
 | 
			
		||||
    // We need to spread out the API calls so as not to overload the results queue and cause Communications.BLE_QUEUE_FULL (-101) error.
 | 
			
		||||
    // This function is called by a timer every Globals.menuItemUpdateInterval ms.
 | 
			
		||||
    function updateNextMenuItem() as Void {
 | 
			
		||||
        var itu = itemsToUpdate as Lang.Array<HomeAssistantToggleMenuItem>;
 | 
			
		||||
        itu[nextItemToUpdate].getState();
 | 
			
		||||
        nextItemToUpdate = (nextItemToUpdate + 1) % itu.size();
 | 
			
		||||
        var itu = mItemsToUpdate as Lang.Array<HomeAssistantToggleMenuItem>;
 | 
			
		||||
        itu[mNextItemToUpdate].getState();
 | 
			
		||||
        mNextItemToUpdate = (mNextItemToUpdate + 1) % itu.size();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -24,10 +24,10 @@ using Toybox.Graphics;
 | 
			
		||||
using Toybox.Application.Properties;
 | 
			
		||||
 | 
			
		||||
class HomeAssistantMenuItem extends WatchUi.MenuItem {
 | 
			
		||||
    hidden var api_key = Properties.getValue("api_key");
 | 
			
		||||
    hidden var mApiKey = Properties.getValue("api_key");
 | 
			
		||||
    hidden var strNoInternet as Lang.String;
 | 
			
		||||
    hidden var strApiFlood   as Lang.String;
 | 
			
		||||
    hidden var mService as Lang.String or Null;
 | 
			
		||||
    hidden var mService      as Lang.String;
 | 
			
		||||
 | 
			
		||||
    function initialize(
 | 
			
		||||
        label as Lang.String or Lang.Symbol,
 | 
			
		||||
@@ -53,12 +53,12 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
 | 
			
		||||
    // Callback function after completing the POST request to call a script.
 | 
			
		||||
    //
 | 
			
		||||
    function onReturnExecScript(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
 | 
			
		||||
        if (Globals.debug) {
 | 
			
		||||
        if (Globals.scDebug) {
 | 
			
		||||
            System.println("HomeAssistantMenuItem onReturnExecScript() Response Code: " + responseCode);
 | 
			
		||||
            System.println("HomeAssistantMenuItem onReturnExecScript() Response Data: " + data);
 | 
			
		||||
        }
 | 
			
		||||
        if (responseCode == Communications.BLE_QUEUE_FULL) {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantMenuItem onReturnExecScript() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
 | 
			
		||||
            }
 | 
			
		||||
            var cw = WatchUi.getCurrentView();
 | 
			
		||||
@@ -70,7 +70,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
 | 
			
		||||
            var d = data as Lang.Array;
 | 
			
		||||
            for(var i = 0; i < d.size(); i++) {
 | 
			
		||||
                if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) {
 | 
			
		||||
                    if (Globals.debug) {
 | 
			
		||||
                    if (Globals.scDebug) {
 | 
			
		||||
                        System.println("HomeAssistantMenuItem Note - onReturnExecScript(): Correct script executed.");
 | 
			
		||||
                    }
 | 
			
		||||
                    if (WatchUi has :showToast) {
 | 
			
		||||
@@ -88,7 +88,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
 | 
			
		||||
                    }
 | 
			
		||||
                    if (!(WatchUi has :showToast) && !(Attention has :vibrate)) {
 | 
			
		||||
                        new Alert({
 | 
			
		||||
                            :timeout => Globals.alertTimeout,
 | 
			
		||||
                            :timeout => Globals.scAlertTimeout,
 | 
			
		||||
                            :font    => Graphics.FONT_MEDIUM,
 | 
			
		||||
                            :text    => (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String,
 | 
			
		||||
                            :fgcolor => Graphics.COLOR_WHITE,
 | 
			
		||||
@@ -105,7 +105,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
 | 
			
		||||
            :method  => Communications.HTTP_REQUEST_METHOD_POST,
 | 
			
		||||
            :headers => {
 | 
			
		||||
                "Content-Type"  => Communications.REQUEST_CONTENT_TYPE_JSON,
 | 
			
		||||
                "Authorization" => "Bearer " + api_key
 | 
			
		||||
                "Authorization" => "Bearer " + mApiKey
 | 
			
		||||
            },
 | 
			
		||||
            :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
 | 
			
		||||
        };
 | 
			
		||||
@@ -115,7 +115,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
 | 
			
		||||
            var id = mIdentifier as Lang.String;
 | 
			
		||||
            if (mService == null) {
 | 
			
		||||
                var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + id.substring(0, id.find(".")) + "/" + id.substring(id.find(".")+1, id.length());
 | 
			
		||||
                if (Globals.debug) {
 | 
			
		||||
                if (Globals.scDebug) {
 | 
			
		||||
                    System.println("HomeAssistantMenuItem execScript() URL=" + url);
 | 
			
		||||
                    System.println("HomeAssistantMenuItem execScript() mIdentifier=" + mIdentifier);
 | 
			
		||||
                }
 | 
			
		||||
@@ -127,7 +127,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
 | 
			
		||||
                );
 | 
			
		||||
            } else {
 | 
			
		||||
                var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null);
 | 
			
		||||
                if (Globals.debug) {
 | 
			
		||||
                if (Globals.scDebug) {
 | 
			
		||||
                    System.println("HomeAssistantMenuItem execScript() URL=" + url);
 | 
			
		||||
                    System.println("HomeAssistantMenuItem execScript() mIdentifier=" + mIdentifier);
 | 
			
		||||
                }
 | 
			
		||||
@@ -141,7 +141,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
 | 
			
		||||
                );
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantMenuItem Note - execScript(): No Internet connection, skipping API call.");
 | 
			
		||||
            }
 | 
			
		||||
            WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP);
 | 
			
		||||
 
 | 
			
		||||
@@ -24,7 +24,7 @@ using Toybox.Graphics;
 | 
			
		||||
using Toybox.Application.Properties;
 | 
			
		||||
 | 
			
		||||
class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
 | 
			
		||||
    hidden var api_key = Properties.getValue("api_key");
 | 
			
		||||
    hidden var mApiKey = Properties.getValue("api_key");
 | 
			
		||||
    hidden var strNoInternet as Lang.String;
 | 
			
		||||
    hidden var strApiFlood   as Lang.String;
 | 
			
		||||
 | 
			
		||||
@@ -44,7 +44,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
 | 
			
		||||
        strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet);
 | 
			
		||||
        strApiFlood   = WatchUi.loadResource($.Rez.Strings.ApiFlood);
 | 
			
		||||
        WatchUi.ToggleMenuItem.initialize(label, subLabel, identifier, enabled, options);
 | 
			
		||||
        api_key = Properties.getValue("api_key");
 | 
			
		||||
        mApiKey = Properties.getValue("api_key");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function setUiToggle(state as Null or Lang.String) as Void {
 | 
			
		||||
@@ -62,12 +62,12 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
 | 
			
		||||
    // Callback function after completing the GET request to fetch the status.
 | 
			
		||||
    //
 | 
			
		||||
    function onReturnGetState(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
 | 
			
		||||
        if (Globals.debug) {
 | 
			
		||||
        if (Globals.scDebug) {
 | 
			
		||||
            System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: " + responseCode);
 | 
			
		||||
            System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Data: " + data);
 | 
			
		||||
        }
 | 
			
		||||
        if (responseCode == Communications.BLE_QUEUE_FULL) {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
 | 
			
		||||
            }
 | 
			
		||||
            var cw = WatchUi.getCurrentView();
 | 
			
		||||
@@ -77,7 +77,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
 | 
			
		||||
            }
 | 
			
		||||
        } else if (responseCode == 200) {
 | 
			
		||||
            var state = data.get("state") as Lang.String;
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println((data.get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state);
 | 
			
		||||
            }
 | 
			
		||||
            if (getLabel().equals("...")) {
 | 
			
		||||
@@ -91,13 +91,13 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
 | 
			
		||||
        var options = {
 | 
			
		||||
            :method  => Communications.HTTP_REQUEST_METHOD_GET,
 | 
			
		||||
            :headers => {
 | 
			
		||||
                "Authorization" => "Bearer " + api_key
 | 
			
		||||
                "Authorization" => "Bearer " + mApiKey
 | 
			
		||||
            },
 | 
			
		||||
            :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
 | 
			
		||||
        };
 | 
			
		||||
        if (System.getDeviceSettings().phoneConnected && System.getDeviceSettings().connectionAvailable) {
 | 
			
		||||
            var url = Properties.getValue("api_url") + "/states/" + mIdentifier;
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantToggleMenuItem getState() URL=" + url);
 | 
			
		||||
            }
 | 
			
		||||
            Communications.makeWebRequest(
 | 
			
		||||
@@ -107,7 +107,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
 | 
			
		||||
                method(:onReturnGetState)
 | 
			
		||||
            );
 | 
			
		||||
        } else {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantToggleMenuItem Note - getState(): No Internet connection, skipping API call.");
 | 
			
		||||
            }
 | 
			
		||||
            WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP);
 | 
			
		||||
@@ -117,12 +117,12 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
 | 
			
		||||
    // Callback function after completing the POST request to set the status.
 | 
			
		||||
    //
 | 
			
		||||
    function onReturnSetState(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
 | 
			
		||||
        if (Globals.debug) {
 | 
			
		||||
        if (Globals.scDebug) {
 | 
			
		||||
            System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: " + responseCode);
 | 
			
		||||
            System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Data: " + data);
 | 
			
		||||
        }
 | 
			
		||||
        if (responseCode == Communications.BLE_QUEUE_FULL) {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
 | 
			
		||||
            }
 | 
			
		||||
            var cw = WatchUi.getCurrentView();
 | 
			
		||||
@@ -136,7 +136,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
 | 
			
		||||
            for(var i = 0; i < d.size(); i++) {
 | 
			
		||||
                if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) {
 | 
			
		||||
                    state = d[i].get("state") as Lang.String;
 | 
			
		||||
                    if (Globals.debug) {
 | 
			
		||||
                    if (Globals.scDebug) {
 | 
			
		||||
                        System.println((d[i].get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state);
 | 
			
		||||
                    }
 | 
			
		||||
                    setUiToggle(state);
 | 
			
		||||
@@ -150,7 +150,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
 | 
			
		||||
            :method  => Communications.HTTP_REQUEST_METHOD_POST,
 | 
			
		||||
            :headers => {
 | 
			
		||||
                "Content-Type"  => Communications.REQUEST_CONTENT_TYPE_JSON,
 | 
			
		||||
                "Authorization" => "Bearer " + api_key
 | 
			
		||||
                "Authorization" => "Bearer " + mApiKey
 | 
			
		||||
            },
 | 
			
		||||
            :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
 | 
			
		||||
        };
 | 
			
		||||
@@ -164,7 +164,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
 | 
			
		||||
            } else {
 | 
			
		||||
                url = Properties.getValue("api_url") + "/services/" + id.substring(0, id.find(".")) + "/turn_off";
 | 
			
		||||
            }
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantToggleMenuItem setState() URL=" + url);
 | 
			
		||||
                System.println("HomeAssistantToggleMenuItem setState() mIdentifier=" + mIdentifier);
 | 
			
		||||
            }
 | 
			
		||||
@@ -177,7 +177,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
 | 
			
		||||
                method(:onReturnSetState)
 | 
			
		||||
            );
 | 
			
		||||
        } else {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("HomeAssistantToggleMenuItem Note - setState(): No Internet connection, skipping API call.");
 | 
			
		||||
            }
 | 
			
		||||
            WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP);
 | 
			
		||||
 
 | 
			
		||||
@@ -115,25 +115,25 @@ class HomeAssistantViewDelegate extends WatchUi.Menu2InputDelegate {
 | 
			
		||||
    function onSelect(item as WatchUi.MenuItem) as Void {
 | 
			
		||||
        if (item instanceof HomeAssistantToggleMenuItem) {
 | 
			
		||||
            var haToggleItem = item as HomeAssistantToggleMenuItem;
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println(haToggleItem.getLabel() + " " + haToggleItem.getId() + " " + haToggleItem.isEnabled());
 | 
			
		||||
            }
 | 
			
		||||
            haToggleItem.setState(haToggleItem.isEnabled());
 | 
			
		||||
        } else if (item instanceof HomeAssistantMenuItem) {
 | 
			
		||||
            var haItem = item as HomeAssistantMenuItem;
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println(haItem.getLabel() + " " + haItem.getId());
 | 
			
		||||
            }
 | 
			
		||||
            haItem.execScript();
 | 
			
		||||
        } else if (item instanceof HomeAssistantViewMenuItem) {
 | 
			
		||||
            var haMenuItem = item as HomeAssistantViewMenuItem;
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println("Menu: " + haMenuItem.getLabel() + " " + haMenuItem.getId());
 | 
			
		||||
            }
 | 
			
		||||
            // No delegate state to be amended, so re-use 'self'.
 | 
			
		||||
            WatchUi.pushView(haMenuItem.getMenuView(), self, WatchUi.SLIDE_LEFT);
 | 
			
		||||
        } else {
 | 
			
		||||
            if (Globals.debug) {
 | 
			
		||||
            if (Globals.scDebug) {
 | 
			
		||||
                System.println(item.getLabel() + " " + item.getId());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,7 @@ using Toybox.Lang;
 | 
			
		||||
using Toybox.WatchUi;
 | 
			
		||||
 | 
			
		||||
class HomeAssistantViewMenuItem extends WatchUi.MenuItem {
 | 
			
		||||
    hidden var menu as HomeAssistantView;
 | 
			
		||||
    hidden var mMenu as HomeAssistantView;
 | 
			
		||||
 | 
			
		||||
    function initialize(definition as Lang.Dictionary) {
 | 
			
		||||
        // definitions.get(...) are Strings here as they have been checked by HomeAssistantView first
 | 
			
		||||
@@ -33,11 +33,11 @@ class HomeAssistantViewMenuItem extends WatchUi.MenuItem {
 | 
			
		||||
            null
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        menu = new HomeAssistantView(definition, null);
 | 
			
		||||
        mMenu = new HomeAssistantView(definition, null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function getMenuView() as HomeAssistantView {
 | 
			
		||||
        return menu;
 | 
			
		||||
        return mMenu;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,7 @@ using Toybox.WatchUi;
 | 
			
		||||
using Toybox.Math;
 | 
			
		||||
 | 
			
		||||
class ScalableView extends WatchUi.View {
 | 
			
		||||
    hidden var screenWidth;
 | 
			
		||||
    hidden var mScreenWidth;
 | 
			
		||||
 | 
			
		||||
    function initialize() {
 | 
			
		||||
        View.initialize();
 | 
			
		||||
@@ -40,9 +40,9 @@ class ScalableView extends WatchUi.View {
 | 
			
		||||
    // height > width.
 | 
			
		||||
    //
 | 
			
		||||
    function pixelsForScreen(pc as Lang.Float) as Lang.Number {
 | 
			
		||||
        if (screenWidth == null) {
 | 
			
		||||
            screenWidth = System.getDeviceSettings().screenWidth;
 | 
			
		||||
        if (mScreenWidth == null) {
 | 
			
		||||
            mScreenWidth = System.getDeviceSettings().screenWidth;
 | 
			
		||||
        }
 | 
			
		||||
        return Math.round(pc * screenWidth) / 100;
 | 
			
		||||
        return Math.round(pc * mScreenWidth) / 100;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user