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:
Philip Abbey
2024-01-19 18:04:55 +00:00
parent 67a5f0a14e
commit 2be255bb71
7 changed files with 60 additions and 72 deletions

View File

@ -31,13 +31,10 @@ class HomeAssistantGroupMenuItem extends WatchUi.IconMenuItem {
:alignment as WatchUi.MenuItem.Alignment :alignment as WatchUi.MenuItem.Alignment
} or Null) { } or Null) {
var label = definition.get("name") as Lang.String;
var identifier = definition.get("entity") as Lang.String;
WatchUi.IconMenuItem.initialize( WatchUi.IconMenuItem.initialize(
label, definition.get("name") as Lang.String,
null,
null, null,
identifier,
icon, icon,
options options
); );

View File

@ -64,27 +64,31 @@ class HomeAssistantMenuItemFactory {
return instance; 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( return new HomeAssistantToggleMenuItem(
label, label,
null, { "entity_id" => entity_id },
identifier,
false,
mMenuItemOptions mMenuItemOptions
); );
} }
function template_tap( function template_tap(
label as Lang.String or Lang.Symbol, 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, template as Lang.String or Null,
service as Lang.String or Null, service as Lang.String or Null,
confirm as Lang.Boolean, confirm as Lang.Boolean,
data as Lang.Dictionary or Null data as Lang.Dictionary or Null
) as WatchUi.MenuItem { ) as WatchUi.MenuItem {
if (entity != null) {
if (data == null) {
data = { "entity_id" => entity };
} else {
data.put("entity_id", entity);
}
}
return new HomeAssistantTemplateMenuItem( return new HomeAssistantTemplateMenuItem(
label, label,
identifier,
template, template,
service, service,
confirm, confirm,
@ -96,20 +100,15 @@ class HomeAssistantMenuItemFactory {
} }
function template_notap( function template_notap(
label as Lang.String or Lang.Symbol, label as Lang.String or Lang.Symbol,
identifier as Lang.Object or Null, template 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 { ) as WatchUi.MenuItem {
return new HomeAssistantTemplateMenuItem( return new HomeAssistantTemplateMenuItem(
label, label,
identifier,
template, template,
service, null,
confirm, false,
data, null,
mInfoTypeIcon, mInfoTypeIcon,
mMenuItemOptions, mMenuItemOptions,
mHomeAssistantService mHomeAssistantService
@ -118,15 +117,20 @@ class HomeAssistantMenuItemFactory {
function tap( function tap(
label as Lang.String or Lang.Symbol, 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, service as Lang.String or Null,
confirm as Lang.Boolean, confirm as Lang.Boolean,
data as Lang.Dictionary or Null data as Lang.Dictionary or Null
) as WatchUi.MenuItem { ) as WatchUi.MenuItem {
if (entity != null) {
if (data == null) {
data = { "entity_id" => entity };
} else {
data.put("entity_id", entity);
}
}
return new HomeAssistantTapMenuItem( return new HomeAssistantTapMenuItem(
label, label,
null,
identifier,
service, service,
confirm, confirm,
data, data,

View File

@ -43,7 +43,7 @@ class HomeAssistantService {
data as Null or Lang.Dictionary or Lang.String, data as Null or Lang.Dictionary or Lang.String,
context as Lang.Object context as Lang.Object
) as Void { ) as Void {
var identifier = context as Lang.String; var entity_id = context as Lang.String or Null;
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantService onReturnCall() Response Code: " + responseCode); System.println("HomeAssistantService onReturnCall() Response Code: " + responseCode);
System.println("HomeAssistantService onReturnCall() Response Data: " + data); System.println("HomeAssistantService onReturnCall() Response Data: " + data);
@ -99,7 +99,7 @@ class HomeAssistantService {
var d = data as Lang.Array; var d = data as Lang.Array;
var toast = RezStrings.getExecuted(); var toast = RezStrings.getExecuted();
for(var i = 0; i < d.size(); i++) { 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; toast = (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String;
} }
} }
@ -125,9 +125,8 @@ class HomeAssistantService {
} }
function call( function call(
identifier as Lang.String, service as Lang.String,
service as Lang.String, data as Lang.Dictionary or Null
data as Lang.Dictionary or Null
) as Void { ) as Void {
if (! System.getDeviceSettings().phoneConnected) { if (! System.getDeviceSettings().phoneConnected) {
if (Globals.scDebug) { if (Globals.scDebug) {
@ -146,9 +145,15 @@ class HomeAssistantService {
System.println("HomeAssistantService call() URL=" + url); System.println("HomeAssistantService call() URL=" + url);
System.println("HomeAssistantService call() service=" + service); System.println("HomeAssistantService call() service=" + service);
} }
var entity_id = data.get("entity_id");
if (entity_id == null) {
entity_id = "";
}
Communications.makeWebRequest( Communications.makeWebRequest(
url, url,
data, // Includes {"entity_id": identifier} data, // Includes {"entity_id": xxxx}
{ {
:method => Communications.HTTP_REQUEST_METHOD_POST, :method => Communications.HTTP_REQUEST_METHOD_POST,
:headers => { :headers => {
@ -156,7 +161,7 @@ class HomeAssistantService {
"Authorization" => "Bearer " + Settings.getApiKey() "Authorization" => "Bearer " + Settings.getApiKey()
}, },
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON, :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
:context => identifier :context => entity_id
}, },
method(:onReturnCall) method(:onReturnCall)
); );

View File

@ -30,8 +30,6 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem {
function initialize( function initialize(
label as Lang.String or Lang.Symbol, 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, service as Lang.String or Null,
confirm as Lang.Boolean, confirm as Lang.Boolean,
data as Lang.Dictionary or Null, data as Lang.Dictionary or Null,
@ -43,8 +41,8 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem {
) { ) {
WatchUi.IconMenuItem.initialize( WatchUi.IconMenuItem.initialize(
label, label,
subLabel, null,
identifier, null,
icon, icon,
options options
); );
@ -53,11 +51,6 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem {
mService = service; mService = service;
mConfirm = confirm; mConfirm = confirm;
mData = data; mData = data;
if (mData == null) {
mData = {"entity_id" => identifier};
} else {
mData.put("entity_id", identifier);
}
} }
function callService() as Void { function callService() as Void {
@ -73,7 +66,7 @@ class HomeAssistantTapMenuItem extends WatchUi.IconMenuItem {
} }
function onConfirm() as Void { function onConfirm() as Void {
mHomeAssistantService.call(mIdentifier as Lang.String, mService, mData); mHomeAssistantService.call(mService, mData);
} }
} }

View File

@ -35,21 +35,20 @@ class HomeAssistantTemplateMenuItem extends WatchUi.IconMenuItem {
function initialize( function initialize(
label as Lang.String or Lang.Symbol, label as Lang.String or Lang.Symbol,
identifier as Lang.Object or Null,
template as Lang.String, template as Lang.String,
service as Lang.String or Null, service as Lang.String or Null,
confirm as Lang.Boolean, confirm as Lang.Boolean,
data as Lang.Dictionary or Null, data as Lang.Dictionary or Null,
icon as Graphics.BitmapType or WatchUi.Drawable, icon as Graphics.BitmapType or WatchUi.Drawable,
options as { options as {
:alignment as WatchUi.MenuItem.Alignment, :alignment as WatchUi.MenuItem.Alignment
} or Null, } or Null,
haService as HomeAssistantService haService as HomeAssistantService
) { ) {
WatchUi.IconMenuItem.initialize( WatchUi.IconMenuItem.initialize(
label, label,
null, null,
identifier, null,
icon, icon,
options options
); );
@ -59,11 +58,6 @@ class HomeAssistantTemplateMenuItem extends WatchUi.IconMenuItem {
mService = service; mService = service;
mConfirm = confirm; mConfirm = confirm;
mData = data; mData = data;
if (mData == null) {
mData = {"entity_id" => identifier};
} else {
mData.put("entity_id", identifier);
}
} }
function callService() as Void { function callService() as Void {
@ -80,7 +74,7 @@ class HomeAssistantTemplateMenuItem extends WatchUi.IconMenuItem {
function onConfirm() as Void { function onConfirm() as Void {
if (mService != null) { if (mService != null) {
mHomeAssistantService.call(mIdentifier as Lang.String, mService, mData); mHomeAssistantService.call(mService, mData);
} }
} }

View File

@ -25,21 +25,18 @@ using Toybox.Application.Properties;
using Toybox.Timer; using Toybox.Timer;
class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
private var mData as Lang.Dictionary;
function initialize( function initialize(
label as Lang.String or Lang.Symbol, label as Lang.String or Lang.Symbol,
subLabel as Lang.String or Lang.Symbol or { data as Lang.Dictionary or Null,
: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 { options as {
:alignment as WatchUi.MenuItem.Alignment, :alignment as WatchUi.MenuItem.Alignment,
:icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol :icon as Graphics.BitmapType or WatchUi.Drawable or Lang.Symbol
} or Null } 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 { private function setUiToggle(state as Null or Lang.String) as Void {
@ -114,9 +111,9 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
if (msg != null) { if (msg != null) {
// Should be an HTTP 404 according to curl queries // Should be an HTTP 404 according to curl queries
if (Globals.scDebug) { 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 { } else {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: 404, page not found. Check API URL setting."); 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: case 405:
if (Globals.scDebug) { 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; break;
@ -170,7 +167,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
ErrorView.show(RezStrings.getNoInternet() + "."); ErrorView.show(RezStrings.getNoInternet() + ".");
getApp().setApiStatus(RezStrings.getUnavailable()); getApp().setApiStatus(RezStrings.getUnavailable());
} else { } else {
var url = Settings.getApiUrl() + "/states/" + mIdentifier; var url = Settings.getApiUrl() + "/states/" + mData.get("entity_id");
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem getState() URL=" + url); System.println("HomeAssistantToggleMenuItem getState() URL=" + url);
} }
@ -239,7 +236,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
var state; var state;
var d = data as Lang.Array; var d = data as Lang.Array;
for(var i = 0; i < d.size(); i++) { 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; state = d[i].get("state") as Lang.String;
if (Globals.scDebug) { if (Globals.scDebug) {
System.println((d[i].get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state); System.println((d[i].get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state);
@ -277,7 +274,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
} else { } else {
// Updated SDK and got a new error // Updated SDK and got a new error
// ERROR: venu: Cannot find symbol ':substring' on type 'PolyType<Null or $.Toybox.Lang.Object>'. // 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/"; var url = Settings.getApiUrl() + "/services/";
if (s) { if (s) {
url = url + id.substring(0, id.find(".")) + "/turn_on"; 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"; url = url + id.substring(0, id.find(".")) + "/turn_off";
} }
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem setState() URL=" + url); System.println("HomeAssistantToggleMenuItem setState() URL = " + url);
System.println("HomeAssistantToggleMenuItem setState() mIdentifier=" + mIdentifier); System.println("HomeAssistantToggleMenuItem setState() entity_id = " + id);
} }
Communications.makeWebRequest( Communications.makeWebRequest(
url, url,
{ mData,
"entity_id" => mIdentifier
},
{ {
:method => Communications.HTTP_REQUEST_METHOD_POST, :method => Communications.HTTP_REQUEST_METHOD_POST,
:headers => { :headers => {

View File

@ -66,13 +66,13 @@ class HomeAssistantView extends WatchUi.Menu2 {
if (type.equals("toggle") && entity != null) { if (type.equals("toggle") && entity != null) {
addItem(HomeAssistantMenuItemFactory.create().toggle(name, entity)); addItem(HomeAssistantMenuItemFactory.create().toggle(name, entity));
} else if (type.equals("template") && content != null) { } else if (type.equals("template") && content != null) {
if (tap_action == null) { if (service == null) {
addItem(HomeAssistantMenuItemFactory.create().template_notap(name, entity, content, service, confirm, data)); addItem(HomeAssistantMenuItemFactory.create().template_notap(name, content));
} else { } else {
addItem(HomeAssistantMenuItemFactory.create().template_tap(name, entity, content, service, confirm, data)); 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)); addItem(HomeAssistantMenuItemFactory.create().tap(name, entity, service, confirm, data));
} else if (type.equals("group")) { } else if (type.equals("group")) {
addItem(HomeAssistantMenuItemFactory.create().group(items[i])); addItem(HomeAssistantMenuItemFactory.create().group(items[i]));