mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-06-17 03:48:32 +00:00
Single HomeAssistantService for all Taps
1. Amended code for a single HomeAssistantService for all 'tap's 2. Removed now redundant GET request for taps without a service now that having a service is enforced. 3. Determined that migrating API code from 'toggle's to the HomeAssistantService is awkward due to the close coupling with other methods in the class.
This commit is contained in:
@ -30,12 +30,12 @@ using Toybox.Timer;
|
||||
const bRadius = 10;
|
||||
|
||||
class Alert extends WatchUi.View {
|
||||
hidden var mTimer;
|
||||
hidden var mTimeout;
|
||||
hidden var mText;
|
||||
hidden var mFont;
|
||||
hidden var mFgcolor;
|
||||
hidden var mBgcolor;
|
||||
private var mTimer;
|
||||
private var mTimeout;
|
||||
private var mText;
|
||||
private var mFont;
|
||||
private var mFgcolor;
|
||||
private var mBgcolor;
|
||||
|
||||
function initialize(params as Lang.Dictionary) {
|
||||
View.initialize();
|
||||
|
@ -26,14 +26,14 @@ using Toybox.WatchUi;
|
||||
using Toybox.Communications;
|
||||
|
||||
class ErrorView extends ScalableView {
|
||||
hidden const cSettings as Lang.Dictionary = {
|
||||
private const cSettings as Lang.Dictionary = {
|
||||
:errorIconMargin => 7f
|
||||
};
|
||||
// Vertical spacing between the top of the face and the error icon
|
||||
hidden var mErrorIconMargin;
|
||||
hidden var mText as Lang.String;
|
||||
hidden var mErrorIcon;
|
||||
hidden var mTextArea;
|
||||
private var mErrorIconMargin;
|
||||
private var mText as Lang.String;
|
||||
private var mErrorIcon;
|
||||
private var mTextArea;
|
||||
|
||||
function initialize(text as Lang.String) {
|
||||
ScalableView.initialize();
|
||||
|
@ -24,20 +24,20 @@ using Toybox.WatchUi;
|
||||
using Toybox.Application.Properties;
|
||||
|
||||
class HomeAssistantApp extends Application.AppBase {
|
||||
hidden var mHaMenu;
|
||||
hidden var strNoApiKey as Lang.String;
|
||||
hidden var strNoApiUrl as Lang.String;
|
||||
hidden var strNoConfigUrl as Lang.String;
|
||||
hidden var strNoPhone as Lang.String;
|
||||
hidden var strNoInternet as Lang.String;
|
||||
hidden var strNoResponse as Lang.String;
|
||||
hidden var strNoMenu as Lang.String;
|
||||
hidden var strApiFlood as Lang.String;
|
||||
hidden var strConfigUrlNotFound as Lang.String;
|
||||
hidden var strUnhandledHttpErr as Lang.String;
|
||||
hidden var strTrailingSlashErr as Lang.String;
|
||||
hidden var mItemsToUpdate; // Array initialised by onReturnFetchMenuConfig()
|
||||
hidden var mNextItemToUpdate = 0; // Index into the above array
|
||||
private var mHaMenu;
|
||||
private var strNoApiKey as Lang.String;
|
||||
private var strNoApiUrl as Lang.String;
|
||||
private var strNoConfigUrl as Lang.String;
|
||||
private var strNoPhone as Lang.String;
|
||||
private var strNoInternet as Lang.String;
|
||||
private var strNoResponse as Lang.String;
|
||||
private var strNoMenu as Lang.String;
|
||||
private var strApiFlood as Lang.String;
|
||||
private var strConfigUrlNotFound as Lang.String;
|
||||
private var strUnhandledHttpErr as Lang.String;
|
||||
private var strTrailingSlashErr as Lang.String;
|
||||
private var mItemsToUpdate; // Array initialised by onReturnFetchMenuConfig()
|
||||
private var mNextItemToUpdate = 0; // Index into the above array
|
||||
|
||||
function initialize() {
|
||||
AppBase.initialize();
|
||||
|
@ -24,17 +24,20 @@ using Toybox.Graphics;
|
||||
using Toybox.Application.Properties;
|
||||
|
||||
class HomeAssistantIconMenuItem extends WatchUi.IconMenuItem {
|
||||
hidden var mHomeAssistantService as HomeAssistantService;
|
||||
private var mHomeAssistantService as HomeAssistantService;
|
||||
private var mService as Lang.String;
|
||||
// private var mIdentifier as Lang.String;
|
||||
|
||||
function initialize(
|
||||
label as Lang.String or Lang.Symbol,
|
||||
subLabel as Lang.String or Lang.Symbol or Null,
|
||||
label as Lang.String or Lang.Symbol,
|
||||
subLabel as Lang.String or Lang.Symbol or Null,
|
||||
identifier as Lang.Object or Null,
|
||||
service as Lang.String or Null,
|
||||
icon as Graphics.BitmapType or WatchUi.Drawable,
|
||||
options as {
|
||||
service as Lang.String or Null,
|
||||
icon as Graphics.BitmapType or WatchUi.Drawable,
|
||||
options as {
|
||||
:alignment as WatchUi.MenuItem.Alignment
|
||||
} or Null
|
||||
} or Null,
|
||||
haService as HomeAssistantService
|
||||
) {
|
||||
WatchUi.IconMenuItem.initialize(
|
||||
label,
|
||||
@ -44,12 +47,13 @@ class HomeAssistantIconMenuItem extends WatchUi.IconMenuItem {
|
||||
options
|
||||
);
|
||||
|
||||
mHomeAssistantService = new HomeAssistantService(service, identifier);
|
||||
|
||||
mHomeAssistantService = haService;
|
||||
mIdentifier = identifier;
|
||||
mService = service;
|
||||
}
|
||||
|
||||
function callService() as Void {
|
||||
mHomeAssistantService.call();
|
||||
mHomeAssistantService.call(mIdentifier as Lang.String, mService);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,7 +24,8 @@ using Toybox.Graphics;
|
||||
using Toybox.Application.Properties;
|
||||
|
||||
class HomeAssistantMenuItem extends WatchUi.MenuItem {
|
||||
hidden var mHomeAssistantService as HomeAssistantService;
|
||||
private var mHomeAssistantService as HomeAssistantService;
|
||||
private var mService as Lang.String;
|
||||
|
||||
function initialize(
|
||||
label as Lang.String or Lang.Symbol,
|
||||
@ -34,7 +35,8 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
|
||||
options as {
|
||||
:alignment as WatchUi.MenuItem.Alignment,
|
||||
:icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol
|
||||
} or Null
|
||||
} or Null,
|
||||
haService as HomeAssistantService
|
||||
) {
|
||||
WatchUi.MenuItem.initialize(
|
||||
label,
|
||||
@ -43,11 +45,12 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
|
||||
options
|
||||
);
|
||||
|
||||
mHomeAssistantService = new HomeAssistantService(service, identifier);
|
||||
mHomeAssistantService = haService;
|
||||
mService = service;
|
||||
}
|
||||
|
||||
function callService() as Void {
|
||||
mHomeAssistantService.call();
|
||||
mHomeAssistantService.call(mIdentifier as Lang.String, mService);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,57 +23,57 @@ using Toybox.Lang;
|
||||
using Toybox.WatchUi;
|
||||
|
||||
class HomeAssistantMenuItemFactory {
|
||||
|
||||
private var mMenuItemAlignment;
|
||||
private var mLabelToggle;
|
||||
private var strMenuItemTap;
|
||||
private var bRepresentTypesWithLabels;
|
||||
|
||||
private var mTapTypeIcon;
|
||||
|
||||
private var mGroupTypeIcon;
|
||||
private var mMenuItemOptions as Lang.Dictionary;
|
||||
private var mLabelToggle as Lang.Dictionary;
|
||||
private var strMenuItemTap as Lang.String;
|
||||
private var bRepresentTypesWithLabels as Lang.Boolean;
|
||||
private var mTapTypeIcon as WatchUi.Bitmap;
|
||||
private var mGroupTypeIcon as WatchUi.Bitmap;
|
||||
private var mHomeAssistantService as HomeAssistantService;
|
||||
|
||||
private static var instance;
|
||||
|
||||
private function initialize() {
|
||||
mLabelToggle = {
|
||||
:enabled => WatchUi.loadResource($.Rez.Strings.MenuItemOn) as Lang.String,
|
||||
:disabled => WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String
|
||||
};
|
||||
|
||||
:enabled => WatchUi.loadResource($.Rez.Strings.MenuItemOn) as Lang.String,
|
||||
:disabled => WatchUi.loadResource($.Rez.Strings.MenuItemOff) as Lang.String
|
||||
};
|
||||
bRepresentTypesWithLabels = Application.Properties.getValue("types_representation") as Lang.Boolean;
|
||||
|
||||
var menuItemAlignment = Application.Properties.getValue("menu_alignment") as Lang.Boolean;
|
||||
|
||||
if(menuItemAlignment == true){
|
||||
mMenuItemAlignment = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT};
|
||||
if(menuItemAlignment){
|
||||
mMenuItemOptions = {
|
||||
:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT
|
||||
};
|
||||
} else {
|
||||
mMenuItemAlignment = {:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT};
|
||||
mMenuItemOptions = {
|
||||
:alignment => WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
strMenuItemTap = WatchUi.loadResource($.Rez.Strings.MenuItemTap);
|
||||
mTapTypeIcon = new WatchUi.Bitmap({
|
||||
:rezId=>$.Rez.Drawables.TapTypeIcon,
|
||||
:locX=>WatchUi.LAYOUT_HALIGN_CENTER,
|
||||
:locY=>WatchUi.LAYOUT_VALIGN_CENTER
|
||||
});
|
||||
:rezId => $.Rez.Drawables.TapTypeIcon,
|
||||
:locX => WatchUi.LAYOUT_HALIGN_CENTER,
|
||||
:locY => WatchUi.LAYOUT_VALIGN_CENTER
|
||||
});
|
||||
|
||||
mGroupTypeIcon = new WatchUi.Bitmap({
|
||||
:rezId=>$.Rez.Drawables.GroupTypeIcon,
|
||||
:locX=>WatchUi.LAYOUT_HALIGN_CENTER,
|
||||
:locY=>WatchUi.LAYOUT_VALIGN_CENTER
|
||||
:rezId => $.Rez.Drawables.GroupTypeIcon,
|
||||
:locX => WatchUi.LAYOUT_HALIGN_CENTER,
|
||||
:locY => WatchUi.LAYOUT_VALIGN_CENTER
|
||||
});
|
||||
mHomeAssistantService = new HomeAssistantService();
|
||||
}
|
||||
|
||||
static function create() {
|
||||
static function create() as HomeAssistantMenuItemFactory {
|
||||
if (instance == null) {
|
||||
instance = new HomeAssistantMenuItemFactory();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
function toggle(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null) as WatchUi.MenuItem{
|
||||
function toggle(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null) as WatchUi.MenuItem {
|
||||
var subLabel = null;
|
||||
|
||||
if (bRepresentTypesWithLabels == true){
|
||||
@ -85,36 +85,38 @@ class HomeAssistantMenuItemFactory {
|
||||
subLabel,
|
||||
identifier,
|
||||
false,
|
||||
mMenuItemAlignment
|
||||
mMenuItemOptions
|
||||
);
|
||||
}
|
||||
|
||||
function tap(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null, service as Lang.String or Null) as WatchUi.MenuItem{
|
||||
function tap(label as Lang.String or Lang.Symbol, identifier as Lang.Object or Null, service as Lang.String or Null) as WatchUi.MenuItem {
|
||||
if (bRepresentTypesWithLabels) {
|
||||
return new HomeAssistantMenuItem(
|
||||
label,
|
||||
strMenuItemTap,
|
||||
identifier,
|
||||
service,
|
||||
mMenuItemAlignment
|
||||
);
|
||||
label,
|
||||
strMenuItemTap,
|
||||
identifier,
|
||||
service,
|
||||
mMenuItemOptions,
|
||||
mHomeAssistantService
|
||||
);
|
||||
} else {
|
||||
return new HomeAssistantIconMenuItem(
|
||||
label,
|
||||
null,
|
||||
identifier,
|
||||
service,
|
||||
mTapTypeIcon,
|
||||
mMenuItemAlignment
|
||||
);
|
||||
label,
|
||||
null,
|
||||
identifier,
|
||||
service,
|
||||
mTapTypeIcon,
|
||||
mMenuItemOptions,
|
||||
mHomeAssistantService
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function group(definition as Lang.Dictionary) as WatchUi.MenuItem{
|
||||
function group(definition as Lang.Dictionary) as WatchUi.MenuItem {
|
||||
if (bRepresentTypesWithLabels) {
|
||||
return new HomeAssistantViewMenuItem(definition);
|
||||
} else {
|
||||
return new HomeAssistantViewIconMenuItem(definition, mGroupTypeIcon, mMenuItemAlignment);
|
||||
return new HomeAssistantViewIconMenuItem(definition, mGroupTypeIcon, mMenuItemOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,21 +23,16 @@ using Toybox.WatchUi;
|
||||
using Toybox.Graphics;
|
||||
using Toybox.Application.Properties;
|
||||
|
||||
class HomeAssistantService{
|
||||
hidden var mApiKey as Lang.String;
|
||||
hidden var strNoPhone as Lang.String;
|
||||
hidden var strNoInternet as Lang.String;
|
||||
hidden var strNoResponse as Lang.String;
|
||||
hidden var strApiFlood as Lang.String;
|
||||
hidden var strApiUrlNotFound as Lang.String;
|
||||
hidden var strUnhandledHttpErr as Lang.String;
|
||||
hidden var mService as Lang.String;
|
||||
hidden var mIdentifier as Lang.Object;
|
||||
class HomeAssistantService {
|
||||
private var mApiKey as Lang.String;
|
||||
private var strNoPhone as Lang.String;
|
||||
private var strNoInternet as Lang.String;
|
||||
private var strNoResponse as Lang.String;
|
||||
private var strApiFlood as Lang.String;
|
||||
private var strApiUrlNotFound as Lang.String;
|
||||
private var strUnhandledHttpErr as Lang.String;
|
||||
|
||||
function initialize(
|
||||
service as Lang.String or Null,
|
||||
identifier as Lang.Object or Null
|
||||
) {
|
||||
function initialize() {
|
||||
strNoPhone = WatchUi.loadResource($.Rez.Strings.NoPhone);
|
||||
strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet);
|
||||
strNoResponse = WatchUi.loadResource($.Rez.Strings.NoResponse);
|
||||
@ -45,14 +40,13 @@ class HomeAssistantService{
|
||||
strApiUrlNotFound = WatchUi.loadResource($.Rez.Strings.ApiUrlNotFound);
|
||||
strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr);
|
||||
mApiKey = Properties.getValue("api_key");
|
||||
mService = service;
|
||||
mIdentifier = identifier;
|
||||
}
|
||||
|
||||
// Callback function after completing the POST request to call a service.
|
||||
//
|
||||
function onReturnCall(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
|
||||
if (Globals.scDebug) {
|
||||
function onReturnCall(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String, context as Lang.Object) as Void {
|
||||
var identifier = context as Lang.String;
|
||||
if (Globals.scDebug) {
|
||||
System.println("HomeAssistantService onReturnCall() Response Code: " + responseCode);
|
||||
System.println("HomeAssistantService onReturnCall() Response Data: " + data);
|
||||
}
|
||||
@ -86,7 +80,7 @@ class HomeAssistantService{
|
||||
var d = data as Lang.Array;
|
||||
var toast = "Executed";
|
||||
for(var i = 0; i < d.size(); i++) {
|
||||
if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) {
|
||||
if ((d[i].get("entity_id") as Lang.String).equals(identifier)) {
|
||||
toast = (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String;
|
||||
}
|
||||
}
|
||||
@ -109,14 +103,15 @@ class HomeAssistantService{
|
||||
}
|
||||
}
|
||||
|
||||
function call() as Void {
|
||||
function call(identifier as Lang.String, service as Lang.String) as Void {
|
||||
var options = {
|
||||
:method => Communications.HTTP_REQUEST_METHOD_POST,
|
||||
:headers => {
|
||||
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON,
|
||||
"Authorization" => "Bearer " + mApiKey
|
||||
},
|
||||
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
|
||||
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
|
||||
:context => identifier
|
||||
};
|
||||
if (! System.getDeviceSettings().phoneConnected) {
|
||||
if (Globals.scDebug) {
|
||||
@ -129,36 +124,19 @@ class HomeAssistantService{
|
||||
}
|
||||
WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP);
|
||||
} else {
|
||||
// Updated SDK and got a new error
|
||||
// ERROR: venu: Cannot find symbol ':substring' on type 'PolyType<Null or $.Toybox.Lang.Object>'.
|
||||
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, null);
|
||||
if (Globals.scDebug) {
|
||||
System.println("HomeAssistantService call() URL=" + url);
|
||||
System.println("HomeAssistantService call() mIdentifier=" + mIdentifier);
|
||||
}
|
||||
Communications.makeWebRequest(
|
||||
url,
|
||||
null,
|
||||
options,
|
||||
method(:onReturnCall)
|
||||
);
|
||||
} else {
|
||||
var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null);
|
||||
if (Globals.scDebug) {
|
||||
System.println("HomeAssistantService call() URL=" + url);
|
||||
System.println("HomeAssistantService call() mService=" + mService);
|
||||
}
|
||||
Communications.makeWebRequest(
|
||||
url,
|
||||
{
|
||||
"entity_id" => id
|
||||
},
|
||||
options,
|
||||
method(:onReturnCall)
|
||||
);
|
||||
var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + service.substring(0, service.find(".")) + "/" + service.substring(service.find(".")+1, null);
|
||||
if (Globals.scDebug) {
|
||||
System.println("HomeAssistantService call() URL=" + url);
|
||||
System.println("HomeAssistantService call() service=" + service);
|
||||
}
|
||||
Communications.makeWebRequest(
|
||||
url,
|
||||
{
|
||||
"entity_id" => identifier
|
||||
},
|
||||
options,
|
||||
method(:onReturnCall)
|
||||
);
|
||||
if (Attention has :vibrate) {
|
||||
Attention.vibrate([
|
||||
new Attention.VibeProfile(50, 100), // On for 100ms
|
||||
|
@ -25,23 +25,23 @@ using Toybox.Application.Properties;
|
||||
using Toybox.Timer;
|
||||
|
||||
class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
|
||||
hidden var mApiKey as Lang.String;
|
||||
hidden var strNoPhone as Lang.String;
|
||||
hidden var strNoInternet as Lang.String;
|
||||
hidden var strNoResponse as Lang.String;
|
||||
hidden var strApiFlood as Lang.String;
|
||||
hidden var strApiUrlNotFound as Lang.String;
|
||||
hidden var strUnhandledHttpErr as Lang.String;
|
||||
private var mApiKey as Lang.String;
|
||||
private var strNoPhone as Lang.String;
|
||||
private var strNoInternet as Lang.String;
|
||||
private var strNoResponse as Lang.String;
|
||||
private var strApiFlood as Lang.String;
|
||||
private var strApiUrlNotFound as Lang.String;
|
||||
private var strUnhandledHttpErr as Lang.String;
|
||||
|
||||
function initialize(
|
||||
label as Lang.String or Lang.Symbol,
|
||||
subLabel as Lang.String or Lang.Symbol or {
|
||||
label as Lang.String or Lang.Symbol,
|
||||
subLabel as Lang.String or Lang.Symbol or {
|
||||
:enabled as Lang.String or Lang.Symbol or Null,
|
||||
:disabled as Lang.String or Lang.Symbol or Null
|
||||
} or Null,
|
||||
identifier,
|
||||
enabled as Lang.Boolean,
|
||||
options as {
|
||||
enabled as Lang.Boolean,
|
||||
options as {
|
||||
:alignment as WatchUi.MenuItem.Alignment,
|
||||
:icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol
|
||||
} or Null
|
||||
|
@ -24,10 +24,9 @@ using Toybox.Graphics;
|
||||
using Toybox.WatchUi;
|
||||
|
||||
class HomeAssistantView extends WatchUi.Menu2 {
|
||||
|
||||
// List of items that need to have their status updated periodically
|
||||
hidden var mListToggleItems = [];
|
||||
hidden var mListMenuItems = [];
|
||||
private var mListToggleItems = [];
|
||||
private var mListMenuItems = [];
|
||||
|
||||
function initialize(
|
||||
definition as Lang.Dictionary,
|
||||
|
@ -22,7 +22,7 @@ using Toybox.Lang;
|
||||
using Toybox.WatchUi;
|
||||
|
||||
class HomeAssistantViewIconMenuItem extends WatchUi.IconMenuItem {
|
||||
hidden var mMenu as HomeAssistantView;
|
||||
private var mMenu as HomeAssistantView;
|
||||
|
||||
function initialize(definition as Lang.Dictionary, icon as WatchUi.Drawable, options as {
|
||||
:alignment as WatchUi.MenuItem.Alignment
|
||||
|
@ -22,7 +22,7 @@ using Toybox.Lang;
|
||||
using Toybox.WatchUi;
|
||||
|
||||
class HomeAssistantViewMenuItem extends WatchUi.MenuItem {
|
||||
hidden var mMenu as HomeAssistantView;
|
||||
private var mMenu as HomeAssistantView;
|
||||
|
||||
function initialize(definition as Lang.Dictionary) {
|
||||
// definitions.get(...) are Strings here as they have been checked by HomeAssistantView first
|
||||
|
@ -23,7 +23,7 @@ using Toybox.WatchUi;
|
||||
using Toybox.Math;
|
||||
|
||||
class ScalableView extends WatchUi.View {
|
||||
hidden var mScreenWidth;
|
||||
private var mScreenWidth;
|
||||
|
||||
function initialize() {
|
||||
View.initialize();
|
||||
|
Reference in New Issue
Block a user