Moved template status updates to webhooks

This seems to work for non-privileged users.
This commit is contained in:
Philip Abbey
2024-07-19 15:15:32 +01:00
parent 1c4add693d
commit dfa4cdd9b8
5 changed files with 87 additions and 40 deletions

View File

@ -39,7 +39,7 @@ class Settings {
private static var mPollDelay as Lang.Number = 0; // seconds
private static var mConfirmTimeout as Lang.Number = 3; // seconds
private static var mMenuAlignment as Lang.Number = WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT;
private static var mIsBatteryLevelEnabled as Lang.Boolean = false;
private static var mIsSensorsLevelEnabled as Lang.Boolean = false;
private static var mBatteryRefreshRate as Lang.Number = 15; // minutes
private static var mIsApp as Lang.Boolean = false;
private static var mHasService as Lang.Boolean = false;
@ -60,7 +60,7 @@ class Settings {
mPollDelay = Properties.getValue("poll_delay");
mConfirmTimeout = Properties.getValue("confirm_timeout");
mMenuAlignment = Properties.getValue("menu_alignment");
mIsBatteryLevelEnabled = Properties.getValue("enable_battery_level");
mIsSensorsLevelEnabled = Properties.getValue("enable_battery_level");
mBatteryRefreshRate = Properties.getValue("battery_level_refresh_rate");
if (System has :ServiceDelegate) {
@ -69,19 +69,40 @@ class Settings {
// Manage this inside the application or widget only (not a glance or background service process)
if (mIsApp) {
if (mIsBatteryLevelEnabled and mHasService) {
if (mHasService) {
mWebhookManager = new WebhookManager();
if (getWebhookId().equals("")) {
mWebhookManager = new WebhookManager();
// System.println("Settings update(): Doing full webhook & sensor creation.");
mWebhookManager.requestWebhookId();
} else {
// System.println("Settings update(): Doing just sensor creation.");
// We already have a Webhook ID, so just enable or disable the sensor in Home Assistant.
// Its a multiple step process, hence starting at step 0.
mWebhookManager.registerWebhookSensor({
"device_class" => "battery",
"name" => "Battery Level",
"state" => System.getSystemStats().battery,
"type" => "sensor",
"unique_id" => "battery_level",
"unit_of_measurement" => "%",
"state_class" => "measurement",
"entity_category" => "diagnostic",
"disabled" => !Settings.isSensorsLevelEnabled()
}, 0);
}
if ((Background.getTemporalEventRegisteredTime() == null) or
(Background.getTemporalEventRegisteredTime() != (mBatteryRefreshRate * 60))) {
Background.registerForTemporalEvent(new Time.Duration(mBatteryRefreshRate * 60)); // Convert to seconds
if (mIsSensorsLevelEnabled) {
// Create the timed activity
if ((Background.getTemporalEventRegisteredTime() == null) or
(Background.getTemporalEventRegisteredTime() != (mBatteryRefreshRate * 60))) {
Background.registerForTemporalEvent(new Time.Duration(mBatteryRefreshRate * 60)); // Convert to seconds
}
} else if (Background.getTemporalEventRegisteredTime() != null) {
Background.deleteTemporalEvent();
}
} else {
// Explicitly disable the background event which persists when the application closes.
// If !mHasService disable the Settings option as user feedback
unsetIsBatteryLevelEnabled();
unsetIsSensorsLevelEnabled();
unsetWebhookId();
}
}
@ -152,9 +173,13 @@ class Settings {
return mMenuAlignment; // Either WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_RIGHT or WatchUi.MenuItem.MENU_ITEM_LABEL_ALIGN_LEFT
}
static function unsetIsBatteryLevelEnabled() {
mIsBatteryLevelEnabled = false;
Properties.setValue("enable_battery_level", mIsBatteryLevelEnabled);
static function isSensorsLevelEnabled() as Lang.Boolean {
return mIsSensorsLevelEnabled;
}
static function unsetIsSensorsLevelEnabled() {
mIsSensorsLevelEnabled = false;
Properties.setValue("enable_battery_level", mIsSensorsLevelEnabled);
if (mHasService and (Background.getTemporalEventRegisteredTime() != null)) {
Background.deleteTemporalEvent();
}