OctoPrint-MyStromSwitch/octoprint_mystromswitch/__init__.py

139 lines
4.3 KiB
Python
Raw Normal View History

2019-11-19 21:29:32 +01:00
# coding=utf-8
from __future__ import absolute_import
import ssl
import octoprint.plugin
from octoprint.util import RepeatedTimer
2019-11-19 21:46:26 +01:00
class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.TemplatePlugin,
octoprint.plugin.StartupPlugin):
2019-11-19 21:29:32 +01:00
def __init__(self):
self._pollTimer = None
self.ip = None
self.intervall = 1
2019-11-19 21:29:32 +01:00
self.ctx = ssl.create_default_context()
self.ctx.check_hostname = False
self.ctx.verify_mode = ssl.CERT_NONE
def initialize(self):
self.ip = self._settings.get(["ip"])
self._logger.debug("ip: %s" % self.ip)
2019-11-19 21:29:32 +01:00
self.intervall = self._settings.get_int(["intervall"])
self._logger.debug("intervall: %s" % self.intervall)
2019-11-19 21:29:32 +01:00
def get_assets(self):
2019-11-19 21:55:31 +01:00
return dict(js=["js/mystromswitch.js"], css=["css/mystromswitch.css"])
2019-11-19 21:29:32 +01:00
def get_template_configs(self):
return [dict(type="sidebar",
2019-11-19 21:46:26 +01:00
name="MyStrom Switch",
2019-11-19 21:29:32 +01:00
custom_bindings=False,
icon="power-off"),
dict(type="settings", custom_bindings=False)]
def _temperature_target(self):
if self._abort_timer_temp is not None:
return
if self.temperatureTarget:
self._abort_timer_temp = RepeatedTimer(2, self._temperature_task)
self._abort_timer_temp.start()
else:
self._timer_start()
def _temperature_task(self):
if self._printer.get_state_id() == "PRINTING" and self._printer.is_printing() == True:
self._abort_timer_temp.cancel()
self._abort_timer_temp = None
return
self._temp = self._printer.get_current_temperatures()
tester = 0;
number = 0;
for tool in self._temp.keys():
if not tool == "bed":
if self._temp[tool]["actual"] <= self.temperatureValue:
tester += 1
number += 1
if tester == number:
self._abort_timer_temp.cancel()
self._abort_timer_temp = None
self._timer_start()
def _timer_start(self):
if self._abort_timer is not None:
return
self._logger.info("Starting abort shutdown printer timer.")
self._timeout_value = self.abortTimeout
self._abort_timer = RepeatedTimer(1, self._timer_task)
self._abort_timer.start()
def _timer_task(self):
if self._timeout_value is None:
return
self._timeout_value -= 1
2019-11-19 21:29:32 +01:00
if self._printer.get_state_id() == "PRINTING" and self._printer.is_printing() == True:
self._timeout_value = 0
2019-11-19 21:29:32 +01:00
self._abort_timer.cancel()
self._abort_timer = None
return
if self._timeout_value <= 0:
if self._abort_timer is not None:
self._abort_timer.cancel()
self._abort_timer = None
self._shutdown_printer()
def on_after_startup(self):
self._logger.info("Hello World!")
def get_settings_defaults(self):
return dict(
ip="",
intervall=1
2019-11-19 21:29:32 +01:00
)
def on_settings_save(self, data):
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
self.ip = self._settings.get(["ip"])
self.intervall = self._settings.get_int(["intervall"])
2019-11-19 21:29:32 +01:00
def get_update_information(self):
return dict(
2019-11-19 21:55:31 +01:00
mystromswitch=dict(
2019-11-19 21:35:43 +01:00
displayName="OctoPrint-MyStromSwitch",
2019-11-19 21:29:32 +01:00
displayVersion=self._plugin_version,
# version check: github repository
type="github_release",
2019-11-19 21:35:43 +01:00
user="da4id",
repo="OctoPrint-MyStromSwitch",
2019-11-19 21:29:32 +01:00
current=self._plugin_version,
# update method: pip w/ dependency links
2019-11-19 21:46:26 +01:00
pip="https://github.com/da4id/OctoPrint-MyStromSwitch/archive/{target_version}.zip"
2019-11-19 21:29:32 +01:00
)
)
2019-11-19 21:35:43 +01:00
__plugin_name__ = "OctoPrint-MyStromSwitch"
2019-11-19 21:29:32 +01:00
def __plugin_load__():
global __plugin_implementation__
2019-11-19 21:46:26 +01:00
__plugin_implementation__ = MyStromSwitchPlugin()
2019-11-19 21:29:32 +01:00
global __plugin_hooks__
__plugin_hooks__ = {
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
}