Skip to content

Commit

Permalink
cleanup + updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
skrollme committed Sep 17, 2022
1 parent 308b74c commit 0f4d153
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 16 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
[![npm](https://img.shields.io/npm/dt/homebridge-eveatmo.svg?style=plastic)](https://www.npmjs.com/package/homebridge-eveatmo)
[![GitHub last commit](https://img.shields.io/github/last-commit/skrollme/homebridge-eveatmo.svg?style=plastic)](https://github.com/skrollme/homebridge-eveatmo)

# Warning
Since Netatmo announced a change in their authentification-policies this plugin **will probably stop working on October 2022**. For more information see: https://github.com/skrollme/homebridge-eveatmo/issues/62

# homebridge-eveatmo

This is a [homebridge](https://github.com/nfarina/homebridge) plugin which lets you integrate your non-HomeKit Netatmo Weatherstation and Indoor Air Quality monitor into HomeKit.
Expand All @@ -29,8 +26,7 @@ You can also configure this plugin via [ConfigUI-X's settings](https://github.co
"auth": {
"client_id": "XXXXX Create at https://dev.netatmo.com/",
"client_secret": "XXXXX Create at https://dev.netatmo.com/",
"username": "your netatmo username",
"password": "your netatmo password"
"refresh_token": "a valid refresh token for the given client_id",
}
}
],
Expand Down Expand Up @@ -82,6 +78,8 @@ If the whitelist contains at least one entry, all other ids will be excluded.
2. After successful registration create your own app by using the menu entry "CREATE AN APP"
3. On the following page, enter a name for your app. Any name can be chosen. All other fields of the form (like callback url, etc.) can be left blank.
4. After successfully submitting the form the overview page of your app should show client id and secret.
5. Do an initial auth with the newly created app via the "Token generator" on your app's page https://dev.netatmo.com/apps/ to get a refresh token
6. Add the _client_id_, the _client_secret_ and the _refresh_token_ to the config's _auth_-section

## Siri Voice Commands

Expand Down Expand Up @@ -126,7 +124,7 @@ see [HISTORY.md](https://github.com/skrollme/homebridge-eveatmo/blob/master/HIST
- <del>Support Indoor Air Quality monitor (see: https://github.com/skrollme/homebridge-eveatmo/issues/51)</del>


## Thanks
## Thanks and disclaimer

This plugin's basic structure and most of its basic code is a fork (ok, lets say "copy") of [homebridge-netatmo](https://github.com/planetk/homebridge-netatmo). So big thanks to @planetk and all the other contributors of this project.

Expand All @@ -138,6 +136,8 @@ Thanks go also to the following direct contributors:
- @foliveira (https://github.com/skrollme/homebridge-eveatmo/pull/52)
- @RyanHS7VM (https://github.com/skrollme/homebridge-eveatmo/pull/54)

**Since Netatmo announced some changes on what kind of authentication their API will support and I did not found a good solution to override the code of the [netatmo](https://github.com/karbassi/netatmo)-dependency to continue working, this module contains an altered full-copy of the module. All credits for the original code go to the respective authors.**

## What else

Like this and want to express your feelings? Please buy me a beer :beers: ...
Expand Down
12 changes: 3 additions & 9 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,11 @@
"required": true,
"description": "Create this at https://dev.netatmo.com/"
},
"username": {
"title": "Username",
"refresh_token": {
"title": "Refresh Token",
"type": "string",
"required": true,
"description": "Your Netatmo username"
},
"password": {
"title": "Password",
"type": "string",
"required": true,
"description": "Your Netatmo password"
"description": "A valid Netatmo refreshToken, see https://dev.netatmo.com/apidocumentation/oauth for more information"
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class EveatmoPlatform {
this.log.warn('CAUTION! USING FAKE NETATMO API: ' + config.mockapi);
this.api = require("./lib/netatmo-api-mock")(config.mockapi);
} else {
if (config.auth.username) {
throw 'username / password auth is not supported anymore! Please see the readme and use a "refresh_token" instead.'
} else if (!config.auth.refresh_token) {
throw "Authenticate 'refresh_token' not set."
}

this.api = new netatmo(config.auth);
}
this.api.on("error", function(error) {
Expand Down
84 changes: 83 additions & 1 deletion lib/netatmo-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var netatmo = function (args) {
client_id = args.client_id;
client_secret = args.client_secret;
refresh_token = args.refresh_token;
scope = args.scope;

this.authenticate_refresh();
};
Expand Down Expand Up @@ -57,6 +56,89 @@ netatmo.prototype.handleRequestError = function (err, response, body, message, c
return error;
};

/**
* https://dev.netatmo.com/dev/resources/technical/guides/authentication
* @param args
* @param callback
* @returns {netatmo}
*/
netatmo.prototype.authenticate = function (args, callback) {
if (!args) {
this.emit("error", new Error("Authenticate 'args' not set."));
return this;
}

if (args.access_token) {
access_token = args.access_token;
return this;
}

if (!args.client_id) {
this.emit("error", new Error("Authenticate 'client_id' not set."));
return this;
}

if (!args.client_secret) {
this.emit("error", new Error("Authenticate 'client_secret' not set."));
return this;
}

if (!args.username) {
this.emit("error", new Error("Authenticate 'username' not set."));
return this;
}

if (!args.password) {
this.emit("error", new Error("Authenticate 'password' not set."));
return this;
}

username = args.username;
password = args.password;
client_id = args.client_id;
client_secret = args.client_secret;
scope = args.scope || 'read_station read_thermostat write_thermostat read_camera write_camera access_camera read_presence access_presence read_smokedetector read_homecoach';

var form = {
client_id: client_id,
client_secret: client_secret,
username: username,
password: password,
scope: scope,
grant_type: 'password',
};

var url = util.format('%s/oauth2/token', BASE_URL);

request({
url: url,
method: "POST",
form: form,
}, function (err, response, body) {
if (err || response.statusCode != 200) {
return this.handleRequestError(err, response, body, "Authenticate error", true);
}

body = JSON.parse(body);

access_token = body.access_token;

if (body.expires_in) {
setTimeout(this.authenticate_refresh.bind(this), body.expires_in * 1000, body.refresh_token);
}

this.emit('authenticated');

if (callback) {
return callback();
}

return this;
}.bind(this));

return this;
};

/**
* https://dev.netatmo.com/dev/resources/technical/guides/authentication/refreshingatoken
* @param refresh_token
Expand Down

0 comments on commit 0f4d153

Please sign in to comment.