-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolykevo_types.py
138 lines (100 loc) · 4.05 KB
/
polykevo_types.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from polyglot.nodeserver_api import Node
from kevoPy import KevoPy
from login import USERNAME, PASSWORD
import hashlib
class KevoDiscovery(Node):
def __init__(self, *args, **kwargs):
super(KevoDiscovery, self).__init__(*args, **kwargs)
self.kevo = KevoPy(USERNAME, PASSWORD)
self.kevo.logger = self.parent.poly.logger
def id_2_addr(self, udn):
''' convert udn id to isy address '''
hasher = hashlib.md5()
hasher.update(udn)
return hasher.hexdigest()[0:14]
def discover(self, **kwargs):
try:
manifest = self.parent.config.get('manifest', {})
self.parent.poly.logger.info("Discovering Kevo Locks...")
self.parent.poly.logger.info("User: %s", USERNAME)
self.kevo.connect()
self.parent.poly.logger.info("Found %d Locks", len(self.kevo.locks()))
if len(self.kevo.locks()) > 0:
for lock in self.kevo.locks():
name = lock.name()
address = self.id_2_addr(lock.identifier())
lnode = self.parent.get_node(address)
if not lnode:
self.logger.info('Adding new Kevo Lock: %s(%s)', name, lock.lock_id)
lock = KevoLock(self.parent, self.parent.get_node('kevodisco'), address, lock, manifest)
self.parent.locks.append(lock)
else:
self.logger.info('Kevo Lock: %s(%s) already added', name, lock.lock_id)
else:
self.logger.info("No Locks found")
return True
except TypeError as e:
self.logger.info('KevoDiscovery discover caught exception: %s', e)
return False
def query(self, **kwargs):
self.parent.report_drivers()
return True
def refreshAll(self):
self.kevo.refreshAll(self.parent.report_drivers)
_drivers = {}
_commands = {'NETDISCO': discover}
node_def_id = 'KEVODISCO'
class KevoLock(Node):
def __init__(self, parent, primary, address, lock, manifest=None):
lock.parent = self
self.logger = parent.poly.logger
self.parent = parent
self.name = lock.name()
self.lock = lock
self.logger.info('Address: %s', address)
super(KevoLock, self).__init__(parent, address, "Kevo " + lock.name(), primary, manifest)
self.logger.info('Initializing New Kevo Lock')
self.update_info()
def _setOn(self, **kwargs):
self.logger.info("Locking: %s", self.name)
result = self.lock.lock()
if result == True:
self.report_driver()
self.logger.info("Locked: %s", self.name)
else:
self.logger.info("Locking: %s failed", self.name)
return result
def _setOff(self, **kwargs):
self.logger.info("Locking: %s", self.name)
result = self.lock.unlock()
if result == True:
self.report_driver()
self.logger.info("Unlocked: %s", self.name)
else:
self.logger.info("Unlocking: %s failed", self.name)
return result
def _stateToUOM(self, state):
return {'Unlocked' : 0,
'Locked' : 100,
'Unknown' : 101,
'Bolt Jam' : 102,
'Processing' : 303,
'Confirming' : 304}[state]
def query(self, **kwargs):
self.update_info()
self.report_driver()
return True
def update_info(self):
self.lock.refresh(self.update_driver)
def update_driver(self):
try:
self.logger.info("Setting ST to %d", self._stateToUOM(self.lock.query()))
self.set_driver('ST', self._stateToUOM(self.lock.query()))
except requests.exceptions.ConnectionError as e:
self.logger.error('Connection error to ISY: %s', e)
_drivers = {'ST': [0, 11, int]}
_commands = {'DON': _setOn,
'DOF': _setOff,
'ST' : query,
'QUERY' : query}
node_def_id = 'KEVO'