Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
Improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
peternijssen committed Dec 28, 2020
1 parent bb832fe commit e963362
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ There is also a [Homey integration](https://github.com/lvanderree/com.synplywork
## Contributors
* [Peter Nijssen](https://github.com/peternijssen)
* [Stefan Oude Vrielink](https://github.com/soudevrielink)
* [Bennert van Wijs](https://github.com/bennert)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='spiderpy',
version='1.4.2',
version='1.5.0',
description='Python wrapper for the Spider API, a way to manage your Spider installation',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
2 changes: 1 addition & 1 deletion spiderpy/devices/thermostat.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ def set_fan_speed(self, fanspeed):
return self.is_online & self.api.set_fan_speed(self.data, fanspeed)

def __str__(self):
return f"{self.id} {self.name} {self.model} {self.manufacturer} {self.type} {self.is_online} {self.operation_mode} {self.has_operation_mode} {self.has_fan_mode} {self.current_temperature} {self.target_temperature} {self.minimum_temperature} {self.maximum_temperature} {self.temperature_steps} {self.current_fan_speed}"
return f"{self.id} {self.name} {self.model} {self.manufacturer} {self.type} {self.is_online} {self.operation_mode} {self.operation_values} {self.has_operation_mode} {self.has_fan_mode} {self.fan_speed_values} {self.current_temperature} {self.target_temperature} {self.minimum_temperature} {self.maximum_temperature} {self.temperature_steps} {self.current_fan_speed}"
31 changes: 23 additions & 8 deletions spiderpy/spiderapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ def set_temperature(self, thermostat, temperature):
thermostat['properties'][key]['statusLastUpdated'] = str(datetime.now())

url = DEVICES_URL + "/" + thermostat['id']
return self._request_action(url, json.dumps(thermostat))
try:
return self._request_action(url, json.dumps(thermostat))
except SpiderApiException:
_LOGGER.error(f"Unable to set temperature to {temperature}.")
return False

def set_operation_mode(self, thermostat, mode):
""" Set the operation mode. Unfortunately, the API requires the complete object"""
Expand All @@ -107,7 +111,11 @@ def set_operation_mode(self, thermostat, mode):
thermostat['properties'][key]['statusLastUpdated'] = str(datetime.now())

url = DEVICES_URL + "/" + thermostat['id']
return self._request_action(url, json.dumps(thermostat))
try:
return self._request_action(url, json.dumps(thermostat))
except SpiderApiException:
_LOGGER.error(f"Unable to set operation mode to {mode}. Is this operation mode supported?")
return False

def set_fan_speed(self, thermostat, fan_speed):
""" Set the fan speed. Unfortunately, the API requires the complete object"""
Expand All @@ -121,11 +129,10 @@ def set_fan_speed(self, thermostat, fan_speed):

url = DEVICES_URL + "/" + thermostat['id']
try:
action_requested = self._request_action(url, json.dumps(thermostat))
# Exception will occur when fan_speed is not supported
return self._request_action(url, json.dumps(thermostat))
except SpiderApiException:
action_requested = False
return action_requested
_LOGGER.error(f"Unable to set fan speed to {fan_speed}. Is this fan speed supported?")
return False

def update_power_plugs(self):
""" Retrieve power plugs """
Expand Down Expand Up @@ -170,12 +177,20 @@ def get_power_plug(self, unique_id):
def turn_power_plug_on(self, power_plug_id):
""" Turn the power_plug on"""
url = POWER_PLUGS_URL + "/" + power_plug_id + "/switch"
return self._request_action(url, "true")
try:
return self._request_action(url, "true")
except SpiderApiException:
_LOGGER.error("Unable to turn power plug on.")
return False

def turn_power_plug_off(self, power_plug_id):
""" Turn the power plug off"""
url = POWER_PLUGS_URL + "/" + power_plug_id + "/switch"
return self._request_action(url, "false")
try:
return self._request_action(url, "false")
except SpiderApiException:
_LOGGER.error("Unable to turn power plug off.")
return False

def _is_authenticated(self):
""" Check if access token is expired """
Expand Down
5 changes: 1 addition & 4 deletions spiderpy/test_spiderapi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import time

from spiderpy import SpiderApi

Expand Down Expand Up @@ -52,7 +51,7 @@ def main():
speed_set = thermostat.set_fan_speed(speed)
assert speed_set & (thermostat.current_fan_speed == speed), "Failed to set fan speed"

if fan_speed_curr != None:
if fan_speed_curr is not None:
print("Set fan speed back to " + str(fan_speed_curr))
thermostat.set_fan_speed(fan_speed_curr)

Expand All @@ -61,8 +60,6 @@ def main():
thermostat = api.get_thermostat(unique_id)
print(thermostat)

time.sleep(10)

# Get power plugs
unique_id = None
print("Get power plugs")
Expand Down

0 comments on commit e963362

Please sign in to comment.