From d376d9cd2702147749ea28b5d7f99077cc9cac86 Mon Sep 17 00:00:00 2001 From: Rich O Date: Fri, 22 Nov 2019 09:10:02 -0500 Subject: [PATCH 1/5] Line 41 was causing problems, needs to be moved into CommandOnCooldown, Added check for CommandInvokeError --- r0b3bot.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/r0b3bot.py b/r0b3bot.py index 30d7bc1..4ad4290 100644 --- a/r0b3bot.py +++ b/r0b3bot.py @@ -38,11 +38,16 @@ hassapi = remote.API(HASS_IP_ADDRESS, HASS_API_KEY) @bot.event async def on_command_error(ctx, error): - print(error.retry_after) if isinstance(error, commands.CommandOnCooldown): #msg = 'This command is ratelimited, please try again in {:.2f}s'.format(error.retry_after) seconds = round(error.retry_after,1) + print(f" - Error, user tried to use command while in cooldown (wait is: {seconds}") await ctx.send(f"This command is ratelimited per user, please try again in {seconds}s") + if isinstance(error, commands.CommandInvokeError): + print(" - ERROR: encountered 'CommandInvokeError'") + print(f" - Dumping error: {error}") + await ctx.send("Error, unable to complete your request.") + await ctx.send() else: print('Oopsie, I found an error...') print(f"Error: {error}") From 8088aa1ca5e975d68e6093c6598af3deb4f3d69d Mon Sep 17 00:00:00 2001 From: Rich O Date: Fri, 22 Nov 2019 09:13:53 -0500 Subject: [PATCH 2/5] Removed empty ctx.send --- r0b3bot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/r0b3bot.py b/r0b3bot.py index 4ad4290..1f0d310 100644 --- a/r0b3bot.py +++ b/r0b3bot.py @@ -47,7 +47,6 @@ async def on_command_error(ctx, error): print(" - ERROR: encountered 'CommandInvokeError'") print(f" - Dumping error: {error}") await ctx.send("Error, unable to complete your request.") - await ctx.send() else: print('Oopsie, I found an error...') print(f"Error: {error}") From af97204c278de4bdf06e4ba2e4209284df43ed1c Mon Sep 17 00:00:00 2001 From: Rich O Date: Fri, 22 Nov 2019 09:27:36 -0500 Subject: [PATCH 3/5] adding state attribute if api call fails --- r0b3bot.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/r0b3bot.py b/r0b3bot.py index 1f0d310..d364026 100644 --- a/r0b3bot.py +++ b/r0b3bot.py @@ -222,7 +222,10 @@ async def printstat(ctx): # We weren't able to get the status. Display this text to the chat channel print(" ! Unable to get light status, notifying chat channel") await ctx.send("Hmm, I can't tell if the light is on... oh well") - work_lights = "Unknown" + class State: + state = "unknown" + work_lights = State() + work_lights.state = "Unknown" # If the light is off, let's turn it on before we take a picture print(" - Lights are off, turning on for image capture") From d8edd1b06786dd774dd9a57ad42c9c1ae6e3e7a2 Mon Sep 17 00:00:00 2001 From: Rich O Date: Fri, 22 Nov 2019 09:34:24 -0500 Subject: [PATCH 4/5] added try/except to check for state attribute --- r0b3bot.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/r0b3bot.py b/r0b3bot.py index d364026..153ddf3 100644 --- a/r0b3bot.py +++ b/r0b3bot.py @@ -222,14 +222,20 @@ async def printstat(ctx): # We weren't able to get the status. Display this text to the chat channel print(" ! Unable to get light status, notifying chat channel") await ctx.send("Hmm, I can't tell if the light is on... oh well") + + try: + work_lights.state() + except: + print(" ! Unable to get light status, notifying chat channel") + await ctx.send("Hmm, I can't tell if the light is on... oh well") class State: state = "unknown" work_lights = State() work_lights.state = "Unknown" # If the light is off, let's turn it on before we take a picture - print(" - Lights are off, turning on for image capture") if work_lights.state == 'off': + print(" - Lights are off, turning on for image capture") turned_on_light = True try: # REST API call to home assistant to turn the light off. From 43811751b59db03246678df179fcbe2290000604 Mon Sep 17 00:00:00 2001 From: Rich O Date: Fri, 22 Nov 2019 09:39:12 -0500 Subject: [PATCH 5/5] Eliminated defunct code and added comments --- r0b3bot.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/r0b3bot.py b/r0b3bot.py index 153ddf3..4f33d4c 100644 --- a/r0b3bot.py +++ b/r0b3bot.py @@ -216,18 +216,17 @@ async def printstat(ctx): # Try to get the status of the 3D printer light print(" - Getting Printer Light Status") - try: - work_lights = remote.get_state(hassapi, 'switch.work_lights') - except: - # We weren't able to get the status. Display this text to the chat channel - print(" ! Unable to get light status, notifying chat channel") - await ctx.send("Hmm, I can't tell if the light is on... oh well") + work_lights = remote.get_state(hassapi, 'switch.work_lights') + # Making sure that the API request succeeded. If it has, there will be a state attribute added to + # work_lights. try: work_lights.state() except: - print(" ! Unable to get light status, notifying chat channel") - await ctx.send("Hmm, I can't tell if the light is on... oh well") + print(" ! Unable to get light status from Homeassistant, notifying chat channel") + await ctx.send("Hmm, I can't tell if the light is on... oh well [Hass API error]") + + # Create State class and add state attribute to work_lights class State: state = "unknown" work_lights = State()