Skip to content

Commit

Permalink
Add boost
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgengenbach committed Oct 28, 2021
1 parent f83e7f5 commit c0c7ab6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
13 changes: 11 additions & 2 deletions david_home_automation/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json
import logging
import subprocess
import typing
from dataclasses import dataclass
from subprocess import CalledProcessError


@dataclass
Expand All @@ -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:
Expand Down
23 changes: 21 additions & 2 deletions david_home_automation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
8 changes: 6 additions & 2 deletions david_home_automation/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
<section class="section" id="app">
<div class="container" v-cloak>
<h1 class="title is-2">Home automation</h1>
<hr>

<div v-if="config.hosts && config.hosts.length">
<h2 class="subtitle is-3">Wake on lan</h2>
<div v-for="host in config.hosts">
<button class="button" @click="wakeupHost(host.name)">Wake up "{{host.name}}"</button>
</div>
<hr/>
</div>

<hr/>

<div v-if="config.thermostats && config.thermostats.length">
<h2 class="subtitle is-3">Thermostats</h2>
<div class="box" v-for="thermostat in config.thermostats">
Expand Down Expand Up @@ -47,6 +48,9 @@ <h3 class="subtitle is-5">{{thermostat.name}}</h3>
<button class="button" @click="changeToAutomatic(thermostat.name)">
Automatic
</button>
<button class="button" @click="enableBoost(thermostat.name)">
Boost
</button>
</div>
</div>
</div>
Expand Down
24 changes: 14 additions & 10 deletions david_home_automation/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
})();

0 comments on commit c0c7ab6

Please sign in to comment.