Skip to content

Commit

Permalink
Add deviceId to existing devices on init
Browse files Browse the repository at this point in the history
  • Loading branch information
philipostli committed Oct 23, 2024
1 parent 124a1b8 commit f485756
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
24 changes: 24 additions & 0 deletions drivers/go/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class GoCharger extends Homey.Device {
);

await this.migrateCapabilities();
await this.migrateSettings();
this.registerCapabilityListeners();

this.cronTasks.push(
Expand All @@ -44,6 +45,29 @@ export class GoCharger extends Homey.Device {
this.log('GoCharger has been initialized');
}

/**
* Migrate settings from the old settings format to the new one.
* If the deviceid setting is empty, poll the charger info and store the device id.
*/
private async migrateSettings() {
if (this.api === undefined) return;

if (this.getSetting('deviceid') === ""){
await this.api.getCharger(this.getData().id)
.then((charger) => {
this.setSettings({
deviceid: charger.DeviceId,
});
})
.then(() => {
this.logToDebug(`Got charger info - added device id`);
})
.catch((e) => {
this.logToDebug(`Failed to poll charger info: ${e}`);
});
}
}

/**
* Verify all expected capabilities and apply changes to capabilities.
*
Expand Down
24 changes: 24 additions & 0 deletions drivers/home/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class ProCharger extends Homey.Device {
);

await this.migrateCapabilities();
await this.migrateSettings();
this.registerCapabilityListeners();

this.cronTasks.push(
Expand All @@ -44,6 +45,29 @@ export class ProCharger extends Homey.Device {
this.log('ProCharger has been initialized');
}

/**
* Migrate settings from the old settings format to the new one.
* If the deviceid setting is empty, poll the charger info and store the device id.
*/
private async migrateSettings() {
if (this.api === undefined) return;

if (this.getSetting('deviceid') === ""){
await this.api.getCharger(this.getData().id)
.then((charger) => {
this.setSettings({
deviceid: charger.DeviceId,
});
})
.then(() => {
this.logToDebug(`Got charger info - added device id`);
})
.catch((e) => {
this.logToDebug(`Failed to poll charger info: ${e}`);
});
}
}

/**
* Verify all expected capabilities and apply changes to capabilities.
*
Expand Down
24 changes: 24 additions & 0 deletions drivers/pro/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class ProCharger extends Homey.Device {
);

await this.migrateCapabilities();
await this.migrateSettings();
this.registerCapabilityListeners();

this.cronTasks.push(
Expand All @@ -44,6 +45,29 @@ export class ProCharger extends Homey.Device {
this.log('ProCharger has been initialized');
}

/**
* Migrate settings from the old settings format to the new one.
* If the deviceid setting is empty, poll the charger info and store the device id.
*/
private async migrateSettings() {
if (this.api === undefined) return;

if (this.getSetting('deviceid') === ""){
await this.api.getCharger(this.getData().id)
.then((charger) => {
this.setSettings({
deviceid: charger.DeviceId,
});
})
.then(() => {
this.logToDebug(`Got charger info - added device id`);
})
.catch((e) => {
this.logToDebug(`Failed to poll charger info: ${e}`);
});
}
}

/**
* Verify all expected capabilities and apply changes to capabilities.
*
Expand Down

0 comments on commit f485756

Please sign in to comment.