diff --git a/appdaemon/apps/base.py b/appdaemon/apps/base.py new file mode 100644 index 0000000..3faf66b --- /dev/null +++ b/appdaemon/apps/base.py @@ -0,0 +1,40 @@ +import appdaemon.plugins.hass.hassapi as hass + +class Base(hass.Hass): + def initialize(self): + if(getattr(super(), 'initialize', False)): + super().initialize() + +class Timers(Base): + def initialize(self): + if(getattr(super(), 'initialize', False)): + super().initialize() + self._timers = {} + + functions = [ 'run_in', 'run_once', 'run_at', 'run_daily', + 'run_hourly', 'run_minutely', 'run_every', 'run_at_sunrise', + 'run_at_sunset' ] + for f in functions: + self.override(f) + + setattr(self, '_cancel_timer', super().cancel_timer) + + def cancel_timer(self, name, *args, **kwargs): + if type(name) is str: + if name in self._timers: + return super().cancel_timer(self._timers[name]) + else: + return super().cancel_timer(*args, **kwargs) + + def override(self, f): + setattr(self, f'_{f}', getattr(self, f)) + def fn(name, *args, **kwargs): + if type(name) is str: + if name in self._timers: + super().cancel_timer(self._timers[name]) + self._timers[name] = getattr(self, f'_{f}')(*args, **kwargs) + return self._timers[name] + else: + return getattr(self, f'_{f}')(name, *args, **kwargs) + setattr(self, f, fn) + diff --git a/appdaemon/apps/hello.py b/appdaemon/apps/hello.py index 0ef8f01..49dd2e4 100644 --- a/appdaemon/apps/hello.py +++ b/appdaemon/apps/hello.py @@ -1,12 +1,19 @@ -import appdaemon.plugins.hass.hassapi as hass -# -# Hellow World App -# -# Args: -# +import base -class HelloWorld(hass.Hass): +class HelloWorld(base.Timers): - def initialize(self): - self.log("Hello from AppDaemon") - self.log("You are now ready to run Apps!") + def initialize(self): + super().initialize() + + self.log("Hello from AppDaemon") + self.log("You are now ready to run Apps!") + + self.run_in('test1', self.after_time, 3) + self.run_in('test2', self.after_time2, 5) + self.run_in(self.after_time2, 7) + self.cancel_timer('test2') + + def after_time(self, kwargs): + self.log("Running function") + def after_time2(self, kwargs): + self.log("Running function2") diff --git a/appdaemon/apps/helloworld.yaml b/appdaemon/apps/helloworld.yaml index daff739..8b1aa49 100644 --- a/appdaemon/apps/helloworld.yaml +++ b/appdaemon/apps/helloworld.yaml @@ -1,4 +1,9 @@ +global_modules: + - base + hello_world: + global_dependencies: + - base module: hello class: HelloWorld