mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-04-30 20:52:27 +00:00
Removal of menu identifiers as not actually required
Toggle menu is now consistent with the use of data objects in tap and template. HA 'notify' actions now work. Co-Authored-By: Joseph Abbey <me@josephabbey.dev>
This commit is contained in:
@ -31,13 +31,10 @@ class HomeAssistantGroupMenuItem extends WatchUi.IconMenuItem {
|
||||
:alignment as WatchUi.MenuItem.Alignment
|
||||
} or Null) {
|
||||
|
||||
var label = definition.get("name") as Lang.String;
|
||||
var identifier = definition.get("entity") as Lang.String;
|
||||
|
||||
WatchUi.IconMenuItem.initialize(
|
||||
label,
|
||||
definition.get("name") as Lang.String,
|
||||
null,
|
||||
null,
|
||||
identifier,
|
||||
icon,
|
||||
options
|
||||
);
|
||||
|
@ -64,27 +64,31 @@ class 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, entity_id as Lang.String or Null) as WatchUi.MenuItem {
|
||||
return new HomeAssistantToggleMenuItem(
|
||||
label,
|
||||
null,
|
||||
identifier,
|
||||
false,
|
||||
{ "entity_id" => entity_id },
|
||||
mMenuItemOptions
|
||||
);
|
||||
}
|
||||
|
||||
function template_tap(
|
||||
label as Lang.String or Lang.Symbol,
|
||||
identifier as Lang.Object or Null,
|
||||
entity as Lang.String or Null,
|
||||
template as Lang.String or Null,
|
||||
service as Lang.String or Null,
|
||||
confirm as Lang.Boolean,
|
||||
data as Lang.Dictionary or Null
|
||||
) as WatchUi.MenuItem {
|
||||
if (entity != null) {
|
||||
if (data == null) {
|
||||
data = { "entity_id" => entity };
|
||||
} else {
|
||||
data.put("entity_id", entity);
|
||||
}
|
||||
}
|
||||
return new HomeAssistantTemplateMenuItem(
|
||||
label,
|
||||
identifier,
|
||||
template,
|
||||
service,
|
||||
confirm,
|
||||
@ -96,20 +100,15 @@ class HomeAssistantMenuItemFactory {
|
||||
}
|
||||
|
||||
function template_notap(
|
||||
label as Lang.String or Lang.Symbol,
|
||||
identifier as Lang.Object or Null,
|
||||
template as Lang.String or Null,
|
||||
service as Lang.String or Null,
|
||||
confirm as Lang.Boolean,
|
||||
data as Lang.Dictionary or Null
|
||||
label as Lang.String or Lang.Symbol,
|
||||
template as Lang.String or Null
|
||||
) as WatchUi.MenuItem {
|
||||
return new HomeAssistantTemplateMenuItem(
|
||||
label,
|
||||
identifier,
|
||||
template,
|
||||
service,
|
||||
confirm,
|
||||
data,
|
||||
null,
|
||||
false,
|
||||
null,
|
||||
mInfoTypeIcon,
|
||||
mMenuItemOptions,
|
||||
mHomeAssistantService
|
||||
@ -118,15 +117,20 @@ class HomeAssistantMenuItemFactory {
|
||||
|
||||
function tap(
|
||||
label as Lang.String or Lang.Symbol,
|
||||
identifier as Lang.Object or Null,
|
||||
entity as Lang.String or Null,
|
||||
service as Lang.String or Null,
|
||||
confirm as Lang.Boolean,
|
||||
data as Lang.Dictionary or Null
|
||||
) as WatchUi.MenuItem {
|
||||
if (entity != null) {
|
||||
if (data == null) {
|
||||
data = { "entity_id" => entity };
|
||||
} else {
|
||||
data.put("entity_id", entity);
|
||||
}
|
||||
}
|
||||
return new HomeAssistantTapMenuItem(
|
||||
label,
|
||||
null,
|
||||
identifier,
|
||||
service,
|
||||
confirm,
|
||||
data,
|
||||
|
@ -43,7 +43,7 @@ class HomeAssistantService {
|
||||
data as Null or Lang.Dictionary or Lang.String,
|
||||
context as Lang.Object
|
||||
) as Void {
|
||||
var identifier = context as Lang.String;
|
||||
var entity_id = context as Lang.String or Null;
|
||||
if (Globals.scDebug) {
|
||||
System.println("HomeAssistantService onReturnCall() Response Code: " + responseCode);
|
||||
System.println("HomeAssistantService onReturnCall() Response Data: " + data);
|
||||
@ -99,7 +99,7 @@ class HomeAssistantService {
|
||||
var d = data as Lang.Array;
|
||||
var toast = RezStrings.getExecuted();
|
||||
for(var i = 0; i < d.size(); i++) {
|
||||
if ((d[i].get("entity_id") as Lang.String).equals(identifier)) {
|
||||
if ((d[i].get("entity_id") as Lang.String).equals(entity_id)) {
|
||||
toast = (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String;
|
||||
}
|
||||
}
|
||||
@ -125,9 +125,8 @@ class HomeAssistantService {
|
||||
}
|
||||
|
||||
function call(
|
||||
identifier as Lang.String,
|
||||
service as Lang.String,
|
||||
data as Lang.Dictionary or Null
|
||||
service as Lang.String,
|
||||
data as Lang.Dictionary or Null
|
||||
) as Void {
|
||||
if (! System.getDeviceSettings().phoneConnected) {
|
||||
if (Globals.scDebug) {
|
||||
@ -146,9 +145,15 @@ class HomeAssistantService {
|
||||
System.println("HomeAssistantService call() URL=" + url);
|
||||
System.println("HomeAssistantService call() service=" + service);
|
||||
}
|
||||
|
||||
var entity_id = data.get("entity_id");
|
||||
if (entity_id == null) {
|
||||
entity_id = "";
|
||||
}
|
||||
|
||||
Communications.makeWebRequest(
|
||||
url,
|
||||
data, // Includes {"entity_id": identifier}
|
||||
data, // Includes {"entity_id": xxxx}
|
||||
{
|
||||
:method => Communications.HTTP_REQUEST_METHOD_POST,
|
||||
:headers => {
|
||||
@ -156,7 +161,7 @@ class HomeAssistantService {
|
||||
"Authorization" => "Bearer " + Settings.getApiKey()
|
||||
},
|
||||
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
|
||||
:context => identifier
|
||||
:context => entity_id
|
||||
},
|
||||
method(:onReturnCall)
|
||||
);
|
||||
|
@ -30,8 +30,6 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem {
|
||||
|
||||
function initialize(
|
||||
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,
|
||||
confirm as Lang.Boolean,
|
||||
data as Lang.Dictionary or Null,
|
||||
@ -43,8 +41,8 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem {
|
||||
) {
|
||||
WatchUi.IconMenuItem.initialize(
|
||||
label,
|
||||
subLabel,
|
||||
identifier,
|
||||
null,
|
||||
null,
|
||||
icon,
|
||||
options
|
||||
);
|
||||
@ -53,11 +51,6 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem {
|
||||
mService = service;
|
||||
mConfirm = confirm;
|
||||
mData = data;
|
||||
if (mData == null) {
|
||||
mData = {"entity_id" => identifier};
|
||||
} else {
|
||||
mData.put("entity_id", identifier);
|
||||
}
|
||||
}
|
||||
|
||||
function callService() as Void {
|
||||
@ -73,7 +66,7 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem {
|
||||
}
|
||||
|
||||
function onConfirm() as Void {
|
||||
mHomeAssistantService.call(mIdentifier as Lang.String, mService, mData);
|
||||
mHomeAssistantService.call(mService, mData);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -35,21 +35,20 @@ class HomeAssistantTemplateMenuItem extends WatchUi.IconMenuItem {
|
||||
|
||||
function initialize(
|
||||
label as Lang.String or Lang.Symbol,
|
||||
identifier as Lang.Object or Null,
|
||||
template as Lang.String,
|
||||
service as Lang.String or Null,
|
||||
confirm as Lang.Boolean,
|
||||
data as Lang.Dictionary or Null,
|
||||
icon as Graphics.BitmapType or WatchUi.Drawable,
|
||||
options as {
|
||||
:alignment as WatchUi.MenuItem.Alignment,
|
||||
:alignment as WatchUi.MenuItem.Alignment
|
||||
} or Null,
|
||||
haService as HomeAssistantService
|
||||
) {
|
||||
WatchUi.IconMenuItem.initialize(
|
||||
label,
|
||||
null,
|
||||
identifier,
|
||||
null,
|
||||
icon,
|
||||
options
|
||||
);
|
||||
@ -59,11 +58,6 @@ class HomeAssistantTemplateMenuItem extends WatchUi.IconMenuItem {
|
||||
mService = service;
|
||||
mConfirm = confirm;
|
||||
mData = data;
|
||||
if (mData == null) {
|
||||
mData = {"entity_id" => identifier};
|
||||
} else {
|
||||
mData.put("entity_id", identifier);
|
||||
}
|
||||
}
|
||||
|
||||
function callService() as Void {
|
||||
@ -80,7 +74,7 @@ class HomeAssistantTemplateMenuItem extends WatchUi.IconMenuItem {
|
||||
|
||||
function onConfirm() as Void {
|
||||
if (mService != null) {
|
||||
mHomeAssistantService.call(mIdentifier as Lang.String, mService, mData);
|
||||
mHomeAssistantService.call(mService, mData);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,21 +25,18 @@ using Toybox.Application.Properties;
|
||||
using Toybox.Timer;
|
||||
|
||||
class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
|
||||
private var mData as Lang.Dictionary;
|
||||
|
||||
function initialize(
|
||||
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,
|
||||
data as Lang.Dictionary or Null,
|
||||
options as {
|
||||
:alignment as WatchUi.MenuItem.Alignment,
|
||||
:icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol
|
||||
} or Null
|
||||
) {
|
||||
WatchUi.ToggleMenuItem.initialize(label, subLabel, identifier, enabled, options);
|
||||
WatchUi.ToggleMenuItem.initialize(label, null, null, false, options);
|
||||
mData = data;
|
||||
}
|
||||
|
||||
private function setUiToggle(state as Null or Lang.String) as Void {
|
||||
@ -114,9 +111,9 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
|
||||
if (msg != null) {
|
||||
// Should be an HTTP 404 according to curl queries
|
||||
if (Globals.scDebug) {
|
||||
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: 404. " + mIdentifier + " " + msg);
|
||||
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: 404. " + mData.get("entity_id") + " " + msg);
|
||||
}
|
||||
ErrorView.show("HTTP 404, " + mIdentifier + ". " + data.get("message"));
|
||||
ErrorView.show("HTTP 404, " + mData.get("entity_id") + ". " + data.get("message"));
|
||||
} else {
|
||||
if (Globals.scDebug) {
|
||||
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: 404, page not found. Check API URL setting.");
|
||||
@ -127,9 +124,9 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
|
||||
|
||||
case 405:
|
||||
if (Globals.scDebug) {
|
||||
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: 405. " + mIdentifier + " " + data.get("message"));
|
||||
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: 405. " + mData.get("entity_id") + " " + data.get("message"));
|
||||
}
|
||||
ErrorView.show("HTTP 405, " + mIdentifier + ". " + data.get("message"));
|
||||
ErrorView.show("HTTP 405, " + mData.get("entity_id") + ". " + data.get("message"));
|
||||
|
||||
break;
|
||||
|
||||
@ -170,7 +167,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
|
||||
ErrorView.show(RezStrings.getNoInternet() + ".");
|
||||
getApp().setApiStatus(RezStrings.getUnavailable());
|
||||
} else {
|
||||
var url = Settings.getApiUrl() + "/states/" + mIdentifier;
|
||||
var url = Settings.getApiUrl() + "/states/" + mData.get("entity_id");
|
||||
if (Globals.scDebug) {
|
||||
System.println("HomeAssistantToggleMenuItem getState() URL=" + url);
|
||||
}
|
||||
@ -239,7 +236,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
|
||||
var state;
|
||||
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 ((d[i].get("entity_id") as Lang.String).equals(mData.get("entity_id"))) {
|
||||
state = d[i].get("state") as Lang.String;
|
||||
if (Globals.scDebug) {
|
||||
System.println((d[i].get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state);
|
||||
@ -277,7 +274,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
|
||||
} 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;
|
||||
var id = mData.get("entity_id") as Lang.String;
|
||||
var url = Settings.getApiUrl() + "/services/";
|
||||
if (s) {
|
||||
url = url + id.substring(0, id.find(".")) + "/turn_on";
|
||||
@ -285,14 +282,12 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
|
||||
url = url + id.substring(0, id.find(".")) + "/turn_off";
|
||||
}
|
||||
if (Globals.scDebug) {
|
||||
System.println("HomeAssistantToggleMenuItem setState() URL=" + url);
|
||||
System.println("HomeAssistantToggleMenuItem setState() mIdentifier=" + mIdentifier);
|
||||
System.println("HomeAssistantToggleMenuItem setState() URL = " + url);
|
||||
System.println("HomeAssistantToggleMenuItem setState() entity_id = " + id);
|
||||
}
|
||||
Communications.makeWebRequest(
|
||||
url,
|
||||
{
|
||||
"entity_id" => mIdentifier
|
||||
},
|
||||
mData,
|
||||
{
|
||||
:method => Communications.HTTP_REQUEST_METHOD_POST,
|
||||
:headers => {
|
||||
|
@ -66,13 +66,13 @@ class HomeAssistantView extends WatchUi.Menu2 {
|
||||
if (type.equals("toggle") && entity != null) {
|
||||
addItem(HomeAssistantMenuItemFactory.create().toggle(name, entity));
|
||||
} else if (type.equals("template") && content != null) {
|
||||
if (tap_action == null) {
|
||||
addItem(HomeAssistantMenuItemFactory.create().template_notap(name, entity, content, service, confirm, data));
|
||||
if (service == null) {
|
||||
addItem(HomeAssistantMenuItemFactory.create().template_notap(name, content));
|
||||
} else {
|
||||
addItem(HomeAssistantMenuItemFactory.create().template_tap(name, entity, content, service, confirm, data));
|
||||
}
|
||||
|
||||
} else if (type.equals("tap") && entity != null && service != null) {
|
||||
} else if (type.equals("tap") && service != null) {
|
||||
addItem(HomeAssistantMenuItemFactory.create().tap(name, entity, service, confirm, data));
|
||||
} else if (type.equals("group")) {
|
||||
addItem(HomeAssistantMenuItemFactory.create().group(items[i]));
|
||||
|
Reference in New Issue
Block a user