-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython3-MQTT-To-HomeAssistant.py
More file actions
executable file
·320 lines (251 loc) · 11.9 KB
/
Copy pathPython3-MQTT-To-HomeAssistant.py
File metadata and controls
executable file
·320 lines (251 loc) · 11.9 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/python3
import subprocess
import argparse
import configparser
import os
import logging
import datetime
import glob
import paho.mqtt.client as mqtt
import time
# This is for my custom logger, that should make it possible to write all types of erros to files (Even the python script itself)
class CustomLogger(logging.Logger):
def __init__(self, name, log_filename):
super().__init__(name)
# Set up formatter for log messages
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# Set up log file handler only if log_filename is provided
if log_filename:
file_handler = logging.FileHandler(log_filename)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)
self.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(formatter)
self.addHandler(console_handler)
# This is for my custom logger, that should make it possible to write all types of erros to files (Even the python script itself)
def setup_logger(log_folder, log_date):
global err_filepath # Use the global variable
log_filename = f"MQTT-To-HomeAssistant-Date{log_date}.log"
log_filepath = os.path.join(log_folder, log_filename) if log_folder.lower() != "no" else None
# Set up logger for normal output
logger = CustomLogger("MQTT-To-HomeAssistant-Date", log_filepath)
# Set the logger level
logger.setLevel(logging.DEBUG)
# Set up logger for errors
err_filename = f"MQTT-To-HomeAssistant-Date{log_date}.err"
err_filepath = os.path.join(log_folder, err_filename) if log_folder.lower() != "no" else None
error_logger = CustomLogger("MQTT-To-HomeAssistant-Date-Error", err_filepath)
# Set the error logger level
error_logger.setLevel(logging.ERROR)
return logger, error_logger
# This is where we connect to the MQTT broker when everything is ready to be sent
def mqtt_connect(client, userdata, flags, rc, mqtt_topic, mqtt_message, logger, error_logger):
try:
if rc == 0:
print_separator(logger)
logger.info('Publishing Topic and Message to MQTT')
# Publish the message after connecting
client.publish(mqtt_topic, mqtt_message, retain=True)
# Disconnect from the MQTT broker
client.disconnect()
else:
raise Exception('Failed publishing message to MQTT')
except Exception as e:
# Handle the exception and capture the error message
error_message = str(e)
print_separator(logger, error_logger)
error_logger.error('MQTT Error message: ' + error_message)
# This is for finding the newest log files to attach to the mail if logs is enabled
def get_newest_files(log_dir, prefix):
files = glob.glob(os.path.join(log_dir, f"{prefix}*"))
files.sort(key=os.path.getctime, reverse=True)
newest_log = None
newest_err = None
for file in files:
ext = os.path.splitext(file)[-1][1:] # Get the file extension without the dot
if ext == "log" and not newest_log:
newest_log = file
elif ext == "err" and not newest_err:
newest_err = file
if newest_log and newest_err:
break
return newest_log, newest_err
# This is is for the send mail part
def send_mail(subject, body, recipient, attachment_files=None):
mail_command = ['mail', '-s', subject, recipient]
if attachment_files:
for file in attachment_files:
mail_command.extend(['--attach', file])
print("Mail command : ", mail_command)
process = subprocess.Popen(mail_command, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
_, stderr_output = process.communicate(input=body.encode())
mail_exit_code = process.returncode
return mail_exit_code, stderr_output.decode().strip()
# This is for the send mail function
# In case one needs to be notified of succes or errors
def MailTo(logger, error_logger, recipient, subject):
#log_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
print_separator(logger)
logger.info('There is an option to send a mail')
# Define subject
#subject = "Error snapshotting or cleaning up snapshots/logs - attaching logs"
# Prepare the list for attachment files
attachment_files = []
# Get the latest .log and .err files
if log_folder.upper() != "NO":
newest_log, newest_err = get_newest_files(log_folder, "MQTT-To-HomeAssistant-Date")
# Add the latest .log and .err files to the attachment list
if newest_log:
attachment_files.append(newest_log)
if newest_err:
attachment_files.append(newest_err)
# Start by creating an empty body
body = ""
if log_folder.upper() != "NO":
# Read contents of .err file if not empty
if os.path.exists(err_filepath) and not os.path.getsize(err_filepath) == 0:
with open(newest_err, 'r') as err_file:
err_contents = err_file.read()
body += "----------\n\n.err file\n" + err_contents
# Read contents of .log file
if os.path.isfile(newest_log):
with open(newest_log, 'r') as log_file:
log_contents = log_file.read()
body += "----------\n\n.log file\n" + log_contents
else:
body += "----------\n\n" + "Logs has beend disabled, enable if nessecary"
# Send the Mail
mail_exit_code, stderr_output = send_mail(subject, body, recipient, attachment_files)
if mail_exit_code == 0:
WasMailSent(logger, error_logger, 0)
else:
WasMailSent(logger, error_logger, mail_exit_code, stderr_output)
# This is an attempt to insure the mail was send succesfully
def WasMailSent(logger, error_logger, MailExitCode, popenstderr=None):
if MailExitCode == 0:
print_separator(logger)
logger.info('Mail was send succesfully')
else:
print_separator(logger, error_logger)
error_logger.error('There was an error sending the mail')
error_logger.error('This is what popen said')
error_logger.error('')
error_logger.error(popenstderr)
print_separator(logger, error_logger)
# This is in case one wishes to etc. shutdown the system or run another custom script after a succesfully MQTT message has been broadcasted
def SystemAction(logger, error_logger):
global MailOption
global SystemOption
if not MailOption.upper() == "NO":
print_separator(logger)
logger.info('The system has an option after the script finishes')
logger.info('')
logger.info('The options is')
logger.info('')
logger.info(SystemOption)
logger.info('')
logger.info('Gonna sleep for 2 minutes to insure mail is sent')
logger.info('')
logger.info('Then execute the command : ' + SystemOption)
print_separator(logger)
# Sleep before executing the desired action
time.sleep(120)
os.system(SystemOption)
elif not SystemOption.upper() == "NO" and MailOption.upper() == "NO":
print_separator(logger)
logger.info('The system has an option after the script finishes')
logger.info('')
logger.info('The options is')
logger.info('')
logger.info(SystemOption)
logger.info('')
logger.info('No mail option chosen')
logger.info('')
logger.info('Gonna execute the command : ' + SystemOption)
print_separator(logger)
os.system(SystemOption)
def print_separator(logger, error_logger=None):
separator_length = 20
separator = "\n" + "\n" + "-" * separator_length + "\n"
if error_logger:
error_logger.error(separator)
else:
logger.info(separator)
def main():
global err_filepath # Use the global variable
global MailOption
global SystemOption
global log_folder
# Setup the arguments the script need
parser = argparse.ArgumentParser(description='Send a message to HomeAssistant with MQTT')
parser.add_argument('-c', '--config', required=True, help='Command: Location for config file')
args = parser.parse_args()
config = configparser.RawConfigParser()
config.read(args.config)
# This is for creating the Date format for the Log Files
DateTime = config.get('MQTT-To-HomeAssistant', 'DateTime')
log_date = datetime.datetime.now().strftime(DateTime)
# This is for the logfile creation
log_folder=config.get('MQTT-To-HomeAssistant', 'LogDestination')
# This is to get the mail or "No" option for mail
MailOption = (config.get('MQTT-To-HomeAssistant', 'Mail'))
# This is for the command after the script has succesfully run
SystemOption = (config.get('MQTT-To-HomeAssistant', 'SystemAction'))
# Check if it is for homeassistant
Use_HomeAssistant = (config.get('MQTT-To-HomeAssistant', 'Use_HomeAssistant'))
if log_folder.upper() != "NO": os.makedirs(log_folder, exist_ok=True)
# Create separate loggers for main logs and error logs
logger, error_logger = setup_logger(log_folder, log_date)
try:
# Your MQTT logic here
mqtt_topic = config.get('MQTT-To-HomeAssistant', 'mqtt_topic')
mqtt_message = config.get('MQTT-To-HomeAssistant', 'mqtt_message')
broker_address = config.get('MQTT-To-HomeAssistant', 'broker_address')
broker_port = config.get('MQTT-To-HomeAssistant', 'broker_port')
broker_port = int(broker_port)
mqtt_username = config.get('MQTT-To-HomeAssistant', 'mqtt_username')
mqtt_password = config.get('MQTT-To-HomeAssistant', 'mqtt_password')
# Create MQTT client instance
client = mqtt.Client()
client.enable_logger(logging.getLogger("paho"))
client.username_pw_set(mqtt_username, mqtt_password)
client.on_connect = lambda client, userdata, flags, rc: mqtt_connect(client, userdata, flags, rc, mqtt_topic, mqtt_message, logger, error_logger)
try:
# Attempt to connect to the MQTT broker
client.connect(broker_address, broker_port)
except OSError as e:
# Handle the specific error [Errno 113] No route to host
if e.errno == 113:
logging.error('MQTT server is not reachable. Check IP and Port.')
# Add your specific error handling code here
else:
logging.error(f'Failed to connect to MQTT broker: {str(e)}')
# Optionally, send a message if HomeAssistant is an option
if Use_HomeAssistant.upper() == "YES":
homeassistant_topic = config.get('MQTT-To-HomeAssistant', 'HomeAssistant_Available')
homeassistant_message = "online"
client.publish(homeassistant_topic, homeassistant_message, retain=True)
# Publish a normal message
client.publish(mqtt_topic, mqtt_message, retain=True)
# Start the MQTT network loop
client.loop_forever()
# Decide if there is an option to send mail
if not MailOption.upper() == "NO":
MailTo(logger, error_logger, MailOption, "Succesfully send MQTT message")
# Decide if there is a shutdown action for the system on succesfull comletion
if not SystemOption.upper() == "NO":
SystemAction(logger, error_logger)
except Exception as e:
print_separator(logger, error_logger)
error_logger.exception("An error occurred:")
print_separator(logger, error_logger)
if not MailOption.upper() == "NO":
MailTo(logger, error_logger, MailOption, "Error pls check or enable logs if nessesary")
finally:
# Check if the .err file is empty, and remove it if it is
if log_folder.upper() != "NO" and os.path.exists(err_filepath) and os.path.getsize(err_filepath) == 0:
os.remove(err_filepath)
if __name__ == "__main__":
main()