Skip to content

Commit

Permalink
Use get update type function
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Jan 21, 2025
1 parent fa38441 commit dc9abc8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
37 changes: 18 additions & 19 deletions src/data/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,32 +207,31 @@ export const computeUpdateStateDisplay = (
return hass.formatEntityState(stateObj);
};

export const isAddonUpdate = (
type UpdateType = "addon" | "home_assistant" | "generic";

export const getUpdateType = (
stateObj: UpdateEntity,
entitySources: EntitySources
): boolean => {
): UpdateType => {
const entity_id = stateObj.entity_id;
const domain = entitySources[entity_id]?.domain;
if (domain !== "hassio") {
return false;
return "generic";
}

const title = stateObj.attributes.title || "";
return ![
HOME_ASSISTANT_CORE_TITLE,
HOME_ASSISTANT_SUPERVISOR_TITLE,
HOME_ASSISTANT_OS_TITLE,
].includes(title);
};
if (title === HOME_ASSISTANT_CORE_TITLE) {
return "home_assistant";
}

export const isHomeAssistantUpdate = (
stateObj: UpdateEntity,
entitySources: EntitySources
): boolean => {
const entity_id = stateObj.entity_id;
const domain = entitySources[entity_id]?.domain;
if (domain !== "hassio") {
return false;
if (
![
HOME_ASSISTANT_CORE_TITLE,
HOME_ASSISTANT_SUPERVISOR_TITLE,
HOME_ASSISTANT_OS_TITLE,
].includes(title)
) {
return "addon";
}
const title = stateObj.attributes.title || "";
return title === HOME_ASSISTANT_CORE_TITLE;
return "generic";
};
20 changes: 9 additions & 11 deletions src/dialogs/more-info/controls/more-info-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import type { EntitySources } from "../../../data/entity_sources";
import { fetchEntitySourcesWithCache } from "../../../data/entity_sources";
import type { UpdateEntity } from "../../../data/update";
import {
isAddonUpdate,
isHomeAssistantUpdate,
getUpdateType,
UpdateEntityFeature,
updateIsInstalling,
updateReleaseNotes,
Expand Down Expand Up @@ -66,11 +65,12 @@ class MoreInfoUpdate extends LitElement {
return undefined;
}

const updateType = this._entitySources
? getUpdateType(this.stateObj, this._entitySources)
: "generic";

// Automatic or manual for Home Assistant update
if (
this._entitySources &&
isHomeAssistantUpdate(this.stateObj, this._entitySources)
) {
if (updateType === "home_assistant") {
const isBackupConfigValid =
!!this._backupConfig &&
!!this._backupConfig.create_backup.password &&
Expand Down Expand Up @@ -116,10 +116,7 @@ class MoreInfoUpdate extends LitElement {
}

// Addon backup
if (
this._entitySources &&
isAddonUpdate(this.stateObj, this._entitySources)
) {
if (updateType === "addon") {
const version = this.stateObj.attributes.installed_version;
return {
title: this.hass.localize(
Expand Down Expand Up @@ -320,7 +317,8 @@ class MoreInfoUpdate extends LitElement {
}
if (supportsFeature(this.stateObj!, UpdateEntityFeature.BACKUP)) {
this._fetchEntitySources().then(() => {
if (isHomeAssistantUpdate(this.stateObj!, this._entitySources!)) {
const type = getUpdateType(this.stateObj!, this._entitySources!);
if (type === "home_assistant") {
this._fetchBackupConfig();
}
});
Expand Down

0 comments on commit dc9abc8

Please sign in to comment.