forked from Benoit3/Diematic_to_MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hassio.py
133 lines (121 loc) · 5.32 KB
/
Hassio.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging,json
#This class allow to interface with Home Assistant through the MQTT Discovery Protocol
class Hassio:
def __init__(self,mqttClient,topicRoot,clientId,discovery_prefix):
#logger
self.logger = logging.getLogger(__name__);
#attribute init
self.availabilityTopic=None;
#mqttClient instance ref saving
self.mqtt=mqttClient;
self.topicRoot=topicRoot;
self.clientId=clientId;
self.discovery_prefix=discovery_prefix;
self.device = {
"identifiers": [clientId],
"manufacturer": "Dietrich",
"name": clientId
}
def availabilityInfo(self,shortTopic,payload_available,payload_not_available):
#availability info saving
self.availabilityTopic=self.topicRoot+'/'+shortTopic;
self.payload_available=payload_available;
self.payload_not_available=payload_not_available;
def addSensor(self,object_id,name,deviceClass,shortStateTopic,valueTemplate,unit_of_measurement):
#build discovery topic
discoveryTopic=self.discovery_prefix+'/sensor/'+self.clientId+'/'+object_id+'/config';
#build discovery message payload
payload={"name":name};
payload["object_id"]=object_id;
payload["unique_id"]=self.clientId+'.'+object_id;
if (deviceClass is not None):
payload["device_class"]=deviceClass;
payload["state_topic"]=self.topicRoot+'/'+shortStateTopic;
if (valueTemplate is not None):
payload["value_template"]=valueTemplate;
payload["availability_topic"]=self.availabilityTopic;
payload["payload_available"]=self.payload_available;
payload["payload_not_available"]=self.payload_not_available;
if (unit_of_measurement is not None):
payload["unit_of_measurement"]=unit_of_measurement;
payload['device'] = self.device
#send discovery message
self.mqtt.publish(discoveryTopic,json.dumps(payload),1,False);
def addBinarySensor(self,object_id,name,deviceClass,shortStateTopic,payload_on,payload_off):
#build discovery topic
discoveryTopic=self.discovery_prefix+'/binary_sensor/'+self.clientId+'/'+object_id+'/config';
#build discovery message payload
payload={"name":name};
payload["object_id"]=object_id;
payload["unique_id"]=self.clientId+'.'+object_id;
if (deviceClass is not None):
payload["device_class"]=deviceClass;
payload["state_topic"]=self.topicRoot+'/'+shortStateTopic;
payload["payload_on"]=payload_on;
payload["payload_off"]=payload_off;
payload["availability_topic"]=self.availabilityTopic;
payload["payload_available"]=self.payload_available;
payload["payload_not_available"]=self.payload_not_available;
payload["enabled_by_default"]=False;
payload['device'] = self.device
#send discovery message
self.mqtt.publish(discoveryTopic,json.dumps(payload),1,False);
def addNumber(self,object_id,name,shortStateTopic,shortCommandTopic,min,max,step,unit_of_measurement):
#build discovery topic
discoveryTopic=self.discovery_prefix+'/number/'+self.clientId+'/'+object_id+'/config';
#build discovery message payload
payload={"name":name};
payload["object_id"]=object_id;
payload["unique_id"]=self.clientId+'.'+object_id;
payload["state_topic"]=self.topicRoot+'/'+shortStateTopic;
payload["command_topic"]=self.topicRoot+'/'+shortCommandTopic;
payload["availability_topic"]=self.availabilityTopic;
payload["payload_available"]=self.payload_available;
payload["payload_not_available"]=self.payload_not_available;
payload["qos"]=2;
payload["min"]=min;
payload["max"]=max;
payload["step"]=step;
if (unit_of_measurement is not None):
payload["unit_of_measurement"]=unit_of_measurement;
payload['device'] = self.device
#send discovery message
self.mqtt.publish(discoveryTopic,json.dumps(payload),1,False);
def addSelect(self,object_id,name,shortStateTopic,shortCommandTopic,options):
#build discovery topic
discoveryTopic=self.discovery_prefix+'/select/'+self.clientId+'/'+object_id+'/config';
#build discovery message payload
payload={"name":name};
payload["object_id"]=object_id;
payload["unique_id"]=self.clientId+'.'+object_id;
payload["state_topic"]=self.topicRoot+'/'+shortStateTopic;
payload["command_topic"]=self.topicRoot+'/'+shortCommandTopic;
payload["availability_topic"]=self.availabilityTopic;
payload["payload_available"]=self.payload_available;
payload["payload_not_available"]=self.payload_not_available;
payload["qos"]=2;
payload["options"]=options;
payload['device'] = self.device
#send discovery message
self.mqtt.publish(discoveryTopic,json.dumps(payload),1,False);
def addSwitch(self,object_id,name,shortStateTopic,shortCommandTopic,payload_off,payload_on):
#build discovery topic
discoveryTopic=self.discovery_prefix+'/switch/'+self.clientId+'/'+object_id+'/config';
#build discovery message payload
payload={"name":name};
payload["object_id"]=object_id;
payload["unique_id"]=self.clientId+'.'+object_id;
if (shortStateTopic is not None):
payload["state_topic"]=self.topicRoot+'/'+shortStateTopic;
payload["command_topic"]=self.topicRoot+'/'+shortCommandTopic;
payload["availability_topic"]=self.availabilityTopic;
payload["payload_available"]=self.payload_available;
payload["payload_not_available"]=self.payload_not_available;
payload["payload_off"]=payload_off;
payload["payload_on"]=payload_on;
payload["qos"]=2;
payload['device'] = self.device
#send discovery message
self.mqtt.publish(discoveryTopic,json.dumps(payload),1,False);