homebridge / plugins

Information and resources for Homebridge plugins.
https://homebridge.io/w/Verified-Plugins
GNU General Public License v3.0
368 stars 17 forks source link

homebridge-http-leak-sensor #556

Closed josephsamela closed 1 year ago

josephsamela commented 1 year ago

Link To GitHub Repo

https://github.com/josephsamela/homebridge-http-leak-sensor

Link To NPM Package

https://www.npmjs.com/package/homebridge-http-leak-sensor

github-actions[bot] commented 1 year ago

:white_check_mark: Pre-checks completed successfully.

bwp91 commented 1 year ago

Hi @josephsamela

Couple of things:

The plugin must run on all supported LTS versions of Node.js, at the time of writing this is Node.js v16 and v18.

Please update your package version node engine to show support for node 16 (and ensure the plugin works with node 16, at least the latest version), something like this:

   "node": "^16.20.1 || ^18.17.0"

The plugin must not throw unhandled exceptions, the plugin must catch and log its own errors.

Looks like you aren't catching any errors that node-fetch might throw when requesting an invalid URL (for example the default config URL). I saved the default config, and it is repeatedly crashing my homebridge instance:

[29/07/2023, 15:19:20] [Leak Sensor] Child bridge process ended
[29/07/2023, 15:19:20] [Leak Sensor] Process Ended. Code: 1, Signal: null
[29/07/2023, 15:19:27] [Leak Sensor] Restarting Process...
[29/07/2023, 15:19:28] [Leak Sensor] Launched child bridge with PID 4636
[29/07/2023, 15:19:28] The plugin "homebridge-http-leak-sensor" requires a Homebridge version of ^1.6.0 which does not satisfy the current Homebridge version of 2.0.0-beta.1. You may need to update this plugin (or Homebridge) to a newer version. You may face unexpected issues or stability problems running this plugin.
[29/07/2023, 15:19:28] The plugin "homebridge-http-leak-sensor" requires Node.js version of ^18.17.0 which does not satisfy the current Node.js version of v20.5.0. You may need to upgrade your installation of Node.js - see https://homebridge.io/w/JTKEF
[29/07/2023, 15:19:28] Registering accessory 'homebridge-http-leak-sensor.HomebridgeHTTPLeakSensor'
[29/07/2023, 15:19:28] [Leak Sensor] Loaded homebridge-http-leak-sensor v1.0.1 child bridge successfully
[29/07/2023, 15:19:28] Loaded 0 cached accessories from cachedAccessories.0E5760A93851.
[29/07/2023, 15:19:28] Homebridge v2.0.0-beta.1 (HAP v1.0.0-beta.8) (Leak Sensor) is running on port 57457.
FetchError: invalid json response body at http://localhost/status reason: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
    at /usr/local/lib/node_modules/homebridge-http-leak-sensor/node_modules/node-fetch/lib/index.js:273:32
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at LeakSensorAccessory.handleLeakSensorSetState (/usr/local/lib/node_modules/homebridge-http-leak-sensor/src/index.ts:59:18)
[29/07/2023, 15:20:28] [Leak Sensor] Child bridge process ended
[29/07/2023, 15:20:28] [Leak Sensor] Process Ended. Code: 1, Signal: null
[29/07/2023, 15:20:35] [Leak Sensor] Restarting Process...
[29/07/2023, 15:20:36] [Leak Sensor] Launched child bridge with PID 4652

Maybe putting all of this in a try/catch

https://github.com/josephsamela/homebridge-http-leak-sensor/blob/eada06cbfb7c416b79d033b8f258da148a20b62a/src/index.ts#L58-L72

and then in the catch use this.log.warn to output the error to the homebridge log.

This would be sufficient for verification, as the plugin would no longer be crashing homebridge.

What you could do (appreciate this is a bit more involved) is try to detect whether the http response is due to:

Just some ideas!

josephsamela commented 1 year ago

I appreciate the feedback! I'll work on incorporating the changes you suggested. Thanks!

josephsamela commented 1 year ago

Hi @bwp91

I'm ready for your review. I published a new version v1.0.2 implementing the changes you suggested. Here are links to the published releases. git npm

Changes

  1. Fixed package version node engine. 1933efb
  2. Added try/catch + error logging. 1cf5c25

I also addressed your more involved suggestion:

I created a user-configurable property called failedRequestsLimit. The plugin will continue sending requests to the configured url until it hits this limit then it stop making requests. A value of 0 allows unlimited failed requests. I think this is a good middle-ground because it prevents the plugin from continuously making bad requests forever but also allows a grace period for interrupted devices to get back online.

If you have any other suggestions please let me know!

bwp91 commented 1 year ago

/check

github-actions[bot] commented 1 year ago

:white_check_mark: Pre-checks completed successfully.

github-actions[bot] commented 1 year ago

Congratulations! Your plugin has been verified.

You can now add the Verified by Homebridge badge to your plugin's README:

verified-by-homebridge

[![verified-by-homebridge](https://badgen.net/badge/homebridge/verified/purple)](https://github.com/homebridge/homebridge/wiki/Verified-Plugins)

Your plugin is now also eligible to display a :heart: Donate button on its tile in the Homebridge UI. See https://github.com/oznu/homebridge-config-ui-x/wiki/Developers:-Donation-Links for instructions.

If for any reason in the future you can no longer maintain your plugin, please consider transferring it to our unmaintained plugins repo. We can take ownership until another willing developer comes along.

Thank you for your contribution to the Homebridge Community. https://homebridge.io

bwp91 commented 1 year ago

Thanks for the changes! Have verified the plugin now, please in the future adhere to:

A GitHub release - with patch notes - should be created for every new version of your plugin.

So that each version you release has some changes from the previous version listed!