node-red-contrib-miio-airfresh
Version:
Miio node for Air Fresh (Xiaomi MJXFJ-150-A1)
170 lines (158 loc) • 7.65 kB
HTML
<script type="text/x-red" data-template-name="miio-airfresh-output">
<link rel="stylesheet" href="miio-airfresh/static/css/common.css" type="text/css" />
<div class="form-row">
<label for="node-input-name" class="l-width"><i class="icon-tag"></i> <span data-i18n="label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]placeholder.name">
</div>
<div class="form-row">
<label for="node-input-server" class="l-width"><i class="fa fa-globe"></i> <span data-i18n="label.server"></span></label>
<input type="text" id="node-input-server">
</div>
<div class="form-row">
<label for="node-input-command" class="l-width"><i class="fa fa-tasks"></i> <span data-i18n="label.command"></span></label>
<input type="text" id="node-input-command" style="width:70%">
<input type="hidden" id="node-input-commandType">
</div>
<div class="form-row">
<label for="node-input-payload" class="l-width"><i class="fa fa-envelope"></i> <span data-i18n="label.payload"></span></label>
<input type="text" id="node-input-payload" style="width:70%">
<input type="hidden" id="node-input-payloadType">
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType('miio-airfresh-output', {
category: 'miio-airfresh',
color: '#00bc9c',
icon: "mi.png",
align: 'right',
defaults: {
name: {
value: ""
},
server: {
type: "miio-airfresh-server",
required: true
},
command_name: {
value: null
},
command: {
value: 'set_power',
},
commandType: {
value: 'miio_cmd',
},
payload: {
value: 'payload',
},
payloadType: {
value: 'msg',
}
},
inputs: 1,
outputs: 0,
outputLabels: ["state"],
paletteLabel: 'AirFresh Out',
label: function () {
var label = 'AirFresh Out';
if (this.name) {
label = this.name;
} else if (typeof(this.command_name) == 'string' && this.command_name.length) {
label = this.command_name;
}
return label;
},
oneditprepare: function () {
var node = this;
var topicTypes = {
value: 'miio_cmd',
label: 'mi',
icon: 'icons/node-red-contrib-miio-airfresh/mi.png',
options: [
{'value':'set_power', 'label':RED._("node-red-contrib-miio-airfresh/out:commands.set_power")},
{'value':'set_mode', 'label':RED._("node-red-contrib-miio-airfresh/out:commands.set_mode")},
{'value':'set_ptc_on', 'label':RED._("node-red-contrib-miio-airfresh/out:commands.set_ptc_on")},
{'value':'set_favourite_speed', 'label':RED._("node-red-contrib-miio-airfresh/out:commands.set_favourite_speed")},
{'value':'set_display', 'label':RED._("node-red-contrib-miio-airfresh/out:commands.set_display")},
{'value':'set_sound', 'label':RED._("node-red-contrib-miio-airfresh/out:commands.set_sound")},
{'value':'set_child_lock', 'label':RED._("node-red-contrib-miio-airfresh/out:commands.set_child_lock")}
]
};
$('#node-input-command').typedInput({
types: [topicTypes, {value:'homekit',label:'homekit',icon: 'icons/node-red-contrib-miio-airfresh/homekit-logo.png',options:['homekit']}, 'str', 'msg', {value:'object',label:'object',options:['json']}],
default: 'msg',
value: 'topic',
typeField: $('#node-input-commandType'),
});
$('#node-input-device').on('change', function(type, value) {
$('#node-input-command').trigger('change');
});
$('#node-input-command').on('change', function(type, value) {
var val = $(this).val();
var miioOptions = {};
if ('set_power' === val || 'set_ptc_on' === val || 'set_display' === val || 'set_sound' === val || 'set_child_lock' === val) {
miioOptions = [
{'value':'true', 'label':RED._("node-red-contrib-miio-airfresh/out:arguments.true")},
{'value':'false', 'label':RED._("node-red-contrib-miio-airfresh/out:arguments.false")}
];
} else if ('set_mode' === val) {
miioOptions = [
{'value':'auto', 'label':RED._("node-red-contrib-miio-airfresh/out:arguments.auto")},
{'value':'sleep', 'label':RED._("node-red-contrib-miio-airfresh/out:arguments.sleep")},
{'value':'favourite', 'label':RED._("node-red-contrib-miio-airfresh/out:arguments.favourite")}
];
} else if ('set_favourite_speed' === val) {
miioOptions = [
{'value':'60', 'label':"10%"},
{'value':'70', 'label':"20%"},
{'value':'80', 'label':"30%"},
{'value':'90', 'label':"40%"},
{'value':'100', 'label':"50%"},
{'value':'110', 'label':"60%"},
{'value':'120', 'label':"70%"},
{'value':'130', 'label':"80%"},
{'value':'140', 'label':"90%"},
{'value':'150', 'label':"100%"}
];
}
if (miioOptions.length) {
var payloadTypes = {
value: 'miio_payload',
label: 'mi',
icon: 'icons/node-red-contrib-miio-airfresh/mi.png',
options: miioOptions
};
$('#node-input-payload').typedInput('types', [payloadTypes, 'msg', 'flow', 'global', 'str', 'num']);
} else {
$('#node-input-payload').typedInput('types', ['msg', 'flow', 'global', 'str', 'num']);
}
});
//init typedInput
var payloadTypes = {
value: 'miio_payload',
label: 'mi',
icon: 'icons/node-red-contrib-miio-airfresh/mi.png',
options: []
};
$('#node-input-payload').typedInput({
types: [payloadTypes, 'msg', 'flow', 'global', 'str', 'num'],
default: 'msg',
value: 'payload',
typeField: $('#node-input-payloadType'),
});
$('#node-input-commandType').val(node.commandType);
$('#node-input-payloadType').val(node.payloadType);
},
oneditsave: function () {
var commandCode = $('#node-input-command').val();
var commandType = $('#node-input-commandType').val();
if (commandType == 'miio_cmd') {
var commandCode = $('#node-input-command').val();
var commandLabel = $('.red-ui-typedInput-options [value=' + commandCode + ']').text();
this.command_name = commandLabel?commandLabel:null;
} else {
this.command_name = commandCode;
}
}
});
</script>