11"""Support for AIS SUPLA MQTT"""
22import asyncio
33import logging
4- import os
54
65from homeassistant .components .ais_dom import ais_global
76from 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
9466async 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 (
0 commit comments