/** * * Hubitat Package Manager v1.9.16 * * Copyright 2020 Dominick Meglio * * If you find this useful, donations are always appreciated * https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7LBRPJRLJSDDN&source=url * * * * csteele v1.9.16 Use the platform updateSelfFromUrl method on Hubitat Platform v2.5.3.103 and newer so * HPM source replacement is deferred until the self-update invocation exits. * Retain the asynchronous editor fallback for older platforms and recover * missing manifest handoff state during the transition from v1.9.15. * csteele v1.9.15 Correct automatic-update failure notifications so the generic message is sent * when per-package notifications are disabled, without duplicating it when * individual package failures are available. * csteele v1.9.14 Process HPM self-updates last in a separate asynchronous invocation, preventing * old compiled code from continuing after HPM replaces and recompiles itself. * Commit the HPM manifest and version only after the editor callback confirms success. * Remove the premature HPM-upgraded log and version event from the download phase. * csteele v1.9.13 Added code to address Issue #22: Large app updates * Added pre-flight check for Apps and Drivers in use during uninstall, requiring 2.3.6.126 and newer Platform * Null safe when Package is in use during uninstall * Corrected text and binary file restoration during uninstall rollback * csteele v1.9.12 Added "menu" to definition metadata. * Added maintained by CSteele to banner. * jono v1.9.12 During update, self-heal stale/removed app & driver heIDs cached in state.manifests * (e.g. left behind after a driver was deleted/reinstalled on the hub, or removed * entirely outside of HPM) instead of crashing with "Stream closed" and rolling * back the whole update; look up the current heID by name, or mark the item as * not installed if it's genuinely gone. Fixed a rollback NPE caused by * completedActions["bundleInstalls"] tracking the wrong variable. * csteele v1.9.11 Borrowing from a PR by neeravmodi, added a list of Apps and Drivers that are not managed by HPM. * mavrrick 1.9.10 Enhanced file processing to allow for a binary file download when new tag is specified and 'binary' is used. * csteele v1.9.9 UpgradeApp() moved to the Top to remediate HPM crashing during Upgrade of itself due to the methods moving post-upgrade. * Replaced "Fast Search" algorithm to also use fuzzy, thus eliminating the need for two choices. Delete Azure query. * Created an external Fast-Track Match Up tool. Default on but Optional. * mavrrick v1.9.8 Fix issue with "shouldInstallBeta" and "getItemDownloadLocation" methods * Added ability to install items with beta turned on. Update needed values accordingly * Updated Release Notes for install to display proper release info based on selection of Beta flag during install * mavrrick v1.9.7 Add ability to individually select apps/packages to include Beta Channels * csteele v1.9.7 display Release Notes for Installs (prefInstallVerify) * mavrrick v1.9.6 Update to allow separate release notes for Stable and Beta Release * Updated ability to list installed bundle files from View Apps and Driver Page * Make Bundles upgradeable on their own * csteele v1.9.5 Take advantage of v2.3.4 deleteHubFile() * csteele v1.9.4 Take advantage of v2.3.4 uploadHubFile() * csteele v1.9.3 improved displayHeader to include the Main Menu Option selected * refactored delete app to use new endpoint * csteele v1.9.2 added 'Connection': 'keep-alive' to install/uninstall Apps, Drivers and Bundles * extended timeout to 7 min (420 seconds) * Take advantage of v2.3.8 Bundle install/uninstall * csteele v1.9.1 updatePackage() ignores blank app or driver definitions * Take advantage of endpoints in v2.3.7 for app and driver uninstall * take advantage of endpoint for driver code list in v2.3.6 * clarify "failed download" error message with App or Driver hint * change updateTime to be NOT the 00 second * extended installBundle() timeout to 5 min (300 seconds) * csteele v1.9.0 Install 'location:' can't be empty. * add color to download icon * take advantage of endpoint for app code list in v2.3.6 * csteele v1.8.11 merge of: Add support for hub SSL (HTTPS) from kevdliu PR * csteele v1.8.10 correct File where contents are HTML [installFile() & uninstallFile()] * fix for default installBundle() (tomw) * csteele v1.8.9 allow required: true as string also * fix for a ProBundle install (a) * csteele v1.8.8 check for Null for Invalid Category & Tags * log.error changed to log.warn where the event was handled * detect/delete missing Repositories * csteele v1.8.7 fix to Update bundle. (birdslikewires) * csteele v1.8.6 Un-Match added * moved Bundles before Apps/Drivers for Install and Repair * included Bundles into modify * csteele v1.8.5 Bundles support added * csteele v1.8.4 Migrated to HubitatCommunity * added txtEnable to silence log.info messages * use httpS for Fast Search * csteele v1.8.3 No change here. Changes were to Dominic's repo to cause an upgrade to here * csteele v1.8.2.A Converted to using HubitatCommunity.com as the search resource. [Lines 66-67 & 379-380] * added footer to display version and copyright fields * added feature to identify Azure search vs sql search */ public static String version() { return "v1.9.16" } def getThisCopyright(){"© 2020 Dominick Meglio"} definition( name: "Hubitat Package Manager", namespace: "dcm.hpm", author: "Dominick Meglio", description: "Provides a utility to maintain the apps and drivers on your Hubitat making both installation and updates easier", category: "My Apps", menu: "Apps", // Available options are: "Integrations", "Automations", or "Apps" importUrl: "https://raw.githubusercontent.com/HubitatCommunity/hubitatpackagemanager/main/apps/Package_Manager.groovy", documentationLink: "https://hubitatpackagemanager.hubitatcommunity.com/", iconUrl: "", iconX2Url: "", iconX3Url: "", singleInstance: true) preferences { page(name: "prefSettings") page(name: "prefOptions") page(name: "prefPkgInstall") page(name: "prefPkgInstallUrl") page(name: "prefInstallRepositorySearch") page(name: "prefInstallRepositorySearchResults") page(name: "prefPkgInstallRepository") page(name: "prefPkgInstallRepositoryChoose") page(name: "prefPkgModify") page(name: "prefPkgRepair") page(name: "prefPkgRepairExecute") page(name: "prefPkgUpdate") page(name: "prefPkgUninstall") page(name: "prefInstallChoices") page(name: "prefInstallVerify") page(name: "prefInstall") page(name: "prefPkgModifyChoices") page(name: "prefVerifyPackageChanges") page(name: "prefMakePackageChanges") page(name: "prefPkgUninstallConfirm") page(name: "prefUninstall") page(name: "prefPkgVerifyUpdates") page(name: "prefPkgUpdatesComplete") page(name: "prefPkgMatchUp") page(name: "prefPkgMatchUpVerify") page(name: "prefPkgMatchUpComplete") page(name: "prefPkgView") page(name: "prefPkgUnMatch") page(name: "prefPkgUnMatchVerify") page(name: "prefPkgUnMatchComplete") page(name: "prefPkgViewUnmanaged") page(name: "prefError") } import groovy.transform.Field import java.util.regex.Matcher @Field static String repositoryListing = "https://raw.githubusercontent.com/HubitatCommunity/hubitat-packagerepositories/master/repositories.json" @Field static String settingsFile = "https://raw.githubusercontent.com/HubitatCommunity/hubitat-packagerepositories/master/settings.json" @Field static String searchApiUrl = "https://hubitatpackagemanager.hubitatcommunity.com/searchHPMpkgs2.php" @Field static String preBuiltUrl = "https://hubitatpackagemanager.hubitatcommunity.com/hpmRepoCache.php" @Field static List categories = [] @Field static List allPackages = [] @Field static def completedActions = [:] @Field static def manifestForRollback = null @Field static def downloadQueue = [:] @Field static Integer maxDownloadQueueSize = 10 @Field static String installAction = "" @Field static String installMode = "" @Field static String statusMessage = "" @Field static String errorTitle = "" @Field static String errorMessage = "" @Field static Boolean errorOccurred = false @Field static def packagesWithUpdates = [:] @Field static def optionalItemsToShow = [:] @Field static def updateDetails = [:] @Field static List appsToInstallForModify = [] @Field static List appsToUninstallForModify = [] @Field static List driversToInstallForModify = [] @Field static List driversToUninstallForModify = [] @Field static List packagesMatchingInstalledEntries = [] @Field static List iconTags = ["ZWave", "Zigbee", "Cloud", "LAN", "Matter"] @Field static String srchSrcTxt = "" // method moved to the Top to possibly remediate HPM crashing during Upgrade of itself. def upgradeApp(id,appCode) { return withHttpRetry("Error upgrading app", 2, 1000) { def originalVersion = getAppVersion(id) def originalLastModified def metadataParams = [ uri: getBaseUrl(), path: "/hub2/userAppTypes", headers: [ "Cookie": state.cookie ], timeout: 30, ignoreSSLIssues: true ] httpGet(metadataParams) { resp -> originalLastModified = resp.data?.find { it.id?.toString() == id?.toString() }?.lastModified?.toString() } if (!originalLastModified) throw new IllegalStateException("Error retrieving app editor metadata for ${id}: response did not contain lastModified") def params = [ uri: getBaseUrl(), path: "/app/ajax/update", requestContentType: "application/x-www-form-urlencoded", headers: [ "Connection": 'keep-alive', "Cookie": state.cookie ], body: [ id: id, version: originalVersion, source: appCode ], timeout: 420, ignoreSSLIssues: true ] def result = false try { httpPost(params) { resp -> result = resp.data.status == "success" if (!result) { try { log.error "Error upgrading app ${id}: server responded with non-success status, full response: ${resp.data}" } catch (ignored) {} recordLastUpgradeError("Error upgrading app ${id}: non-success status, full response: ${resp.data}") } } } catch (e) { def timeoutCause = e while (timeoutCause != null && !(timeoutCause instanceof java.net.SocketTimeoutException)) timeoutCause = timeoutCause.cause if (!(timeoutCause instanceof java.net.SocketTimeoutException) || !timeoutCause.message?.toLowerCase()?.contains("read timed out")) throw e return waitForAppVersionChange(id, originalVersion, originalLastModified) } result } } // HPM must update itself from a separate invocation. On platform 2.5.3.103 and newer, // updateSelfFromUrl() defers the source swap until this invocation exits. Older platforms // retain the asynchronous editor fallback. def performHpmSelfUpdate() { def pending = atomicState.pendingHpmSelfUpdate ?: state.pendingHpmSelfUpdate if (!pending?.packageLocation) { log.warn "HPM self-update was scheduled without pending update state" atomicState.backgroundActionInProgress = false return } try { def manifest = getJSONFile(pending.packageLocation) if (manifest == null) { finishHpmSelfUpdateFailure("The HPM manifest ${pending.packageLocation} could not be downloaded") return } if (shouldInstallBeta(manifest)) { def betaManifest = getJSONFile(getItemDownloadLocation(manifest)) if (betaManifest != null) { manifest = betaManifest manifest.beta = true } else manifest.beta = false } else manifest.beta = false def installedManifest = state.manifests[pending.packageLocation] def installedHpmApp = installedManifest?.apps?.find { it.heID != null } def hpmApp = manifest.apps?.find { it.id == installedHpmApp?.id } if (installedHpmApp == null || hpmApp == null) { finishHpmSelfUpdateFailure("The installed or downloaded HPM manifest does not contain the HPM app") return } hpmApp.beta = shouldInstallBeta(hpmApp) copyInstalledItemsToNewManifest(installedManifest, manifest) def sourceLocation = getItemDownloadLocation(hpmApp) if (verifyHEVersion("2.5.3.103")) { setBackgroundStatusMessage("Upgrading ${hpmApp.name}") logDebug "Submitting HPM self-update through updateSelfFromUrl; this invocation will now end" def updateAccepted = updateSelfFromUrl(sourceLocation) if (!updateAccepted) { finishHpmSelfUpdateFailure("Hubitat rejected the HPM self-update from ${sourceLocation}") return } finishHpmSelfUpdateSuccess(pending + [manifest: manifest], "HPM self-update accepted by updateSelfFromUrl") return } if (!login()) { finishHpmSelfUpdateFailure("Error logging in to the hub") return } def resolvedHeID = resolveCurrentHeID(getAppList(), installedHpmApp.heID, hpmApp.name) if (resolvedHeID == null) { finishHpmSelfUpdateFailure("The HPM app editor entry could not be found") return } installedHpmApp.heID = resolvedHeID hpmApp.heID = resolvedHeID setBackgroundStatusMessage("Downloading ${hpmApp.name}") def sourceCode = downloadFile(sourceLocation) if (sourceCode == null) { finishHpmSelfUpdateFailure("The HPM source ${sourceLocation} could not be downloaded") return } atomicState.pendingHpmSelfUpdate = pending + [manifest: manifest] def editorVersion = getAppVersion(resolvedHeID) def formBody = "id=${java.net.URLEncoder.encode(resolvedHeID.toString(), 'UTF-8')}" + "&version=${java.net.URLEncoder.encode(editorVersion.toString(), 'UTF-8')}" + "&source=${java.net.URLEncoder.encode(sourceCode, 'UTF-8')}" def params = [ uri: getBaseUrl(), path: "/app/ajax/update", contentType: "application/json", requestContentType: "application/x-www-form-urlencoded", headers: [ "Connection": "keep-alive", "Cookie": state.cookie ], body: formBody, timeout: 300, ignoreSSLIssues: true ] setBackgroundStatusMessage("Upgrading ${hpmApp.name}") logDebug "Submitting HPM self-update request; this invocation will now end" asynchttpPost("hpmSelfUpdateComplete", params) return } catch (e) { finishHpmSelfUpdateFailure("Unable to submit the HPM self-update: ${describeHttpError(e)}") } } // This callback is deliberately stable and runs as a fresh invocation. If the editor // accepted the new source, Hubitat resolves this method from the newly compiled app. def hpmSelfUpdateComplete(resp, data) { def httpStatus def responseData def responseHasError = false try { httpStatus = resp.getStatus() } catch (ignored) { } try { responseHasError = resp.hasError() } catch (ignored) { } try { responseData = resp.json } catch (ignored) { } if (responseData == null) { try { responseData = resp.getData() } catch (ignored) { } } def responseText = responseData?.toString() def editorReportedSuccess = (responseData instanceof Map && responseData.status == "success") || responseText?.contains('"status":"success"') || responseText?.contains('status:success') def httpSucceeded = httpStatus != null && httpStatus >= 200 && httpStatus < 300 def succeeded = !responseHasError && httpSucceeded && editorReportedSuccess if (!succeeded) { finishHpmSelfUpdateFailure("HPM editor update failed: HTTP ${httpStatus}, response ${responseText}") return } def pending = atomicState.pendingHpmSelfUpdate ?: state.pendingHpmSelfUpdate if (!pending?.packageLocation || !pending?.manifest) { pending = recoverHpmSelfUpdateState(pending) if (!pending?.packageLocation || !pending?.manifest) { finishHpmSelfUpdateFailure("HPM editor update succeeded but pending manifest state was missing") return } logDebug "Recovered missing HPM self-update manifest state after editor update" } finishHpmSelfUpdateSuccess(pending, "HPM self-update callback confirmed") } // The release which introduces updateSelfFromUrl() must itself arrive through the old // editor endpoint. Recover from the known state handoff race during that final transition. def recoverHpmSelfUpdateState(pending) { def packageLocation = pending?.packageLocation ?: state.repositoryListingJSON?.hpm?.location if (!packageLocation) return null def manifest = getJSONFile(packageLocation) if (manifest == null) return null if (shouldInstallBeta(manifest)) { def betaManifest = getJSONFile(getItemDownloadLocation(manifest)) if (betaManifest != null) { manifest = betaManifest manifest.beta = true } else manifest.beta = false } else manifest.beta = false def installedManifest = state.manifests[packageLocation] if (installedManifest != null) copyInstalledItemsToNewManifest(installedManifest, manifest) return (pending ?: [:]) + [packageLocation: packageLocation, manifest: manifest] } def finishHpmSelfUpdateSuccess(pending, confirmationMessage) { state.manifests[pending.packageLocation] = pending.manifest minimizeStoredManifests() sendLocationEvent(name: "hpmVersion", value: pending.manifest.version) state.updatesNotified = false atomicState.remove("pendingHpmSelfUpdate") state.remove("pendingHpmSelfUpdate") logDebug "${confirmationMessage} v${pending.manifest.version}" logDebug "Updates complete" if (pending.runInBackground == true) { if (notifyOnSuccess) { def notificationText = notifySpecificPackages ? "Hubitat Package Manager was updated successfully" : "The packages were updated successfully" notifyUpdateSuccessDevices*.deviceNotification(buildNotification(notificationText)) } } else atomicState.backgroundActionInProgress = false } def finishHpmSelfUpdateFailure(message) { def pending = atomicState.pendingHpmSelfUpdate ?: state.pendingHpmSelfUpdate try { log.error message } catch (ignored) { } recordLastUpgradeError(message) atomicState.remove("pendingHpmSelfUpdate") state.remove("pendingHpmSelfUpdate") if (pending?.runInBackground == true) { if (notifyOnFailure) notifyUpdateFailureDevices*.deviceNotification(buildNotification("Hubitat Package Manager failed to update. Check the logs for more information")) app.updateLabel("Hubitat Package Manager Updates Available") } else { triggerError("HPM Self-Update Failed", message, false) atomicState.backgroundActionInProgress = false } } def installed() { initialize() } def updated() { unschedule() initialize() } def initialize() { def timeOfDayForUpdateChecks if (updateCheckTime == null) timeOfDayForUpdateChecks = timeToday("00:00") else timeOfDayForUpdateChecks = timeToday(updateCheckTime, location.timeZone) schedule("13 ${timeOfDayForUpdateChecks.minutes} ${timeOfDayForUpdateChecks.hours} ? * *", checkForUpdates) performMigrations() } def uninstalled() { log.warn "uninstalling app" unschedule() } def appButtonHandler(btn) { switch (btn) { case "btnMainMenu": state.mainMenu = true break case "btnBack": state.back = true case "btnAddRepo": state.customRepo = true break case "btnUnMatch": state.UnMatch = true break case "btnPkgUnmanaged": state.UnManaged = true break case ~/^btnDeleteRepo(\d+)/: deleteCustomRepository(Matcher.lastMatcher[0][1].toInteger()) break } } def prefOptions() { state.remove("mainMenu") if (state.customRepo && customRepo != "" && customRepo != null) { def repoListing = getJSONFile(customRepo) if (repoListing == null) { clearStateSettings(true) return buildErrorPage("Error loading repository", "The repository file you specified could not be loaded.") } else { installedRepositories << customRepo if (state.customRepositories == null) state.customRepositories = [:] if (state.customRepositories[customRepo] == null) state.customRepositories << ["${customRepo}":repoListing.author] } } if (state.firstRun == true) return prefPkgMatchUp() else { clearStateSettings(true) initialize() installHPMManifest() } if (installedRepositories == null) { logDebug "No installed repositories, grabbing all" def repos = [] as List state.repositoryListingJSON.repositories.each { it -> repos << it.location } app.updateSetting("installedRepositories", repos) } state.categoriesAndTags = loadSettingsFile() return dynamicPage(name: "prefOptions", title: "", install: true, uninstall: false) { displayHeader() if (isHubSecurityEnabled() && !hpmSecurity) { section { paragraph "Hub Security appears to be enabled but is not configured in HPM. Please configure hub security." } } if (state.newRepoMessage != "") { section { paragraph state.newRepoMessage state.newRepoMessage = "" } } section { paragraph "What would you like to do?" href(name: "prefPkgInstall", title: "Install", required: false, page: "prefPkgInstall", description: "Install a new package.") href(name: "prefPkgUpdate", title: "Update", required: false, page: "prefPkgUpdate", description: "Check for updates for your installed packages.") href(name: "prefPkgModify", title: "Modify", required: false, page: "prefPkgModify", description: "Modify an already installed package. This allows you to add or remove optional components.") href(name: "prefPkgRepair", title: "Repair", required: false, page: "prefPkgRepair", description: "Repair a package by ensuring all of the newest versions are installed in case something went wrong.") href(name: "prefPkgUninstall", title: "Uninstall", required: false, page: "prefPkgUninstall", description: "Uninstall packages.") href(name: "prefPkgMatchUp", title: "Match Up", required: false, page: "prefPkgMatchUp", description: "Match up the apps and drivers you already have installed with packages available so that you can use the package manager to get future updates.") href(name: "prefPkgView", title: "View Apps and Drivers", required: false, page: "prefPkgView", description: "View the apps and drivers that are managed by packages.") href(name: "prefSettings", title: "Package Manager Settings", required: false, page: "prefSettings", params: [force:true], description: "Modify Hubitat Package Manager Settings.") } displayFooter() } } def prefSettings(params) { if (state.UnMatch) return prefPkgUnMatch() def showSettingsForSecurityEnablement = false state.newRepoMessage = "" if (state.manifests == null) state.manifests = [:] def pkgsToList = getInstalledPackages(false) performMigrations() if (updateRepositoryListing()?.size() > 0) { state.newRepoMessage = "One or more new repositories have been added. You may want to do a Match Up to ensure all of your packages are detected." } if (isHubSecurityEnabled() && !hpmSecurity) { showSettingsForSecurityEnablement = true } installHPMManifest() if (app.getInstallationState() == "COMPLETE" && params?.force != true && !showSettingsForSecurityEnablement) return prefOptions() else { def showInstall = app.getInstallationState() == "INCOMPLETE" if (showInstall) state.firstRun = true return dynamicPage(name: "prefSettings", title: "", nextPage: "prefOptions", install: showInstall, uninstall: false) { displayHeader(' Settings') section ("Hub Security") { if (showSettingsForSecurityEnablement) { paragraph "Hub Security appears to be enabled on your hub but is not enabled within HPM. Please configure hub security below" } paragraph "In order to automatically install apps and drivers you must specify your Hubitat admin username and password if Hub Security is enabled." input "hpmSecurity", "bool", title: "Hub Security Enabled", submitOnChange: true if (hpmSecurity) { input "hpmUsername", "string", title: "Hub Security username", required: true input "hpmPassword", "password", title: "Hub Security password", required: true } paragraph "If you have SSL (HTTPS) enabled on your hub, you must enable the option below. If you're not sure, leave the option disabled." input "sslEnabled", "bool", title: "SSL (HTTPS) Enabled", submitOnChange: true if (showInstall) paragraph "Please click Done and restart the app to continue." } if (!state.firstRun) { section ("General") { input "debugOutput", "bool", title: "Enable debug logging", defaultValue: true input "txtEnable", "bool", title: "Enable text logging", defaultValue: true input "includeBetas", "bool", title: "When updating, install pre-release versions. Note: Pre-releases often include more bugs and should be considered beta software", defaultValue: false, submitOnChange: true if (includeBetas) { input "pkgBetaOn", "enum", title: "Choose which app/packages to enable Beta/Early Access code", options: pkgsToList.values(), multiple: true, required: true } } section { paragraph "


Package Updates" input "updateCheckTime", "time", title: "Specify what time update checking should be performed", defaultValue: "00:00", required: true input "notifyUpdatesAvailable", "bool", title: "Notify me when updates are available", submitOnChange: true if (notifyUpdatesAvailable) input "notifyDevices", "capability.notification", title: "Devices to notify", required: true, multiple: true input "autoUpdateMode", "enum", title: "Install updates automatically when", options: ["Always":"Always", "Never":"Never", "Include":"Only those I list", "Exclude":"All packages except the ones I list"], submitOnChange: true if (autoUpdateMode != "Never") { if (autoUpdateMode == "Include") { def listOfPackages = getInstalledPackages(false) input "appsToAutoUpdate", "enum", title: "Which packages should be automatically updated?", required: true, multiple: true, options:listOfPackages } else if (autoUpdateMode == "Exclude") { def listOfPackages = getInstalledPackages(false) input "appsNotToAutoUpdate", "enum", title: "Which packages should NOT be automatically updated?", required: true, multiple: true, options:listOfPackages } input "notifyOnSuccess", "bool", title: "Notify me if automatic updates are successful", submitOnChange: true if (notifyOnSuccess) input "notifyUpdateSuccessDevices", "capability.notification", title: "Devices to notify", required: true, multiple: true input "notifyOnFailure", "bool", title: "Notify me if automatic updates are unsuccessful", submitOnChange: true if (notifyOnFailure) input "notifyUpdateFailureDevices", "capability.notification", title: "Devices to notify", required: true, multiple: true } if (notifyUpdatesAvailable || notifyOnSuccess || notifyOnFailure) input "notifyIncludeHubName", "bool", title: "Include hub name in notifications", defaultValue: false if (notifyOnSuccess || notifyOnFailure) input "notifySpecificPackages", "bool", title: "Send notifications for each specific package", defaultValue: false paragraph "
" } def reposToShow = [:] state.repositoryListingJSON.repositories.each { r -> reposToShow << ["${r.location}":r.name] } if (state.customRepositories != null) state.customRepositories.each { r -> reposToShow << ["${r.key}":r.value] } reposToShow = reposToShow.sort { r -> r.value } section ("Repositories") { input "installedRepositories", "enum", title: "Available repositories", options: reposToShow, multiple: true, required: true if (state.customRepositories && state.customRepositories.size()) { def i = 0 for (customRepo in state.customRepositories.keySet()) { paragraph "${state.customRepositories[customRepo]} - ${customRepo}", width: 10 input "btnDeleteRepo${i}", "button", title: "Remove", width: 2 i++ } } if (!state.customRepo) input "btnAddRepo", "button", title: "Add a Custom Repository", submitOnChange: false if (state.customRepo) input "customRepo", "text", title: "Enter the URL of the repository's directory listing file", required: true paragraph "
" } section ("Un-Match a Package") { paragraph "Un-Match selected Apps or Drivers by removing the cached manifest. Follow this step with a Match Up to install the latest Manifest. This feature is best used by experienced users. Or when instructed by the owner of a Package due to a change they made in their Manifest structure." input "btnUnMatch", "button", title: "Remove a Matched Package" } } } } } // Install a package pathway def prefPkgInstall() { if (state.mainMenu) return prefOptions() logDebug "prefPkgInstall" app.updateSetting("installBeta", false) return dynamicPage(name: "prefPkgInstall", title: "", install: true, uninstall: false) { displayHeader(' Install') section { paragraph "Install a Package" paragraph "How would you like to install this package?" href(name: "prefInstallRepositorySearch", title: "Search by Keywords", required: false, page: "prefInstallRepositorySearch", description: "Search for packages by searching for keywords. This will only include the standard repositories, not custom repositories.") href(name: "prefPkgInstallRepository", title: "Browse by Tags", required: false, page: "prefPkgInstallRepository", description: "Choose a package from a repository browsing by tags. This will include both the standard repositories and any custom repositories you have setup.") href(name: "prefPkgInstallUrl", title: "From a URL", required: false, page: "prefPkgInstallUrl", description: "Install a package using a URL to a specific package. This is an advanced feature, only use it if you know how to find a package's manifest manually.") } section { paragraph "
" input "btnMainMenu", "button", title: "Main Menu", width: 3 } } } def prefInstallRepositorySearch() { if (state.mainMenu) return prefOptions() state.remove("back") logDebug "prefInstallRepositorySearch" installMode = "search" return dynamicPage(name: "prefInstallRepositorySearch", title: "", nextPage: "prefInstallRepositorySearchResults", install: false, uninstall: false) { displayHeader(' Install') section { paragraph "Search" input "pkgSearch", "text", title: "Enter your search criteria", required: true input "btnMainMenu", "button", title: "Main Menu", width: 3 } } } def prefInstallRepositorySearchResults() { if (state.mainMenu) return prefOptions() if (state.back) return prefInstallRepositorySearch() logDebug "prefInstallRepositorySearchResults" installMode = "search" def params = [ uri: searchApiUrl, contentType: "application/json", requestContentType: "application/json", body: [ "operationName": null, "variables": [ "searchQuery": pkgSearch ], "query": 'query Search($searchQuery: String) { repositories { author, gitHubUrl, payPalUrl, packages (search: $searchQuery) {name, description, location, tags}}}' ] ] def result = null httpPost(params) { resp -> result = resp.data } if (result?.data?.repositories) { def searchResults = [] for (repo in result.data.repositories) { for (packageItem in repo.packages) { def pkg_tags = packageItem.tags[1..-2].tokenize(',') packageItem.tags = pkg_tags packageItem << [author: repo.author, gitHubUrl: repo.gitHubUrl, payPalUrl: repo.payPalUrl, installed: state.manifests[packageItem.location] != null] searchResults << packageItem } } searchResults = searchResults.sort { it -> it.name } return dynamicPage(name: "prefInstallRepositorySearch", title: "", nextPage: "prefInstallRepositorySearchResults", install: false, uninstall: false) { displayHeader(' Install') section { paragraph "Search Results for ${pkgSearch} by $srchSrcTxt Search" addCss() } section { if (searchResults.size() > 0) { def i = 0 for (searchResult in searchResults) { renderPackageButton(searchResult,i) i++ } } else paragraph "No matching packages were found. Click Back to return to the search screen." } section { paragraph "
" input "btnMainMenu", "button", title: "Main Menu", width: 3 input "btnBack", "button", title: "Back", width: 3 } } } } def prefPkgInstallUrl() { if (state.mainMenu) return prefOptions() logDebug "prefPkgInstallUrl" installMode = "url" return dynamicPage(name: "prefPkgInstallUrl", title: "", nextPage: "prefInstallChoices", install: false, uninstall: false) { displayHeader(' Install') section { paragraph "Install a Package from URL" input "pkgInstall", "text", title: "Enter the URL of a package you wish to install (this should be a path to a packageManifest.json file).", required: true } section { paragraph "
" input "btnMainMenu", "button", title: "Main Menu", width: 3 } } } def prefPkgInstallRepository() { if (errorOccurred == true) { return buildErrorPage(errorTitle, errorMessage) } if (atomicState.backgroundActionInProgress == null) { logDebug "prefPkgInstallRepository" atomicState.backgroundActionInProgress = true getMultipleJSONFiles(installedRepositories, performRepositoryRefreshComplete, performRepositoryRefreshStatus) } if (atomicState.backgroundActionInProgress != false) { return dynamicPage(name: "prefPkgInstallRepository", title: "", nextPage: "prefPkgInstallRepository", install: false, uninstall: false, refreshInterval: 2) { section { showHideNextButton(false) paragraph "Install a Package" paragraph "Refreshing repositories... Please wait..." paragraph getBackgroundStatusMessage() } } } else { installMode = "repository" return prefInstallChoices(null) } } def renderTags(pkgList) { def tags = [] for (pkg in pkgList) { if (state.categoriesAndTags.categories.contains(pkg.category)) { if (!tags.contains(pkg.category)) tags << pkg.category } else if (pkg.category?.trim()) { log.warn "Invalid category found: ${pkg.category} for ${pkg.location}" } for (tag in pkg.tags) { if (state.categoriesAndTags.tags.contains(tag)) { if (!tags.contains(tag)) tags << tag } else if (tag?.trim()) { log.warn "Invalid tag found: ${tag} for ${pkg.location}" } } } tags = tags.sort() input "pkgTags", "enum", title: "Choose tag(s)", options: tags, submitOnChange: true, multiple: true } def renderSortBy() { input "pkgSortBy", "enum", title: "Sort By", options: ["Author", "Name"], submitOnChange: true, defaultValue: "Name" } def prefInstallChoices(params) { if (state.mainMenu) return prefOptions() logDebug "prefInstallChoices" return dynamicPage(name: "prefInstallChoices", title: "", nextPage: "prefInstallVerify", install: false, uninstall: false) { displayHeader(' Install') section { addCss() paragraph "Install a Package from a Repository" if (installMode == "repository" && params == null) { //input "pkgCategory", "enum", title: "Choose a category", options: categories, required: true, submitOnChange: true renderTags(allPackages) if(pkgTags) { renderSortBy() def matchingPackages = [] for (pkg in allPackages) { if (state.manifests.containsKey(pkg.location)) { pkg.installed = true } if (pkgTags.contains(pkg.category) || pkg.tags.find { pkgTags.contains(it)}) { matchingPackages << pkg } } def sortedMatchingPackages if (pkgSortBy == "Name") sortedMatchingPackages = matchingPackages.sort { x -> x.name } else sortedMatchingPackages = matchingPackages.sort { y -> y.author } if (sortedMatchingPackages.size() > 0) { def i = 0 for (pkg in sortedMatchingPackages) { renderPackageButton(pkg,i) i++ } } else paragraph "No matching packages were found. Please choose a different category." } } } if (installMode == "search" || (installMode == "repository" && params != null)) { pkgInstall = params.location app.updateSetting("pkgInstall", params.location) state.payPalUrl = params.payPalUrl } if(pkgInstall) { if (state.manifests == null) state.manifests = [:] def manifest = getJSONFile(pkgInstall) if (manifest == null) { return buildErrorPage("Invalid Package File", "${pkgInstall} does not appear to be a valid Hubitat Package or does not exist.") } if (state.manifests[pkgInstall] != null) { return buildErrorPage("Package Already Installed", "${pkgInstall} has already been installed. If you would like to look for upgrades, use the Update function.") } if (manifest.minimumHEVersion != null && !verifyHEVersion(manifest.minimumHEVersion)) { return buildErrorPage("Unsupported Hubitat Firmware", "Your Hubitat Elevation firmware is not supported. You are running ${location.hub.firmwareVersionString} and this package requires at least ${manifest.minimumHEVersion}. Please upgrade your firmware to continue installing.") } else { def apps = getOptionalAppsFromManifest(manifest) def drivers = getOptionalDriversFromManifest(manifest) def bundles = getOptionalBundlesFromManifest(manifest) def title = "Choose the components to install" if (apps.size() == 0 && drivers.size() == 0) title = "Ready to install" section("${title}") { if (apps.size() > 0 || drivers.size() > 0 || bundles.size() > 0) paragraph "You are about to install ${manifest.packageName}. This package includes some optional components. Please choose which ones you would like to include below. Click Next when you are ready." else paragraph "You are about to install ${manifest.packageName}. Click next when you are ready." if (apps.size() > 0) input "appsToInstall", "enum", title: "Select the apps to install", options: apps, hideWhenEmpty: true, multiple: true if (drivers.size() > 0) input "driversToInstall", "enum", title: "Select the drivers to install", options: drivers, hideWhenEmpty: true, multiple: true if (bundles.size() > 0) input "bundlesToInstall", "enum", title: "Select the bundles to install", options: bundles, hideWhenEmpty: true, multiple: true } } section { if (manifest.toMapString().contains("betaVersion")) { input "installBeta", "bool", title: "Install beta package", defaultValue: false } } } section { paragraph "
" input "btnMainMenu", "button", title: "Main Menu", width: 3 } } } def getRepoName(location) { return state.repositoryListingJSON.repositories.find { it -> it.location == location }?.name } def performRepositoryRefreshStatus(uri, data) { def repoName = getRepoName(uri) setBackgroundStatusMessage("Refreshed ${repoName}") } def performRepositoryRefreshComplete(results, data) { allPackages = [] categories = [] for (uri in results.keySet()) { def repoName = getRepoName(uri) def result = results[uri] def fileContents = result.result if (fileContents == null) { setBackgroundStatusMessage("Unable to Refresh ${repoName}", "warn") continue } for (pkg in fileContents.packages) { def pkgDetails = [ repository: repoName, author: fileContents.author, githubUrl: fileContents.gitHubUrl, payPalUrl: fileContents.payPalUrl, name: pkg.name, description: pkg.description, location: pkg.location, category: pkg.category, tags: pkg.tags ] allPackages << pkgDetails if (!categories.contains(pkgDetails.category)) categories << pkgDetails.category } } allPackages = allPackages.sort() categories = categories.sort() atomicState.backgroundActionInProgress = false logDebug "Repositories refreshed" } def prefInstallVerify() { if (state.mainMenu) return prefOptions() logDebug "prefInstallVerify" if(!pkgInstall) return buildErrorPage("no package selected", "whats up?") // pr #114 atomicState.backgroundActionInProgress = null statusMessage = "" errorOccurred = null errorTitle = null errorMessage = null return dynamicPage(name: "prefInstallVerify", title: "", nextPage: "prefInstall", install: false, uninstall: false) { displayHeader(' Install') section { paragraph "Ready to install" def manifest = getJSONFile(pkgInstall) def pkgToInstall = "" } if (manifest.licenseFile) { def license = downloadFile(manifest.licenseFile) paragraph "By clicking next you accept the below license agreement:" paragraph "" paragraph "Click next to continue. This make take some time..." } else { paragraph "The following will be installed: ${pkgToInstall}" paragraph "Click the next button to install your selections. This may take some time..." } def primaryApp = manifest?.apps?.find { item -> item.primary == true } if (primaryApp) input "launchInstaller", "bool", defaultValue: true, title: "Configure the installed package after installation completes." } section { paragraph "
" input "btnMainMenu", "button", title: "Main Menu", width: 3 } } } def prefInstall() { if (state.mainMenu) return prefOptions() if (errorOccurred == true) { return buildErrorPage(errorTitle, errorMessage) } if (atomicState.backgroundActionInProgress == null) { logDebug "prefInstall" logDebug "Install beginning" atomicState.backgroundActionInProgress = true runInMillis(1,performInstallation) } if (atomicState.backgroundActionInProgress != false) { return dynamicPage(name: "prefInstall", title: "", nextPage: "prefInstall", install: false, uninstall: false, refreshInterval: 2) { displayHeader(' Install') section { showHideNextButton(false) paragraph "Installing" paragraph "Your installation is currently in progress... Please wait..." paragraph getBackgroundStatusMessage() } } } else { def primaryApp = state.manifests[pkgInstall]?.apps?.find {it -> it.primary == true } if (primaryApp == null || !launchInstaller) return complete("Installation complete", "The package was sucessfully installed, click Next to return to the Main Menu.") else return complete("Installation complete", "The package was sucessfully installed, click Next to configure your new package.", false, primaryApp.heID) } } def performInstallation() { if (!login()) return triggerError("Error logging in to hub", "An error occurred logging into the hub. Please verify your Hub Security username and password.", false) def manifest = getJSONFile(pkgInstall) if (installBeta){ if (!includeBetas) { app.updateSetting("includeBetas", true) } if (!pkgBetaOn) { List tmp = [manifest.packageName] app.updateSetting("pkgBetaOn", tmp as List) } else { curretSetting = app.getSetting("pkgBetaOn") log.warn "curretSetting is set to ${curretSetting}" curretSetting.add(manifest.packageName) app.updateSetting("pkgBetaOn", curretSetting) } } if (shouldInstallBeta(manifest)) { manifest = getJSONFile(getItemDownloadLocation(manifest)) manifest.beta = true } else manifest.beta = false state.manifests[pkgInstall] = manifest state.manifests[pkgInstall].payPalUrl = state.payPalUrl state.payPalUrl = null minimizeStoredManifests() // Download all files first to reduce the chances of a network error def appFiles = [:] def driverFiles = [:] def fileManagerFiles = [:] def bundleFiles = [:] def fileMgrResults = downloadFileManagerFiles(manifest) if (fileMgrResults.success) fileManagerFiles = fileMgrResults.files else return triggerError("Failed download of file", "An error occurred downloading ${fileMgrResults.name}", false) def requiredApps = getRequiredAppsFromManifest(manifest) def requiredDrivers = getRequiredDriversFromManifest(manifest) def requiredBundles = getRequiredBundlesFromManifest(manifest) for (requiredApp in requiredApps) { // required = true def location = getItemDownloadLocation(requiredApp.value) setBackgroundStatusMessage("Downloading ${requiredApp.value.name}") def fileContents = downloadFile(location) if (fileContents == null) { state.manifests.remove(pkgInstall) return triggerError("Failed download of required app file", "An error occurred downloading ${location}", false) } appFiles[location] = fileContents } for (appToInstall in appsToInstall) { // required = false (aka optional) def matchedApp = manifest.apps.find { it.id == appToInstall} if (matchedApp != null) { def location = getItemDownloadLocation(matchedApp) setBackgroundStatusMessage("Downloading ${matchedApp.name}") def fileContents = downloadFile(location) if (fileContents == null) { state.manifests.remove(pkgInstall) return triggerError("Failed download of app file", "An error occurred downloading ${location}", false) } appFiles[location] = fileContents } } for (requiredDriver in requiredDrivers) { // required = true def location = getItemDownloadLocation(requiredDriver.value) setBackgroundStatusMessage("Downloading ${requiredDriver.value.name}") def fileContents = downloadFile(location) if (fileContents == null) { state.manifests.remove(pkgInstall) return triggerError("Failed download of required driver file", "An error occurred downloading ${location}", false) } driverFiles[location] = fileContents } for (driverToInstall in driversToInstall) { // required = false (aka optional) def matchedDriver = manifest.drivers.find { it.id == driverToInstall} if (matchedDriver != null) { def location = getItemDownloadLocation(matchedDriver) setBackgroundStatusMessage("Downloading ${matchedDriver.name}") def fileContents = downloadFile(location) if (fileContents == null) { state.manifests.remove(pkgInstall) return triggerError("Failed download of driver file", "An error occurred downloading ${location}", false) } driverFiles[location] = fileContents } } initializeRollbackState("install") // All files downloaded, execute installs for (bundleToInstall in requiredBundles) { // required = true def location = getItemDownloadLocation(bundleToInstall.value) setBackgroundStatusMessage("Installing ${bundleToInstall.value.name} from $location") if (!installBundle(location, false)) { state.manifests.remove(pkgInstall) return rollback("Failed to install bundle ${bundleToInstall.value.name} using ${location}. Please notify the package developer.", false) } bundleToInstall.value.beta = shouldInstallBeta(bundleToInstall.value) } for (bundleToInstall in bundlesToInstall) { // required = false (aka optional) def matchedBundle = manifest.bundles.find { it.id == bundleToInstall} if (matchedBundle != null) { def location = getItemDownloadLocation(matchedBundle) def primary = matchedBundle.primary ?: false setBackgroundStatusMessage("Installing ${matchedBundle.name} from $location") if (!installBundle(location, primary)) { state.manifests.remove(pkgInstall) return rollback("Failed to install bundle ${matchedBundle.name} using ${location}. Please notify the package developer.", false) } } bundleToInstall.value.beta = shouldInstallBeta(bundleToInstall.value) } for (requiredApp in requiredApps) { // required = true def location = getItemDownloadLocation(requiredApp.value) setBackgroundStatusMessage("Installing ${requiredApp.value.name}") def id = installApp(appFiles[location]) if (id == null) { state.manifests.remove(pkgInstall) return rollback("Failed to install app ${location}. Please notify the package developer.", false) } requiredApp.value.heID = id requiredApp.value.beta = shouldInstallBeta(requiredApp.value) if (requiredApp.value.oauth) enableOAuth(requiredApp.value.heID) } for (appToInstall in appsToInstall) { // required = false (aka optional) def matchedApp = manifest.apps.find { it.id == appToInstall} if (matchedApp != null) { def location = getItemDownloadLocation(matchedApp) setBackgroundStatusMessage("Installing ${matchedApp.name}") def id = installApp(appFiles[location]) if (id == null) { state.manifests.remove(pkgInstall) return rollback("Failed to install app ${location}. Please notify the package developer.", false) } matchedApp.heID = id matchedApp.beta = shouldInstallBeta(matchedApp) if (matchedApp.oauth) enableOAuth(matchedApp.heID) } } for (requiredDriver in requiredDrivers) { // required = true def location = getItemDownloadLocation(requiredDriver.value) setBackgroundStatusMessage("Installing ${requiredDriver.value.name}") def id = installDriver(driverFiles[location]) if (id == null) { state.manifests.remove(pkgInstall) return rollback("Failed to install driver ${location}. Please notify the package developer.", false) } requiredDriver.value.heID = id requiredDriver.value.beta = shouldInstallBeta(requiredDriver.value) } for (driverToInstall in driversToInstall) { // required = false (aka optional) def matchedDriver = manifest.drivers.find { it.id == driverToInstall} if (matchedDriver != null) { def location = getItemDownloadLocation(matchedDriver) setBackgroundStatusMessage("Installing ${matchedDriver.name}") def id = installDriver(driverFiles[location]) if (id == null) { state.manifests.remove(pkgInstall) return rollback("Failed to install driver ${location}. Please notify the package developer.", false) } matchedDriver.heID = id matchedDriver.beta = shouldInstallBeta(matchedDriver) } } for (fileToInstall in manifest.files) { def txType = fileToInstall.transferType def location = getItemDownloadLocation(fileToInstall) def fileContents = fileManagerFiles[location] setBackgroundStatusMessage("Installing ${location}") def fileInstaller = (txType == "binary") ? this.&installFileBinary : this.&installFile if (!fileInstaller(fileToInstall.id, fileToInstall.name, fileContents)) { return rollback("Failed to install file ${location}. Please notify the package developer.", false) } completedActions["fileInstalls"] << fileToInstall } atomicState.backgroundActionInProgress = false app.updateSetting("installBeta", false) } // Modify a package pathway def prefPkgModify() { if (state.mainMenu) return prefOptions() logDebug "prefPkgModify" def pkgsToList = getInstalledPackages(true) return dynamicPage(name: "prefPkgModify", title: "", nextPage: "prefPkgModifyChoices", install: false, uninstall: false) { displayHeader(' Modify') section { paragraph "Modify a Package" paragraph "Only packages that have optional components are shown below." input "pkgModify", "enum", title: "Choose the package to modify", options: pkgsToList, required: true } section { paragraph "
" input "btnMainMenu", "button", title: "Main Menu", width: 3 } } } def prefPkgModifyChoices() { if (state.mainMenu) return prefOptions() logDebug "prefPkgModifyChoices" def manifest = getInstalledManifest(pkgModify) def optionalApps = getOptionalAppsFromManifest(manifest) def optionalDrivers = getOptionalDriversFromManifest(manifest) def optionalBundles = getOptionalBundlesFromManifest(manifest) if (optionalApps?.size() > 0 || optionalDrivers?.size() > 0 || optionalBundles > 0) { def installedOptionalApps = [] def installedOptionalDrivers = [] def installedOptionalBundles = [] for (optApp in optionalApps) { if (isAppInstalled(manifest, optApp.key)) { installedOptionalApps << optApp.key } } for (optDriver in optionalDrivers) { if (isDriverInstalled(manifest, optDriver.key)) { installedOptionalDrivers << optDriver.key } } for (optBundle in optionalBundles) { if (isBundleInstalled(manifest, optBundle.key)) { installedOptionalBundles << optBundle.key } } return dynamicPage(name: "prefPkgModifyChoices", title: "", nextPage: "prefVerifyPackageChanges", install: false, uninstall: false) { displayHeader(' Modify') section { paragraph "Modify a Package" paragraph "Items below that are checked are currently installed. Those that are not checked are currently not installed." if (optionalApps.size() > 0) input "appsToModify", "enum", title: "Select the apps to install/uninstall", options: optionalApps, hideWhenEmpty: true, multiple: true, defaultValue: installedOptionalApps if (optionalDrivers.size() > 0) input "driversToModify", "enum", title: "Select the drivers to install/uninstall", options: optionalDrivers, hideWhenEmpty: true, multiple: true, defaultValue: installedOptionalDrivers } section { paragraph "
" input "btnMainMenu", "button", title: "Main Menu", width: 3 } } } else { return dynamicPage(name: "prefPkgModifyChoices", title: "", install: true, uninstall: false) { section { paragraph "Nothing to modify" paragraph "This package does not have any optional components that you can modify." } section { paragraph "
" input "btnMainMenu", "button", title: "Main Menu", width: 3 } } } } def prefVerifyPackageChanges() { if (state.mainMenu) return prefOptions() logDebug "prefVerifyPackageChanges" def appsToUninstallStr = "