Skip to content

Commit

Permalink
Prepare for 0.107.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Feb 29, 2020
1 parent 9cae899 commit aebd1af
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 90 deletions.
5 changes: 4 additions & 1 deletion src/HacsFrontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
SelectedValue,
LocationChangedEvent,
LovelaceConfig,
LovelaceResourceConfig,
RepositoryActionData,
getConfiguration,
getRepositories,
Expand All @@ -40,7 +41,7 @@ class HacsFrontendBase extends LitElement {
@property() public repository_view: boolean = false;
@property() public hacs!: HACS;
@property() public hass!: HomeAssistant;
@property() public lovelaceconfig: LovelaceConfig;
@property() public lovelaceconfig: LovelaceConfig | LovelaceResourceConfig[];
@property() public route!: Route;

private status: Status;
Expand Down Expand Up @@ -327,6 +328,8 @@ class HacsFrontendBase extends LitElement {
.hacs=${this.hacs}
.hass=${this.hass}
.route=${this.route}
.lovelaceconfig=${this.lovelaceconfig}
.configuration=${this.configuration}
>
</hacs-settings>
`
Expand Down
1 change: 1 addition & 0 deletions src/LoadUIElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import "./components/buttons/HacsButtonRestartHomeAssistant";
import "./components/buttons/HacsButtonUninstall";
import "./components/repositorybanner/IntegrationFirstInstall";
import "./components/repositorybanner/IntegrationPendingRestart";
import "./components/repositorybanner/LegacyUrlForPlugins";
import "./components/repositorybanner/PluginNotLoaded";
import "./components/HacsBody";
import "./components/HacsLink";
Expand Down
36 changes: 22 additions & 14 deletions src/components/buttons/HacsButtonAddToLovelace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export class HacsButtonAddToLovelace extends HacsRepositoryButton {
logger = new Logger("add_to_lovelace");
render(): TemplateResult | void {
if (!this.repository.installed) return html``;
if (this.repository.javascript_type === null) return html``;

return html`
<mwc-button @click=${this.RepositoryAddToLovelace}>
Expand All @@ -22,11 +21,13 @@ export class HacsButtonAddToLovelace extends HacsRepositoryButton {
`;
}

RepositoryAddToLovelace() {
swal(localize("confirm.add_to_lovelace"), {
async RepositoryAddToLovelace() {
const value = await swal(localize("confirm.add_to_lovelace"), {
buttons: [localize("confirm.no"), localize("confirm.yes")]
}).then(value => {
if (value !== null) {
});

if (value !== null) {
if (this.hass.config.version.split(".")[1] <= "106") {
this.hass.connection
.sendMessagePromise({
type: "lovelace/config",
Expand All @@ -36,14 +37,10 @@ export class HacsButtonAddToLovelace extends HacsRepositoryButton {
resp => {
var currentConfig = resp as LovelaceConfig;
const cardConfig: LovelaceResourceConfig = {
type: this.repository.javascript_type as
| "css"
| "js"
| "module"
| "html",
url: `/community_plugin/${
this.repository.full_name.split("/")[1]
}/${this.repository.file_name}`
type: "module",
url: `/hacsfiles/${this.repository.full_name.split("/")[1]}/${
this.repository.file_name
}`
};
if (currentConfig.resources)
currentConfig.resources!.push(cardConfig);
Expand All @@ -58,7 +55,18 @@ export class HacsButtonAddToLovelace extends HacsRepositoryButton {
this.logger.error(err);
}
);
} else {
this.hass.callWS({
type: "lovelace/resources/create",
res_type: "module",
url: `/hacsfiles/${this.repository.full_name.split("/")[1]}/${
this.repository.file_name
}`
});
}
});
this.dispatchEvent(
new CustomEvent("hacs-force-reload", { bubbles: true, composed: true })
);
}
}
}
2 changes: 1 addition & 1 deletion src/components/buttons/HacsButtonOpenPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class HacsButtonOpenPlugin extends HacsRepositoryButton {

return html`
<a
href="/community_plugin/${path.pop()}/${this.repository.file_name}"
href="/hacsfiles/${path.pop()}/${this.repository.file_name}"
target="_blank"
>
<mwc-button>
Expand Down
89 changes: 55 additions & 34 deletions src/components/buttons/HacsButtonUninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,20 @@ export class HacsButtonUninstall extends HacsRepositoryButton {
swal(localize("confirm.bg_task"));
}

RepositoryUnInstall() {
swal(localize("confirm.uninstall", "{item}", this.repository.name), {
buttons: [localize("confirm.no"), localize("confirm.yes")]
}).then(value => {
if (value !== null) {
this.ExecuteAction();
async RepositoryUnInstall() {
const value = await swal(
localize("confirm.uninstall", "{item}", this.repository.name),
{
buttons: [localize("confirm.no"), localize("confirm.yes")]
}
});
);

if (value !== null) {
await this.ExecuteAction();
}
}

ExecuteAction() {
async ExecuteAction() {
RepositoryWebSocketAction(
this.hass,
this.repository.id,
Expand All @@ -80,36 +83,54 @@ export class HacsButtonUninstall extends HacsRepositoryButton {
this.repository.category === "plugin" &&
this.status.lovelace_mode === "storage"
) {
this.RepositoryRemoveFromLovelace();
await this.RepositoryRemoveFromLovelace();
}
RepositoryWebSocketAction(this.hass, this.repository.id, "uninstall");
}
RepositoryRemoveFromLovelace() {
this.hass.connection
.sendMessagePromise({
type: "lovelace/config",
force: false
})
.then(resp => {
const currentConfig = resp as LovelaceConfig;
const url = `/community_plugin/${
this.repository.full_name.split("/")[1]
}/${this.repository.file_name}`;
async RepositoryRemoveFromLovelace() {
const url1 = `/community_plugin/${
this.repository.full_name.split("/")[1]
}/${this.repository.file_name}`;
const url2 = `/hacsfiles/${this.repository.full_name.split("/")[1]}/${
this.repository.file_name
}`;
if (this.hass.config.version.split(".")[1] <= "106") {
this.hass.connection
.sendMessagePromise({
type: "lovelace/config",
force: false
})
.then(resp => {
const currentConfig = resp as LovelaceConfig;

if (currentConfig.resources) {
const resources: LovelaceResourceConfig[] = currentConfig.resources.filter(
(element: LovelaceResourceConfig) => {
if (element.url === url) {
return false;
} else return true;
}
);
currentConfig.resources = resources;
this.hass.callWS({
type: "lovelace/config/save",
config: currentConfig
});
}
if (currentConfig.resources) {
const resources: LovelaceResourceConfig[] = currentConfig.resources.filter(
(element: LovelaceResourceConfig) => {
if (element.url === url1 || element.url === url2) {
return false;
} else return true;
}
);
currentConfig.resources = resources;
this.hass.callWS({
type: "lovelace/config/save",
config: currentConfig
});
}
});
} else {
const resources = await this.hass.callWS<LovelaceResourceConfig[]>({
type: "lovelace/resources"
});
const resource: LovelaceResourceConfig = resources.find(function(
element
) {
return element.url === url1 || element.url === url2;
});
this.hass.callWS({
type: "lovelace/resources/delete",
resource_id: resource.id
});
}
}
}
5 changes: 3 additions & 2 deletions src/components/repositorybanner/HacsRepositoryBanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
Configuration,
Status,
Route,
LovelaceConfig
LovelaceConfig,
LovelaceResourceConfig
} from "../../data";
import { HACS } from "../../Hacs";
import { HomeAssistant } from "custom-card-helpers";
Expand All @@ -21,7 +22,7 @@ export class HacsRepositoryBanner extends LitElement {
@property() public hacs!: HACS;
@property() public configuration: Configuration;
@property() public hass!: HomeAssistant;
@property() public lovelaceconfig: LovelaceConfig;
@property() public lovelaceconfig: LovelaceConfig | LovelaceResourceConfig[];
@property() public repository!: RepositoryData;
@property() public route!: Route;
@property() public status!: Status;
Expand Down
50 changes: 50 additions & 0 deletions src/components/repositorybanner/LegacyUrlForPlugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { customElement, TemplateResult, html, property } from "lit-element";

import { HacsRepositoryBanner } from "./HacsRepositoryBanner";
import { LovelaceResourceConfig } from "../../data";

@customElement("hacs-legacy-url-for-plugins")
export class HacsLegacyUrlForPlugins extends HacsRepositoryBanner {
@property() private _wrongURL: LovelaceResourceConfig[] = [];
protected render(): TemplateResult | void {
const title = "Legacy URL's detected";
const resources = this.lovelaceconfig as LovelaceResourceConfig[];

if (!resources) return html``;

this._wrongURL = resources?.filter(resource => {
return resource.url.startsWith("/community_plugin");
});

if (this._wrongURL.length === 0) return html``;
if (this.hass.config.version.split(".")[1] <= "106") return html``;

return html`
<ha-card class="info" .header="${title}">
<div class="card-content">
You have plugins resources in your lovelace configuration that still
uses the old "/community_plugin" URL and not the new "/hacsfiles"
</div>
<div class="card-actions">
<mwc-button @click=${this.UpdateResources}>
Update resources
</mwc-button>
</div>
</ha-card>
`;
}

async UpdateResources() {
this._wrongURL.forEach(resource => {
const url = resource.url.replace("/community_plugin", "/hacsfiles");
this.hass.callWS({
type: "lovelace/resources/update",
resource_id: resource.id,
url
});
});
this.dispatchEvent(
new CustomEvent("hacs-force-reload", { bubbles: true, composed: true })
);
}
}
21 changes: 21 additions & 0 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export interface LovelaceCardConfig {
export interface LovelaceResourceConfig {
type: "css" | "js" | "module" | "html";
url: string;
id?: string;
}

export interface HacsBanner extends HTMLElement {
Expand Down Expand Up @@ -191,6 +192,13 @@ export const getStatus = async (hass: HomeAssistant) => {
};

export const getLovelaceConfiguration = async (hass: HomeAssistant) => {
if (hass.config.version.split(".")[1] <= "106") {
return await getLovelaceConfigLegacy(hass);
}
return getLovelaceResources(hass);
};

const getLovelaceConfigLegacy = async (hass: HomeAssistant) => {
try {
const response = await hass.connection.sendMessagePromise<LovelaceConfig>({
type: "lovelace/config"
Expand All @@ -200,3 +208,16 @@ export const getLovelaceConfiguration = async (hass: HomeAssistant) => {
return null;
}
};

const getLovelaceResources = async (hass: HomeAssistant) => {
try {
const response = await hass.connection.sendMessagePromise<
LovelaceResourceConfig[]
>({
type: "lovelace/resources"
});
return response;
} catch (e) {
return null;
}
};
24 changes: 17 additions & 7 deletions src/misc/AddedToLovelace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,32 @@ import {

export function AddedToLovelace(
repository: RepositoryData,
lovelaceconfig: LovelaceConfig,
lovelaceconfig: LovelaceConfig | LovelaceResourceConfig[],
status: Status
): boolean {
if (status.lovelace_mode === "yaml") return true;
if (lovelaceconfig) {
var loaded: boolean = false;
var URL: string = `/community_plugin/${
var URL1: string = `/community_plugin/${
repository.full_name.split("/")[1]
}/${repository.file_name}`;
var URL2: string = `/hacsfiles/${repository.full_name.split("/")[1]}/${
repository.file_name
}`;
let resources = lovelaceconfig;
if (lovelaceconfig.hasOwnProperty("resources")) {
resources = (lovelaceconfig as LovelaceConfig).resources;
} else if (lovelaceconfig.hasOwnProperty("views")) {
resources = [];
}

if (lovelaceconfig.resources !== undefined) {
lovelaceconfig.resources.forEach((item: LovelaceResourceConfig) => {
if (!loaded) {
if (item.url === URL) loaded = true;
if (resources) {
(resources as LovelaceResourceConfig[]).forEach(
(item: LovelaceResourceConfig) => {
if (item.url === URL1 || item.url === URL2 || item.type !== "module")
loaded = true;
}
});
);
}
return loaded;
} else return true;
Expand Down
Loading

0 comments on commit aebd1af

Please sign in to comment.