Skip to content

Commit

Permalink
Init repo
Browse files Browse the repository at this point in the history
  • Loading branch information
BOOMER74 committed Apr 10, 2021
0 parents commit b6d9674
Show file tree
Hide file tree
Showing 16 changed files with 4,858 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
env: {
node: true,
},
extends: 'airbnb-base',
root: true,
rules: {
'max-len': ['error', 120],
},
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
node_modules/
5 changes: 5 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
printWidth: 120,
singleQuote: true,
trailingComma: 'all',
};
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright 2021 BOOMER74

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# node-red-contrib-mihome

Add nodes to get data from devices connected to Mi Home.

## Supported devices

### Xiaomi Mi Temperature and Humidity Monitor 2

Models LYWSD03MMC (China) and NUN4126GL (Global, should be checked).

## Available nodes

### mihome-cloud

Provide authorization for Mi Home.

### mihome-devices

#### Input

Send `true` to get all devices.

#### Output

Devices list as array of objects.

### mihome-th-monitor

#### Settings

Device ID: You can get it by checking `did` field from `mihome-devices` output.

#### Input

Send `true` to get temperature and humidity.

#### Output

Object, contains temperature and humidity data. Additionally, returns timestamps.
Binary file added nodes/icons/mihome.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions nodes/mihome-cloud.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<script type="text/javascript">
RED.nodes.registerType('mihome-cloud', {
category: 'config',
color: '#00BC9C',
defaults: {
country: { value: 'ru' },
email: { value: null },
},
credentials: {
email: { type: 'text' },
password: { type: 'password' },
},
label() {
const { email } = this;

if (email == null) {
return 'Login to Mi Home';
}

return `Mi Home: ${email}`;
},
oneditsave() {
this.email = this.credentials.email;
},
});
</script>
<script type="text/html" data-template-name="mihome-cloud">
<div class="form-row">
<label for="node-config-input-email"><i class="fa fa-envelope"></i> Email</label>
<input type="text" id="node-config-input-email" placeholder="Email" />
</div>
<div class="form-row">
<label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
<input type="password" id="node-config-input-password" placeholder="Password" />
</div>
<div class="form-row">
<label for="node-config-input-country"><i class="fa fa-globe"></i> Country</label>
<select id="node-config-input-country">
<option value="ru">ru</option>
<option value="us">us</option>
<option value="tw">tw</option>
<option value="sg">sg</option>
<option value="cn">cn</option>
<option value="de">de</option>
</select>
</div>
</script>
<script type="text/html" data-help-name="mihome-cloud">
<p>Provide authorization for Mi Home</p>
</script>
63 changes: 63 additions & 0 deletions nodes/mihome-cloud.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const mihome = require('node-mihome');

module.exports = (RED) => {
function Cloud(config) {
RED.nodes.createNode(this, config);

this.country = config.country;
this.connected = false;

this.miio = null;
this.protocol = null;

this.init = async () => {
if (this.protocol == null || !this.protocol.isLoggedIn) {
const { miCloudProtocol: protocol, miioProtocol: miio } = mihome;

this.miio = miio;
this.protocol = protocol;

await miio.init();

const { email, password } = this.credentials;

try {
await protocol.login(email, password);

this.emit('connected');
} catch (exception) {
this.emit('close', exception.message);
}
} else {
this.emit('connected');
}
};

this.on('connected', () => {
this.connected = true;
});

this.on('disconnected', () => {
this.connected = false;
});

this.on('close', async () => {
this.emit('disconnected');

if (this.protocol != null && this.protocol.isLoggedIn) {
this.protocol.logout();
}

if (this.miio != null) {
this.miio.destroy();
}
});
}

RED.nodes.registerType('mihome-cloud', Cloud, {
credentials: {
email: { type: 'text' },
password: { type: 'password' },
},
});
};
38 changes: 38 additions & 0 deletions nodes/mihome-devices.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script type="text/javascript">
RED.nodes.registerType('mihome-devices', {
category: 'Mi Home',
color: '#00BC9C',
defaults: {
cloud: { value: null, type: 'mihome-cloud' },
},
icon: 'mihome.png',
inputs: 1,
outputs: 1,
label: () => 'Devices',
});
</script>
<script type="text/html" data-template-name="mihome-devices">
<div class="form-row">
<label for="node-input-cloud"><i class="fa fa-cloud"></i> Cloud</label>
<input id="node-input-cloud" />
</div>
</script>
<script type="text/html" data-help-name="mihome-devices">
<p>Returns list of all connected devices from Mi Home</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt>
payload
<span class="property-type">boolean</span>
</dt>
<dd>If true output returns devices</dd>
</dl>
<h3>Outputs</h3>
<dl class="message-properties">
<dt>
payload
<span class="property-type">Object[]</span>
</dt>
<dd>List of all devices</dd>
</dl>
</script>
37 changes: 37 additions & 0 deletions nodes/mihome-devices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = (RED) => {
function Devices(config) {
RED.nodes.createNode(this, config);

this.status({ fill: 'red', shape: 'dot', text: 'offline' });

const cloud = RED.nodes.getNode(config.cloud);

if (cloud != null) {
cloud.init().then(() => {
const { connected, country, protocol } = cloud;

if (connected) {
this.status({ fill: 'green', shape: 'dot', text: 'online' });
}

cloud.on('connected', async () => {
this.status({ fill: 'green', shape: 'dot', text: 'online' });
});

cloud.on('disconnected', () => {
this.status({ fill: 'red', shape: 'dot', text: 'offline' });
});

this.on('input', async (msg) => {
if (msg.payload === true && cloud.connected) {
const devices = await protocol.getDevices(null, { country });

this.send({ payload: devices });
}
});
});
}
}

RED.nodes.registerType('mihome-devices', Devices);
};
43 changes: 43 additions & 0 deletions nodes/mihome-th-monitor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script type="text/javascript">
RED.nodes.registerType('mihome-th-monitor', {
category: 'Mi Home',
color: '#00BC9C',
defaults: {
cloud: { value: null, type: 'mihome-cloud' },
did: { value: null },
},
icon: 'mihome.png',
inputs: 1,
outputs: 1,
label: () => 'TH Monitor',
});
</script>
<script type="text/html" data-template-name="mihome-th-monitor">
<div class="form-row">
<label for="node-input-cloud"><i class="fa fa-cloud"></i> Cloud</label>
<input id="node-input-cloud" />
</div>
<div class="form-row">
<label for="node-input-did"><i class="fa fa-microchip"></i> Device ID</label>
<input type="text" id="node-input-did" placeholder="Device ID" />
</div>
</script>
<script type="text/html" data-help-name="mihome-th-monitor">
<p>Returns temperature and humidity values</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt>
payload
<span class="property-type">boolean</span>
</dt>
<dd>If true output returns object with temperature and humidity values</dd>
</dl>
<h3>Outputs</h3>
<dl class="message-properties">
<dt>
payload
<span class="property-type">Object</span>
</dt>
<dd>Object with temperature and humidity values</dd>
</dl>
</script>
Loading

0 comments on commit b6d9674

Please sign in to comment.