Changes required for 2-tap-or-toggle-dont-work-on-venu-2

Made the 'service' field for taps mandatory.
This makes scripts work for both the emulator and real devices.
Provide a response for automations started with a tap, and now they can be used for both tap and toggle.
This commit is contained in:
Philip Abbey
2023-11-14 08:35:38 +00:00
parent c57324f7ad
commit 2ccd2bfbff
3 changed files with 47 additions and 29 deletions

View File

@ -34,7 +34,8 @@ Example schema as shown in the images:
{ {
"entity": "script.food_on_table", "entity": "script.food_on_table",
"name": "Food is Ready!", "name": "Food is Ready!",
"type": "tap" "type": "tap",
"service" : "script.turn_on"
}, },
{ {
"entity": "light.bedside_light_switch", "entity": "light.bedside_light_switch",
@ -79,6 +80,12 @@ Example schema as shown in the images:
"name": "Garage Door Check", "name": "Garage Door Check",
"type": "toggle" "type": "toggle"
}, },
{
"entity": "automation.turn_off_usb_chargers",
"name": "Turn off USBs",
"type": "tap",
"service" : "automation.trigger"
},
{ {
"entity": "scene.tv_light", "entity": "scene.tv_light",
"name": "TV Lights Scene", "name": "TV Lights Scene",
@ -99,6 +106,18 @@ The example above illustrates how to configure:
* Service invocation, e.g. Scene setting, (tap) * Service invocation, e.g. Scene setting, (tap)
* A sub-menu to open (tap) * A sub-menu to open (tap)
The example JSON shows an example usage of each of these Home Assistance entity types. Presently, an automation is the only one that can be either a 'tap' or a 'toggle'.
| HA Type | Tap | Toggle |
|------------|:---:|:------:|
| Switch | N | Y |
| Light | N | Y |
| Automation | Y | Y |
| Script | Y | N |
| Scene | Y | N |
NB. All 'tap' items must specify a 'service' tag.
Possible future extensions might include specifying the alternative texts to use instead of "On" and "Off", e.g. "Locked" and "Unlocked" (but wouldn't having locks operated from your watch be a security concern ;-)) Possible future extensions might include specifying the alternative texts to use instead of "On" and "Off", e.g. "Locked" and "Unlocked" (but wouldn't having locks operated from your watch be a security concern ;-))
The [schema](https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json) is checked by using a URL directly back to this GitHub source repository, so you do not need to install that file. You can just copy & paste your entity names from the YAML configuration files used to configure Home Assistant. With a submenu, there's a difference between "title" and "name". The "name" goes on the menu item, and the "title" at the head of the submenu. If your dashboard definition fails to meet the schema, the application will simply drop items with the wrong field names without warning. The [schema](https://raw.githubusercontent.com/house-of-abbey/GarminHomeAssistant/main/config.schema.json) is checked by using a URL directly back to this GitHub source repository, so you do not need to install that file. You can just copy & paste your entity names from the YAML configuration files used to configure Home Assistant. With a submenu, there's a difference between "title" and "name". The "name" goes on the menu item, and the "title" at the head of the submenu. If your dashboard definition fails to meet the schema, the application will simply drop items with the wrong field names without warning.

View File

@ -26,7 +26,7 @@
"type": { "const": "tap" }, "type": { "const": "tap" },
"service": { "$ref": "#/$defs/entity" } "service": { "$ref": "#/$defs/entity" }
}, },
"required": ["entity", "name", "type"], "required": ["entity", "name", "type", "service"],
"additionalProperties": false "additionalProperties": false
}, },
"menu": { "menu": {

View File

@ -77,36 +77,35 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
} }
WatchUi.pushView(new ErrorView(strApiUrlNotFound), new ErrorDelegate(), WatchUi.SLIDE_UP); WatchUi.pushView(new ErrorView(strApiUrlNotFound), new ErrorDelegate(), WatchUi.SLIDE_UP);
} else if (responseCode == 200) { } else if (responseCode == 200) {
var d = data as Lang.Array; if (Globals.scDebug) {
System.println("HomeAssistantMenuItem onReturnExecScript(): Service executed.");
}
var d = data as Lang.Array;
var toast = "Executed";
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(mIdentifier)) {
if (Globals.scDebug) { toast = (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String;
System.println("HomeAssistantMenuItem onReturnExecScript(): Correct script executed.");
}
if (WatchUi has :showToast) {
WatchUi.showToast(
(d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String,
null
);
}
if (Attention has :vibrate) {
Attention.vibrate([
new Attention.VibeProfile(50, 100), // On for 100ms
new Attention.VibeProfile( 0, 100), // Off for 100ms
new Attention.VibeProfile(50, 100) // On for 100ms
]);
}
if (!(WatchUi has :showToast) && !(Attention has :vibrate)) {
new Alert({
:timeout => Globals.scAlertTimeout,
:font => Graphics.FONT_MEDIUM,
:text => (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String,
:fgcolor => Graphics.COLOR_WHITE,
:bgcolor => Graphics.COLOR_BLACK
}).pushView(WatchUi.SLIDE_IMMEDIATE);
}
} }
} }
if (WatchUi has :showToast) {
WatchUi.showToast(toast, null);
}
if (Attention has :vibrate) {
Attention.vibrate([
new Attention.VibeProfile(50, 100), // On for 100ms
new Attention.VibeProfile( 0, 100), // Off for 100ms
new Attention.VibeProfile(50, 100) // On for 100ms
]);
}
if (!(WatchUi has :showToast) && !(Attention has :vibrate)) {
new Alert({
:timeout => Globals.scAlertTimeout,
:font => Graphics.FONT_MEDIUM,
:text => toast,
:fgcolor => Graphics.COLOR_WHITE,
:bgcolor => Graphics.COLOR_BLACK
}).pushView(WatchUi.SLIDE_IMMEDIATE);
}
} else { } else {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantMenuItem onReturnExecScript(): Unhandled HTTP response code = " + responseCode); System.println("HomeAssistantMenuItem onReturnExecScript(): Unhandled HTTP response code = " + responseCode);
@ -144,7 +143,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null); var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null);
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantMenuItem execScript() URL=" + url); System.println("HomeAssistantMenuItem execScript() URL=" + url);
System.println("HomeAssistantMenuItem execScript() mIdentifier=" + mIdentifier); System.println("HomeAssistantMenuItem execScript() mService=" + mService);
} }
Communications.makeWebRequest( Communications.makeWebRequest(
url, url,