Skip to content
Open
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
4 changes: 1 addition & 3 deletions assets/templates/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import Homey from 'homey';

module.exports = class MyApp extends Homey.App {
export default class MyApp extends Homey.App {
Comment thread
wouter-athom marked this conversation as resolved.

Comment thread
wouter-athom marked this conversation as resolved.
/**
* onInit is called when the app is initialized.
Expand Down
19 changes: 11 additions & 8 deletions assets/templates/app/drivers/device.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
/* eslint-disable @typescript-eslint/no-misused-promises */
// All methods are awaited, but some of the types are outdated.
Comment thread
wouter-athom marked this conversation as resolved.

import Homey from 'homey';

module.exports = class MyDevice extends Homey.Device {
export default class MyDevice extends Homey.Device {

/**
* onInit is called when the device is initialized.
*/
async onInit() {
public override async onInit() {
this.log('MyDevice has been initialized');
}

/**
* onAdded is called when the user adds the device, called just after pairing.
*/
async onAdded() {
public override async onAdded() {
this.log('MyDevice has been added');
}

Expand All @@ -24,7 +27,7 @@ module.exports = class MyDevice extends Homey.Device {
* @param {string[]} event.changedKeys An array of keys changed since the previous version
* @returns {Promise<string|void>} return a custom message that will be displayed
*/
async onSettings({
public override async onSettings({
oldSettings,
newSettings,
changedKeys,
Expand All @@ -33,23 +36,23 @@ module.exports = class MyDevice extends Homey.Device {
newSettings: { [key: string]: boolean | string | number | undefined | null };
changedKeys: string[];
}): Promise<string | void> {
this.log("MyDevice settings where changed");
this.log('MyDevice settings were changed');
}

/**
* onRenamed is called when the user updates the device's name.
* This method can be used this to synchronise the name to the device.
* @param {string} name The new name
*/
async onRenamed(name: string) {
public override async onRenamed(name: string) {
this.log('MyDevice was renamed');
}

/**
* onDeleted is called when the user deleted the device.
*/
async onDeleted() {
public override async onDeleted() {
this.log('MyDevice has been deleted');
}

};
}
2 changes: 1 addition & 1 deletion assets/templates/app/drivers/driver.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ export default class MyDriver extends Homey.Driver {
];
}

};
}
8 changes: 4 additions & 4 deletions assets/templates/app/drivers/driver.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import Homey from 'homey';

module.exports = class MyDriver extends Homey.Driver {
export default class MyDriver extends Homey.Driver {

/**
* onInit is called when the driver is initialized.
*/
async onInit() {
public override async onInit() {
this.log('MyDriver has been initialized');
}

/**
* onPairListDevices is called when a user is adding a device and the 'list_devices' view is called.
* This should return an array with the data of devices that are available for pairing.
*/
async onPairListDevices() {
public override async onPairListDevices() {
return [
// Example device data, note that `store` is optional
// {
Expand All @@ -28,4 +28,4 @@ module.exports = class MyDriver extends Homey.Driver {
];
}

};
}
26 changes: 26 additions & 0 deletions assets/templates/app/widgets/api.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default {
async getSomething({ homey, query }) {
// you can access query parameters like "/?foo=bar" through `query.foo`

// you can access the App instance through homey.app
// const result = await homey.app.getSomething();
// return result;

// perform other logic like mapping result data

return 'Hello from App';
},

async addSomething({ homey, body }) {
// access the post body and perform some action on it.
return homey.app.addSomething(body);
},

async updateSomething({ homey, params, body }) {
return homey.app.setSomething(body);
Comment thread
wouter-athom marked this conversation as resolved.
},

async deleteSomething({ homey, params }) {
return homey.app.deleteSomething(params.id);
},
};
43 changes: 43 additions & 0 deletions assets/templates/app/widgets/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { Driver } from 'homey';

type Homey = Driver['homey'];

export default {
async getSomething({ homey, query }: {
homey: Homey,
query: Record<string, string>,
}): Promise<string> {
// you can access query parameters like "/?foo=bar" through `query.foo`

// you can access the App instance through homey.app
// const result = await homey.app.getSomething();
// return result;

// perform other logic like mapping result data

return 'Hello from App';
},

async addSomething({ homey, body }: {
homey: Homey,
body: Record<string, unknown>,
}): Promise<void> {
// access the post body and perform some action on it.
return homey.app.addSomething(body);
},

async updateSomething({ homey, params, body }: {
homey: Homey,
params: Record<string, string>,
body: Record<string, unknown>,
}): Promise<void> {
return homey.app.setSomething(body);
Comment thread
wouter-athom marked this conversation as resolved.
},

async deleteSomething({ homey, params }: {
homey: Homey,
params: Record<string, string>,
}): Promise<void> {
return homey.app.deleteSomething(params.id);
},
};
59 changes: 36 additions & 23 deletions lib/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2195,26 +2195,16 @@ $ sudo systemctl restart docker
}

async copyDriverAndDeviceTemplate(templatePath, driverPath) {
if (App.usesModules({ appPath: this.path })) {
await copyFileAsync(
path.join(templatePath, 'driver.mjs'),
path.join(driverPath, 'driver.js'),
);
await copyFileAsync(
path.join(templatePath, 'device.mjs'),
path.join(driverPath, 'device.js'),
);
} else {
const fileExtension = App.usesTypeScript({ appPath: this.path }) ? '.ts' : '.js';
await copyFileAsync(
path.join(templatePath, `driver${fileExtension}`),
path.join(driverPath, `driver${fileExtension}`),
);
await copyFileAsync(
path.join(templatePath, `device${fileExtension}`),
path.join(driverPath, `device${fileExtension}`),
);
}
const { templateExtension, targetExtension } = this.getAppExtensions();

await copyFileAsync(
path.join(templatePath, `driver${templateExtension}`),
path.join(driverPath, `driver${targetExtension}`),
);
await copyFileAsync(
path.join(templatePath, `device${templateExtension}`),
path.join(driverPath, `device${targetExtension}`),
);
}

async questionDriverWireless() {
Expand Down Expand Up @@ -3274,6 +3264,7 @@ $ sudo systemctl restart docker
JSON.stringify(widgetJson, false, 2),
);

const { templateExtension, targetExtension } = this.getAppExtensions();
const templatePath = path.join(__dirname, '..', 'assets', 'templates', 'app', 'widgets');
await fse.ensureDir(path.join(widgetPath, 'public'));
await copyFileAsync(
Expand All @@ -3284,7 +3275,10 @@ $ sudo systemctl restart docker
path.join(templatePath, 'public/homey-logo.png'),
path.join(widgetPath, 'public/homey-logo.png'),
);
await copyFileAsync(path.join(templatePath, 'api.js'), path.join(widgetPath, 'api.js'));
await copyFileAsync(
path.join(templatePath, `api${templateExtension}`),
path.join(widgetPath, `api${targetExtension}`),
);
await copyFileAsync(
path.join(templatePath, 'preview-dark.png'),
path.join(widgetPath, 'preview-dark.png'),
Expand Down Expand Up @@ -3568,6 +3562,7 @@ $ sudo systemctl restart docker
if (answers.typescript) {
delete packageJson.main;
packageJson.scripts.build = 'tsc';
packageJson.type = 'module';
}

await writeFileAsync(path.join(appPath, 'package.json'), JSON.stringify(packageJson, false, 2));
Expand Down Expand Up @@ -3612,11 +3607,21 @@ $ sudo systemctl restart docker
await HomeyCompose.createComposeDirectories({ appPath });

if (answers.typescript) {
await NpmCommands.installDev(['typescript'], { appPath });
// eslint-config-athom currently relies on tooling that isn't compatible with TypeScript 7 yet.
// As a workaround, install TypeScript 6 under the `typescript` package name for ESLint,
// and also install the latest TypeScript under an alias for running `tsc`.
if (answers.eslint) {
await NpmCommands.installDev(
['typescript@npm:@typescript/typescript6', '@typescript/native@npm:typescript'],
{ appPath },
);
Comment thread
wouter-athom marked this conversation as resolved.
} else {
await NpmCommands.installDev(['typescript'], { appPath });
}
}

if (answers.eslint) {
await NpmCommands.installDev(['eslint@^7.32.0', 'eslint-config-athom'], { appPath });
await NpmCommands.installDev(['eslint@~8.57.1', 'eslint-config-athom@~4.0.1'], { appPath });

const eslintConfig = {
extends: 'athom/homey-app',
Expand Down Expand Up @@ -4041,6 +4046,14 @@ ${readme}`,
const result = await exec('npm list');
Log(result.stdout);
}

getAppExtensions() {
if (App.usesTypeScript({ appPath: this.path }))
return { templateExtension: '.ts', targetExtension: '.ts' };
if (App.usesModules({ appPath: this.path }))
return { templateExtension: '.mjs', targetExtension: '.js' };
return { templateExtension: '.js', targetExtension: '.js' };
}
}

module.exports = App;
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homey",
"version": "4.4.2",
"version": "4.4.3",
"description": "Command-line interface and type declarations for Homey Apps",
"main": "bin/homey.js",
"files": [
Expand Down Expand Up @@ -71,7 +71,7 @@
},
"lint-staged": {
"**/*.{js,mjs}": [
"eslint --max-warnings=0 --fix"
"eslint --max-warnings=0 --no-warn-ignored --fix"
],
"**/*.{js,mjs,json,md,yml,yaml,html}": [
"prettier --write"
Expand Down
Loading