-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scene: add new action send zigbee2mqtt msg (#2160)
- Loading branch information
1 parent
68cdb6c
commit adc1df0
Showing
12 changed files
with
311 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
front/src/routes/scene/edit-scene/actions/SendZigbee2MqttMessage.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Component } from 'preact'; | ||
import { connect } from 'unistore/preact'; | ||
import { Text, Localizer } from 'preact-i18n'; | ||
|
||
import TextWithVariablesInjected from '../../../../components/scene/TextWithVariablesInjected'; | ||
|
||
const helpTextStyle = { | ||
fontSize: 12, | ||
marginBottom: '.375rem' | ||
}; | ||
|
||
class SendZigbee2MqttMessage extends Component { | ||
handleChangeTopic = e => { | ||
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'topic', e.target.value); | ||
}; | ||
handleChangeMessage = text => { | ||
const newMessage = text && text.length > 0 ? text : undefined; | ||
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'message', newMessage); | ||
}; | ||
|
||
render(props) { | ||
return ( | ||
<div> | ||
<form> | ||
<div class="form-group"> | ||
<label class="form-label"> | ||
<Text id="editScene.actionsCard.zigbee2mqttMessage.topic" /> | ||
<span class="form-required"> | ||
<Text id="global.requiredField" /> | ||
</span> | ||
</label> | ||
<Localizer> | ||
<input | ||
type="text" | ||
class="form-control" | ||
value={props.action.topic} | ||
onChange={this.handleChangeTopic} | ||
placeholder={<Text id="editScene.actionsCard.zigbee2mqttMessage.topicPlaceholder" />} | ||
/> | ||
</Localizer> | ||
</div> | ||
<div class="form-group"> | ||
<label class="form-label"> | ||
<Text id="editScene.actionsCard.zigbee2mqttMessage.messageLabel" /> | ||
</label> | ||
<div style={helpTextStyle}> | ||
<Text id="editScene.actionsCard.zigbee2mqttMessage.variablesExplanation" /> | ||
</div> | ||
<Localizer> | ||
<TextWithVariablesInjected | ||
text={props.action.message} | ||
updateText={this.handleChangeMessage} | ||
triggersVariables={props.triggersVariables} | ||
actionsGroupsBefore={props.actionsGroupsBefore} | ||
variables={props.variables} | ||
placeholder={<Text id="editScene.actionsCard.zigbee2mqttMessage.messagePlaceholder" />} | ||
/> | ||
</Localizer> | ||
</div> | ||
</form> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default connect('httpClient', {})(SendZigbee2MqttMessage); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const logger = require('../../../utils/logger'); | ||
const { ServiceNotConfiguredError } = require('../../../utils/coreErrors'); | ||
|
||
/** | ||
* @description Publish a MQTT message. | ||
* @param {string} topic - MQTT Topic. | ||
* @param {string} message - MQTT message. | ||
* @example zigbee2mqtt.publish('zigbee2mqtt/test', '{}'); | ||
*/ | ||
function publish(topic, message) { | ||
if (!this.mqttClient) { | ||
throw new ServiceNotConfiguredError('MQTT is not configured.'); | ||
} | ||
logger.debug(`Publishing MQTT message on topic ${topic}`); | ||
this.mqttClient.publish(topic, message, undefined, (err) => { | ||
if (err) { | ||
logger.warn(`MQTT - Error publishing to ${topic} : ${err}`); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = { | ||
publish, | ||
}; |
87 changes: 87 additions & 0 deletions
87
server/test/lib/scene/actions/scene.action.sendZigbee2MqttMessage.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const { fake, assert } = require('sinon'); | ||
const EventEmitter = require('events'); | ||
|
||
const { ACTIONS } = require('../../../../utils/constants'); | ||
const { executeActions } = require('../../../../lib/scene/scene.executeActions'); | ||
|
||
const StateManager = require('../../../../lib/state'); | ||
|
||
const event = new EventEmitter(); | ||
|
||
describe('scene.send-zigbee2mqtt-message', () => { | ||
it('should send message with value injected from device get-value', async () => { | ||
const stateManager = new StateManager(event); | ||
stateManager.setState('deviceFeature', 'my-device-feature', { | ||
category: 'light', | ||
type: 'binary', | ||
last_value: 15, | ||
}); | ||
const zigbee2MqttService = { | ||
device: { | ||
publish: fake.resolves(null), | ||
}, | ||
}; | ||
const service = { | ||
getService: fake.returns(zigbee2MqttService), | ||
}; | ||
const scope = {}; | ||
await executeActions( | ||
{ stateManager, event, service }, | ||
[ | ||
[ | ||
{ | ||
type: ACTIONS.DEVICE.GET_VALUE, | ||
device_feature: 'my-device-feature', | ||
}, | ||
], | ||
[ | ||
{ | ||
type: ACTIONS.ZIGBEE2MQTT.SEND, | ||
topic: '/my/mqtt/topic', | ||
message: 'Temperature in the living room is {{0.0.last_value}} °C.', | ||
}, | ||
], | ||
], | ||
scope, | ||
); | ||
assert.calledWith(zigbee2MqttService.device.publish, '/my/mqtt/topic', 'Temperature in the living room is 15 °C.'); | ||
}); | ||
it('should send message with value injected from http-request', async () => { | ||
const stateManager = new StateManager(event); | ||
const http = { | ||
request: fake.resolves({ result: [15], error: null }), | ||
}; | ||
const zigbee2MqttService = { | ||
device: { | ||
publish: fake.resolves(null), | ||
}, | ||
}; | ||
const service = { | ||
getService: fake.returns(zigbee2MqttService), | ||
}; | ||
const scope = {}; | ||
await executeActions( | ||
{ stateManager, event, service, http }, | ||
[ | ||
[ | ||
{ | ||
type: ACTIONS.HTTP.REQUEST, | ||
method: 'post', | ||
url: 'http://test.test', | ||
body: '{"toto":"toto"}', | ||
headers: [], | ||
}, | ||
], | ||
[ | ||
{ | ||
type: ACTIONS.ZIGBEE2MQTT.SEND, | ||
topic: '/my/mqtt/topic', | ||
message: 'Temperature in the living room is {{0.0.result.[0]}} °C.', | ||
}, | ||
], | ||
], | ||
scope, | ||
); | ||
assert.calledWith(zigbee2MqttService.device.publish, '/my/mqtt/topic', 'Temperature in the living room is 15 °C.'); | ||
}); | ||
}); |
Oops, something went wrong.