-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathharmony_hub_funcs.py
59 lines (46 loc) · 1.64 KB
/
harmony_hub_funcs.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
from pyharmony import ha_get_token,ha_get_client
import os,socket,struct
def myint(value):
""" round and convert to int """
return int(round(float(value)))
def myfloat(value, prec=4):
""" round and return float """
return round(float(value), prec)
def ip2long(ip):
""" Convert an IP string to long """
packedIP = socket.inet_aton(ip)
return struct.unpack("!L", packedIP)[0]
def long2ip(value):
return socket.inet_ntoa(struct.pack('!L', value))
# isBit() returns True or False if bit at offset is set or not
def isBit(int_type, offset):
mask = 1 << offset
if (int_type & mask) == 0:
return False
return True
# isBit() returns 1 or 0 if bit at offset is set or not
def isBitI(int_type, offset):
mask = 1 << offset
if (int_type & mask) == 0:
return 0
return 1
# testBit() returns a nonzero result, 2**offset, if the bit at 'offset' is one.
def testBit(int_type, offset):
mask = 1 << offset
return(int_type & mask)
# setBit() returns an integer with the bit at 'offset' set to 1.
def setBit(int_type, offset):
mask = 1 << offset
return(int_type | mask)
# clearBit() returns an integer with the bit at 'offset' cleared.
def clearBit(int_type, offset):
mask = ~(1 << offset)
return(int_type & mask)
# toggleBit() returns an integer with the bit at 'offset' inverted, 0 -> 1 and 1 -> 0.
def toggleBit(int_type, offset):
mask = 1 << offset
return(int_type ^ mask)
def harmony_hub_client(host, port=5222):
token = ha_get_token(host, port)
client = ha_get_client(token, host, port)
return client