-
Notifications
You must be signed in to change notification settings - Fork 2
/
pyipxRelay.py
51 lines (40 loc) · 1.27 KB
/
pyipxRelay.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
# -*- coding: utf-8 -*-
import collections
import time
from threading import Lock
from pyipx800 import pyipxBase
class Relay(pyipxBase.pyipxBase):
"""Representing IPX800 Relay."""
_mutex = Lock()
_updatets = time.time()
def __init__(self, ipx, id: int):
super().__init__(ipx, id)
def update(self):
with Relay._mutex:
if (self._content==None or (time.time() - Relay._updatets >= pyipxBase.pyipxBase.scan_interval)):
self._content = self._ipx.request({"Get": "R"})
Relay._updatets = time.time()
return self._content
@staticmethod
def len(ipx):
return len(ipx.request({"Get": "R"}))
@property
def state(self) -> bool:
"""Return the current Relay status."""
self.update()
return self._content[f"R{self.id}"] == 1
def on(self) -> bool:
"""Turn on an Relay and return True if it was successful."""
params = {"SetR": self.id}
self._ipx.request(params)
return True
def off(self) -> bool:
"""Turn off an Relay and return True if it was successful."""
params = {"ClearR": self.id}
self._ipx.request(params)
return True
def toggle(self) -> bool:
"""Toggle an Relay and return True if it was successful."""
params = {"ToggleR": self.id}
self._ipx.request(params)
return True