Skip to content
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
53 changes: 18 additions & 35 deletions src/data/zwave_js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export interface ZWaveJSController {
supports_timers: boolean;
is_heal_network_active: boolean;
inclusion_state: InclusionState;
nodes: number[];
nodes: ZWaveJSNodeStatus[];
}

export interface ZWaveJSNodeStatus {
Expand Down Expand Up @@ -200,8 +200,7 @@ export interface ZWaveJSNodeConfigParamMetadata {

export interface ZWaveJSSetConfigParamData {
type: string;
entry_id: string;
node_id: number;
device_id: string;
property: number;
property_key?: number;
value: string | number;
Expand Down Expand Up @@ -427,49 +426,41 @@ export const unprovisionZwaveSmartStartNode = (

export const fetchZwaveNodeStatus = (
hass: HomeAssistant,
entry_id: string,
node_id: number
device_id: string
): Promise<ZWaveJSNodeStatus> =>
hass.callWS({
type: "zwave_js/node_status",
entry_id,
node_id,
device_id,
});

export const fetchZwaveNodeMetadata = (
hass: HomeAssistant,
entry_id: string,
node_id: number
device_id: string
): Promise<ZwaveJSNodeMetadata> =>
hass.callWS({
type: "zwave_js/node_metadata",
entry_id,
node_id,
device_id,
});

export const fetchZwaveNodeConfigParameters = (
hass: HomeAssistant,
entry_id: string,
node_id: number
device_id: string
): Promise<ZWaveJSNodeConfigParams> =>
hass.callWS({
type: "zwave_js/get_config_parameters",
entry_id,
node_id,
device_id,
});

export const setZwaveNodeConfigParameter = (
hass: HomeAssistant,
entry_id: string,
node_id: number,
device_id: string,
property: number,
value: number,
property_key?: number
): Promise<ZWaveJSSetConfigParamResult> => {
const data: ZWaveJSSetConfigParamData = {
type: "zwave_js/set_config_parameter",
entry_id,
node_id,
device_id,
property,
value,
property_key,
Expand All @@ -479,42 +470,36 @@ export const setZwaveNodeConfigParameter = (

export const reinterviewZwaveNode = (
hass: HomeAssistant,
entry_id: string,
node_id: number,
device_id: string,
callbackFunction: (message: ZWaveJSRefreshNodeStatusMessage) => void
): Promise<UnsubscribeFunc> =>
hass.connection.subscribeMessage(
(message: any) => callbackFunction(message),
{
type: "zwave_js/refresh_node_info",
entry_id,
node_id,
device_id,
}
);

export const healZwaveNode = (
hass: HomeAssistant,
entry_id: string,
node_id: number
device_id: string
): Promise<boolean> =>
hass.callWS({
type: "zwave_js/heal_node",
entry_id,
node_id,
device_id,
});

export const removeFailedZwaveNode = (
hass: HomeAssistant,
entry_id: string,
node_id: number,
device_id: string,
callbackFunction: (message: any) => void
): Promise<UnsubscribeFunc> =>
hass.connection.subscribeMessage(
(message: any) => callbackFunction(message),
{
type: "zwave_js/remove_failed_node",
entry_id,
node_id,
device_id,
}
);

Expand All @@ -538,16 +523,14 @@ export const stopHealZwaveNetwork = (

export const subscribeZwaveNodeReady = (
hass: HomeAssistant,
entry_id: string,
node_id: number,
device_id: string,
callbackFunction: (message) => void
): Promise<UnsubscribeFunc> =>
hass.connection.subscribeMessage(
(message: any) => callbackFunction(message),
{
type: "zwave_js/node_ready",
entry_id,
node_id,
device_id,
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import { customElement, property, state } from "lit/decorators";
import { DeviceRegistryEntry } from "../../../../../../data/device_registry";
import {
fetchZwaveNodeStatus,
getZwaveJsIdentifiersFromDevice,
ZWaveJSNodeIdentifiers,
ZWaveJSNodeStatus,
} from "../../../../../../data/zwave_js";
import { haStyle } from "../../../../../../resources/styles";
import { HomeAssistant } from "../../../../../../types";
import { showZWaveJSReinterviewNodeDialog } from "../../../../integrations/integration-panels/zwave_js/show-dialog-zwave_js-reinterview-node";
import { showZWaveJSHealNodeDialog } from "../../../../integrations/integration-panels/zwave_js/show-dialog-zwave_js-heal-node";
import { showZWaveJSRemoveFailedNodeDialog } from "../../../../integrations/integration-panels/zwave_js/show-dialog-zwave_js-remove-failed-node";
import { getConfigEntries } from "../../../../../../data/config_entries";

@customElement("ha-device-actions-zwave_js")
export class HaDeviceActionsZWaveJS extends LitElement {
Expand All @@ -29,34 +28,37 @@ export class HaDeviceActionsZWaveJS extends LitElement {

@state() private _entryId?: string;

@state() private _nodeId?: number;

@state() private _node?: ZWaveJSNodeStatus;

protected updated(changedProperties: PropertyValues) {
public willUpdate(changedProperties: PropertyValues) {
super.willUpdate(changedProperties);
if (changedProperties.has("device")) {
const identifiers: ZWaveJSNodeIdentifiers | undefined =
getZwaveJsIdentifiersFromDevice(this.device);
if (!identifiers) {
return;
}
this._nodeId = identifiers.node_id;
this._entryId = this.device.config_entries[0];

this._fetchNodeDetails();
}
}

protected async _fetchNodeDetails() {
if (!this._nodeId || !this._entryId) {
if (!this.device) {
return;
}

this._node = await fetchZwaveNodeStatus(
this.hass,
this._entryId,
this._nodeId
this._node = undefined;

const configEntries = await getConfigEntries(this.hass, {
domain: "zwave_js",
});

const configEntry = configEntries.find((entry) =>
this.device.config_entries.includes(entry.entry_id)
);

if (!configEntry) {
return;
}

this._entryId = configEntry.entry_id;

this._node = await fetchZwaveNodeStatus(this.hass, this.device.id);
}

protected render(): TemplateResult {
Expand Down Expand Up @@ -96,33 +98,30 @@ export class HaDeviceActionsZWaveJS extends LitElement {
}

private async _reinterviewClicked() {
if (!this._nodeId || !this._entryId) {
if (!this.device) {
return;
}
showZWaveJSReinterviewNodeDialog(this, {
entry_id: this._entryId,
node_id: this._nodeId,
device_id: this.device.id,
});
}

private async _healNodeClicked() {
if (!this._nodeId || !this._entryId) {
if (!this.device) {
return;
}
showZWaveJSHealNodeDialog(this, {
entry_id: this._entryId,
node_id: this._nodeId,
entry_id: this._entryId!,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need entry_id here, can't we also make that work with device id?

Copy link
Copy Markdown
Contributor Author

@raman325 raman325 May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the entry ID is still needed by dialog-zwave_js-heal-node so that it can get the network status here:

const network: ZWaveJSNetwork = await fetchZwaveNetworkStatus(
this.hass!,
this.entry_id!
);
We can pass just the device ID in and derive the entry ID in dialog-zwave_js-heal-node but we currently already have to figure out entry ID in ha-device-actions-zwave_js so that we can add it to the URL, so why recalculate it? Is it necessary to add it to the URL though? I couldn't figure out why we do that, and if it's not necessary, we can just pass the device ID in and only calculate entry ID in the heal node dialog

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another alternative would be to allow fetchZwaveNetworkStatus to accept either an entry ID or a device ID since it's used in both contexts. This could easily be supported in the core

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like the nicest approach

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to merge this and do that in a new PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea

device: this.device,
});
}

private async _removeFailedNode() {
if (!this._nodeId || !this._entryId) {
if (!this.device) {
return;
}
showZWaveJSRemoveFailedNodeDialog(this, {
entry_id: this._entryId,
node_id: this._nodeId,
device_id: this.device.id,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import {
} from "../../../../../../data/config_entries";
import {
fetchZwaveNodeStatus,
getZwaveJsIdentifiersFromDevice,
nodeStatus,
ZWaveJSNodeStatus,
ZWaveJSNodeIdentifiers,
SecurityClass,
} from "../../../../../../data/zwave_js";
import { haStyle } from "../../../../../../resources/styles";
Expand All @@ -29,57 +27,41 @@ export class HaDeviceInfoZWaveJS extends LitElement {

@property({ attribute: false }) public device!: DeviceRegistryEntry;

@state() private _entryId?: string;

@state() private _configEntry?: ConfigEntry;

@state() private _multipleConfigEntries = false;

@state() private _nodeId?: number;

@state() private _node?: ZWaveJSNodeStatus;

protected updated(changedProperties: PropertyValues) {
public willUpdate(changedProperties: PropertyValues) {
super.willUpdate(changedProperties);
if (changedProperties.has("device")) {
const identifiers: ZWaveJSNodeIdentifiers | undefined =
getZwaveJsIdentifiersFromDevice(this.device);
if (!identifiers) {
return;
}
this._nodeId = identifiers.node_id;
this._entryId = this.device.config_entries[0];

this._fetchNodeDetails();
}
}

protected async _fetchNodeDetails() {
if (!this._nodeId || !this._entryId) {
if (!this.device) {
return;
}

const configEntries = await getConfigEntries(this.hass, {
domain: "zwave_js",
});
let zwaveJsConfEntries = 0;
for (const entry of configEntries) {
if (zwaveJsConfEntries) {
this._multipleConfigEntries = true;
}
if (entry.entry_id === this._entryId) {
this._configEntry = entry;
}
if (this._configEntry && this._multipleConfigEntries) {
break;
}
zwaveJsConfEntries++;
}

this._node = await fetchZwaveNodeStatus(
this.hass,
this._entryId,
this._nodeId
this._multipleConfigEntries = configEntries.length > 1;

const configEntry = configEntries.find((entry) =>
this.device.config_entries.includes(entry.entry_id)
);

if (!configEntry) {
return;
}

this._configEntry = configEntry;

this._node = await fetchZwaveNodeStatus(this.hass, this.device.id);
}

protected render(): TemplateResult {
Expand Down
6 changes: 2 additions & 4 deletions src/panels/config/devices/ha-config-device-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,13 +895,12 @@ export class HaConfigDevicePage extends LitElement {
}

private _renderIntegrationInfo(
device,
device: DeviceRegistryEntry,
integrations: ConfigEntry[],
deviceInfo: TemplateResult[],
deviceActions: (string | TemplateResult)[]
): TemplateResult[] {
) {
const domains = integrations.map((int) => int.domain);
const templates: TemplateResult[] = [];
if (domains.includes("mqtt")) {
import(
"./device-detail/integration-elements/mqtt/ha-device-actions-mqtt"
Expand Down Expand Up @@ -949,7 +948,6 @@ export class HaConfigDevicePage extends LitElement {
></ha-device-actions-zwave_js>
`);
}
return templates;
}

private async _showSettings() {
Expand Down
Loading