-
Notifications
You must be signed in to change notification settings - Fork 7
/
enet2mqtt.py
168 lines (140 loc) · 6.21 KB
/
enet2mqtt.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import paho.mqtt.client as mqtt
import enet
import json
import time
import logging
import argparse
log = logging.getLogger(__name__)
class MqttEnetLight(enet.Channel):
def get_ha_mqtt_config(self):
location = self._device.location.replace("My home:", "")
name=f"{location}:{self.name}"
config = dict(name=name,
unique_id=self.uid,
brightness=self.has_brightness,
schema="json",
device={"name":f"{location}:{self._device.name}",
"identifiers":[self._device.uid],
"model":self._device.device_type,
"manufacturer":"Jung",
},
platform="mqtt",
brightness_scale=100,
command_topic=f"enet/{self.uid}/set",
state_topic=f"enet/{self.uid}/state")
return json.dumps(config)
def handle_mqtt_set(self, cmd):
brightness = cmd.get("brightness")
state = cmd.get("state")
log.debug(f"cmd=cmd brightness={brightness} state={state}")
if state == "ON":
if brightness is not None:
self.set_value(brightness)
else:
self.turn_on()
elif state == "OFF":
self.turn_off()
brightness = 0
return self.get_mqtt_state(brightness)
def get_mqtt_state(self, value=None):
if value is None:
value = self.get_value()
state = dict(state="OFF" if value == 0 else "ON",
brightness=value)
return json.dumps(state)
enet.Channel = MqttEnetLight
class Enet2MqttBridge(mqtt.Client):
def __init__(self, enet_client, mqtt_host, mqtt_port, mqtt_user, mqtt_passwd):
super().__init__()
self.enet = enet_client
self.mqtt_host = mqtt_host
self.mqtt_port = mqtt_port
self.mqtt_user = mqtt_user
self.mqtt_passwd = mqtt_passwd
self.device_map = {}
self.enet_connect()
self.enet_poll_interval = 30
def enet_connect(self):
self.enet.get_account()
self.enet.simple_login()
self.enet.get_account()
enet_devices = self.enet.get_devices()
# Dump raw devices to file
with open("devices.json", "w") as fp:
json.dump([d._raw for d in enet_devices], fp, indent=2)
print("Dumped all device info to devices.json")
self.devices = []
for device in enet_devices:
for channel in device.channels:
self.devices.append(channel)
self.device_map = {device.uid:device for device in self.devices}
def on_connect(self, mqttc, obj, flags, rc):
log.debug("Connected: rc: {rc}")
def on_message(self, mqttc, obj, msg):
log.info(f"On message: {msg.topic} {msg.payload}")
prefix, device_uid, command = msg.topic.split("/")
if command == "set":
device = self.device_map.get(device_uid)
if not device:
print("Unknown device uid: ", device_uid)
return
payload = json.loads(msg.payload.decode("utf-8"))
state = device.handle_mqtt_set(payload)
self.publish(f"enet/{device_uid}/state", state, retain=True)
else:
log.warning(f"on_message: Unknown command '{command}'")
def on_publish(self, mqttc, obj, mid):
pass
def on_subscribe(self, mqttc, obj, mid, granted_qos):
log.debug(f"Subscribed: {mid} {granted_qos}")
def on_log(self, mqttc, obj, level, string):
pass
def run(self):
self.username_pw_set(self.mqtt_user, self.mqtt_passwd)
self.connect(self.mqtt_host, self.mqtt_port, 60)
self.subscribe("enet/+/set", 0)
self.loop_start()
self.ha_autodiscover()
self.poll_enet()
def ha_autodiscover(self):
for device in self.device_map.values():
config = device.get_ha_mqtt_config()
topic = "{}/light/{}/config".format("homeassistant", device.uid)
log.debug(f"HA Autoconfigure: {topic} {config}")
self.publish(topic, config, retain=True)
#self.publish(topic, "")
def poll_enet(self):
published_state = {}
while True:
log.debug("Polling enet devices...")
for device in self.device_map.values():
try:
state = device.get_mqtt_state()
except:
log.exception("Failed to fetch status from enet:")
time.sleep(self.enet_poll_interval)
continue
topic = f"enet/{device.uid}/state"
if published_state.get(topic) != state:
self.publish(topic, state, retain=True)
published_state[topic] = state
time.sleep(self.enet_poll_interval)
def parseargs():
parser = argparse.ArgumentParser(description='Publish Enet Smart Home devices to MQTT')
parser.add_argument('enet_host', help='Enet server address')
parser.add_argument('--enet_user', default="admin")
parser.add_argument('--enet_passwd', default="admin")
parser.add_argument('mqtt_host', help='MQTT Server address')
parser.add_argument('--mqtt_port', type=int, default=1883)
parser.add_argument('--mqtt_user', default="")
parser.add_argument('--mqtt_passwd', default="")
parser.add_argument('--uri_scheme', choices=['http', 'https'], default="http")
parser.add_argument('--verify_ssl_cert', default="TRUE")
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parseargs()
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
enet_client = enet.EnetClient(args.enet_user, args.enet_passwd, args.enet_host, args.uri_scheme, args.verify_ssl_cert)
bridge = Enet2MqttBridge(enet_client, args.mqtt_host, args.mqtt_port, args.mqtt_user, args.mqtt_passwd)
rc = bridge.run()