Skip to content

Commit 402f177

Browse files
committed
fix(extensions): update sample code
1 parent ca53d81 commit 402f177

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

.husky/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
_
1+
_

src/actions/ExtensionApi.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ export default {
1818
},
1919

2020
saveExtensionCode: (state, extension: Extension): Promise<void> => {
21-
return api.send(`bridge/extension/request/save`, extension);
21+
return api.send(`bridge/request/extension/save`, extension);
2222
}
2323
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1-
class MyExampleExtension%TS% {
1+
class MyExampleExtension_TS_ {
22
constructor(zigbee, mqtt, state, publishEntityState, eventBus, settings, logger) {
3-
logger.info('Loaded MyExampleExtension%TS%');
4-
mqtt.publish('example/extension', 'hello from MyExampleExtension%TS%')
3+
logger.info('Loaded MyExampleExtension_TS_');
4+
mqtt.publish('example/extension', 'hello from MyExampleExtension_TS_');
5+
this.eventBus = eventBus;
6+
this.mqtt = mqtt;
7+
this.eventBus.on('stateChange', this.onStateChange.bind(this));
58
}
9+
10+
async onStateChange(data) {
11+
const { ID, from, to } = data;
12+
13+
const myRemoteIeeeAddre= '0x00124b00219fb9ee'; // change this
14+
const myLampIeeAddr = '0x0017880104292f0a'; // change this
15+
16+
//example how to toggle state
17+
if (ID === myRemoteIeeeAddre) { // state changed for myRemote (clicked a button)
18+
if (to.contact_l1) { // new state
19+
this.mqtt.publish(`${myLampIeeAddr}/set`, JSON.stringify({state: 'toggle'}), undefined, undefined, undefined, false);
20+
}
21+
}
22+
}
23+
// async onMQTTMessage(topic, message) {
24+
// console.log({topic, message});
25+
// }
626
}
727

8-
module.exports = MyExampleExtension%TS%;
28+
module.exports = MyExampleExtension_TS_;

src/components/extensions-editor/index.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from "react";
22
import { connect } from "unistore/react";
33
import actions from "../../actions/actions";
4-
import { Extension, GlobalState } from "../../store";
4+
import { GlobalState } from "../../store";
55

66
import exampleExtensionCode from './example-extension.js.txt';
77

@@ -40,7 +40,7 @@ export class ExtensionsEditorPage extends Component<GlobalState & ExtensionApi,
4040
const { updateExtensionCode } = this.props;
4141
const ts = Date.now() + '';
4242
const newName = prompt("Enter new extension name", `user-extension${ts}.js`);
43-
const templatedCode = exampleExtensionCode.replace(/%TS%/g, ts);
43+
const templatedCode = exampleExtensionCode.replace(/_TS_/g, ts);
4444
if (newName !== null) {
4545
updateExtensionCode({ name: newName, code: templatedCode });
4646
this.setState({ currentExtension: newName });
@@ -49,10 +49,10 @@ export class ExtensionsEditorPage extends Component<GlobalState & ExtensionApi,
4949
render() {
5050
const { currentExtension } = this.state;
5151
const { extensions } = this.props;
52-
const code = extensions.find(e => e.name === currentExtension)?.code;
52+
const code = extensions.find(e => e.name === currentExtension)?.code ?? "";
5353

54-
return <div className="card">
55-
<div className="card-body">
54+
return <div className="card h-100">
55+
<div className="card-body h-100">
5656
<div className="row mb-2">
5757
<div className="col-6">
5858
<select value={currentExtension} className="form-control" onChange={this.loadExtension}>
@@ -61,11 +61,11 @@ export class ExtensionsEditorPage extends Component<GlobalState & ExtensionApi,
6161
</select>
6262
</div>
6363
<div className="col-6">
64-
<Button onClick={this.addNewExtension} className="btn btn-success"><i className="fa fa-plus"></i></Button>
64+
<Button onClick={this.addNewExtension} className="btn btn-success me-2"><i className="fa fa-plus"></i></Button>
65+
<Button disabled={!currentExtension} onClick={this.onSaveClick} className="btn btn-primary">Save</Button>
6566
</div>
6667
</div>
67-
<textarea onChange={this.onExtensionCodeChange} className="form-control mb-2" rows={10} value={code}></textarea>
68-
<Button disabled={!currentExtension} onClick={this.onSaveClick} className="btn btn-primary">Save</Button>
68+
<textarea spellCheck={false} onChange={this.onExtensionCodeChange} className="form-control h-100" value={code} />
6969
</div>
7070
</div>
7171

0 commit comments

Comments
 (0)