Skip to content

Commit c448b04

Browse files
committed
mqtt bridge fixes
1 parent 95dabd2 commit c448b04

File tree

3 files changed

+57
-101
lines changed

3 files changed

+57
-101
lines changed

homeassistant/components/ais_dom/ais_global.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,40 @@ def has_front_clock():
311311
return True
312312

313313

314+
# save ais mqtt connection settings
315+
def save_ais_mqtt_connection_settings(mqtt_bridge_settings=None):
316+
with open(
317+
"/data/data/pl.sviete.dom/files/usr/etc/mosquitto/mosquitto.conf", "w"
318+
) as conf_file:
319+
# 1. standard ais settings
320+
conf_file.write("# AIS Config file for mosquitto on gate\n")
321+
conf_file.write("listener 1883 0.0.0.0\n")
322+
conf_file.write("allow_anonymous true\n")
323+
if mqtt_bridge_settings is not None:
324+
# 2. SUPLA MQTT bridge connection settings
325+
conf_file.write("\n")
326+
conf_file.write("# SUPLA MQTT bridge connection\n")
327+
conf_file.write("connection bridge-" + get_sercure_android_id_dom() + "\n")
328+
conf_file.write(
329+
"address "
330+
+ mqtt_bridge_settings["host"]
331+
+ ":"
332+
+ str(mqtt_bridge_settings["port"])
333+
+ "\n"
334+
)
335+
conf_file.write("topic supla/# in\n")
336+
conf_file.write("topic homeassistant/# in\n")
337+
conf_file.write("topic supla/+/devices/+/channels/+/execute_action out\n")
338+
conf_file.write("topic supla/+/devices/+/channels/+/set/+ out\n")
339+
conf_file.write(
340+
"remote_username " + mqtt_bridge_settings["username"] + "\n"
341+
)
342+
conf_file.write(
343+
"remote_password " + mqtt_bridge_settings["password"] + "\n"
344+
)
345+
conf_file.write(
346+
"bridge_cafile /data/data/pl.sviete.dom/files/usr/etc/tls/cert.pem\n"
347+
)
348+
349+
314350
set_global_my_ip(None)

homeassistant/components/ais_supla_mqtt/__init__.py

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Support for AIS SUPLA MQTT"""
22
import asyncio
33
import logging
4-
import os
54

65
from homeassistant.components.ais_dom import ais_global
76
from homeassistant.config_entries import ConfigEntry
@@ -23,6 +22,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
2322
"""Set up SUPLA MQTT from a config entry."""
2423
hass.data[DOMAIN][entry.entry_id] = entry
2524

25+
# after reload from app the the async_unload_entry is called
26+
# check if we still have bridge definition
27+
bridge_settings = "connection bridge-" + ais_global.get_sercure_android_id_dom()
28+
conf_file = open("/data/data/pl.sviete.dom/files/usr/etc/mosquitto/mosquitto.conf")
29+
if bridge_settings not in conf_file:
30+
_LOGGER.info("Connection bridge not exists in mosquitto.conf, reload")
31+
mqtt_broker_settings = entry.data
32+
ais_global.save_ais_mqtt_connection_settings(mqtt_broker_settings)
33+
# restart mqtt broker
34+
await hass.services.async_call(
35+
"ais_shell_command", "restart_pm2_service", {"service": "mqtt"}
36+
)
37+
2638
for component in PLATFORMS:
2739
hass.async_create_task(
2840
hass.config_entries.async_forward_entry_setup(entry, component)
@@ -36,50 +48,10 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
3648
_LOGGER.info("Migrating from version %s", config_entry.version)
3749

3850
if config_entry.version == 1:
39-
# save mqtt connection info
40-
# 1. check if mosquitto.conf exists
41-
if not os.path.isfile(
42-
"/data/data/pl.sviete.dom/files/usr/etc/mosquitto/mosquitto.conf"
43-
):
44-
_LOGGER.error("No mosquitto.conf file exit")
45-
return False
46-
47-
# 2. configuration file exist, check if we have bridge definition
48-
bridge_settings = "connection bridge-" + ais_global.get_sercure_android_id_dom()
49-
conf_file = open(
50-
"/data/data/pl.sviete.dom/files/usr/etc/mosquitto/mosquitto.conf"
51-
)
52-
if bridge_settings in conf_file:
53-
_LOGGER.error("Connection bridge exists in mosquitto.conf, exit")
54-
return False
55-
56-
# 3. configuration add bridge definition
57-
mqtt_settings = config_entry.data
58-
with open(
59-
"/data/data/pl.sviete.dom/files/usr/etc/mosquitto/mosquitto.conf", "w"
60-
) as conf_file:
61-
# save connection in file
62-
conf_file.write("# AIS Config file for mosquitto\n")
63-
conf_file.write("listener 1883 0.0.0.0\n")
64-
conf_file.write("allow_anonymous true\n")
65-
conf_file.write("\n")
66-
conf_file.write(bridge_settings + "\n")
67-
conf_file.write(
68-
"address "
69-
+ mqtt_settings["host"]
70-
+ ":"
71-
+ str(mqtt_settings["port"])
72-
+ "\n"
73-
)
74-
conf_file.write("topic supla/# in\n")
75-
conf_file.write("topic homeassistant/# in\n")
76-
conf_file.write("topic supla/+/devices/+/channels/+/execute_action out\n")
77-
conf_file.write("topic supla/+/devices/+/channels/+/set/+ out\n")
78-
conf_file.write("remote_username " + mqtt_settings["username"] + "\n")
79-
conf_file.write("remote_password " + mqtt_settings["password"] + "\n")
80-
conf_file.write(
81-
"bridge_cafile /data/data/pl.sviete.dom/files/usr/etc/tls/cert.pem\n"
82-
)
51+
# save mqtt configuration add bridge definition
52+
mqtt_broker_settings = config_entry.data
53+
ais_global.save_ais_mqtt_connection_settings(mqtt_broker_settings)
54+
8355
# restart mqtt broker
8456
await hass.services.async_call(
8557
"ais_shell_command", "restart_pm2_service", {"service": "mqtt"}
@@ -93,19 +65,8 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
9365

9466
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
9567
"""Unload a config entry."""
96-
# 1. check if mosquitto.conf exists
97-
if not os.path.isfile(
98-
"/data/data/pl.sviete.dom/files/usr/etc/mosquitto/mosquitto.conf"
99-
):
100-
_LOGGER.error("No mosquitto.conf file exit")
101-
return False
102-
# 2. remove settings from file
103-
with open(
104-
"/data/data/pl.sviete.dom/files/usr/etc/mosquitto/mosquitto.conf", "w"
105-
) as conf_file:
106-
conf_file.write("# AIS Config file for mosquitto\n")
107-
conf_file.write("listener 1883 0.0.0.0\n")
108-
conf_file.write("allow_anonymous true\n")
68+
# remove mqtt bridge settings
69+
ais_global.save_ais_mqtt_connection_settings(None)
10970

11071
# restart mqtt broker
11172
await hass.services.async_call(

homeassistant/components/ais_supla_mqtt/config_flow.py

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -125,53 +125,12 @@ async def async_step_use_bridge_settings(self, user_input=None):
125125
)
126126

127127
# save mqtt connection info
128-
# 1. check if mosquitto.conf exists
129-
if not os.path.isfile(
130-
"/data/data/pl.sviete.dom/files/usr/etc/mosquitto/mosquitto.conf"
131-
):
132-
_LOGGER.error("No mosquitto.conf file exit")
133-
return False
134-
135-
# 2. configuration file exist, check if we have bridge definition
136-
bridge_settings = "connection bridge-" + ais_global.get_sercure_android_id_dom()
137-
conf_file = open(
138-
"/data/data/pl.sviete.dom/files/usr/etc/mosquitto/mosquitto.conf"
139-
)
140-
if bridge_settings in conf_file:
141-
_LOGGER.error("Connection bridge exists in mosquitto.conf, exit")
142-
return False
143-
144-
# 3. configuration add bridge definition
145-
mqtt_settings = self.bridge_config
146-
with open(
147-
"/data/data/pl.sviete.dom/files/usr/etc/mosquitto/mosquitto.conf", "w"
148-
) as conf_file:
149-
# save connection in file
150-
conf_file.write("# AIS Config file for mosquitto\n")
151-
conf_file.write("listener 1883 0.0.0.0\n")
152-
conf_file.write("allow_anonymous true\n")
153-
conf_file.write("\n")
154-
conf_file.write(bridge_settings + "\n")
155-
conf_file.write(
156-
"address "
157-
+ mqtt_settings["host"]
158-
+ ":"
159-
+ str(mqtt_settings["port"])
160-
+ "\n"
161-
)
162-
conf_file.write("topic supla/# in\n")
163-
conf_file.write("topic homeassistant/# in\n")
164-
conf_file.write("topic supla/+/devices/+/channels/+/execute_action out\n")
165-
conf_file.write("topic supla/+/devices/+/channels/+/set/+ out\n")
166-
conf_file.write("remote_username " + mqtt_settings["username"] + "\n")
167-
conf_file.write("remote_password " + mqtt_settings["password"] + "\n")
168-
conf_file.write(
169-
"bridge_cafile /data/data/pl.sviete.dom/files/usr/etc/tls/cert.pem\n"
170-
)
128+
ais_global.save_ais_mqtt_connection_settings(self.bridge_config)
171129
# restart mqtt broker
172130
await self.hass.services.async_call(
173131
"ais_shell_command", "restart_pm2_service", {"service": "mqtt"}
174132
)
133+
# finish the config flow
175134
return self.async_create_entry(
176135
title="SUPLA MQTT BRIDGE", data=self.bridge_config
177136
)

0 commit comments

Comments
 (0)