From bb832fe382562e60cccfd8bc3c882992f380310f Mon Sep 17 00:00:00 2001 From: Bennert Date: Mon, 28 Dec 2020 21:40:32 +0100 Subject: [PATCH] Added fan & operation mode checks. Added tests. * Added fan & operation mode checks. Added tests. --- spiderpy/devices/thermostat.py | 25 +++++++++++++++++++++++-- spiderpy/spiderapi.py | 13 +++++++++---- spiderpy/test_spiderapi.py | 34 ++++++++++++++++++++++++++-------- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/spiderpy/devices/thermostat.py b/spiderpy/devices/thermostat.py index 15586b4..09e85d3 100644 --- a/spiderpy/devices/thermostat.py +++ b/spiderpy/devices/thermostat.py @@ -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): @@ -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'): @@ -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'): @@ -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}" \ No newline at end of file diff --git a/spiderpy/spiderapi.py b/spiderpy/spiderapi.py index c9fbed3..c69f9a7 100644 --- a/spiderpy/spiderapi.py +++ b/spiderpy/spiderapi.py @@ -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 """ @@ -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 diff --git a/spiderpy/test_spiderapi.py b/spiderpy/test_spiderapi.py index 9379ae1..4122fe5 100755 --- a/spiderpy/test_spiderapi.py +++ b/spiderpy/test_spiderapi.py @@ -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")