Skip to content

Fixed version checks and added debug output #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PluginUpdatePlatform implements DynamicPlatformPlugin {
this.config = config as PluginUpdatePlatformConfig
this.api = api

this.uiApi = new UiApi(this.api.user.storagePath())
this.uiApi = new UiApi(this.api.user.storagePath(), this.log)
this.useNcu = this.config.forceNcu || !this.uiApi.isConfigured()
this.isDocker = fs.existsSync('/homebridge/package.json')
this.sensorInfo = this.getSensorInfo(this.config.sensorType)
Expand Down Expand Up @@ -153,35 +153,44 @@ class PluginUpdatePlatform implements DynamicPlatformPlugin {

if (homebridge.updateAvailable) {
updatesAvailable.push(homebridge)

this.log.debug(`Homebridge update available: ${homebridge.latestVersion}`)
}
}

if (this.checkHBUI || this.checkPlugins) {
let plugins = await this.uiApi.getPlugins()
const plugins = await this.uiApi.getPlugins()

// Filter out Homebridge UI plugin
if (!this.checkHBUI) {
plugins = plugins.filter(plugin => plugin.name !== 'homebridge-config-ui-x')
}
if (this.checkHBUI) {
const filteredPlugins = plugins.filter(plugin => plugin.name === 'homebridge-config-ui-x')
updatesAvailable.push(...filteredPlugins)

// Select only Homebridge UI plugin
if (!this.checkPlugins) {
plugins = plugins.filter(plugin => plugin.name === 'homebridge-config-ui-x')
filteredPlugins.forEach((plugin) => {
this.log.debug(`Homebridge UI update available: ${plugin.latestVersion}`)
})
}

plugins = plugins.filter(plugin => plugin.updateAvailable)
updatesAvailable.push(...plugins)
if (this.checkPlugins) {
const filteredPlugins = plugins.filter(plugin => plugin.name !== 'homebridge-config-ui-x')
updatesAvailable.push(...filteredPlugins)

filteredPlugins.forEach((plugin) => {
this.log.debug(`Homebridge plugin update available: ${plugin.name} ${plugin.latestVersion}`)
})
}
}

if (this.isDocker && this.checkDocker) {
const docker = await this.uiApi.getDocker()

if (docker.updateAvailable) {
updatesAvailable.push(docker)

this.log.debug(`Docker update available: ${docker.latestVersion}`)
}
}

this.log.debug(`homebridge-config-ui-x reports ${updatesAvailable.length} available update(s): ${JSON.stringify(updatesAvailable)}`)
this.log.debug(`Found ${updatesAvailable.length} available update(s)`)

return updatesAvailable.length
}
Expand Down
27 changes: 18 additions & 9 deletions src/ui-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/* eslint-disable node/prefer-global/process */
/* eslint-disable style/operator-linebreak */
/* eslint-disable object-shorthand */
/* eslint no-console: ["error", { allow: ["info", "warn", "error"] }] */

import type {
HomebridgeConfig,
Logging,
PlatformIdentifier,
PlatformName,
} from 'homebridge'
Expand Down Expand Up @@ -33,13 +39,16 @@ interface UiConfig {
}

export class UiApi {
private log: Logging
private readonly secrets?: SecretsFile
private readonly baseUrl?: string
private readonly httpsAgent?: https.Agent
private token?: string
private readonly dockerUrl?: string

constructor(hbStoragePath: string) {
constructor(hbStoragePath: string, log: Logging) {
this.log = log

const configPath = path.resolve(hbStoragePath, 'config.json')
const hbConfig = JSON.parse(readFileSync(configPath, 'utf8')) as HomebridgeConfig
const config = hbConfig.platforms.find((config: { platform: string }) =>
Expand Down Expand Up @@ -97,22 +106,22 @@ export class UiApi {
const currentDockerVersion = process.env.DOCKER_HOMEBRIDGE_VERSION

let dockerInfo: InstalledPlugin = {
name: '',
installedVersion: '',
latestVersion: '',
updateAvailable: false,
}
name: '',
installedVersion: '',
latestVersion: '',
updateAvailable: false,
}

if (this.isConfigured() && currentDockerVersion !== undefined) {
const json = await this.makeDockerCall('/v2/repositories/homebridge/homebridge/tags/?page_size=10&page=1&ordering=last_updated')
const versions = JSON.parse(json).results as any[]
const json = await this.makeDockerCall('/v2/repositories/homebridge/homebridge/tags/?page_size=30&page=1&ordering=last_updated')
const versions = json.results as any[]

const installedVersion = versions.filter(version => version.name === currentDockerVersion)[0]
const installedVersionDate = Date.parse(installedVersion.last_updated)

const availableVersions = versions.filter(version =>
!(version.name as string).includes('beta') &&
(Date.parse(version.last_updated) > installedVersionDate)
(Date.parse(version.last_updated) > installedVersionDate),
)
if (availableVersions.length > 0) {
dockerInfo = {
Expand Down
Loading