Skip to content

Commit 08bed01

Browse files
authored
Link to create BinarySensors from project table (#203)
1 parent b647b13 commit 08bed01

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

src/components/knx-configure-entity.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "@ha/components/ha-expansion-panel";
1010
import "@ha/components/ha-selector/ha-selector";
1111
import "@ha/components/ha-settings-row";
1212

13+
import { mainWindow } from "@ha/common/dom/get_main_window";
1314
import { fireEvent } from "@ha/common/dom/fire_event";
1415
import type { HomeAssistant } from "@ha/types";
1516

@@ -43,9 +44,34 @@ export class KNXConfigureEntity extends LitElement {
4344
connectedCallback(): void {
4445
super.connectedCallback();
4546
if (!this.config) {
46-
// fill base keys to get better validation error messages
47+
// set base keys to get better validation error messages
4748
this.config = { entity: {}, knx: {} };
49+
50+
// url params are extracted to config.
51+
// /knx/entities/create/binary_sensor?knx.ga_sensor.state=0/1/4
52+
// would set this.conifg.knx.ga_sensor.state to "0/1/4"
53+
// TODO: this is not checked against any schema
54+
const urlParams = new URLSearchParams(mainWindow.location.search);
55+
this._url_suggestions = Object.fromEntries(urlParams.entries());
56+
for (const [path, value] of Object.entries(this._url_suggestions)) {
57+
this._setNestedValue(path, value);
58+
fireEvent(this, "knx-entity-configuration-changed", this.config);
59+
}
60+
}
61+
}
62+
63+
private _setNestedValue(path: string, value: any) {
64+
const keys = path.split(".");
65+
const keysTail = keys.pop();
66+
if (!keysTail) return;
67+
let current = this.config!;
68+
for (const key of keys) {
69+
if (!(key in current)) {
70+
current[key] = {};
71+
}
72+
current = current[key];
4873
}
74+
current[keysTail] = value;
4975
}
5076

5177
protected render(): TemplateResult | void {

src/views/entities_create.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ export class KNXCreateEntity extends LitElement {
116116
this._loading = false;
117117
});
118118
}
119-
// const urlParams = new URLSearchParams(mainWindow.location.search);
120-
// const referrerGA = urlParams.get("ga");
121-
// console.log(referrerGA);
122119
}
123120
}
124121

src/views/project_view.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { mdiFilterVariant } from "@mdi/js";
1+
import { mdiFilterVariant, mdiPlus } from "@mdi/js";
22
import type { TemplateResult } from "lit";
33
import { LitElement, html, css, nothing } from "lit";
44
import { customElement, property, state } from "lit/decorators";
55

66
import memoize from "memoize-one";
77

88
import type { HASSDomEvent } from "@ha/common/dom/fire_event";
9+
import { navigate } from "@ha/common/navigate";
910
import "@ha/layouts/hass-loading-screen";
1011
import "@ha/layouts/hass-tabs-subpage";
1112
import type { PageNavigation } from "@ha/layouts/hass-tabs-subpage";
@@ -14,8 +15,8 @@ import "@ha/components/ha-icon-button";
1415
import "@ha/components/ha-icon-overflow-menu";
1516
import "@ha/components/data-table/ha-data-table";
1617
import type { DataTableColumnContainer } from "@ha/components/data-table/ha-data-table";
18+
import type { IconOverflowMenuItem } from "@ha/components/ha-icon-overflow-menu";
1719
import { relativeTime } from "@ha/common/datetime/relative_time";
18-
import { navigate } from "@ha/common/navigate";
1920

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

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

108110
return {
109111
address: {
@@ -159,9 +161,36 @@ export class KNXProjectView extends LitElement {
159161
</div>`;
160162
},
161163
},
164+
actions: {
165+
title: "",
166+
minWidth: overflowMenuWidth,
167+
type: "overflow-menu",
168+
template: (ga: GroupAddress) => this._groupAddressMenu(ga),
169+
},
162170
};
163171
});
164172

173+
private _groupAddressMenu(groupAddress: GroupAddress): TemplateResult | typeof nothing {
174+
const items: IconOverflowMenuItem[] = [];
175+
if (groupAddress.dpt?.main === 1) {
176+
items.push({
177+
path: mdiPlus,
178+
label: "Create binary sensor",
179+
action: () => {
180+
navigate(
181+
"/knx/entities/create/binary_sensor?knx.ga_sensor.state=" + groupAddress.address,
182+
);
183+
},
184+
});
185+
}
186+
187+
return items.length
188+
? html`
189+
<ha-icon-overflow-menu .hass=${this.hass} narrow .items=${items}> </ha-icon-overflow-menu>
190+
`
191+
: nothing;
192+
}
193+
165194
private _getRows(visibleGroupAddresses: string[]): GroupAddress[] {
166195
if (!visibleGroupAddresses.length)
167196
// if none is set, default to show all

0 commit comments

Comments
 (0)