-
Notifications
You must be signed in to change notification settings - Fork 1
/
HomieAdapter.py
69 lines (56 loc) · 2.53 KB
/
HomieAdapter.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
from .HomieMQTT import HomieMQTT
from .homie_classes import HomieDevice
#from mycroft.util.log import getLogger
#LOGGER = getLogger(__name__)
class HomieAdapter:
""" Class for controlling Homie Convention.
The Homie Convention follows the following format:
root/system name/node/property/command """
DEVICES = []
def __init__(self,host, port,root,authentication,user,password):
self.homieroot = root
self.homiemqtt = HomieMQTT(host,port,root,authentication,user,password)
#print("sleeping....")
#time.sleep(10)
def getmessages(self):
result1, result2, result3 = self.homiemqtt.getmessages()
return result1, result2, result3
def getdevices(self):
message, parent, deviceid = self.homiemqtt.getmessages()
device=HomieDevice(deviceid,message,parent)
return deviceid, message, parent, device
def getdevicesjson(self):
message, parent, deviceid = self.homiemqtt.getmessages()
device = HomieDevice(deviceid, message, parent)
result_devices = []
i = 0
result_nodes = []
for node in device._nodes:
properties = device._nodes[node]._properties
result_properties = []
ha_type = ""
for prop in properties:
ha_type = device._nodes[node]._type
if ha_type == "sensor":
if prop.find("measure") > -1:
ha_type = prop[8:]
result_properties.append(
{"Name": prop, "Settable": properties[prop]._settable, "Unit": properties[prop]._unit,
"Value": properties[prop]._value, "Datatype": properties[prop]._datatype,
"Format": properties[prop]._format})
result_nodes.append({"Node_id": device._nodes[node]._node_id, "Name": device._nodes[node]._name.replace(u'\xa0', ' '), "Type": ha_type,
"Topicbase": device._nodes[node]._topic_base, "Properties": result_properties})
i = i + 1
result_devices.append({"Nodes": result_nodes})
result = {'Devices': result_devices}
return result
def take_action(self,cmd):
action = cmd[0]
payload = cmd[1]
root = self.homieroot
topic = root+"/"+action
self.homiemqtt.mqttc.publish(topic,str(payload),1,True)
#print("topic: "+topic+"=>"+payload)
#LOGGER.info("topic: "+topic+"=>"+payload)
def check_mqttconnection(self):
return self.homiemqtt.mq_connected