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

Commit e963362

Browse files
committed
Improved error handling
1 parent bb832fe commit e963362

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ There is also a [Homey integration](https://github.com/lvanderree/com.synplywork
3030
## Contributors
3131
* [Peter Nijssen](https://github.com/peternijssen)
3232
* [Stefan Oude Vrielink](https://github.com/soudevrielink)
33+
* [Bennert van Wijs](https://github.com/bennert)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='spiderpy',
8-
version='1.4.2',
8+
version='1.5.0',
99
description='Python wrapper for the Spider API, a way to manage your Spider installation',
1010
long_description=long_description,
1111
long_description_content_type="text/markdown",

spiderpy/devices/thermostat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,4 @@ def set_fan_speed(self, fanspeed):
112112
return self.is_online & self.api.set_fan_speed(self.data, fanspeed)
113113

114114
def __str__(self):
115-
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}"
115+
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}"

spiderpy/spiderapi.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ def set_temperature(self, thermostat, temperature):
9595
thermostat['properties'][key]['statusLastUpdated'] = str(datetime.now())
9696

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

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

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

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

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

130137
def update_power_plugs(self):
131138
""" Retrieve power plugs """
@@ -170,12 +177,20 @@ def get_power_plug(self, unique_id):
170177
def turn_power_plug_on(self, power_plug_id):
171178
""" Turn the power_plug on"""
172179
url = POWER_PLUGS_URL + "/" + power_plug_id + "/switch"
173-
return self._request_action(url, "true")
180+
try:
181+
return self._request_action(url, "true")
182+
except SpiderApiException:
183+
_LOGGER.error("Unable to turn power plug on.")
184+
return False
174185

175186
def turn_power_plug_off(self, power_plug_id):
176187
""" Turn the power plug off"""
177188
url = POWER_PLUGS_URL + "/" + power_plug_id + "/switch"
178-
return self._request_action(url, "false")
189+
try:
190+
return self._request_action(url, "false")
191+
except SpiderApiException:
192+
_LOGGER.error("Unable to turn power plug off.")
193+
return False
179194

180195
def _is_authenticated(self):
181196
""" Check if access token is expired """

spiderpy/test_spiderapi.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse
2-
import time
32

43
from spiderpy import SpiderApi
54

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

55-
if fan_speed_curr != None:
54+
if fan_speed_curr is not None:
5655
print("Set fan speed back to " + str(fan_speed_curr))
5756
thermostat.set_fan_speed(fan_speed_curr)
5857

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

64-
time.sleep(10)
65-
6663
# Get power plugs
6764
unique_id = None
6865
print("Get power plugs")

0 commit comments

Comments
 (0)