Skip to content

Commit

Permalink
Avoid cancelling non existent timers
Browse files Browse the repository at this point in the history
  • Loading branch information
EvTheFuture committed May 1, 2021
1 parent e814709 commit 2996df1
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions apps/virtual_thermostat/virtual_thermostat.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import appdaemon.plugins.mqtt.mqttapi as mqtt
import json

VERSION = "0.9.9"
VERSION = "0.9.10"
MANUFACTURER = "Valitron AB"
MODEL = "Virtual Thermostat"

Expand Down Expand Up @@ -102,6 +102,8 @@ def initialize(self):
)

def terminate(self):
self.remove_timer(self.publish_timer)

"""Store persistance data to file when app terminates"""
try:
topic = self.topic_subscription
Expand All @@ -123,6 +125,18 @@ def debug(self, text):
"""
self.get_main_log().debug(text)

def remove_timer(self, th):
"""Wrapper to cancel_timer with sanity checks
Parameters
----------
th : str
Timer Handle from run_in
"""
if th is not None and self.hass.timer_running(th):
self.hass.cancel_timer(th)
self.log(f"Cancelled the timer with handle: {th}")

def load_persistance_file(self):
"""Load persistance data from file when app starts
and initialize mandatory data if it doenst exist."""
Expand Down Expand Up @@ -384,7 +398,7 @@ def publish_state(self):
self.debug("Setting up timer to make sure we reqularily publish")

try:
self.hass.cancel_timer(self.publish_timer)
self.remove_timer(self.publish_timer)
self.debug("Successfully canceled publish timer")
except Exception as e:
pass
Expand Down

0 comments on commit 2996df1

Please sign in to comment.