diff --git a/david_home_automation/config.py b/david_home_automation/config.py index a27298f..5cb2a88 100644 --- a/david_home_automation/config.py +++ b/david_home_automation/config.py @@ -1,7 +1,9 @@ import json +import logging import subprocess import typing from dataclasses import dataclass +from subprocess import CalledProcessError @dataclass @@ -21,8 +23,15 @@ class BluetoothThermostat(object): def eq3_cmd(self, cmd): return f'eq3.exp {self.mac_address} {cmd}' - def execute_eq3_cmd(self, cmd): - return subprocess.check_output(f'{self.eq3_cmd(cmd)}'.split(' ')).decode('utf-8') + def execute_eq3_cmd(self, cmd, tries: int = 2): + whole_command = f'{self.eq3_cmd(cmd)}'.split(' ') + for x in range(tries): + try: + return subprocess.check_output(whole_command).decode('utf-8') + except CalledProcessError as e: + logging.error(f'Could not execute command: {whole_command}. Retrying', exc_info=e) + logging.error(f'Could not execute command: {whole_command}') + raise Exception(f'Command "{whole_command}" exited with non-zero code after {tries} tries') def get_temperature(self): if not self.status: diff --git a/david_home_automation/main.py b/david_home_automation/main.py index effdc5a..70e384a 100644 --- a/david_home_automation/main.py +++ b/david_home_automation/main.py @@ -37,8 +37,27 @@ def change_thermostat_temperature(): except: return dict(error='could not parse temperature'), 400 - response = thermostat.execute_eq3_cmd(f'temp {temperature:.1f}') - return jsonify(dict(message=response)) + try: + response = thermostat.execute_eq3_cmd(f'temp {temperature:.1f}') + return jsonify(dict(message=response)) + except Exception as e: + return jsonify(dict(message=str(e))), 500 + + +@app.route("/api/thermostats/set-boost", methods=['POST']) +def set_thermostat_boost(): + body = request.get_json(force=True) + name = body.get('name') + thermostat = CONFIG.get_thermostat_by_name(name) + # ToDo: a lot of duplication + if not name or not thermostat: + return dict(error='invalid name'), 400 + enable_boost = body.get('enable', True) + try: + response = thermostat.execute_eq3_cmd('boost' if enable_boost else 'boost off') + return jsonify(dict(message=response)) + except Exception as e: + return jsonify(dict(message=str(e))), 500 @app.route("/api/thermostats/change-to-automatic", methods=['POST']) diff --git a/david_home_automation/static/index.html b/david_home_automation/static/index.html index 4ecc9e3..fbb825b 100644 --- a/david_home_automation/static/index.html +++ b/david_home_automation/static/index.html @@ -10,15 +10,16 @@

Home automation

-

Wake on lan

-
+ +
+

Thermostats

@@ -47,6 +48,9 @@

{{thermostat.name}}

+
diff --git a/david_home_automation/static/script.js b/david_home_automation/static/script.js index 0d5878e..04b9ee7 100644 --- a/david_home_automation/static/script.js +++ b/david_home_automation/static/script.js @@ -25,18 +25,22 @@ name: name }) }, + enableBoost: (name) => { + axios + .post('/api/thermostats/set-boost', { + name: name + }) + }, changeTemperatureTo: (name, temperature) => { - changeTemperatureTo(name, temperature) + axios + .post('/api/thermostats/change-temperature', { + name: name, + temperature: temperature + }) + .catch((error) => { + console.log(this); + }); } } }); - - - function changeTemperatureTo(name, temperature) { - axios - .post('/api/thermostats/change-temperature', { - name: name, - temperature: temperature - }) - } })(); \ No newline at end of file