-
Notifications
You must be signed in to change notification settings - Fork 0
/
water_heater.py
57 lines (45 loc) · 1.32 KB
/
water_heater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Water heater support via Z-Wave.me
"""
import datetime
import device
import gin
import json
import requests
import os
import time
from pathlib import Path
from typing import List
_REPEATS = 5
@gin.configurable
class Waterheater(device.Device):
def __init__(self, url:str, token:str, schedule:List[int]):
super(Waterheater, self).__init__('waterheater')
self._url = url
self._session = requests.Session()
self._session.headers.update({'Authorization':token})
self._schedule = schedule
def is_it_on(self):
response = self._session.get(self._url)
if not response.ok:
return None
data = json.loads(response.content)
return data['data']['metrics']['level'] == 'on'
def _switch_to(self, on:bool):
url = self._url + '/command/' + ('on' if on else 'off')
last_status = None
for _ in range(_REPEATS):
response = self._session.get(url)
if not response.ok:
time.sleep(1)
continue
last_status = self.is_it_on()
if last_status:
break
return last_status == on
def turn_on(self):
return self._switch_to(True)
def turn_off(self):
return self._switch_to(False)
def should_be_on(self):
return datetime.datetime.now().hour in self._schedule
gin.parse_config_file(os.path.join(str(Path.home()), '.autohome/waterheater.gin'))