-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcutreronte_domoticz.py
50 lines (39 loc) · 1.48 KB
/
cutreronte_domoticz.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
import urllib.request
import urllib.parse
from time import sleep
import logging
logger = logging.getLogger(__name__)
class CutreronteDomoticz:
domoticz_api = "/json.htm?type=command¶m=switchlight&idx={}&switchcmd={}"
def __init__(self, host, port, idx_open, idx_pestillera):
self.host = host
self.port = port
self.idx_open = idx_open
self.idx_pestillera = idx_pestillera
# TODO con usuario y contraseña
def activar(self):
self._api_domoticz(self.idx_open, True)
def desactivar(self):
self._api_domoticz(self.idx_open, False)
def pestillera(self):
self._api_domoticz(self.idx_pestillera, True)
def _api_domoticz(self, idx, encenderapagar):
accion = "On" if encenderapagar else "Off"
ruta = self.domoticz_api.format(idx, accion)
url = 'http://{}:{}{}'.format(self.host, self.port, ruta)
try:
f = urllib.request.urlopen(url, data=None, timeout=2) # TODO credenciales irian aqui
# print(f.read().decode('utf-8'))
status_code = f.getcode()
except Exception as e:
status_code = e
if status_code != 200:
logging.error("DOMOTICZ no se pudo hacer la peticion a domoticz. Status code: {}".format(status_code))
if __name__ == '__main__':
""" Probar la Api """
dz = CutreronteDomoticz("192.168.1.10", 8080, 1)
dz.activar()
print("Encendido")
sleep(3)
dz.desactivar()
print("Apagado")