Fix race condition in zwave migration (#8268)

This commit is contained in:
Bram Kragten 2021-01-28 20:20:07 +01:00 committed by GitHub
parent 448d19bfbb
commit 101067d018
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,8 +37,7 @@ import { showAlertDialog } from "../../../../../dialogs/generic/show-dialog-box"
import { computeStateName } from "../../../../../common/entity/compute_state_name";
import {
computeDeviceName,
DeviceRegistryEntry,
fetchDeviceRegistry,
subscribeDeviceRegistry,
} from "../../../../../data/device_registry";
@customElement("zwave-migration")
@ -53,8 +52,6 @@ export class ZwaveMigration extends LitElement {
@internalProperty() private _networkStatus?: ZWaveNetworkStatus;
@internalProperty() private _unsub?: Promise<UnsubscribeFunc>;
@internalProperty() private _step = 0;
@internalProperty() private _stoppingNetwork = false;
@ -65,10 +62,18 @@ export class ZwaveMigration extends LitElement {
@internalProperty() private _migratedZwaveEntities?: string[];
@internalProperty() private _deviceRegistry?: DeviceRegistryEntry[];
@internalProperty() private _deviceNameLookup: { [id: string]: string } = {};
private _unsub?: Promise<UnsubscribeFunc>;
private _unsubDevices?: UnsubscribeFunc;
public disconnectedCallback(): void {
this._unsubscribe();
if (this._unsubDevices) {
this._unsubDevices();
this._unsubDevices = undefined;
}
}
protected render(): TemplateResult {
@ -89,7 +94,8 @@ export class ZwaveMigration extends LitElement {
"ui.panel.config.zwave.migration.ozw.introduction"
)}
</div>
${!this.hass.config.components.includes("mqtt")
${!this.hass.config.components.includes("hassio") &&
!this.hass.config.components.includes("mqtt")
? html`
<ha-card class="content" header="MQTT Required">
<div class="card-content">
@ -277,9 +283,9 @@ export class ZwaveMigration extends LitElement {
).map(
(device_id) =>
html`<li>
${this._computeDeviceName(
${this._deviceNameLookup[
device_id
)}
] || device_id}
</li>`
)}
</ul>`
@ -372,10 +378,7 @@ export class ZwaveMigration extends LitElement {
private async _setupOzw() {
const ozwConfigFlow = await startOzwConfigFlow(this.hass);
if (
!this.hass.config.components.includes("hassio") &&
this.hass.config.components.includes("ozw")
) {
if (this.hass.config.components.includes("ozw")) {
this._getMigrationData();
this._step = 3;
return;
@ -399,18 +402,29 @@ export class ZwaveMigration extends LitElement {
this._migrationData.migration_entity_map
);
if (Object.keys(this._migrationData.migration_device_map).length) {
this._deviceRegistry = await fetchDeviceRegistry(this.hass);
this._fetchDevices();
}
}
private _computeDeviceName(deviceId) {
const device = this._deviceRegistry?.find(
(devReg) => devReg.id === deviceId
private _fetchDevices() {
this._unsubDevices = subscribeDeviceRegistry(
this.hass.connection,
(devices) => {
if (!this._migrationData) {
return;
}
const migrationDevices = Object.keys(
this._migrationData.migration_device_map
);
const deviceNameLookup = {};
devices.forEach((device) => {
if (migrationDevices.includes(device.id)) {
deviceNameLookup[device.id] = computeDeviceName(device, this.hass);
}
});
this._deviceNameLookup = deviceNameLookup;
}
);
if (!device) {
return deviceId;
}
return computeDeviceName(device, this.hass);
}
private async _doMigrate() {