Skip to content

Commit

Permalink
Link to create BinarySensors from project table (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
farmio authored Jan 30, 2025
1 parent b647b13 commit 08bed01
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
28 changes: 27 additions & 1 deletion src/components/knx-configure-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "@ha/components/ha-expansion-panel";
import "@ha/components/ha-selector/ha-selector";
import "@ha/components/ha-settings-row";

import { mainWindow } from "@ha/common/dom/get_main_window";
import { fireEvent } from "@ha/common/dom/fire_event";
import type { HomeAssistant } from "@ha/types";

Expand Down Expand Up @@ -43,9 +44,34 @@ export class KNXConfigureEntity extends LitElement {
connectedCallback(): void {
super.connectedCallback();
if (!this.config) {
// fill base keys to get better validation error messages
// set base keys to get better validation error messages
this.config = { entity: {}, knx: {} };

// url params are extracted to config.
// /knx/entities/create/binary_sensor?knx.ga_sensor.state=0/1/4
// would set this.conifg.knx.ga_sensor.state to "0/1/4"
// TODO: this is not checked against any schema
const urlParams = new URLSearchParams(mainWindow.location.search);
this._url_suggestions = Object.fromEntries(urlParams.entries());
for (const [path, value] of Object.entries(this._url_suggestions)) {
this._setNestedValue(path, value);
fireEvent(this, "knx-entity-configuration-changed", this.config);
}
}
}

private _setNestedValue(path: string, value: any) {
const keys = path.split(".");
const keysTail = keys.pop();
if (!keysTail) return;
let current = this.config!;
for (const key of keys) {
if (!(key in current)) {
current[key] = {};
}
current = current[key];
}
current[keysTail] = value;
}

protected render(): TemplateResult | void {
Expand Down
3 changes: 0 additions & 3 deletions src/views/entities_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ export class KNXCreateEntity extends LitElement {
this._loading = false;
});
}
// const urlParams = new URLSearchParams(mainWindow.location.search);
// const referrerGA = urlParams.get("ga");
// console.log(referrerGA);
}
}

Expand Down
33 changes: 31 additions & 2 deletions src/views/project_view.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { mdiFilterVariant } from "@mdi/js";
import { mdiFilterVariant, mdiPlus } from "@mdi/js";
import type { TemplateResult } from "lit";
import { LitElement, html, css, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";

import memoize from "memoize-one";

import type { HASSDomEvent } from "@ha/common/dom/fire_event";
import { navigate } from "@ha/common/navigate";
import "@ha/layouts/hass-loading-screen";
import "@ha/layouts/hass-tabs-subpage";
import type { PageNavigation } from "@ha/layouts/hass-tabs-subpage";
Expand All @@ -14,8 +15,8 @@ import "@ha/components/ha-icon-button";
import "@ha/components/ha-icon-overflow-menu";
import "@ha/components/data-table/ha-data-table";
import type { DataTableColumnContainer } from "@ha/components/data-table/ha-data-table";
import type { IconOverflowMenuItem } from "@ha/components/ha-icon-overflow-menu";
import { relativeTime } from "@ha/common/datetime/relative_time";
import { navigate } from "@ha/common/navigate";

import "../components/knx-project-tree-view";

Expand Down Expand Up @@ -104,6 +105,7 @@ export class KNXProjectView extends LitElement {
private _columns = memoize((_narrow, _language): DataTableColumnContainer<GroupAddress> => {
const addressWidth = "100px";
const dptWidth = "82px";
const overflowMenuWidth = "72px";

return {
address: {
Expand Down Expand Up @@ -159,9 +161,36 @@ export class KNXProjectView extends LitElement {
</div>`;
},
},
actions: {
title: "",
minWidth: overflowMenuWidth,
type: "overflow-menu",
template: (ga: GroupAddress) => this._groupAddressMenu(ga),
},
};
});

private _groupAddressMenu(groupAddress: GroupAddress): TemplateResult | typeof nothing {
const items: IconOverflowMenuItem[] = [];
if (groupAddress.dpt?.main === 1) {
items.push({
path: mdiPlus,
label: "Create binary sensor",
action: () => {
navigate(
"/knx/entities/create/binary_sensor?knx.ga_sensor.state=" + groupAddress.address,
);
},
});
}

return items.length
? html`
<ha-icon-overflow-menu .hass=${this.hass} narrow .items=${items}> </ha-icon-overflow-menu>
`
: nothing;
}

private _getRows(visibleGroupAddresses: string[]): GroupAddress[] {
if (!visibleGroupAddresses.length)
// if none is set, default to show all
Expand Down

0 comments on commit 08bed01

Please sign in to comment.