11import asyncio
22import binascii
3+ from collections import namedtuple
34import hashlib
45import json
56import logging
67import os
78import typing
8- from collections import namedtuple
99
1010from paho .mqtt .client import Client , MQTTMessage
1111
1212from paradox .config import config as cfg
1313from paradox .event import Change , Event , EventLevel , Notification
1414from paradox .lib import ps
15- from paradox .lib .utils import (JSONByteEncoder , call_soon_in_main_loop ,
16- sanitize_key )
15+ from paradox .lib .utils import JSONByteEncoder , call_soon_in_main_loop , sanitize_key
1716
1817from .core import AbstractMQTTInterface
1918from .helpers import ELEMENT_TOPIC_MAP , get_control_topic_prefix
@@ -32,7 +31,7 @@ def mqtt_handle_decorator(
3231 async def try_func (* args , ** kwargs ):
3332 try :
3433 return await func (* args , ** kwargs )
35- except :
34+ except Exception :
3635 logger .exception ("Exception executing MQTT function" )
3736
3837 def wrapper (
@@ -66,7 +65,7 @@ def wrapper(
6665 topics = topic .split ("/" )
6766
6867 if len (topics ) < 3 :
69- logger .error ("Invalid topic in mqtt message: {}" . format ( message .topic ) )
68+ logger .error (f "Invalid topic in mqtt message: { message .topic } " )
7069 return
7170
7271 content = message .payload .decode ("utf-8" ).strip ()
@@ -78,7 +77,7 @@ def wrapper(
7877 call_soon_in_main_loop (
7978 try_func (self , ParsedMessage (topics , element , content ))
8079 )
81- except :
80+ except Exception :
8281 logger .exception ("Failed to execute command" )
8382
8483 return wrapper
@@ -119,19 +118,20 @@ def __init__(self, alarm):
119118 ps .subscribe (self ._handle_panel_event , "events" )
120119
121120 def on_connect (self , mqttc , userdata , flags , result ):
122- self .subscribe_callback (get_control_topic_prefix ("output" )+ "/#" ,
121+ self .subscribe_callback (
122+ get_control_topic_prefix ("output" ) + "/#" ,
123123 self ._mqtt_handle_output_control ,
124124 )
125125 self .subscribe_callback (
126- get_control_topic_prefix ("door" )+ "/#" ,
126+ get_control_topic_prefix ("door" ) + "/#" ,
127127 self ._mqtt_handle_door_control ,
128128 )
129129 self .subscribe_callback (
130- get_control_topic_prefix ("zone" )+ "/#" ,
130+ get_control_topic_prefix ("zone" ) + "/#" ,
131131 self ._mqtt_handle_zone_control ,
132132 )
133133 self .subscribe_callback (
134- get_control_topic_prefix ("partition" )+ "/#" ,
134+ get_control_topic_prefix ("partition" ) + "/#" ,
135135 self ._mqtt_handle_partition_control ,
136136 )
137137 self .subscribe_callback (
@@ -172,7 +172,7 @@ async def _mqtt_handle_zone_control(self, prep: ParsedMessage):
172172 else :
173173 command , user = _extract_command_user (command )
174174
175- message = "Zone command: {}={} user: {}" . format ( element , command , user )
175+ message = f "Zone command: { element } ={ command } user: { user } "
176176 logger .debug (message )
177177 self ._publish_command_status (message )
178178
@@ -204,17 +204,17 @@ async def _mqtt_handle_partition_control(self, prep: ParsedMessage):
204204 if command .startswith ("code_toggle-" ):
205205 tokens = command .split ("-" )
206206 if len (tokens ) < 2 :
207- logger .warning ("Invalid token length {}" . format ( len (tokens )) )
207+ logger .warning (f "Invalid token length { len (tokens )} " )
208208 return
209209
210210 if tokens [1 ] not in cfg .MQTT_TOGGLE_CODES :
211- logger .warning ("Invalid toggle code {}" . format ( tokens [1 ]) )
211+ logger .warning (f "Invalid toggle code { tokens [1 ]} " )
212212 return
213213
214214 if element .lower () == "all" :
215215 command = "arm"
216216
217- for k , v in self .partitions .items ():
217+ for k , _ in self .partitions .items ():
218218 # If "all" and a single partition is armed, default is
219219 # to disarm
220220 for k1 , v1 in self .partitions [k ].items ():
@@ -239,7 +239,7 @@ async def _mqtt_handle_partition_control(self, prep: ParsedMessage):
239239 else :
240240 command = "arm"
241241 else :
242- logger .warning ("Element {} not found" . format ( element ) )
242+ logger .warning (f "Element { element } not found" )
243243 return
244244
245245 ps .sendNotification (
@@ -252,7 +252,7 @@ async def _mqtt_handle_partition_control(self, prep: ParsedMessage):
252252 )
253253 )
254254
255- message = "Partition command: {}={} user: {}" . format ( element , command , user )
255+ message = f "Partition command: { element } ={ command } user: { user } "
256256 logger .info (message )
257257 self ._publish_command_status (message )
258258
@@ -280,7 +280,7 @@ async def _mqtt_handle_output_control(self, prep: ParsedMessage):
280280 else :
281281 command , user = _extract_command_user (command )
282282
283- message = "Output command: {}={} user: {}" . format ( element , command , user )
283+ message = f "Output command: { element } ={ command } user: { user } "
284284 logger .debug (message )
285285 self ._publish_command_status (message )
286286
@@ -310,15 +310,19 @@ async def _mqtt_handle_send_panic(self, prep: ParsedMessage):
310310 else :
311311 userid , user = _extract_command_user (userid )
312312
313- message = "Send panic command: partition: {}, userid: {}, user: {}, type: {}" .format (
314- partition , userid , user , panic_type
313+ message = (
314+ "Send panic command: partition: {}, userid: {}, user: {}, type: {}" .format (
315+ partition , userid , user , panic_type
316+ )
315317 )
316318 logger .debug (message )
317319 self ._publish_command_status (message )
318320
319321 if not await self .alarm .send_panic (partition , panic_type , userid ):
320- message = "Send panic command refused: {}, userid: {}, user: {}, type: {}" .format (
321- partition , userid , user , panic_type
322+ message = (
323+ "Send panic command refused: {}, userid: {}, user: {}, type: {}" .format (
324+ partition , userid , user , panic_type
325+ )
322326 )
323327
324328 logger .warning (message )
@@ -341,7 +345,7 @@ async def _mqtt_handle_door_control(self, prep: ParsedMessage):
341345 else :
342346 command , user = _extract_command_user (command )
343347
344- message = "Door command: {}={} user=" . format ( element , command , user )
348+ message = f "Door command: { element } ={ command } user="
345349
346350 logger .debug (message )
347351 self ._publish_command_status (message )
@@ -440,14 +444,14 @@ def _publish(
440444 try :
441445 publish_value = int (value )
442446 except (TypeError , ValueError ):
443- logger .debug (' Conversion int(%s) failed, use original value' , value )
447+ logger .debug (" Conversion int(%s) failed, use original value" , value )
444448 publish_value = value
445449 else :
446450 publish_value = value
447451
448452 self .publish (
449- "{ }/{}/{}/{}" . format ( base , element_topic , sanitize_key (label ), attribute ) ,
450- "{}" . format ( publish_value ) ,
453+ f" { base } /{ element_topic } /{ sanitize_key (label )} / { attribute } " ,
454+ f" { publish_value } " ,
451455 qos = cfg .MQTT_QOS ,
452456 retain = cfg .MQTT_RETAIN ,
453457 )
@@ -489,21 +493,19 @@ def _publish_dash(self, fname, partitions):
489493 return
490494
491495 if os .path .exists (fname ):
492- with open (fname , "r" ) as f :
496+ with open (fname ) as f :
493497 data = f .read ()
494498
495499 for k in partitions .keys ():
496500 data = data .replace (f"__PARTITION{ k } __" , partitions [k ]["label" ])
497501
498502 self .publish (cfg .MQTT_DASH_TOPIC , data , 2 , True )
499- logger .info (
500- "MQTT Dash panel published to {}" .format (cfg .MQTT_DASH_TOPIC )
501- )
503+ logger .info (f"MQTT Dash panel published to { cfg .MQTT_DASH_TOPIC } " )
502504 else :
503- logger .warning ("MQTT DASH Template not found: {}" . format ( fname ) )
505+ logger .warning (f "MQTT DASH Template not found: { fname } " )
504506
505507 def _refresh_challenge (self ):
506- self .challenge = binascii .hexlify (os .urandom (16 ))
508+ self .challenge = binascii .hexlify (os .urandom (16 )). decode ( "utf-8" )
507509 self .publish (
508510 f"{ cfg .MQTT_BASE_TOPIC } /{ cfg .MQTT_STATES_TOPIC } /{ cfg .MQTT_CHALLENGE_TOPIC } " ,
509511 self .challenge ,
@@ -544,7 +546,7 @@ def _validate_command_with_challenge(self, command):
544546
545547 h = hashlib .new ("SHA1" )
546548 i = cfg .MQTT_CHALLENGE_ROUNDS
547- text = f"{ challenge } { secret } " .encode ("utf-8" )
549+ text = f"{ challenge } { secret } " .encode ()
548550
549551 while i > 0 :
550552 h .update (text )
0 commit comments