Skip to content

Commit

Permalink
Remove device subscription from zwave node config (#21891)
Browse files Browse the repository at this point in the history
remove device subscription from zwave node config
  • Loading branch information
bramkragten authored Sep 5, 2024
1 parent 0ff2f1b commit 618cd9d
Showing 1 changed file with 15 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import {
mdiCloseCircle,
mdiProgressClock,
} from "@mdi/js";
import { UnsubscribeFunc } from "home-assistant-js-websocket";
import {
css,
CSSResultGroup,
html,
LitElement,
nothing,
PropertyValues,
TemplateResult,
css,
html,
nothing,
} from "lit";
import { customElement, property, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map";
import memoizeOne from "memoize-one";
import { groupBy } from "../../../../../common/util/group-by";
import "../../../../../components/ha-alert";
import "../../../../../components/ha-card";
import "../../../../../components/ha-icon-next";
Expand All @@ -27,25 +26,19 @@ import "../../../../../components/ha-settings-row";
import "../../../../../components/ha-svg-icon";
import "../../../../../components/ha-switch";
import "../../../../../components/ha-textfield";
import { groupBy } from "../../../../../common/util/group-by";
import {
computeDeviceName,
DeviceRegistryEntry,
subscribeDeviceRegistry,
} from "../../../../../data/device_registry";
import { computeDeviceName } from "../../../../../data/device_registry";
import {
fetchZwaveNodeConfigParameters,
fetchZwaveNodeMetadata,
setZwaveNodeConfigParameter,
ZWaveJSNodeConfigParam,
ZWaveJSNodeConfigParams,
ZwaveJSNodeMetadata,
ZWaveJSSetConfigParamResult,
ZwaveJSNodeMetadata,
fetchZwaveNodeConfigParameters,
fetchZwaveNodeMetadata,
setZwaveNodeConfigParameter,
} from "../../../../../data/zwave_js";
import "../../../../../layouts/hass-error-screen";
import "../../../../../layouts/hass-loading-screen";
import "../../../../../layouts/hass-tabs-subpage";
import { SubscribeMixin } from "../../../../../mixins/subscribe-mixin";
import { haStyle } from "../../../../../resources/styles";
import type { HomeAssistant, Route } from "../../../../../types";
import "../../../ha-config-section";
Expand All @@ -57,16 +50,8 @@ const icons = {
error: mdiCloseCircle,
};

const getDevice = memoizeOne(
(
deviceId: string,
entries?: DeviceRegistryEntry[]
): DeviceRegistryEntry | undefined =>
entries?.find((device) => device.id === deviceId)
);

@customElement("zwave_js-node-config")
class ZWaveJSNodeConfig extends SubscribeMixin(LitElement) {
class ZWaveJSNodeConfig extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) public route!: Route;
Expand All @@ -79,8 +64,6 @@ class ZWaveJSNodeConfig extends SubscribeMixin(LitElement) {

@property() public deviceId!: string;

@state() private _deviceRegistryEntries?: DeviceRegistryEntry[];

@state() private _nodeMetadata?: ZwaveJSNodeMetadata;

@state() private _config?: ZWaveJSNodeConfigParams;
Expand All @@ -94,19 +77,8 @@ class ZWaveJSNodeConfig extends SubscribeMixin(LitElement) {
this.deviceId = this.route.path.substr(1);
}

public hassSubscribe(): UnsubscribeFunc[] {
return [
subscribeDeviceRegistry(this.hass.connection, (entries) => {
this._deviceRegistryEntries = entries;
}),
];
}

protected updated(changedProps: PropertyValues): void {
if (
(!this._config || changedProps.has("deviceId")) &&
changedProps.has("_deviceRegistryEntries")
) {
if (!this._config || changedProps.has("deviceId")) {
this._fetchData();
}
}
Expand All @@ -125,7 +97,7 @@ class ZWaveJSNodeConfig extends SubscribeMixin(LitElement) {
return html`<hass-loading-screen></hass-loading-screen>`;
}

const device = this._device!;
const device = this.hass.devices[this.deviceId];

return html`
<hass-tabs-subpage
Expand Down Expand Up @@ -392,7 +364,7 @@ class ZWaveJSNodeConfig extends SubscribeMixin(LitElement) {
try {
const result = await setZwaveNodeConfigParameter(
this.hass,
this._device!.id,
this.deviceId,
target.property,
target.endpoint,
value,
Expand Down Expand Up @@ -420,16 +392,12 @@ class ZWaveJSNodeConfig extends SubscribeMixin(LitElement) {
this._results = { ...this._results, [key]: errorParam };
}

private get _device(): DeviceRegistryEntry | undefined {
return getDevice(this.deviceId, this._deviceRegistryEntries);
}

private async _fetchData() {
if (!this.configEntryId || !this._deviceRegistryEntries) {
if (!this.configEntryId) {
return;
}

const device = this._device;
const device = this.hass.devices[this.deviceId];
if (!device) {
this._error = "device_not_found";
return;
Expand Down

0 comments on commit 618cd9d

Please sign in to comment.