-
Notifications
You must be signed in to change notification settings - Fork 0
/
icloud_homie_bridge.py
executable file
·154 lines (131 loc) · 7.93 KB
/
icloud_homie_bridge.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python3
import yaml
import time
from pyicloud import PyiCloudService
from homie.device_base import Device_Base
from homie.node.node_base import Node_Base
from homie.node.property.property_string import Property_String
from homie.node.property.property_integer import Property_Integer
from homie.node.property.property_float import Property_Float
from homie.node.property.property_enum import Property_Enum
server_config = {}
mqtt_config = {}
node_config = {}
def get_config():
try:
with open('/etc/icloud/config.yaml') as f:
# use safe_load instead load
configMap = yaml.safe_load(f)
except FileNotFoundError:
with open('config.yaml') as f:
configMap = yaml.safe_load(f)
for item in configMap.items():
if 'account' in item[0]:
for device in item[1].items():
if 'device' in device[0]:
node_config[device[1]['device_name']] = {}
node_config[device[1]['device_name']]['icloud_device_id'] = device[1]['device_id']
node_config[device[1]['device_name']]['icloud_username'] = item[1]['username']
node_config[device[1]['device_name']]['icloud_password'] = item[1]['password']
node_config[device[1]['device_name']]['cachedLocation'] = 'ON'
node_config[device[1]['device_name']]['enableLocation'] = 'ON'
elif 'mqtt' in item[0]:
data = item[1]
for key, value in data.items():
mqtt_config[key] = value
elif 'update' in item[0]:
server_config['update'] = {}
data = item[1]
for key, value in data.items():
server_config['update'][key] = value
def location_status_handler(node, value):
print(node, value)
node_config[node]['enableLocation'] = value
node_config[node]['location_status'].value = value
def location_cache_handler(node, value):
node_config[node]['cachedLocation'] = value
node_config[node]['location_cache'].value = value
def play_sound_handler(node):
for key, item_value in node_config[node]['api'].devices.items():
if node_config[node]['device'] in str(item_value):
node_config[node]['api'].devices.get(key).play_sound()
def send_message_handler(node, value):
for key, item_value in node_config[node]['api'].devices.items():
if node_config[node]['device'] in str(item_value):
node_config[node]['api'].devices.get(key).display_message(subject='Message', message="%s" %(value), sounds=True)
def bridge_node_refresh_handler(value):
server_config['update']['interval'] = value
bridge_node_refresh.value = value
def icloud_login():
for account in node_config.items():
node_config[account[0]]['api'] = PyiCloudService(node_config[account[0]]['icloud_username'], node_config[account[0]]['icloud_password'])
def icloud_get_updates():
icloud_login()
for node in node_config.items():
for key, value in node_config[node[0]]['api'].devices.items():
if node_config[node[0]]['icloud_device_id'] in str(key):
status = node_config[node[0]]['api'].devices.get(key).status(['batteryStatus'])
if str(status['deviceStatus']) != 203:
node_config[node[0]]['batteryStatus'] = status['batteryStatus']
if node_config[node[0]]['batteryStatus'] != 'Unknown':
node_config[node[0]]['batteryLevel'] = float(status['batteryLevel']) * 100
if node_config[node[0]]['enableLocation'] == "ON":
if node_config[node[0]]['cachedLocation'] == "OFF":
location = node_config[node[0]]['api'].devices.get(key).location()
time.sleep(20)
location = node_config[node[0]]['api'].devices.get(key).location()
if location:
node_config[node[0]]['longitude'] = location['longitude']
node_config[node[0]]['latitude'] = location['latitude']
else:
node_config[node[0]]['longitude'] = ''
node_config[node[0]]['latitude'] = ''
node_config[node[0]]['battery_status'].value = node_config[node[0]]['batteryStatus']
if node_config[node[0]]['batteryStatus'] != 'Unknown':
node_config[node[0]]['battery_level'].value = node_config[node[0]]['batteryLevel']
if node_config[node[0]]['enableLocation'] == "ON" and node_config[node[0]]['longitude'] != '':
node_config[node[0]]['location'].value = "%s, %s" %(node_config[node[0]]['latitude'], node_config[node[0]]['longitude'])
node_config[node[0]]['location_status'].value = node_config[node[0]]['enableLocation']
node_config[node[0]]['location_cache'].value = node_config[node[0]]['cachedLocation']
get_config()
iCloud = Device_Base(device_id='icloud-bridge', name='iCloud Homie Bridge', mqtt_settings=mqtt_config)
for node in node_config.items():
node_id = node[0]
lstatus_handler = lambda value: location_status_handler(node_id, value)
node_config[node[0]]['node'] = Node_Base(iCloud, node[0], node[0].title(), node[0])
iCloud.add_node(node_config[node[0]]['node'])
node_config[node[0]]['battery_status'] = Property_String(node_config[node[0]]['node'], id="batterystatus", name="%s Battery Status" % (node[0].title()), settable=False, value=None)
node_config[node[0]]['node'].add_property(node_config[node[0]]['battery_status'])
node_config[node[0]]['battery_level'] = Property_Float(node_config[node[0]]['node'], id="batterylevel", name="%s Battery Level" % (node[0].title()), settable=False, value=None)
node_config[node[0]]['node'].add_property(node_config[node[0]]['battery_level'])
node_config[node[0]]['location'] = Property_String(node_config[node[0]]['node'], id="location", name="%s Location Coordinates" % (node[0].title()), settable=False, value=None)
node_config[node[0]]['node'].add_property(node_config[node[0]]['location'])
node_config[node[0]]['location_status'] = Property_Enum(node_config[node[0]]['node'], id="locationstatus", name="%s Location Status" % (node[0].title()), data_format='ON,OFF', value=node_config[node[0]]['enableLocation'], set_value = lstatus_handler)
node_config[node[0]]['node'].add_property(node_config[node[0]]['location_status'])
node_config[node[0]]['location_cache'] = Property_Enum(node_config[node[0]]['node'], id="locationcache", name="%s Location Cache" % (node[0].title()), data_format='ON,OFF', value=node_config[node[0]]['cachedLocation'], set_value = lambda value: location_cache_handler(value))
node_config[node[0]]['node'].add_property(node_config[node[0]]['location_cache'])
node_config[node[0]]['play_sound'] = Property_Enum(node_config[node[0]]['node'], id="playsound", name="%s Play Sound" % (node[0].title()), data_format='ON,OFF', retained=False, value='OFF', set_value = lambda value: play_sound_handler(value))
node_config[node[0]]['node'].add_property(node_config[node[0]]['play_sound'])
node_config[node[0]]['send_message'] = Property_String(node_config[node[0]]['node'], id="sendmessage", name="%s Send Message" % (node[0].title()), retained=False, value=None, set_value = lambda value: send_message_handler(node[0], value))
node_config[node[0]]['node'].add_property(node_config[node[0]]['send_message'])
bridge_node = Node_Base(iCloud, "bridge", "Bridge", "bridge")
iCloud.add_node(bridge_node)
bridge_node_refresh = Property_Integer(bridge_node, id="refresh-timer", name="iCloud Refresh Timer", value=server_config['update']['interval'], set_value = lambda value: bridge_node_refresh_handler(value))
bridge_node.add_property(bridge_node_refresh)
iCloud.start()
def main():
last_report_time = 0
for node in node_config.items():
node_config[node[0]]['location_status'].value = node_config[node[0]]['enableLocation']
node_config[node[0]]['location_cache'].value = node_config[node[0]]['cachedLocation']
while True:
if (time.time() - last_report_time) > (int(server_config['update']['interval']) * 60):
icloud_get_updates()
bridge_node_refresh.value = server_config['update']['interval']
last_report_time = time.time()
time.sleep(1)
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, SystemExit):
print("Quitting.")