Skip to content

Commit

Permalink
add LED control via M150 commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jneilliii committed Apr 4, 2021
1 parent c1a3a57 commit 65f2fff
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
52 changes: 51 additions & 1 deletion octoprint_tasmota/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def on_settings_save(self, data):
self.poll_status.start()

def get_settings_version(self):
return 10
return 11

def on_settings_migrate(self, target, current=None):
if current is None or current < 6:
Expand Down Expand Up @@ -261,6 +261,14 @@ def on_settings_migrate(self, target, current=None):
plug["event_on_connecting"] = False
arrSmartplugs_new.append(plug)
self._settings.set(["arrSmartplugs"], arrSmartplugs_new)
if current < 11:
# Add new fields
arrSmartplugs_new = []
for plug in self._settings.get(['arrSmartplugs']):
plug["is_led"] = False
plug["brightness"] = 50
arrSmartplugs_new.append(plug)
self._settings.set(["arrSmartplugs"], arrSmartplugs_new)

##~~ AssetPlugin mixin

Expand Down Expand Up @@ -676,6 +684,18 @@ def gcode_off(self, plug):
def gcode_on(self, plug):
self.turn_on(plug["ip"], plug["idx"])

def gcode_led(self, plugip, led_data):
self._tasmota_logger.debug("Received LED Command for {} with parameters {}".format(plugip, led_data))
for plug in self._settings.get(["arrSmartplugs"]):
if plug["is_led"]:
if led_data["LEDBrightness"] == -1:
led_data["LEDBrightness"] = plug["brightness"]
try:
requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": "backlog dimmer {}; color2 {},{},{}; white {}; power{} on".format(led_data["LEDBrightness"], led_data["LEDRed"], led_data["LEDGreen"], led_data["LEDBlue"], led_data["LEDWhite"], plug["idx"])}, timeout=3)
self._plugin_manager.send_plugin_message(self._identifier, dict(currentState="on", ip=plug["ip"], idx=plug["idx"], color=led_data))
except Exception as e:
self._logger.debug("Error: {}".format(e))

def processGCODE(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **kwargs):
if gcode:
if gcode in ["M80", "M81"] and cmd.count(" ") >= 2:
Expand All @@ -699,6 +719,36 @@ def processGCODE(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **kwar
return
else:
return
elif gcode == "M150":
workleds = dict(LEDRed=0, LEDBlue=0, LEDGreen=0, LEDWhite=0, LEDBrightness=-1)
workval = cmd.upper().split()
for i in workval:
firstchar = str(i[0].upper())
leddata = str(i[1:].strip())
if not leddata.isdigit() and firstchar != 'I':
self._tasmota_logger.debug(leddata)
return

if firstchar == 'M':
continue
elif firstchar == "I":
plugip = leddata
elif firstchar == 'R':
workleds['LEDRed'] = int(leddata)
elif firstchar == 'G' or firstchar == 'U':
workleds['LEDGreen'] = int(leddata)
elif firstchar == 'B':
workleds['LEDBlue'] = int(leddata)
elif firstchar == "W":
workleds['LEDWhite'] = int(float(leddata)/255*100)
elif firstchar == "P":
workleds['LEDBrightness'] = int(float(leddata)/255*100)
else:
self._tasmota_logger.debug(leddata)

t = threading.Timer(0, self.gcode_led, [plugip, workleds])
t.daemon = True
t.start()
elif self.powerOffWhenIdle and not (gcode in self._idleIgnoreCommandsArray):
self._waitForHeaters = False
self._reset_idle_timer()
Expand Down
5 changes: 5 additions & 0 deletions octoprint_tasmota/static/css/tasmota.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ input[type="datetime-local"].alert-error {
background-color: #f2dede;
border-color: #b94a48;
}

#TasmotaEditor input[type=checkbox] {
margin-top: 0px;
vertical-align: baseline;
}
13 changes: 9 additions & 4 deletions octoprint_tasmota/static/js/tasmota.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $(function() {
self.arrSmartplugs = ko.observableArray();
self.arrSmartplugsTooltips = ko.observableDictionary({});
self.arrSmartplugsStates = ko.observableDictionary({});
self.arrSmartplugsLEDColors = ko.observableDictionary({});
self.isPrinting = ko.observable(false);
self.gcodeOnString = function(data){return 'M80 '+data.ip()+' '+data.idx();};
self.gcodeOffString = function(data){return 'M81 '+data.ip()+' '+data.idx();};
Expand All @@ -23,9 +24,9 @@ $(function() {
self.get_color = function(data){
switch(self.arrSmartplugsStates.get(data.ip()+'_'+data.idx())()) {
case "on":
return data.on_color();
return self.arrSmartplugsLEDColors.get(data.ip()+'_'+data.idx())() ? self.arrSmartplugsLEDColors.get(data.ip()+'_'+data.idx())() : data.on_color();
case "off":
return data.off_color();
return self.arrSmartplugsLEDColors.get(data.ip()+'_'+data.idx())() ? data.unknown_color() : data.off_color();
default:
return data.unknown_color();
}
Expand Down Expand Up @@ -386,7 +387,7 @@ $(function() {
var tooltip = plug.label();
if(data.sensor_data) {
for(k in data.sensor_data) {
tooltip += '<br>' + k + ': ' + data.sensor_data[k]
tooltip += '<br>' + k + ': ' + data.sensor_data["k"]
}
}
if(data.energy_data) {
Expand All @@ -403,7 +404,7 @@ $(function() {
}
try {
self.arrSmartplugsStates.set(data.ip + '_' + data.idx, data.currentState);
}catch (error) {
} catch (error) {
self.processing.remove(data.ip);
console.log('currentState', error);
}
Expand All @@ -415,6 +416,10 @@ $(function() {
hide: true
});
}
if(data.color){
var color = (data.color.LEDBrightness > 0) ? 'RGB(' + data.color.LEDRed + ',' + data.color.LEDGreen + ',' + data.color.LEDBlue + ')' : plug["unknown_color"];
self.arrSmartplugsLEDColors.set(data.ip + '_' + data.idx, color);
}
self.processing.remove(data.ip);
}
};
Expand Down
6 changes: 3 additions & 3 deletions octoprint_tasmota/templates/tasmota_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,16 @@
<td><div class="controls"><label class="control-label">{{ _('Off Color') }}</label><input type="color" class="input input-small" data-bind="value: off_color" /></div></td>
<td><div class="controls"><label class="control-label">{{ _('Unknown Color') }}</label><input type="color" class="input input-small" data-bind="value: unknown_color" /></div></td>
<td><div class="controls"><label class="control-label">{{ _('Icon Class') }}</label><input type="text" class="input-block-level" data-bind="value: icon, iconpicker: icon,iconpickerOptions: {hideOnSelect: true, collision: true}" /></div></td>
<td></td>
<td><div class="controls"><label class="control-label"><input type="checkbox" data-bind="checked: is_led" title="Is an LED or WS2812 type device connected." /> {{ _('LED') }}</label><div class="input-append"><input type="number" min="1" max="100" class="input-small" data-bind="value: brightness, visible: is_led" /><span class="add-on">%</span></div></div></td>
<td><div class="controls"><label class="control-label">{{ _('Sensor Identifier') }}</label><input type="text" class="input-block-level" data-bind="value: sensor_identifier" /></div></td>
</tr>
<tr>
<td><div class="controls"><label class="checkbox"><input type="checkbox" data-bind="checked: event_on_upload, enable: $root.settings.settings.plugins.tasmota.event_on_upload_monitoring()" title="Automatically power on when file is uploaded." /> On with Upload</label></div></td>
<td><div class="controls"><label class="checkbox"><input type="checkbox" data-bind="checked: event_on_connecting, enable: $root.settings.settings.plugins.tasmota.event_on_connecting_monitoring()" title="Automatically power on when pressing Connect button" /> On with Connect</label></div></td>
<td><div class="controls"><label class="checkbox"><input type="checkbox" data-bind="checked: automaticShutdownEnabled, enable: $root.settings.settings.plugins.tasmota.powerOffWhenIdle()" title="Automatically power off when printer is idle." /> Off on Idle</label></div></td>
<td><div class="controls"><label class="checkbox"><input type="checkbox" data-bind="checked: thermal_runaway" title="Power off if temperature exceeds configured max temperatures." /> Thermal Runaway</label></div></td>
<td><div class="controls"><label class="checkbox"><input type="checkbox" title="{{ _('Automatically power off this relay when Error Event Monitoring is enabled.') }}" data-toggle="tooltip" data-bind="checked: event_on_error, enable: $root.settings.settings.plugins.tasmota.event_on_error_monitoring(), tooltip: {}" disabled /> {{ _('Off on Error') }}</label></div></td>
<td><div class="controls"><label class="checkbox"><input type="checkbox" title="{{ _('Automatically power off this relay when Disconnect Event Monitoring is enabled.') }}" data-toggle="tooltip" data-bind="checked: event_on_disconnect, enable: $root.settings.settings.plugins.tasmota.event_on_disconnect_monitoring(), tooltip: {}" disabled /> {{ _('Off on Disconnect') }}</label></div></td>
<td><div class="controls" data-toggle="tooltip" data-bind="tooltip: {container: '#TasmotaEditor'}" title="{{ _('Automatically power off this relay when Error Event Monitoring is enabled.') }}"><label class="checkbox"><input type="checkbox" data-bind="checked: event_on_error, enable: $root.settings.settings.plugins.tasmota.event_on_error_monitoring()" disabled /> {{ _('Off on Error') }}</label></div></td>
<td><div class="controls"><label class="checkbox"><input type="checkbox" title="{{ _('Automatically power off this relay when Disconnect Event Monitoring is enabled.') }}" data-toggle="tooltip" data-bind="checked: event_on_disconnect, enable: $root.settings.settings.plugins.tasmota.event_on_disconnect_monitoring(), tooltip: {container: '#TasmotaEditor'}" disabled /> {{ _('Off on Disconnect') }}</label></div></td>
</tr>
<tr>
<td>
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint-Tasmota"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "1.0.2rc3"
plugin_version = "1.0.2rc4"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit 65f2fff

Please sign in to comment.