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

Commit

Permalink
Added fan & operation mode checks. Added tests.
Browse files Browse the repository at this point in the history
* Added fan & operation mode checks. Added tests.
  • Loading branch information
bennert authored Dec 28, 2020
1 parent a1d0a04 commit bb832fe
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
25 changes: 23 additions & 2 deletions spiderpy/devices/thermostat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

# noinspection SpellCheckingInspection
class SpiderThermostat(SpiderDevice):
def get_values(self, property):
values = []
for choice in property['scheduleChoices']:
if not choice['disabled']:
values.append(choice['value'])
return values

@property
def operation_mode(self):
Expand All @@ -12,6 +18,14 @@ def operation_mode(self):

return "Idle"

@property
def operation_values(self):
values = []
for prop in self.data.get('properties'):
if prop['id'] == 'OperationMode':
values = self.get_values(prop)
return values

@property
def has_operation_mode(self):
for prop in self.data.get('properties'):
Expand All @@ -28,6 +42,14 @@ def has_fan_mode(self):

return False

@property
def fan_speed_values(self):
values = []
for prop in self.data.get('properties'):
if prop['id'] == 'FanSpeed':
values = self.get_values(prop)
return values

@property
def current_temperature(self):
for prop in self.data.get('properties'):
Expand Down Expand Up @@ -87,8 +109,7 @@ def set_operation_mode(self, operation):

def set_fan_speed(self, fanspeed):
""" Set the fanspeed. Either 'Auto', 'Low', 'Medium', 'High', 'Boost 10', 'Boost 20', 'Boost 30'"""
if self.is_online is True:
self.api.set_fan_speed(self.data, 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}"
13 changes: 9 additions & 4 deletions spiderpy/spiderapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,23 @@ def set_operation_mode(self, thermostat, mode):
url = DEVICES_URL + "/" + thermostat['id']
return self._request_action(url, json.dumps(thermostat))

def set_fan_speed(self, thermostat, fanspeed):
def set_fan_speed(self, thermostat, fan_speed):
""" Set the fan speed. Unfortunately, the API requires the complete object"""
self._reset_status_modified(thermostat) # Make sure only fan speed will be modified
for key, prop in enumerate(thermostat['properties']):
# noinspection SpellCheckingInspection
if prop['id'] == 'FanSpeed':
thermostat['properties'][key]['status'] = fanspeed[0].upper() + fanspeed[1:]
thermostat['properties'][key]['status'] = fan_speed[0].upper() + fan_speed[1:]
thermostat['properties'][key]['statusModified'] = True
thermostat['properties'][key]['statusLastUpdated'] = str(datetime.now())

url = DEVICES_URL + "/" + thermostat['id']
return self._request_action(url, json.dumps(thermostat))
try:
action_requested = self._request_action(url, json.dumps(thermostat))
# Exception will occur when fan_speed is not supported
except SpiderApiException:
action_requested = False
return action_requested

def update_power_plugs(self):
""" Retrieve power plugs """
Expand Down Expand Up @@ -204,7 +209,7 @@ def _request_action(self, url, data):
raise SpiderApiException("Access denied. Failed to refresh?")

if response.status_code != 200:
raise SpiderApiException(f"Unable to perform action. Status code: {response.status_code}")
raise SpiderApiException(f"Unable to perform action. Status code: {response.status_code}. Data: {data}")

return True

Expand Down
34 changes: 26 additions & 8 deletions spiderpy/test_spiderapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,35 @@ def main():
print("Listing thermostats:")
for thermostat in thermostats:
print(thermostat)
print("Set temperature to 19 degrees")
#thermostat.set_temperature(19)
temp_target_curr = thermostat.target_temperature
temp_list = [(temp_target_curr - 1), temp_target_curr]
for temp in temp_list:
print("Set temperature to " + str(temp) + " degrees")
thermostat.set_temperature(temp)
assert (temp == thermostat.target_temperature), "Failed to set target temperature"

if thermostat.has_operation_mode:
print("Set to cool")
#thermostat.set_operation_mode('Cool')
print("Set to heat")
#thermostat.set_operation_mode('Heat')
operation_mode_list = thermostat.operation_values
if operation_mode_list[-1] != thermostat.operation_mode:
operation_mode_list.reverse()
for operation_mode in operation_mode_list:
print("Set to " + operation_mode)
thermostat.set_operation_mode(operation_mode)
assert thermostat.operation_mode == operation_mode, "Failed to set operation mode"

if thermostat.has_fan_mode:
print("Set fan speed to auto")
#thermostat.set_fan_speed('Auto')
fan_speed_curr = thermostat.current_fan_speed
print("Current fan speed: " + str(fan_speed_curr))
speed_list = thermostat.fan_speed_values
speed_list.reverse()
for speed in speed_list:
print("Set fan speed to " + speed)
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:
print("Set fan speed back to " + str(fan_speed_curr))
thermostat.set_fan_speed(fan_speed_curr)

if unique_id is not None:
print("Retrieve by id")
Expand Down

0 comments on commit bb832fe

Please sign in to comment.