10
10
from jinja2 import Environment , FileSystemLoader
11
11
from .pyconst import AlEnum , AlTransport , PanelConfig , AlConfiguration , AlPanelMode , AlPanelCommand , AlPanelStatus , AlTroubleType , AlAlarmType , AlSensorCondition , AlCommandStatus , AlX10Command , AlCondition , AlSensorDevice , AlLogPanelEvent , AlSensorType , AlSwitchDevice
12
12
from .pyvisonic import VisonicProtocol
13
+ from functools import partial
13
14
14
15
from enum import IntEnum
15
16
from requests import ConnectTimeout , HTTPError
@@ -98,7 +99,7 @@ class PanelCondition(IntEnum):
98
99
CHECK_EVENT_LOG_COMMAND = 13
99
100
CHECK_X10_COMMAND = 14
100
101
101
- CLIENT_VERSION = "0.9.1.0 "
102
+ CLIENT_VERSION = "0.9.1.1 "
102
103
103
104
MAX_CLIENT_LOG_ENTRIES = 100
104
105
@@ -124,10 +125,15 @@ def close(self):
124
125
# transport needs to have 2 functions: write(bytearray) and close()
125
126
class ClientVisonicProtocol (asyncio .Protocol , VisonicProtocol ):
126
127
127
- def __init__ (self , * args , ** kwargs ):
128
+ def __init__ (self , client = None , * args , ** kwargs ):
128
129
_LOGGER .debug ("Initialising ClientVisonicProtocol A" )
129
130
super ().__init__ (* args , ** kwargs )
130
131
_LOGGER .debug ("Initialising ClientVisonicProtocol B" )
132
+ if client is not None :
133
+ _LOGGER .debug ("Initialising ClientVisonicProtocol C" )
134
+ client .tellemaboutme (self )
135
+ _LOGGER .debug ("Initialising ClientVisonicProtocol D" )
136
+ _LOGGER .debug ("Initialising ClientVisonicProtocol E" )
131
137
132
138
def data_received (self , data ):
133
139
super ().vp_data_received (data )
@@ -851,26 +857,52 @@ async def async_create_tcp_visonic_connection(self, address, port, panelConfig :
851
857
sock .close ()
852
858
return None , None
853
859
860
+ def tellemaboutme (self , thisisme ):
861
+ self .logstate_debug (f"Here This is me { thisisme } " )
862
+ self .vp = thisisme
854
863
855
864
# Create a connection using asyncio through a linux port (usb or rs232)
856
865
async def async_create_usb_visonic_connection (self , path , baud = "9600" , panelConfig : PanelConfig = None , loop = None ):
857
866
"""Create Visonic manager class, returns rs232 transport coroutine."""
858
867
from serial_asyncio import create_serial_connection
859
868
869
+ self .logstate_debug ("Here AA" )
860
870
loop = loop if loop else asyncio .get_event_loop ()
861
871
872
+ self .logstate_debug ("Setting USB Options" )
873
+
874
+ # use default protocol if not specified
875
+ protocol = partial (
876
+ ClientVisonicProtocol ,
877
+ client = self ,
878
+ panelConfig = panelConfig ,
879
+ loop = loop ,
880
+ )
881
+
882
+ self .logstate_debug ("Here BB" )
883
+
862
884
# setup serial connection
863
885
path = path
864
886
baud = int (baud )
865
887
try :
866
- vp = ClientVisonicProtocol (panelConfig = panelConfig , loop = loop )
888
+ self .logstate_debug (f"Here CC { path } { baud } " )
889
+ self .vp = None
867
890
# create the connection to the panel as an asyncio protocol handler and then set it up in a task
868
- conn = create_serial_connection (loop , vp , path , baud )
869
- self .logstate_debug ("The coro type is " + str (type (coro )) + " with value " + str (coro ))
891
+ conn = create_serial_connection (loop , protocol , path , baud )
892
+ self .logstate_debug ("The coro type is " + str (type (conn )) + " with value " + str (conn ))
870
893
visonicTask = loop .create_task (conn )
871
- return visonicTask , vp
872
- except :
873
- self .logstate_debug ("Setting USB Options Exception" )
894
+ self .logstate_debug ("Here DD" )
895
+ ctr = 0
896
+ while self .vp is None and ctr < 20 : # 20 with a sleep of 0.1 is approx 2 seconds. Wait up to 2 seconds for this to start.
897
+ self .logstate_debug ("Here EE" )
898
+ await asyncio .sleep (0.1 ) # This should only happen once while the Protocol Handler starts up and calls tellemaboutme to set self.vp
899
+ ctr = ctr + 1
900
+ if self .vp is not None :
901
+ self .logstate_debug ("Here FF" )
902
+ return visonicTask , self .vp
903
+ self .logstate_debug ("Here GG" )
904
+ except Exception as ex :
905
+ self .logstate_debug (f"Setting USB Options Exception { ex } " )
874
906
return None , None
875
907
876
908
async def connect_to_alarm (self ) -> bool :
0 commit comments