forked from zigbeefordomoticz/Domoticz-Zigbee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
1812 lines (1455 loc) · 74.8 KB
/
plugin.py
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Implementation of Zigbee for Domoticz plugin.
#
# This file is part of Zigbee for Domoticz plugin. https://github.com/zigbeefordomoticz/Domoticz-Zigbee
# (C) 2015-2024
#
# Initial authors: zaraki673 & pipiche38
#
# SPDX-License-Identifier: GPL-3.0 license
"""
<plugin key="Zigate" name="Zigbee for domoticz plugin (zigpy enabled)" author="pipiche38" version="7.1">
<description>
<h1> Plugin Zigbee for domoticz</h1><br/>
<br/><h2> Informations</h2><br/>
<ul style="list-style-type:square">
<li>&Documentations : &<a href="https://github.com/pipiche38/Domoticz-Zigate-Wiki/blob/master/en-eng/Home.md">English wiki</a>|<a href="https://github.com/pipiche38/Domoticz-Zigate-Wiki/blob/master/fr-fr/Home.md">Wiki Français</a></li>
<li>&Forums : &<a href="https://www.domoticz.com/forum/viewforum.php?f=68">English (www.domoticz.com)</a>|<a href="https://easydomoticz.com/forum/viewforum.php?f=28">Français (www.easydomoticz.com)</a></li>
<li>&List of supported devices : &<a href="https://zigbee.blakadder.com/z4d.html">www.zigbee.blakadder.com</a></li>
</ul>
<br/><h2>Parameters</h2>
</description>
<params>
<param field="Mode1" label="Coordinator Model" width="200px" required="true" default="None">
<description><br/><h3>Zigbee Coordinator definition</h3><br/>Select the Zigbee radio Coordinator version : ZiGate (V1), ZiGate+ (V2), Texas Instrument ZNP, Silicon Labs EZSP or ConBee/RasBee</description>
<options>
<option label="ZiGate" value="V1"/>
<option label="ZiGate+" value="V2"/>
<option label="Texas Instruments ZNP (via zigpy)" value="ZigpyZNP"/>
<option label="Silicon Labs EZSP (via zigpy)" value="ZigpyEZSP"/>
<option label="Conbee/Rasbee I, II, III (via zigpy)" value="ZigpydeCONZ"/>
</options>
</param>
<param field="Mode2" label="Coordinator Type" width="75px" required="true" default="None">
<description><br/>Select the Radio Coordinator connection type : USB, DIN, Pi, TCPIP (Wifi, Ethernet) or Socket. In case of Socket use the IP to put the remote ip</description>
<options>
<option label="USB" value="USB" />
<option label="DIN" value="DIN" />
<option label="PI" value="PI" />
<option label="TCPIP" value="Wifi"/>
<option label="Socket" value="Socket"/>
<option label="None" value="None"/>
</options>
</param>
<param field="SerialPort" label="Serial Port" width="200px" required="true" default="/dev/ttyUSB0" >
<description><br/>Set the serial port where the Radio Coordinator is connected (/dev/ttyUSB0 for example)</description>
</param>
<param field="Address" label="IP" width="150px" required="true" default="0.0.0.0">
<description><br/>Set the Radio Coordinator IP adresse (0.0.0.0 if not applicable)</description>
</param>
<param field="Port" label="Port" width="75px" required="true" default="9999">
<description><br/>Set the Radio Coordinator Port (9999 by default)</description>
</param>
<param field="Mode5" label="API base url <br/>(http://username:[email protected]:port)" width="250px" default="http://127.0.0.1:8080" required="true" >
<description>
<br/><h3>Domoticz Json/API base ( http://127.0.0.1:8080 should be the default)</h3>In case Domoticz listen to an other port change 8080 by what ever is the port,
and if you have setup an authentication please add the username:password</description>
</param>
<param field="Mode4" label="WebUI port" width="150px" required="true" default="9440" >
<description><br/><h3>Plugin definition</h3><br/>Set the plugin Dashboard port (9440 by default, None to disable)<br/>
In case you would like to restrict the interface and to secure the access to the dashboard , you can also add use the format IP:PORT ( 127.0.0.1:9440)<br/>
To access the plugin WebUI, replace your DomoticZ port (8080 by default) in your web adress by your WebUI port (9440 by default).</description>
</param>
<param field="Mode3" label="Initialize Coordinator" width="75px" required="true" default="False" >
<description><br/><h3>Coordinator initialization</h3>Required step only with a new Coordinator or if you want to create a new Zigbee network.<br/>
This can be usefull if you want to use a specific Extended PanId requires for Wiser (legacy) devices.
Be aware : this will reset the Coordinator and all paired devices will be lost. After that, you'll have to re-pair each devices.<br/>
This is not removing any data from DomoticZ nor the plugin database.</description>
<options>
<option label="True" value="True"/>
<option label="False" value="False" default="true" />
</options>
</param>
<param field="Mode6" label="Debugging" width="150px" default="None" required="true">
<description><br/><h3>Plugin debug</h3>This debugging option has been moved to the WebUI > Tools > Debug<br/></description>
<options>
<option label="None" value="0" default="true" />
<option label="Python Only" value="2"/>
<option label="Basic Debugging" value="62"/>
<option label="Basic+Messages" value="126"/>
<option label="Connections Only" value="16"/>
<option label="Connections+Queue" value="144"/>
<option label="All" value="-1"/>
</options>
</param>
</params>
</plugin>
"""
#import DomoticzEx as Domoticz
import Domoticz
try:
#from DomoticzEx import Devices, Images, Parameters, Settings
from Domoticz import Devices, Images, Parameters, Settings
except ImportError:
pass
import gc
import json
import os
import os.path
import pathlib
import sys
import threading
import time
import z4d_certified_devices
from Classes.AdminWidgets import AdminWidgets
from Classes.ConfigureReporting import ConfigureReporting
from Classes.DomoticzDB import (DomoticzDB_DeviceStatus, DomoticzDB_Hardware,
DomoticzDB_Preferences)
from Classes.GroupMgtv2.GroupManagement import GroupsManagement
from Classes.IAS import IAS_Zone_Management
from Classes.LoggingManagement import LoggingManagement
from Classes.NetworkEnergy import NetworkEnergy
from Classes.NetworkMap import NetworkMap
from Classes.OTA import OTAManagement
from Classes.PluginConf import PluginConf
from Classes.TransportStats import TransportStatistics
from Classes.WebServer.WebServer import WebServer
from Classes.ZigpyTopology import ZigpyTopology
from Modules.basicOutputs import (ZigatePermitToJoin, leaveRequest,
setExtendedPANID, setTimeServer,
start_Zigate, zigateBlueLed)
from Modules.casaia import restart_plugin_reset_ModuleIRCode
from Modules.checkingUpdate import (check_plugin_version_against_dns,
is_internet_available,
is_plugin_update_available,
is_zigate_firmware_available)
from Modules.command import domoticz_command
from Modules.database import (LoadDeviceList, WriteDeviceList,
checkDevices2LOD, checkListOfDevice2Devices,
import_local_device_conf)
from Modules.domoticzAbstractLayer import (domo_read_Name,
find_legacy_DeviceID_from_unit,
how_many_legacy_slot_available,
is_domoticz_extended,
load_list_of_domoticz_widget)
from Modules.heartbeat import processListOfDevices
from Modules.input import zigbee_receive_message
from Modules.matomo_request import (matomo_coordinator_initialisation,
matomo_plugin_analytics_infos,
matomo_plugin_restart,
matomo_plugin_shutdown,
matomo_plugin_started)
from Modules.paramDevice import initialize_device_settings
from Modules.piZigate import switchPiZigate_mode
from Modules.pluginHelpers import (check_firmware_level,
check_python_modules_version,
check_requirements, decodeConnection,
get_domoticz_version,
list_all_modules_loaded, networksize_update,
update_DB_device_status_to_reinit)
from Modules.profalux import profalux_fake_deviceModel
from Modules.readZclClusters import load_zcl_cluster
from Modules.restartPlugin import restartPluginViaDomoticzJsonApi
from Modules.schneider_wiser import wiser_thermostat_monitoring_heating_demand
from Modules.tools import (build_list_of_device_model,
chk_and_update_IEEE_NWKID, lookupForIEEE,
night_shift_jobs, removeDeviceInList)
from Modules.txPower import set_TxPower
from Modules.zigateCommands import (zigate_erase_eeprom,
zigate_get_firmware_version,
zigate_get_list_active_devices,
zigate_get_nwk_state, zigate_get_time,
zigate_remove_device,
zigate_set_certificate, zigate_set_mode)
from Modules.zigateConsts import CERTIFICATION, HEARTBEAT, MAX_FOR_ZIGATE_BUZY
from Modules.zigpyBackup import handle_zigpy_backup
from Zigbee.zdpCommands import zdp_get_permit_joint_status
VERSION_FILENAME = ".hidden/VERSION"
TEMPO_NETWORK = 2 # Start HB totrigget Network Status
TIMEDOUT_START = 10 # Timeoud for the all startup
TIMEDOUT_FIRMWARE = 5 # HB before request Firmware again
TEMPO_START_ZIGATE = 1 # Nb HB before requesting a Start_Zigate
STARTUP_TIMEOUT_DELAY_FOR_WARNING = 60
STARTUP_TIMEOUT_DELAY_FOR_STOP = 120
ZNP_STARTUP_TIMEOUT_DELAY_FOR_WARNING = 110
ZNP_STARTUP_TIMEOUT_DELAY_FOR_STOP = 160
class BasePlugin:
enabled = False
def __init__(self):
self.internet_available = None
self.ListOfDevices = (
{}
) # {DevicesAddresse : { status : status_de_detection, data : {ep list ou autres en fonctions du status}}, DevicesAddresse : ...}
self.DiscoveryDevices = {} # Used to collect pairing information
self.IEEE2NWK = {}
self.ControllerData = {}
self.DeviceConf = {} # Store DeviceConf.txt, all known devices configuration
self.ModelManufMapping = {}
self.readZclClusters = {}
self.ListOfDomoticzWidget = {}
# Objects from Classe
self.configureReporting = None
self.ControllerLink= None
self.groupmgt = None
self.networkmap = None
self.zigpy_topology = None
self.networkenergy = None
self.domoticzdb_DeviceStatus = None # Object allowing direct access to Domoticz DB DeviceSatus
self.domoticzdb_Hardware = None # Object allowing direct access to Domoticz DB Hardware
self.domoticzdb_Preferences = None # Object allowing direct access to Domoticz DB Preferences
self.adminWidgets = None # Manage AdminWidgets object
self.pluginconf = None # PlugConf object / all configuration parameters
self.OTA = None
self.statistics = None
self.iaszonemgt = None # Object to manage IAS Zone
self.webserver = None
self.transport = None # USB or Wifi
self.log = None
self.zigpy_backup = None
# self._ReqRcv = bytearray()
self.UnknownDevices = [] # List of unknown Device NwkId
self.permitTojoin = {"Duration": 0, "Starttime": 0}
self.CommiSSionning = (
False # This flag is raised when a Device Annocement is receive, in order to give priority to commissioning
)
self.busy = (
False # This flag is raised when a Device Annocement is receive, in order to give priority to commissioning
)
self.homedirectory = None
self.HardwareID = None
self.Key = None
self.DomoticzVersion = None
self.StartupFolder = None
self.DeviceListName = None
self.pluginParameters = None
self.PluginHealth = {}
self.Ping = {}
self.Ping["Nb Ticks"] = self.Ping["Status"] = self.Ping["TimeStamp"] = None
self.connectionState = None
self.HBcount = 0
self.HeartbeatCount = 0
self.internalHB = 0
self.currentChannel = None # Curent Channel. Set in Decode8009/Decode8024
self.ControllerIEEE = None # Zigate IEEE. Set in CDecode8009/Decode8024
self.ControllerNWKID = None # Zigate NWKID. Set in CDecode8009/Decode8024
self.ZiGateModel = None # V1 or V2
self.FirmwareVersion = None
self.FirmwareMajorVersion = None
self.FirmwareBranch = None
self.mainpowerSQN = None # Tracking main Powered SQN
# self.ForceCreationDevice = None #
self.VersionNewFashion = None
self.DomoticzBuild = None
self.DomoticzMajor = None
self.DomoticzMinor = None
self.WebUsername = None
self.WebPassword = None
self.PluzzyFirmware = False
self.pluginVersion = {}
self.z4d_certified_devices_version = None
self.loggingFileHandle = None
self.level = 0
self.PDM = None
self.PDMready = False
self.InitPhase3 = False
self.InitPhase2 = False
self.InitPhase1 = False
self.ErasePDMDone = False
self.ErasePDMinProgress = False
self.startZigateNeeded = False
self.SchneiderZone = None # Manage Zone for Wiser Thermostat and HACT
self.CasaiaPAC = None # To manage Casa IA PAC configuration
self.internalError = 0 # Use to count the number of repeat 0x8000 error
self.MajDomoDevice_timing_cnt = (
self.MajDomoDevice_timing_cumul
) = self.MajDomoDevice_timing_avrg = self.MajDomoDevice_timing_max = 0
self.ReadCluster_timing_cnt = (
self.ReadCluster_timing_cumul
) = self.ReadCluster_timing_avrg = self.ReadCluster_timing_max = 0
self.ZigateRead_timing_cnt = (
self.ZigateRead_timing_cumul
) = self.ZigateRead_timing_avrg = self.ZigateRead_timing_max = 0
# Zigpy
self.zigbee_communication = None # "zigpy" or "native"
#self.pythonModuleVersion = {}
self.device_settings = {}
initialize_device_settings(self)
def onStart(self):
Domoticz.Status( "Welcome to Zigbee for Domoticz (Z4D) plugin.")
_current_python_version_major = sys.version_info.major
_current_python_version_minor = sys.version_info.minor
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", ):
self.transport = Parameters["Mode2"]
self.zigbee_communication = "native"
elif Parameters["Mode1"] == "V2" and Parameters["Mode2"] in ( "USB", "DIN", "PI", "Wifi", ):
self.transport = "V2-" + Parameters["Mode2"]
self.zigbee_communication = "native"
elif Parameters["Mode2"] == "None":
self.zigbee_communication = "native"
self.transport = "None"
elif Parameters["Mode1"] in ( "ZigpyZiGate", "ZigpyZNP", "ZigpydeCONZ", "ZigpyEZSP"):
self.transport = Parameters["Mode1"]
self.zigbee_communication = "zigpy"
else:
Domoticz.Error(
"Please cross-check the plugin starting parameters Mode1: %s Mode2: %s and make sure you have restarted Domoticz after updating the plugin"
% (Parameters["Mode1"] == "V1", Parameters["Mode2"])
)
self.onStop()
return
if (
Parameters["Mode5"] == ""
or ( "http://" not in Parameters["Mode5"].lower() and "https://" not in Parameters["Mode5"].lower() )
):
Domoticz.Error("Please cross-check the Domoticz Hardware setting for the plugin instance. >%s< You must set the API base URL" %Parameters["Mode5"])
self.onStop()
# Set plugin heartbeat to 1s
Domoticz.Heartbeat(1)
# Copy the Domoticz.Parameters to a variable accessible in the all objetc
self.pluginParameters = dict(Parameters)
# Open VERSION file in .hidden
version_filename = pathlib.Path(Parameters["HomeFolder"]) / VERSION_FILENAME
with open( version_filename, "rt") as versionfile:
try:
_pluginversion = json.load(versionfile)
except Exception as e:
Domoticz.Error("Error when opening: %s -- %s" % (Parameters["HomeFolder"] + VERSION_FILENAME, e))
self.onStop()
return
self.z4d_certified_devices_version = z4d_certified_devices.__version__
self.pluginParameters["PluginBranch"] = _pluginversion["branch"]
self.pluginParameters["PluginVersion"] = _pluginversion["version"]
self.pluginParameters["CertifiedDbVersion"] = z4d_certified_devices.__version__
self.pluginParameters["TimeStamp"] = 0
self.pluginParameters["available"] = None
self.pluginParameters["available-firmMajor"] = None
self.pluginParameters["available-firmMinor"] = None
self.pluginParameters["FirmwareVersion"] = None
self.pluginParameters["FirmwareUpdate"] = None
self.pluginParameters["PluginUpdate"] = None
self.busy = True
self.DomoticzVersion = Parameters["DomoticzVersion"]
self.homedirectory = Parameters["HomeFolder"]
self.HardwareID = Parameters["HardwareID"]
self.Key = Parameters["Key"]
if not get_domoticz_version( self, Parameters["DomoticzVersion"] ):
return
# Import PluginConf.txt
Domoticz.Log("Z4D loading PluginConf")
self.pluginconf = PluginConf(
self.zigbee_communication, self.VersionNewFashion, self.DomoticzMajor, self.DomoticzMinor, Parameters["HomeFolder"], self.HardwareID
)
if self.internet_available is None:
self.internet_available = is_internet_available()
if self.internet_available:
if check_requirements( Parameters[ "HomeFolder"] ):
# Check_requirements() return True if requirements not meet.
self.onStop()
return
# Create Domoticz Sub menu
if "DomoticzCustomMenu" in self.pluginconf.pluginConf and self.pluginconf.pluginConf["DomoticzCustomMenu"] :
install_Z4D_to_domoticz_custom_ui( )
# Create the adminStatusWidget if needed
self.PluginHealth["Flag"] = 4
self.PluginHealth["Txt"] = "Startup"
if self.log is None:
self.log = LoggingManagement(
self.pluginconf,
self.PluginHealth,
self.HardwareID,
self.ListOfDevices,
self.permitTojoin,
)
self.log.loggingUpdatePluginVersion(
str(self.pluginParameters["PluginBranch"] + "-" + self.pluginParameters["PluginVersion"])
)
self.log.openLogFile()
# We can use from now the self.log.logging()
self.log.logging( "Plugin", "Status", "Z4D starting with %s-%s" % (
self.pluginParameters["PluginBranch"], self.pluginParameters["PluginVersion"]), )
if ( _current_python_version_major , _current_python_version_minor) <= ( 3, 7):
self.log.logging( "Plugin", "Error", "** Please do consider upgrading to a more recent python3 version %s.%s is not supported anymore **" %(
_current_python_version_major , _current_python_version_minor))
# 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(
Parameters["Mode5"],
self.pluginconf,
self.HardwareID,
self.log,
self.DomoticzBuild,
self.DomoticzMajor,
self.DomoticzMinor,
)
self.log.logging("Plugin", "Debug", " - Hardware table")
self.domoticzdb_Hardware = DomoticzDB_Hardware(
Parameters["Mode5"],
self.pluginconf,
self.HardwareID,
self.log,
self.pluginParameters ,
self.DomoticzBuild,
self.DomoticzMajor,
self.DomoticzMinor,
)
if (
self.zigbee_communication
and self.zigbee_communication == "zigpy"
and ( self.pluginconf.pluginConf["forceZigpy_noasyncio"] or self.domoticzdb_Hardware.multiinstances_z4d_plugin_instance())
):
# https://github.com/python/cpython/issues/91375
self.log.logging("Plugin", "Status", "Z4D Multi-instances detected. Enable workaround")
sys.modules["_asyncio"] = None
if "LogLevel" not in self.pluginParameters:
log_level = self.domoticzdb_Hardware.get_loglevel_value()
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,
self.log,
self.DomoticzBuild,
self.DomoticzMajor,
self.DomoticzMinor,
)
self.WebUsername, self.WebPassword = self.domoticzdb_Preferences.retreiveWebUserNamePassword()
self.adminWidgets = AdminWidgets( self.log , self.pluginconf, self.pluginParameters, self.ListOfDomoticzWidget, Devices, self.ListOfDevices, self.HardwareID, self.IEEE2NWK)
self.adminWidgets.updateStatusWidget(Devices, "Starting up")
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":
self.log.logging("Plugin", "Error", "Something wennt wrong during the import of Load of Devices ...")
self.log.logging(
"Plugin",
"Error",
"Please cross-check your log ... You must be on V3 of the DeviceList and all DeviceID in Domoticz converted to IEEE",
)
self.onStop()
return
# Log ListOfDevices dictionary
self.log.logging("Plugin", "Debug", "ListOfDevices:")
for key, value in self.ListOfDevices.items():
self.log.logging("Plugin", "Debug", f" {key}: {value}")
# Log IEEE2NWK dictionary
self.log.logging("Plugin", "Debug", "IEEE2NWK:")
for key, value in self.IEEE2NWK.items():
self.log.logging("Plugin", "Debug", f" {key}: {value}")
# 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 ( "", {} ):
profalux_fake_deviceModel(self, x)
self.log.logging("Plugin", "Debug", "ListOfDevices after checkListOfDevice2Devices: " + str(self.ListOfDevices))
self.log.logging("Plugin", "Debug", "IEEE2NWK after checkListOfDevice2Devices : " + str(self.IEEE2NWK))
# Create Statistics object
self.statistics = TransportStatistics(self.pluginconf, self.log, self.zigbee_communication)
# Connect to Coordinator only when all initialisation are properly done.
self.log.logging("Plugin", "Status", "Z4D configured to use transport mode: %s" % self.transport)
if len(self.ListOfDevices) > 10:
# Don't do Energy Scan if too many objects, as Energy scan don't make the difference between real traffic and noise
self.pluginconf.pluginConf["EnergyScanAtStatup"] = 0
start_zigbee_transport(self )
if self.transport not in ("ZigpyZNP", "ZigpydeCONZ", "ZigpyEZSP", "ZigpyZiGate", "None" ):
self.log.logging("Plugin", "Debug", "Establish Zigate connection")
self.ControllerLink.open_cie_connection()
# IAS Zone Management
if self.iaszonemgt is None:
# Create IAS Zone object
# 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"])
else:
self.log.logging( "Plugin", "Error", "WebServer disabled du to Parameter Mode4 set to %s" % Parameters["Mode4"] )
if is_domoticz_extended():
framework_status = "Extended Framework"
else:
framework_status = "legacy Framework"
free_slots = how_many_legacy_slot_available(Devices)
usage_percentage = round(((255 - free_slots) / 255) * 100, 1)
self.log.logging("Plugin", "Status", f"Z4D Widgets usage is at {usage_percentage}% ({free_slots} units free)")
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.
Stops various plugin functionalities and saves plugin database.
Returns:
None
"""
Domoticz.Log("onStop()")
if self.internet_available and self.pluginconf.pluginConf["MatomoOptIn"]:
matomo_plugin_shutdown(self)
matomo_plugin_analytics_infos(self)
# Flush ListOfDevices
if self.log:
self.log.logging("Plugin", "Log", "Flushing plugin database onto disk")
WriteDeviceList(self, 0) # write immediatly
# Uninstall Z4D custom UI from Domoticz
uninstall_Z4D_to_domoticz_custom_ui()
# Log imported modules if configured
if self.pluginconf and self.pluginconf.pluginConf["ListImportedModules"]:
list_all_modules_loaded(self)
# Log onStop event
if self.pluginconf and self.log:
self.log.logging("Plugin", "Log", "onStop called")
# Close CIE connection and shutdown transport thread
if self.pluginconf and self.ControllerLink:
self.ControllerLink.thread_transport_shutdown()
self.ControllerLink.close_cie_connection()
# Stop WebServer
if self.pluginconf and self.webserver:
self.webserver.onStop()
# Save plugin database
if self.PDMready and self.pluginconf:
WriteDeviceList(self, 0)
# Print and save statistics if configured
if self.PDMready and self.pluginconf and self.statistics:
self.statistics.printSummary()
self.statistics.writeReport()
# Close logging management
if self.pluginconf and self.log:
self.log.logging("Plugin", "Log", "Closing Logging Management")
self.log.closeLogFile()
# Log running threads that need to be shutdown
for thread in threading.enumerate():
if thread.name != threading.current_thread().name:
Domoticz.Log("'" + thread.name + "' is running, it must be shutdown otherwise Domoticz will abort on plugin exit.")
# Update plugin health status
self.PluginHealth["Flag"] = 3
self.PluginHealth["Txt"] = "No Communication"
if self.adminWidgets:
self.adminWidgets.updateStatusWidget(Devices, "No Communication")
def onDeviceRemoved(self, Unit):
# def onDeviceRemoved(self, DeviceID, Unit):
if not self.ControllerIEEE:
self.log.logging( "Plugin", "Error", "onDeviceRemoved - too early, coordinator and plugin initialisation not completed", )
if self.log:
self.log.logging("Plugin", "Debug", "onDeviceRemoved called")
if not is_domoticz_extended():
DeviceID = find_legacy_DeviceID_from_unit(self, Devices, Unit)
device_name = domo_read_Name( self, Devices, DeviceID, Unit, )
# Let's check if this is End Node, or Group related.
if DeviceID in self.IEEE2NWK:
NwkId = self.IEEE2NWK[DeviceID]
self.log.logging("Plugin", "Status", f"Removing Device {DeviceID} {device_name} in progress")
fullyremoved = removeDeviceInList(self, Devices, DeviceID, Unit)
# We might have to remove also the Device from Groups
if fullyremoved:
if self.groupmgt:
self.groupmgt.RemoveNwkIdFromAllGroups(NwkId)
# sending a Leave Request to device, so the device will send a leave
leaveRequest(self, ShortAddr=NwkId, IEEE=DeviceID)
# for a remove in case device didn't send the leave
zigate_remove_device(self, str(self.ControllerIEEE), str(DeviceID) )
self.log.logging( "Plugin", "Status", f"Request device {device_name} -> {DeviceID} to be removed from coordinator" )
self.log.logging("Plugin", "Debug", f"ListOfDevices :After REMOVE {self.ListOfDevices}")
load_list_of_domoticz_widget(self, Devices)
return
if self.groupmgt and DeviceID in self.groupmgt.ListOfGroups:
self.log.logging("Plugin", "Status", f"Request device {DeviceID} to be remove from Group(s)")
self.groupmgt.FullRemoveOfGroup(Unit, DeviceID)
def onConnect(self, Connection, Status, Description):
self.log.logging( "Plugin", "Debug", "onConnect %s called with status: %s and Desc: %s" % (Connection, Status, Description) )
decodedConnection = decodeConnection(str(Connection))
if "Protocol" in decodedConnection and decodedConnection["Protocol"] in ( "HTTP", "HTTPS", ): # We assumed that is the Web Server
if self.webserver:
self.webserver.onConnect(Connection, Status, Description)
return
self.busy = True
if Status != 0:
_onConnect_status_error(self, Status, Description)
return
self.log.logging("Plugin", "Debug", "Connected successfully")
if self.connectionState is None:
self.PluginHealth["Flag"] = 2
self.PluginHealth["Txt"] = "Starting Up"
self.adminWidgets.updateStatusWidget(Devices, "Starting the plugin up")
elif self.connectionState == 0:
self.log.logging("Plugin", "Status", "Z4D reconnected after failure")
self.PluginHealth["Flag"] = 2
self.PluginHealth["Txt"] = "Reconnecting after failure"
self.connectionState = 1
self.Ping["Status"] = None
self.Ping["TimeStamp"] = None
self.Ping["Permit"] = None
self.Ping["Nb Ticks"] = 1
return True
def onMessage(self, Connection, Data):
# self.log.logging( 'Plugin', 'Debug', "onMessage called on Connection " + " Data = '" +str(Data) + "'")
if self.webserver and isinstance(Data, dict):
self.webserver.onMessage(Connection, Data)
return
if len(Data) == 0:
self.log.logging("Plugin", "Error", "onMessage - empty message received on %s" % Connection)
self.Ping["Nb Ticks"] = 0
self.connectionState = 1
self.ControllerLink.on_message(Data)
def processFrame(self, Data):
if not self.VersionNewFashion:
return
self.connectionState = 1
# start_time = int(time.time() *1000)
zigbee_receive_message(self, Devices, Data)
# stop_time = int(time.time() *1000)
# Domoticz.Log("### Completion: %s is %s ms" %(Data, ( stop_time - start_time)))
def zigpy_chk_upd_device(self, ieee, nwkid ):
chk_and_update_IEEE_NWKID(self, nwkid, ieee)
def zigpy_get_device(self, ieee=None, nwkid=None):
# allow to inter-connect zigpy world and plugin
self.log.logging("TransportZigpy", "Debug", "zigpy_get_device( %s, %s)" %( ieee, nwkid))
sieee = ieee
snwkid = nwkid
if nwkid and nwkid not in self.ListOfDevices and ieee and ieee in self.IEEE2NWK:
# Most likely we have a new Nwkid, let see if we can reconnect
lookupForIEEE(self, nwkid, reconnect=True)
if nwkid and nwkid in self.ListOfDevices and 'IEEE' in self.ListOfDevices[ nwkid ]:
ieee = self.ListOfDevices[ nwkid ]['IEEE']
elif ieee and ieee in self.IEEE2NWK:
nwkid = self.IEEE2NWK[ ieee ]
else:
self.log.logging("TransportZigpy", "Debug", "zigpy_get_device( %s(%s), %s(%s)) NOT FOUND" %( sieee, type(sieee), snwkid, type(snwkid) ))
return None
self.log.logging("TransportZigpy", "Debug", "zigpy_get_device( %s, %s returns %04x %016x" %( sieee, snwkid, int(nwkid,16), int(ieee,16) ))
return int(nwkid,16) ,int(ieee,16)
def zigpy_backup_available(self, backups):
handle_zigpy_backup(self, backups)
def restart_plugin(self):
""" This is used as a call back function for zigpy connection_lost handling"""
error_message = "Connection lost with coordinator, restarting plugin"
self.log.logging("Plugin", "Error", error_message)
self.adminWidgets.updateNotificationWidget(Devices, error_message)
if self.internet_available and self.pluginconf.pluginConf["MatomoOptIn"]:
matomo_plugin_restart(self)
restartPluginViaDomoticzJsonApi(self, stop=False, url_base_api=Parameters["Mode5"])
#def onCommand(self, DeviceID, Unit, Command, Level, Color):
def onCommand(self, Unit, Command, Level, Color):
if ( self.ControllerLink is None or not self.VersionNewFashion or self.pluginconf is None or not self.log ):
self.log.logging( "Command", "Log", "onCommand - Not yet ready, plugin not fully started, we drop the command")
return
self.log.logging( "Command", "Debug", "onCommand - unit: %s, command: %s, level: %s, color: %s" % (Unit, Command, Level, Color) )
if not is_domoticz_extended():
DeviceID = find_legacy_DeviceID_from_unit(self, Devices, Unit)
# Let's check if this is End Node, or Group related.
if DeviceID in self.IEEE2NWK:
# Command belongs to a end node
domoticz_command(self, Devices, DeviceID, Unit, self.IEEE2NWK[ DeviceID], Command, Level, Color)
elif self.groupmgt and DeviceID in self.groupmgt.ListOfGroups:
# Command belongs to a Zigate group
self.log.logging( "Command", "Log", "Command: %s/%s/%s to Group: %s" % (Command, Level, Color, DeviceID), )
self.groupmgt.processCommand(Unit, DeviceID, Command, Level, Color)
elif DeviceID.find("Zigate-01-") != -1:
self.log.logging("Command", "Debug", "onCommand - Command adminWidget: %s " % Command)
self.adminWidgets.handleCommand(self, Command)
else:
self.log.logging( "Command", "Error", "onCommand - Unknown device or GrpMgr not enabled %s, unit %s , id %s" % (domo_read_Name( self, Devices, DeviceID, Unit, ), Unit, DeviceID), )
def onDisconnect(self, Connection):
self.log.logging("Plugin", "Debug", "onDisconnect: %s" % Connection)
decodedConnection = decodeConnection(str(Connection))
if "Protocol" in decodedConnection and decodedConnection["Protocol"] in (
"HTTP",
"HTTPS",
): # We assumed that is the Web Server
if self.webserver:
self.webserver.onDisconnect(Connection)
return
self.connectionState = 0
self.PluginHealth["Flag"] = 0
self.PluginHealth["Txt"] = "Shutdown"
self.adminWidgets.updateStatusWidget(Devices, "Plugin stop")
self.log.logging("Plugin", "Status", "onDisconnect called")
def onHeartbeat(self):
if not self.VersionNewFashion or self.pluginconf is None or self.log is None:
# Not yet ready
return
self.internalHB += 1
if self.PDMready:
if (self.internalHB % HEARTBEAT) != 0:
return
self.HeartbeatCount += 1
# Quiet a bad hack. In order to get the needs for ZigateRestart
# from WebServer
if "startZigateNeeded" in self.ControllerData and self.ControllerData["startZigateNeeded"]:
self.startZigateNeeded = self.HeartbeatCount
del self.ControllerData["startZigateNeeded"]
# Starting PDM on Host firmware version, we have to wait that Zigate is fully initialized ( PDM loaded into memory from Host).
# We wait for self.zigateReady which is set to True in th pdmZigate module
if not _coordinator_ready(self):
return
if self.transport != "None":
self.log.logging(
"Plugin",
"Debug",
"onHeartbeat - busy = %s, Health: %s, startZigateNeeded: %s/%s, InitPhase1: %s InitPhase2: %s, InitPhase3: %s PDM_LOCK: %s ErasePDMinProgress: %s ErasePDMDone: %s"
% ( self.busy, self.PluginHealth, self.startZigateNeeded, self.HeartbeatCount, self.InitPhase1, self.InitPhase2, self.InitPhase3, self.ControllerLink.pdm_lock_status(), self.ErasePDMinProgress, self.ErasePDMDone, ),
)
if not _post_readiness_startup_completed( self ):
return
if self.transport != "None" and not self.InitPhase3:
zigateInit_Phase3(self)
return
# Checking Version
if self.internet_available:
_check_plugin_version( self )
if self.pluginconf.pluginConf["MatomoOptIn"] and self.HeartbeatCount % ( (9 * 3600) // HEARTBEAT) == 0:
matomo_plugin_analytics_infos(self)
if self.transport == "None":
return
# Memorize the size of Devices. This is will allow to trigger a backup of live data to file, if the size change.
prevLenDevices = len(Devices)
# Manage all entries in ListOfDevices (existing and up-coming devices)
processListOfDevices(self, Devices)
# Check and Update Heating demand for Wiser if applicable (this will be check in the call)
wiser_thermostat_monitoring_heating_demand(self, Devices)
# Group Management
if self.groupmgt:
self.groupmgt.hearbeat_group_mgt()
# Write the ListOfDevice every 15 minutes or immediatly if we have remove or added a Device
if len(Devices) == prevLenDevices:
WriteDeviceList(self, ( (15 * 60) // HEARTBEAT) )
else:
self.log.logging("Plugin", "Debug", "Devices size has changed , let's write ListOfDevices on disk")
WriteDeviceList(self, 0) # write immediatly
networksize_update(self)
_trigger_coordinator_backup( self )
if self.CommiSSionning:
self.PluginHealth["Flag"] = 2
self.PluginHealth["Txt"] = "Enrollment in Progress"
self.adminWidgets.updateStatusWidget(Devices, "Enrollment")
# Maintain trend statistics
self.statistics._Load = self.ControllerLink.loadTransmit()
self.statistics.addPointforTrendStats(self.HeartbeatCount)
return
# OTA upgrade
if self.OTA:
self.OTA.heartbeat()
# Check PermitToJoin
_check_permit_to_joint_status(self)
if self.HeartbeatCount % (3600 // HEARTBEAT) == 0:
self.log.loggingCleaningErrorHistory()
zigate_get_time(self)
#sendZigateCmd(self, "0017", "")
if self.zigbee_communication == "zigpy" and self.pluginconf.pluginConf["ZigpyTopologyReport"] and self.zigpy_topology and self.HeartbeatCount % (60 // HEARTBEAT) == 0:
retreive_zigpy_topology_data(self)
self.busy = _check_if_busy(self)
return True
def _onConnect_status_error(self, Status, Description):
self.log.logging("Plugin", "Error", "Failed to connect (" + str(Status) + ")")
self.log.logging("Plugin", "Debug", "Failed to connect (" + str(Status) + ") with error: " + Description)
self.connectionState = 0
self.ControllerLink.re_conn()
self.PluginHealth["Flag"] = 3
self.PluginHealth["Txt"] = "No Communication"
self.adminWidgets.updateStatusWidget(Devices, "No Communication")
def retreive_zigpy_topology_data(self):
# Determine sync period based on existing data
if self.zigpy_topology is None:
return
if self.zigpy_topology.is_zigpy_topology_in_progress():
# Scan in progress
self.zigpy_topology.new_scan_detected = True
return
is_manual_scan = self.ListOfDevices["0000"].get("ZigpyTopologyRequested", False)
if self.zigpy_topology.new_scan_detected and ( self.pluginconf.pluginConf["ZigpyTopologyReportAutoBackup"] or is_manual_scan):
# Scan is completed. Time for a backup
self.zigpy_topology.save_topology_report()
self.zigpy_topology.new_scan_detected = False
self.ListOfDevices["0000"]["ZigpyTopologyRequested"] = False
return
# coordinator_data = self.ListOfDevices.get("0000", {})
# if "ZigpyNeighbors" in coordinator_data and "ZigpyRoutes" in coordinator_data:
# return
# self.zigpy_topology.copy_zigpy_infos_to_plugin()
# coordinator_data = self.ListOfDevices.get("0000", {})
# if "ZigpyNeighbors" not in coordinator_data and "ZigpyRoutes" not in coordinator_data:
# self.log.logging("Plugin", "Log", "onHeartbeat request zigpy topology scan as not data available")
# self.ControllerLink.sendData("ZIGPY-TOPOLOGY-SCAN", {})
def start_zigbee_transport(self ):
if self.transport in ("USB", "DIN", "V2-DIN", "V2-USB"):
check_python_modules_version( self )
_start_native_usb_zigate(self)
elif self.transport in ("PI", "V2-PI"):
_start_native_pi_zigate(self)