Revised glance view

Refreshed default view and new customisable view.
This commit is contained in:
Philip Abbey
2025-07-06 14:44:53 +01:00
parent f1c592179d
commit 47a930828a
18 changed files with 398 additions and 61 deletions

View File

@ -24,18 +24,20 @@ using Toybox.Timer;
//
(:glance, :background)
class HomeAssistantApp extends Application.AppBase {
private var mApiStatus as Lang.String or Null;
private var mMenuStatus as Lang.String or Null;
private var mHaMenu as HomeAssistantView or Null;
private var mQuitTimer as QuitTimer or Null;
private var mGlanceTimer as Timer.Timer or Null;
private var mUpdateTimer as Timer.Timer or Null;
private var mApiStatus as Lang.String or Null;
private var mMenuStatus as Lang.String or Null;
private var mHaMenu as HomeAssistantView or Null;
private var mGlanceTemplate as Lang.String or Null = null;
private var mGlanceText as Lang.String or Null = null;
private var mQuitTimer as QuitTimer or Null;
private var mGlanceTimer as Timer.Timer or Null;
private var mUpdateTimer as Timer.Timer or Null;
// Array initialised by onReturnFetchMenuConfig()
private var mItemsToUpdate as Lang.Array<HomeAssistantToggleMenuItem or HomeAssistantTapMenuItem or HomeAssistantGroupMenuItem> or Null;
private var mIsGlance as Lang.Boolean = false;
private var mIsApp as Lang.Boolean = false; // Or Widget
private var mUpdating as Lang.Boolean = false; // Don't start a second chain of updates
private var mTemplates as Lang.Dictionary = {};
private var mItemsToUpdate as Lang.Array<HomeAssistantToggleMenuItem or HomeAssistantTapMenuItem or HomeAssistantGroupMenuItem> or Null;
private var mIsGlance as Lang.Boolean = false;
private var mIsApp as Lang.Boolean = false; // Or Widget
private var mUpdating as Lang.Boolean = false; // Don't start a second chain of updates
private var mTemplates as Lang.Dictionary = {};
//! Class Constructor
//
@ -203,7 +205,9 @@ class HomeAssistantApp extends Application.AppBase {
mMenuStatus = WatchUi.loadResource($.Rez.Strings.Available) as Lang.String;
}
}
if (!mIsGlance) {
if (mIsGlance) {
glanceTemplate(data);
} else {
if (data == null) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoJson) as Lang.String);
} else {
@ -272,7 +276,9 @@ class HomeAssistantApp extends Application.AppBase {
} else {
mMenuStatus = WatchUi.loadResource($.Rez.Strings.Cached) as Lang.String;
WatchUi.requestUpdate();
if (!mIsGlance) {
if (mIsGlance) {
glanceTemplate(menu);
} else {
buildMenu(menu);
}
return true;
@ -303,6 +309,22 @@ class HomeAssistantApp extends Application.AppBase {
}
}
//! Extract the optional template to override the default glance view.
//
function glanceTemplate(menu as Lang.Dictionary) {
if (menu != null) {
if (menu.get("glance") != null) {
var glance = menu.get("glance") as Lang.Dictionary;
if (glance.get("type").equals("info")) {
mGlanceTemplate = glance.get("content") as Lang.String;
// System.println("HomeAssistantApp glanceTemplate() " + mGlanceTemplate);
} else { // if glance.get("type").equals("status")
mGlanceTemplate = null;
}
}
}
}
//! Callback function for each menu update GET request.
//!
//! @param responseCode Response code.
@ -416,10 +438,9 @@ class HomeAssistantApp extends Application.AppBase {
}
}
// https://developers.home-assistant.io/docs/api/native-app-integration/sending-data/#render-templates
var url = Settings.getApiUrl() + "/webhook/" + Settings.getWebhookId();
// System.println("HomeAssistantApp updateMenuItems() URL=" + url + ", Template='" + mTemplate + "'");
Communications.makeWebRequest(
url,
Settings.getApiUrl() + "/webhook/" + Settings.getWebhookId(),
{
"type" => "render_template",
"data" => mTemplates
@ -548,6 +569,100 @@ class HomeAssistantApp extends Application.AppBase {
}
}
//! Callback function after completing the GET request to render the glance template.
//!
//! @param responseCode Response code.
//! @param data Response data.
//
(:glance)
function onReturnFetchGlanceContent(
responseCode as Lang.Number,
data as Null or Lang.Dictionary or Lang.String
) as Void {
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Code: " + responseCode);
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Data: " + data);
switch (responseCode) {
case Communications.BLE_HOST_TIMEOUT:
case Communications.BLE_CONNECTION_UNAVAILABLE:
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed.");
if (!mIsGlance) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoPhone) as Lang.String);
}
break;
case Communications.BLE_QUEUE_FULL:
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
if (!mIsGlance) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.ApiFlood) as Lang.String);
}
break;
case Communications.NETWORK_REQUEST_TIMED_OUT:
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection.");
if (!mIsGlance) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoResponse) as Lang.String);
}
break;
case Communications.INVALID_HTTP_BODY_IN_NETWORK_RESPONSE:
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Code: INVALID_HTTP_BODY_IN_NETWORK_RESPONSE, check JSON is returned.");
if (!mIsGlance) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoJson) as Lang.String);
}
break;
case 404:
// System.println("HomeAssistantApp onReturnFetchGlanceContent() Response Code: 404, page not found. Check Configuration URL setting.");
if (!mIsGlance) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.ConfigUrlNotFound) as Lang.String);
}
break;
case 200:
if (data != null) {
mGlanceText = data.get("glanceTemplate");
}
break;
default:
// System.println("HomeAssistantApp onReturnFetchGlanceContent(): Unhandled HTTP response code = " + responseCode);
if (!mIsGlance) {
ErrorView.show(WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr) as Lang.String + responseCode);
}
}
WatchUi.requestUpdate();
}
//! Construct the GET request to convert the optional glance template to text for display.
//
(:glance)
function fetchGlanceContent() as Void {
if (mGlanceTemplate != null) {
// https://developers.home-assistant.io/docs/api/native-app-integration/sending-data/#render-templates
Communications.makeWebRequest(
Settings.getApiUrl() + "/webhook/" + Settings.getWebhookId(),
{
"type" => "render_template",
"data" => {
"glanceTemplate" => {
"template" => mGlanceTemplate
}
}
},
{
:method => Communications.HTTP_REQUEST_METHOD_POST,
:headers => {
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON
},
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
},
method(:onReturnFetchGlanceContent)
);
}
}
//! Record the API status result.
//!
//! @param s A string describing the API status
@ -574,6 +689,16 @@ class HomeAssistantApp extends Application.AppBase {
return mMenuStatus;
}
//! Return the optional glance text that overrides the default glance content. This
//! is derived from the glance template.
//!
//! @return A string derived from the glance template
//
(:glance)
function getGlanceText() as Lang.String or Null {
return mGlanceText;
}
//! Return the Menu construction status.
//!
//! @return A Boolean indicating if the menu is loaded into the application.
@ -623,12 +748,21 @@ class HomeAssistantApp extends Application.AppBase {
return [new HomeAssistantGlanceView(self)];
}
//! Return the glance theme.
//!
//! @return The glance colour
//
function getGlanceTheme() as Application.AppBase.GlanceTheme {
return Application.AppBase.GLANCE_THEME_LIGHT_BLUE;
}
//! Update the menu and API statuses. Required for the Glance update timer.
//
function updateStatus() as Void {
mGlanceTimer = null;
fetchMenuConfig();
fetchApiStatus();
fetchGlanceContent();
}
//! Code for when the application settings are updated.

View File

@ -21,15 +21,34 @@ using Toybox.Graphics;
//
(:glance)
class HomeAssistantGlanceView extends WatchUi.GlanceView {
private static const scLeftMargin = 5; // in pixels
private static const scMidSep = 10; // Middle Separator "text:_text" in pixels
private var mApp as HomeAssistantApp;
private var mTitle as WatchUi.Text or Null;
private var mApiText as WatchUi.Text or Null;
private var mApiStatus as WatchUi.Text or Null;
private var mMenuText as WatchUi.Text or Null;
private var mMenuStatus as WatchUi.Text or Null;
private var mAntiAlias as Lang.Boolean = false;
//! Margin left of the filled rectangle in pixels.
private static const scLeftRectMargin = 5;
//! Filled rectangle width in pixels.
private static const scRectWidth = 20;
//! Margin right of the filled rectangle in pixels.
private static const scRightRectMargin = 5;
//! Separator between the first column of text and the second in pixels.
//! i.e. Middle Separator "text:_text"
private static const scMidSep = 10;
//! Margin on the right side of the glance in pixels.
private static const scRightGlanceMargin = 15;
//! Margin top and bottom of the rectangles in pixels.
private static const scVertMargin = 5;
//! Size of the rounded rectangle corners in pixels.
private static const scRectRadius = 5;
//! Dynamically scale the width of the first column of text based on the
//! language selection for the word "Menu".
private var mTextWidth as Lang.Number = 0;
// Re-usable text items for drawing
private var mApp as HomeAssistantApp;
private var mTitle as WatchUi.Text or Null;
private var mApiText as WatchUi.Text or Null;
private var mApiStatus as WatchUi.Text or Null;
private var mMenuText as WatchUi.Text or Null;
private var mMenuStatus as WatchUi.Text or Null;
private var mGlanceContent as WatchUi.TextArea or Null;
private var mAntiAlias as Lang.Boolean = false;
//! Class Constructor
//
@ -46,15 +65,16 @@ class HomeAssistantGlanceView extends WatchUi.GlanceView {
//! @param dc Device context
//
function onLayout(dc as Graphics.Dc) as Void {
var h = dc.getHeight();
var tw = dc.getTextWidthInPixels(WatchUi.loadResource($.Rez.Strings.GlanceMenu) as Lang.String, Graphics.FONT_XTINY);
var h = dc.getHeight();
mTextWidth = dc.getTextWidthInPixels(WatchUi.loadResource($.Rez.Strings.GlanceMenu) as Lang.String + ":", Graphics.FONT_XTINY);
mTitle = new WatchUi.Text({
:text => WatchUi.loadResource($.Rez.Strings.AppName) as Lang.String,
:color => Graphics.COLOR_WHITE,
:font => Graphics.FONT_TINY,
:justification => Graphics.TEXT_JUSTIFY_LEFT | Graphics.TEXT_JUSTIFY_VCENTER,
:locX => scLeftMargin,
:locX => scLeftRectMargin,
:locY => 1 * h / 6
});
@ -63,7 +83,7 @@ class HomeAssistantGlanceView extends WatchUi.GlanceView {
:color => Graphics.COLOR_WHITE,
:font => Graphics.FONT_XTINY,
:justification => Graphics.TEXT_JUSTIFY_LEFT | Graphics.TEXT_JUSTIFY_VCENTER,
:locX => scLeftMargin,
:locX => scLeftRectMargin + scRectWidth + scRightRectMargin,
:locY => 3 * h / 6
});
mApiStatus = new WatchUi.Text({
@ -71,15 +91,16 @@ class HomeAssistantGlanceView extends WatchUi.GlanceView {
:color => Graphics.COLOR_WHITE,
:font => Graphics.FONT_XTINY,
:justification => Graphics.TEXT_JUSTIFY_LEFT | Graphics.TEXT_JUSTIFY_VCENTER,
:locX => scLeftMargin + scMidSep + tw,
:locX => scLeftRectMargin + scRectWidth + scRightRectMargin + scMidSep + mTextWidth,
:locY => 3 * h / 6
});
mMenuText = new WatchUi.Text({
:text => WatchUi.loadResource($.Rez.Strings.GlanceMenu) as Lang.String + ":",
:color => Graphics.COLOR_WHITE,
:font => Graphics.FONT_XTINY,
:justification => Graphics.TEXT_JUSTIFY_LEFT | Graphics.TEXT_JUSTIFY_VCENTER,
:locX => scLeftMargin,
:locX => scLeftRectMargin + scRectWidth + scRightRectMargin,
:locY => 5 * h / 6
});
mMenuStatus = new WatchUi.Text({
@ -87,9 +108,20 @@ class HomeAssistantGlanceView extends WatchUi.GlanceView {
:color => Graphics.COLOR_WHITE,
:font => Graphics.FONT_XTINY,
:justification => Graphics.TEXT_JUSTIFY_LEFT | Graphics.TEXT_JUSTIFY_VCENTER,
:locX => scLeftMargin + scMidSep + tw,
:locX => scLeftRectMargin + scRectWidth + scRightRectMargin + scMidSep + mTextWidth,
:locY => 5 * h / 6
});
mGlanceContent = new WatchUi.TextArea({
:text => "A longer piece of text to wrap.",
:color => Graphics.COLOR_WHITE,
:font => Graphics.FONT_XTINY,
:justification => Graphics.TEXT_JUSTIFY_LEFT | Graphics.TEXT_JUSTIFY_VCENTER,
:locX => scLeftRectMargin + scRectWidth + scRightRectMargin + scMidSep,
:locY => (2 * h / 6) + scVertMargin,
:width => dc.getWidth() - scLeftRectMargin - scRightGlanceMargin,
:height => (4 * h / 6) - (2 * scVertMargin)
});
}
//! Update the view with the latest status text.
@ -97,8 +129,17 @@ class HomeAssistantGlanceView extends WatchUi.GlanceView {
//! @param dc Device context
//
function onUpdate(dc as Graphics.Dc) as Void {
var h = dc.getHeight();
var w = dc.getWidth() - scLeftRectMargin - scRightGlanceMargin;
var apiStatus = mApp.getApiStatus();
var menuStatus = mApp.getMenuStatus();
var glanceText = mApp.getGlanceText();
var apiCol;
var menuCol;
// System.println("HomeAssistantGlanceView onUpdate() glanceText=" + glanceText);
GlanceView.onUpdate(dc);
if(mAntiAlias) {
if (mAntiAlias) {
dc.setAntiAlias(true);
}
dc.setColor(
@ -106,12 +147,60 @@ class HomeAssistantGlanceView extends WatchUi.GlanceView {
Graphics.COLOR_TRANSPARENT
);
dc.clear();
mTitle.setColor(Graphics.COLOR_BLUE);
mTitle.draw(dc);
mApiText.draw(dc);
mApiStatus.setText(mApp.getApiStatus());
mApiStatus.draw(dc);
mMenuText.draw(dc);
mMenuStatus.setText(mApp.getMenuStatus());
mMenuStatus.draw(dc);
}
if (apiStatus.equals(WatchUi.loadResource($.Rez.Strings.Checking))) {
apiCol = Graphics.COLOR_YELLOW;
} else if (apiStatus.equals(WatchUi.loadResource($.Rez.Strings.Available))) {
apiCol = Graphics.COLOR_GREEN;
} else {
apiCol = Graphics.COLOR_RED;
}
if (menuStatus.equals(WatchUi.loadResource($.Rez.Strings.Checking))) {
menuCol = Graphics.COLOR_YELLOW;
} else if (menuStatus.equals(WatchUi.loadResource($.Rez.Strings.Available))) {
menuCol = Graphics.COLOR_GREEN;
} else if (menuStatus.equals(WatchUi.loadResource($.Rez.Strings.Cached))) {
menuCol = Graphics.COLOR_GREEN;
} else {
menuCol = Graphics.COLOR_RED;
}
if (glanceText == null) {
// Default Glance View
mApiText.draw(dc);
mApiStatus.setText(apiStatus);
mApiStatus.setColor(apiCol);
dc.setColor(apiCol, apiCol);
dc.drawRoundedRectangle(scLeftRectMargin, 2 * h / 6 + scVertMargin, w, 2 * h / 6 - (2 * scVertMargin), scRectRadius);
dc.fillRoundedRectangle(scLeftRectMargin, 2 * h / 6 + scVertMargin, scRectWidth, 2 * h / 6 - (2 * scVertMargin), scRectRadius);
mApiStatus.draw(dc);
mMenuText.draw(dc);
mMenuStatus.setText(menuStatus);
mMenuStatus.setColor(menuCol);
dc.setColor(menuCol, menuCol);
dc.drawRoundedRectangle(scLeftRectMargin, 4 * h / 6 + scVertMargin, w, 2 * h / 6 - (2 * scVertMargin), scRectRadius);
dc.fillRoundedRectangle(scLeftRectMargin, 4 * h / 6 + scVertMargin, scRectWidth, 2 * h / 6 - (2 * scVertMargin), scRectRadius);
mMenuStatus.draw(dc);
} else {
// Customised Glance View
dc.setColor(Graphics.COLOR_BLUE, Graphics.COLOR_BLUE);
dc.drawRoundedRectangle(
scLeftRectMargin + scRectWidth + scRightRectMargin,
2 * h / 6 + scVertMargin,
w - scRectWidth - scRightRectMargin,
4 * h / 6 - (2 * scVertMargin),
scRectRadius
);
dc.setColor(apiCol, apiCol);
dc.fillRoundedRectangle(scLeftRectMargin, 2 * h / 6 + scVertMargin, scRectWidth, 2 * h / 6 - (2 * scVertMargin), scRectRadius);
dc.setColor(menuCol, menuCol);
dc.fillRoundedRectangle(scLeftRectMargin, 4 * h / 6 + scVertMargin, scRectWidth, 2 * h / 6 - (2 * scVertMargin), scRectRadius);
mGlanceContent.setText(glanceText);
mGlanceContent.draw(dc);
}
}
}

View File

@ -167,7 +167,7 @@ class WebLog {
// }
}
// Callback function to print the outcome of a clear() method. Typically used for debugging this class.
//! Callback function to print the outcome of a clear() method. Typically used for debugging this class.
//!
//! @param responseCode Response code.
//! @param data Response data.