Skip to content

Commit a0ed12c

Browse files
committed
refactoring "set person away"
1 parent f95e206 commit a0ed12c

File tree

5 files changed

+207
-109
lines changed

5 files changed

+207
-109
lines changed

netatmo-set-person-away.html

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<!--
2+
Copyright 2016 IBM Corp.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
16+
<script type="text/javascript">
17+
/* global RED oneditprepareHomeId oneditpreparePersonId */
18+
RED.nodes.registerType('set person away',{
19+
category: 'Netatmo',
20+
color: '#87A980',
21+
defaults: {
22+
name: {value:""},
23+
creds: {value:"",type:"configNode"},
24+
home_id: {value:""}, // for backward compatibility only
25+
person_id: {value:""}, // for backward compatibility only
26+
homeId: {value:""},
27+
personId: {value:""}
28+
},
29+
inputs:1,
30+
outputs:1,
31+
icon: "font-awesome/fa-video-camera",
32+
label: function() {
33+
return this.name||"set person away";
34+
},
35+
oneditprepare: function() {
36+
oneditprepareHomeId(false);
37+
oneditpreparePersonId(false);
38+
}
39+
});
40+
</script>
41+
42+
<script type="text/html" data-template-name="set person away">
43+
<div class="form-row">
44+
<label for="node-input-creds"><i class="fa fa-server"></i> Creds</label>
45+
<input type="text" id="node-input-creds" placeholder="Add netatmo creds">
46+
</div>
47+
<div class="form-row">
48+
<label for="node-input-homeId" title="id of home"><i class="fa fa-home"></i> Home ID</label>
49+
<div style="display: inline-block; position: relative; width: 70%; height: 20px;">
50+
<div style="position: absolute; left: 0px; right: 40px;">
51+
<input type="text" id="node-input-homeId" placeholder="5bXXXXXXXXXXXXXXXXXXXXXX" style="width: 100%"/>
52+
</div>
53+
<a id="node-input-homeId-scan" class="editor-button" style="position: absolute; right: 0px; top: 0px;">
54+
<i class="fa fa-search"></i>
55+
</a>
56+
</div>
57+
</div>
58+
<div class="form-row">
59+
<label for="node-input-personId" title="id of person"><i class="fa fa-user"></i> Person ID</label>
60+
<div style="display: inline-block; position: relative; width: 70%; height: 20px;">
61+
<div style="position: absolute; left: 0px; right: 40px;">
62+
<input type="text" id="node-input-personId" placeholder="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" style="width: 100%"/>
63+
</div>
64+
<a id="node-input-personId-scan" class="editor-button" style="position: absolute; right: 0px; top: 0px;">
65+
<i class="fa fa-search"></i>
66+
</a>
67+
</div>
68+
</div>
69+
<div class="form-row">
70+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
71+
<input type="text" id="node-input-name" placeholder="Name">
72+
</div>
73+
</script>
74+
75+
<script type="text/html" data-help-name="set person away">
76+
<p>This node set person away or home empty.</p>
77+
<p>Parameters:</p>
78+
<ul>
79+
<li>Home ID: something like 577fff842baa3c18948b4571</li>
80+
<li>Person ID (optional): something like 766764b1-dcef-4321-b057-e0c8e2e329cf</li>
81+
<li>If a Person ID is specified, that person will be set as "Away". If no person_id is specified, the Home will be set as "Empty".</li>
82+
</ul>
83+
<p>Home ID </p>
84+
<p>See <a href="https://dev.netatmo.com/apidocumentation/security#setpersonsaway" target=_blank>https://dev.netatmo.com/apidocumentation/security#setpersonsaway</a> for details on format</p>
85+
</script>

netatmo-set-person-away.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright 2016 IBM Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
module.exports = function(RED)
17+
{
18+
"use strict";
19+
20+
function NetatmoSetPersonAway(config) {
21+
RED.nodes.createNode(this, config);
22+
this.creds = RED.nodes.getNode(config.creds);
23+
this.homeId = config.homeId || config.home_id || '';
24+
this.personId = config.personId || config.person_id || '';
25+
var node = this;
26+
this.on('input', function (msg) {
27+
const netatmo = require('netatmo');
28+
29+
const auth = {
30+
"client_id": this.creds.credentials.client_id,
31+
"client_secret": this.creds.credentials.client_secret,
32+
"username": this.creds.credentials.username,
33+
"password": this.creds.credentials.password
34+
};
35+
const api = new netatmo(auth);
36+
var options = {
37+
home_id: node.homeId,
38+
person_id: node.personId
39+
};
40+
41+
if (msg && msg.payload) {
42+
// use home id from msg payload
43+
if (msg.payload.home_id) {
44+
options.home_id = msg.payload.home_id;
45+
}
46+
if (msg.payload.homeId) {
47+
options.home_id = msg.payload.homeId;
48+
}
49+
// use person_id id from msg payload
50+
if (msg.payload.person_id) {
51+
options.person_id = msg.payload.person_id;
52+
}
53+
if (msg.payload.personId) {
54+
options.person_id = msg.payload.personId;
55+
}
56+
}
57+
58+
api.on("error", function (error) {
59+
node.error(error);
60+
});
61+
62+
api.on("warning", function (error) {
63+
node.warn(error);
64+
});
65+
66+
api.setPersonAway(options, function (err, events) {
67+
msg.payload = events;
68+
node.send(msg);
69+
});
70+
});
71+
}
72+
RED.nodes.registerType("set person away",NetatmoSetPersonAway);
73+
}
74+

netatmoNodes.html

Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,53 @@
153153
});
154154
}
155155

156+
// eslint-disable-next-line no-unused-vars
157+
function oneditpreparePersonId(required) {
158+
$('#node-input-personId-scan').click(function()
159+
{
160+
const nodeIdCred = $('#node-input-creds option:selected').val();
161+
162+
const current = $('#node-input-personId').val();
163+
const currentHomeId = $('#node-input-homeId').val();
164+
$('#node-input-personId').replaceWith('<select id="node-input-personId" style="width: 100%"></select>');
165+
$('#node-input-personId').append('<option selected="selected" value="null">Searching for persons...</option>');
166+
167+
$.get('netatmo/homes', {nodeIdCred:nodeIdCred})
168+
.done( function(body) {
169+
const data = JSON.parse(body);
170+
171+
if(data.error) {
172+
RED.notify(data.error, { type: "error" });
173+
}
174+
const homes = data.homes;
175+
// RESET OPTIONS
176+
$('#node-input-personId').empty();
177+
178+
if(homes.length <= 0) {
179+
// Create input
180+
$('#node-input-personId').replaceWith('<input type="text" id="node-input-personId" placeholder="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" style="width: 100%"/>');
181+
$('#node-input-personId').val(current);
182+
RED.notify("no rooms found", { type: "error" });
183+
} else {
184+
// Create select
185+
$('#node-input-personId').replaceWith('<select id="node-input-personId" style="width: 100%"></select>');
186+
if (!required) {
187+
$('#node-input-personId').append('<option value="">(blank)</option>');
188+
}
189+
// SET rooms AS OPTIONS
190+
homes.forEach(function(home) {
191+
if((currentHomeId === '' || home.id === currentHomeId) && home.persons) {
192+
home.persons.forEach(function(person) {
193+
$('#node-input-personId').append('<option value="' + person.id + '">' + person.pseudo + ' - ' + person.id + '</option>');
194+
});
195+
}
196+
});
197+
$('#node-input-personId').val(current);
198+
}
199+
});
200+
});
201+
}
202+
156203
</script>
157204

158205
<!-- ************************************************************* -->
@@ -296,68 +343,6 @@
296343
<p>See <a href="https://dev.netatmo.com/dev/resources/technical/reference/welcome/gethomedata" target=_blank>https://dev.netatmo.com/dev/resources/technical/reference/welcome/gethomedata</a> for details on format</p>
297344
</script>
298345

299-
<!-- ************************************************************* -->
300-
301-
<script type="text/javascript">
302-
console.log("registering netatmo set person away");
303-
RED.nodes.registerType('set person away',{
304-
category: 'Netatmo',
305-
color: '#87A980',
306-
defaults: {
307-
name: {value:""},
308-
creds: {value:"",type:"configNode"},
309-
home_id: {value:""},
310-
person_id: {value:""}
311-
},
312-
inputs:1,
313-
outputs:1,
314-
icon: "font-awesome/fa-video-camera",
315-
label: function() {
316-
return this.name||"set person away";
317-
}
318-
});
319-
</script>
320-
321-
<script type="text/x-red" data-template-name="set person away">
322-
<div class="form-row">
323-
<label for="node-input-creds"><i class="fa fa-server"></i> Creds</label>
324-
<input type="text" id="node-input-creds" placeholder="Add netatmo creds">
325-
</div>
326-
<div class="form-row">
327-
<label for="node-input-home_id"><i class="icon-tag"></i> Home ID:</label>
328-
<input type="text" id="node-input-home_id" placeholder="577fff..."">
329-
</div>
330-
<div class="form-row">
331-
<label for="node-input-person_id"><i class="icon-tag"></i> Person ID:</label>
332-
<input type="text" id="node-input-person_id" placeholder="d1234... or not valued to set Home as Empty.">
333-
</div>
334-
<div class="form-row">
335-
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
336-
<input type="text" id="node-input-name" placeholder="Name">
337-
</div>
338-
</script>
339-
340-
<script type="text/x-red" data-help-name="set person away">
341-
<p>This node set person away or home empty.</p>
342-
<p>Parameters:</p>
343-
<ul>
344-
<li>Home ID: something like 577fff842baa3c18948b4571</li>
345-
<li>Person ID (optional): something like 766764b1-dcef-4321-b057-e0c8e2e329cf</li>
346-
</ul>
347-
<p>See <a href="https://dev.netatmo.com/apidocumentation/security#setpersonsaway" target=_blank>https://dev.netatmo.com/apidocumentation/security#setpersonsaway</a> for details on format</p>
348-
</script>
349-
350-
<!-- ************************************************************* -->
351-
352-
353-
<!-- ************************************************************* -->
354-
355-
356-
<!-- ************************************************************* -->
357-
358-
<!-- ************************************************************* -->
359-
360-
361346
<!-- ************************************************************* -->
362347
<!-- * Configuration Node * -->
363348
<!-- ************************************************************* -->

netatmoNodes.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -130,53 +130,6 @@ module.exports = function(RED) {
130130
RED.nodes.registerType("get home data",NetatmoGetHomeData);
131131
/***************************************************************/
132132
/***************************************************************/
133-
function NetatmoSetPersonAway(config) {
134-
RED.nodes.createNode(this, config);
135-
this.creds = RED.nodes.getNode(config.creds);
136-
this.home_id = config.home_id;
137-
this.person_id = config.person_id;
138-
var node = this;
139-
this.on('input', function (msg) {
140-
var netatmo = require('netatmo');
141-
142-
var auth = {
143-
"client_id": this.creds.client_id,
144-
"client_secret": this.creds.client_secret,
145-
"username": this.creds.username,
146-
"password": this.creds.password
147-
};
148-
var api = new netatmo(auth);
149-
var options = {
150-
home_id: node.home_id,
151-
person_id: node.person_id
152-
};
153-
154-
if (msg && msg.payload) {
155-
// use home id from msg payload
156-
if (msg.payload.home_id) {
157-
options.home_id = msg.payload.home_id;
158-
}
159-
// use person_id id from msg payload
160-
if (msg.payload.person_id) {
161-
options.person_id = msg.payload.person_id;
162-
}
163-
}
164-
165-
api.on("error", function (error) {
166-
node.error(error);
167-
});
168-
169-
api.on("warning", function (error) {
170-
node.warn(error);
171-
});
172-
173-
api.setPersonAway(options, function (err, events) {
174-
msg.payload = events;
175-
node.send(msg);
176-
});
177-
});
178-
}
179-
RED.nodes.registerType("set person away",NetatmoSetPersonAway);
180133
/***************************************************************/
181134
function NetatmoConfigNode(config) {
182135
RED.nodes.createNode(this,config);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"node-red": {
3434
"nodes": {
3535
"netatmo": "netatmoNodes.js",
36+
"netatmo-set-person-away": "netatmo-set-person-away.js",
3637
"netatmo-get-stations-data": "netatmo-get-stations-data.js",
3738
"netatmo-get-public-data": "netatmo-get-public-data.js",
3839
"netatmo-get-measurements": "netatmo-get-measurements.js",

0 commit comments

Comments
 (0)