Oh, I REALLY donāt want to complicate things The simpler the better.
Thanks for your thoughts.
First off, thanks for pointing out the extraneous passing of arguments - a remnant of my lack of understanding of scope in Python.
Second, the doubling of time period is by design (see the pseudo-code - # set timer period to be double current timer period (max 48 hours)
). The rationale is - if the garage door is open then I donāt want my phone buzzing every hour if eg I am away and canāt do anything about it. I want to be notified after 1, 2, 4, 8, 16, 32 hours then 2, 4, 6, etc days. (It said āhoursā rather than āsecondsā in the notifier - but that was a simple division oversight, now amended.)
My code is looking a bit better now. Top priorities now:
- work out how to notify which garage is open (the āxxxā will be replaced by
friendly_name
, as soon as I can work out how to access it #newbieissues )
- look up how to round the hours the garage has been open (rather than the ~20 decimal places that are currently being posted!!)
class warn_if_garage_open(hass.Hass):
def initialize(self):
self.log("Hello from warn_if_garage_open")
self.listen_state(self.garage_open, self.args["garage_name"], new="open")
self.listen_state(self.garage_closed, self.args["garage_name"], new="closed")
def garage_open(self, entity, attribute, old, new, kwargs):
self.timer = 60*60
self.handle = self.run_in(self.garage_notifier, self.timer)
def garage_notifier(self, kwargs):
# friendly_name = self.get_state(kwargs['entity'], attribute='friendly_name')
notifier(self, "Garage door alert!", "The xxx has been open for {} hours".format(self.timer/(60*60)), "zero.wav")
self.timer = min(172800, self.timer*2)
self.handle = self.run_in(self.garage_notifier, self.timer)
def garage_closed(self, entity, attribute, old, new, kwargs):
try:
self.cancel_timer(self.handle)
except AttributeError:
self.log('Tried to cancel a timer for {}, but none existed!'.format(entity), level='DEBUG')