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:
Philip Abbey
2023-11-20 21:32:35 +00:00
parent bcdcfdc66c
commit ce90d9d47f
12 changed files with 136 additions and 150 deletions

View File

@ -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