Skip to content

Commit

Permalink
Use contextlib instead of try/except
Browse files Browse the repository at this point in the history
  • Loading branch information
pipiche38 committed Nov 2, 2024
1 parent 2b98d7b commit 1836e95
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@
"""



#import DomoticzEx as Domoticz
import Domoticz

import contextlib
try:
#from DomoticzEx import Devices, Images, Parameters, Settings
from Domoticz import Devices, Images, Parameters, Settings
Expand Down Expand Up @@ -188,6 +190,8 @@
STARTUP_TIMEOUT_DELAY_FOR_STOP = 120
ZNP_STARTUP_TIMEOUT_DELAY_FOR_WARNING = 110
ZNP_STARTUP_TIMEOUT_DELAY_FOR_STOP = 160
ZNP_STARTUP_TIMEOUT_DELAY_FOR_WARNING = 110
ZNP_STARTUP_TIMEOUT_DELAY_FOR_STOP = 160

class BasePlugin:
enabled = False
Expand Down Expand Up @@ -312,8 +316,6 @@ def __init__(self):

self.tracemalloc_snapshot1 = None
self.tracemalloc_snapshot2 = None


def onStart(self):
Domoticz.Status( "Welcome to Zigbee for Domoticz (Z4D) plugin.")

Expand All @@ -322,7 +324,7 @@ def onStart(self):

Domoticz.Status( "Z4D requires python3.9 or above and you are running %s.%s" %(
_current_python_version_major, _current_python_version_minor))

assert sys.version_info >= (3, 9) # nosec

if Parameters["Mode1"] == "V1" and Parameters["Mode2"] in ( "USB", "DIN", "PI", "Wifi", ):
Expand Down Expand Up @@ -391,7 +393,7 @@ def onStart(self):
self.homedirectory = Parameters["HomeFolder"]
self.HardwareID = Parameters["HardwareID"]
self.Key = Parameters["Key"]

if not get_domoticz_version( self, Parameters["DomoticzVersion"] ):
return

Expand All @@ -402,12 +404,10 @@ def onStart(self):
)

if self.pluginconf.pluginConf.get("tracememoryAllocation", False):
try:
with contextlib.suppress(RuntimeError, ValueError, MemoryError):
Domoticz.Log("!!! Z4D enabling Memory Allocation Tracing !!!")
tracemalloc.start()
self.tracemalloc_snapshot1 = tracemalloc.take_snapshot()
except Exception:
pass

if self.internet_available is None:
self.internet_available = is_internet_available()
Expand Down Expand Up @@ -447,10 +447,10 @@ def onStart(self):

# Debuging information
debuging_information(self, "Debug")

if not self.pluginconf.pluginConf[ "PluginAnalytics" ]:
self.pluginconf.pluginConf[ "PluginAnalytics" ] = -1

self.StartupFolder = Parameters["StartupFolder"]

self.domoticzdb_DeviceStatus = DomoticzDB_DeviceStatus(
Expand All @@ -474,7 +474,7 @@ def onStart(self):
self.DomoticzMajor,
self.DomoticzMinor,
)

if (
self.zigbee_communication
and self.zigbee_communication == "zigpy"
Expand All @@ -489,9 +489,9 @@ def onStart(self):
if log_level:
self.pluginParameters["LogLevel"] = log_level
self.log.logging("Plugin", "Debug", "LogLevel: %s" % self.pluginParameters["LogLevel"])

self.log.logging("Plugin", "Debug", " - Preferences table")

self.domoticzdb_Preferences = DomoticzDB_Preferences(
Parameters["Mode5"],
self.pluginconf,
Expand All @@ -507,26 +507,26 @@ def onStart(self):

self.DeviceListName = "DeviceList-" + str(Parameters["HardwareID"]) + ".txt"
self.log.logging("Plugin", "Log", "Z4D Database found: %s" % self.DeviceListName)

z4d_certified_devices_pathname = os.path.dirname( z4d_certified_devices.__file__ ) + "/"
self.log.logging("Plugin", "Log", "Z4D Certified devices database %s" %z4d_certified_devices_pathname)

# Import Zcl Cluster definitions
load_zcl_cluster(self)

# Import Certified Device Configuration
import_local_device_conf(self)
z4d_certified_devices.z4d_import_device_configuration(self, z4d_certified_devices_pathname )

# if type(self.DeviceConf) is not dict:
if not isinstance(self.DeviceConf, dict):
self.log.logging("Plugin", "Error", "DeviceConf initialisation failure!!! %s" % type(self.DeviceConf))
self.onStop()
return

# Initialize List of Domoticz Widgets
load_list_of_domoticz_widget(self, Devices)

# Import DeviceList.txt Filename is : DeviceListName
self.log.logging("Plugin", "Status", "Z4D loading database")
if LoadDeviceList(self) == "Failed":
Expand All @@ -539,7 +539,7 @@ def onStart(self):
)
self.onStop()
return

# Log ListOfDevices dictionary
self.log.logging("Plugin", "Debug", "ListOfDevices:")
for key, value in self.ListOfDevices.items():
Expand All @@ -553,7 +553,7 @@ def onStart(self):
# Check proper match against Domoticz Devices
checkListOfDevice2Devices(self, Devices)
checkDevices2LOD(self, Devices)

for x in self.ListOfDevices:
# Fixing Profalux Model is required
if "Model" in self.ListOfDevices[x] and self.ListOfDevices[x]["Model"] in ( "", {} ):
Expand Down Expand Up @@ -584,7 +584,6 @@ def onStart(self):
# Domoticz.Log("Init IAS_Zone_management ZigateComm: %s" %self.ControllerLink)
self.iaszonemgt = IAS_Zone_Management(self.pluginconf, self.ControllerLink, self.ListOfDevices, self.IEEE2NWK, self.DeviceConf, self.log, self.zigbee_communication, self.readZclClusters, self.FirmwareVersion)

# Starting WebServer
if self.webserver is None:
if Parameters["Mode4"].isdigit() or ':' in Parameters["Mode4"]:
start_web_server(self, Parameters["Mode4"], Parameters["HomeFolder"])
Expand All @@ -602,8 +601,6 @@ def onStart(self):
self.log.logging("Plugin", "Status", f"Z4D started with {framework_status}")

self.busy = False


def onStop(self):
"""
Performs cleanup actions when the plugin is stopped.
Expand Down

0 comments on commit 1836e95

Please sign in to comment.