Skip to content

Commit

Permalink
feat(nodes): added state, restart and logs nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianheljas committed Mar 21, 2021
1 parent e8f5965 commit 5099a88
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/nodes/bridge/logs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<script type="text/javascript">
RED.nodes.registerType('z2m-bridge-logs', {
category: 'zigbee2mqtt',
color: '#ffcc66',
paletteLabel: 'z2m logs',
icon: 'font-awesome/fa-file-text',
outputs: 1,
label: function () {
return this.name || 'zigbee2mqtt logs';
},
defaults: {
name: { value: '' },
broker: { type: 'z2m-broker', required: true },
level: { value: 'error' },
},
});
</script>

<script type="text/html" data-template-name="z2m-bridge-logs">
<div class="form-row" style="height: 34px;">
<label for="node-input-broker"><i class="fa fa-globe"></i> Z2M Broker</label>
<input type="text" id="node-input-broker" />
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" />
</div>
</script>

<script type="text/html" data-help-name="z2m-bridge-logs">
<p>Receive zigbee2mqtt logs</p>

<p>
Read more at
<a href="https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html#zigbee2mqttbridgestate">
Zigbee2MQTT docs
</a>
</p>
<p></p>

<h3>Outputs</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">string</span></dt>
<dd>Log message</dd>
<dt>level <span class="property-type">string</span></dt>
<dd>
Log message verbosity level
<ul>
<li><code>debug</code> - debug messages</li>
<li><code>info</code> - informational messages</li>
<li><code>warn</code> - warning messages</li>
<li><code>error</code> - error messages</li>
</ul>
</dd>
<dt>topic <span class="property-type">string</span></dt>
<dd>the MQTT topic, in the form of <code>${base_topic}/bridge/logging</code></dd>
</dl>
</script>
17 changes: 17 additions & 0 deletions src/nodes/bridge/logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { NodeAPI } from 'node-red';
import { Z2mNode } from '../../core/z2m-node';

class Z2mEventNode extends Z2mNode {
setup(): void {
this.z2m.subscribe('bridge/logging', 0, this.onLogMessage.bind(this), this);
}

onLogMessage(topic: string, buffer: Buffer) {
const { level, message: payload } = JSON.parse(buffer.toString());
this.send({ topic, level, payload });
}
}

export = (RED: NodeAPI): void => {
Z2mEventNode.register(RED, 'z2m-bridge-logs');
};
46 changes: 46 additions & 0 deletions src/nodes/bridge/restart.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script type="text/javascript">
RED.nodes.registerType('z2m-bridge-restart', {
category: 'zigbee2mqtt',
color: '#ffcc66',
paletteLabel: 'restart z2m',
icon: 'font-awesome/fa-refresh',
align: 'right',
inputs: 1,
label: function () {
return this.name || 'restart zigbee2mqtt';
},
defaults: {
name: { value: '' },
broker: { type: 'z2m-broker', required: true },
},
});
</script>

<script type="text/html" data-template-name="z2m-bridge-restart">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" />
</div>
<div class="form-row">
<label for="node-input-broker"><i class="fa fa-tag"></i> Z2M Broker</label>
<input type="text" id="node-input-broker" />
</div>
</script>

<script type="text/html" data-help-name="z2m-bridge-restart">
<p>Restart Zigbee2MQTT instance</p>

<p>Sends restart request to via MQTT on any input</p>

<h3>References</h3>
<ul>
<li>
<a
href="https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html#zigbee2mqttbridgerequestrestart"
>
Zigbee2MQTT docs
</a>
- <code>explains the restart request</code>
</li>
</ul>
</script>
19 changes: 19 additions & 0 deletions src/nodes/bridge/restart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { NodeAPI } from 'node-red';
import { Z2mNode } from '../../core/z2m-node';

class Z2mRestartNode extends Z2mNode {
setup(): void {
this.on('input', () => {
this.z2m.restart();
this.status({
shape: 'ring',
fill: 'yellow',
text: 'restart requested',
});
});
}
}

export = (RED: NodeAPI): void => {
Z2mRestartNode.register(RED, 'z2m-bridge-restart');
};
62 changes: 62 additions & 0 deletions src/nodes/bridge/state.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<script type="text/javascript">
RED.nodes.registerType('z2m-bridge-state', {
category: 'zigbee2mqtt',
color: '#ffcc66',
paletteLabel: 'z2m state',
icon: 'font-awesome/fa-info',
outputs: 1,
label: function () {
return this.name || 'zigbee2qmtt state';
},
defaults: {
name: { value: '' },
broker: { type: 'z2m-broker', required: true },
initial: { value: true },
},
});
</script>

<script type="text/html" data-template-name="z2m-bridge-state">
<div class="form-row" style="height: 34px;">
<label for="node-input-broker"><i class="fa fa-globe"></i> Z2M Broker</label>
<input type="text" id="node-input-broker" />
</div>
<div class="form-row">
<label for="node-input-initial"><i class="fa fa-filter"></i> Options</label>
<label for="node-input-initial" style="width:70%">
<input type="checkbox" id="node-input-initial" style="display:inline-block; width:22px; vertical-align:top;" />
include initial (retained) value
</label>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" />
</div>
</script>

<script type="text/html" data-help-name="z2m-bridge-state">
<p>Get zigbee2mqtt state updates</p>

<h3>Outputs</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">string</span></dt>
<dd>
either <code>"online"</code> or <code>"offline"</code>, see
<a href="https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html#zigbee2mqttbridgestate">
Zigbee2MQTT docs
</a>
</dd>
<dt>retain <span class="property-type">boolean</span></dt>
<dd>
<ul>
<li><code>true</code> - state was received upon initial connection and did not actually change</li>
<li><code>false</code> - the state was updated either by last will or zigbee2mqtt itself</li>
</ul>
</dd>
<dt>topic <span class="property-type">string</span></dt>
<dd>the MQTT topic, in the form of <code>${base_topic}/bridge/state</code></dd>
</dl>

<h3>Details</h3>
<p>If you want to output only when the state changes, uncheck <code>send initial value</code> in the node options</p>
</script>
24 changes: 24 additions & 0 deletions src/nodes/bridge/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { IPublishPacket } from 'mqtt';
import type { NodeAPI } from 'node-red';
import { Z2mNode } from '../../core/z2m-node';

interface BridgeStateConfig {
initial: boolean;
}

class BridgeStateNode extends Z2mNode<BridgeStateConfig> {
setup(): void {
this.z2m.subscribe('bridge/state', 1, this.onStateMessage.bind(this), this);
}

onStateMessage(topic: string, buffer: Buffer, { retain }: IPublishPacket) {
if (!retain || this.config.initial) {
const payload = buffer.toString();
this.send({ topic, payload, retain });
}
}
}

export = (RED: NodeAPI): void => {
BridgeStateNode.register(RED, 'z2m-bridge-state');
};

0 comments on commit 5099a88

Please sign in to comment.