Skip to content

Commit

Permalink
Merge pull request #154 from ngardiner/hassems_debuglog_cleanup
Browse files Browse the repository at this point in the history
Move HASS EMS module to new debugLog function
  • Loading branch information
ngardiner authored Oct 9, 2020
2 parents 10f8f0e + 72abae0 commit 69b7aaa
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions lib/TWCManager/EMS/HASS.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ def __init__(self, master):
self.master.releaseModule("lib.TWCManager.EMS", "HASS")
return None

def debugLog(self, minlevel, message):
if self.debugLevel >= minlevel:
print("debugLog: (" + str(minlevel) + ") " + message)

def getConsumption(self):

if not self.status:
self.debugLog(10, "HASS EMS Module Disabled. Skipping getConsumption")
self.master.debugLog(10, "HASSEMS", "Module Disabled. Skipping getConsumption")
return 0

# Perform updates if necessary
Expand All @@ -67,7 +63,7 @@ def getConsumption(self):
def getGeneration(self):

if not self.status:
self.debugLog(10, "HASS EMS Module Disabled. Skipping getGeneration")
self.master.debugLog(10, "HASSEMS", "Module Disabled. Skipping getGeneration")
return 0

# Perform updates if necessary
Expand All @@ -90,20 +86,20 @@ def getAPIValue(self, entity):
self.fetchFailed = False

try:
self.debugLog(10, "Fetching HomeAssistant EMS sensor value " + str(entity))
self.master.debugLog(10, "HASSEMS", "Fetching HomeAssistant EMS sensor value " + str(entity))
httpResponse = self.requests.get(url, headers=headers, timeout=self.timeout)
except self.requests.exceptions.ConnectionError as e:
self.debugLog(
4, "Error connecting to HomeAssistant to publish sensor values"
self.master.debugLog(
4, "HASSEMS", "Error connecting to HomeAssistant to fetch sensor values"
)
self.debugLog(10, str(e))
self.master.debugLog(10, "HASSEMS", str(e))
self.fetchFailed = True
return False
except self.requests.exceptions.ReadTimeout as e:
self.debugLog(
4, "Read Timeout occurred fetching HomeAssistant sensor value"
self.master.debugLog(
4, "HASSEMS", "Read Timeout occurred fetching HomeAssistant sensor value"
)
self.debugLog(10, str(e))
self.master.debugLog(10, "HASSEMS", str(e))
self.fetchFailed = True
return False

Expand Down Expand Up @@ -133,26 +129,26 @@ def update(self):
if self.hassEntityConsumption:
apivalue = self.getAPIValue(self.hassEntityConsumption)
if self.fetchFailed is not True:
self.debugLog(10, "HASS getConsumption returns " + str(apivalue))
self.master.debugLog(10, "HASSEMS", "getConsumption returns " + str(apivalue))
self.consumedW = float(apivalue)
else:
self.debugLog(
10, "HASS getConsumption fetch failed, using cached values"
self.master.debugLog(
10, "HASSEMS", "getConsumption fetch failed, using cached values"
)
else:
self.debugLog(10, "HASS Consumption Entity Not Supplied. Not Querying")
self.master.debugLog(10, "HASSEMS", "Consumption Entity Not Supplied. Not Querying")

if self.hassEntityGeneration:
apivalue = self.getAPIValue(self.hassEntityGeneration)
if self.fetchFailed is not True:
self.debugLog(10, "HASS getGeneration returns " + str(apivalue))
self.master.debugLog(10, "HASSEMS", "getGeneration returns " + str(apivalue))
self.generatedW = float(apivalue)
else:
self.debugLog(
10, "HASS getGeneration fetch failed, using cached values"
self.master.debugLog(
10, "HASSEMS", "getGeneration fetch failed, using cached values"
)
else:
self.debugLog(10, "HASS Generation Entity Not Supplied. Not Querying")
self.master.debugLog(10, "HASSEMS", "Generation Entity Not Supplied. Not Querying")

# Update last fetch time
if self.fetchFailed is not True:
Expand Down

0 comments on commit 69b7aaa

Please sign in to comment.