Skip to content

Commit 1f282c6

Browse files
committed
Updated client to fix USB Connectivity
1 parent 315f9f9 commit 1f282c6

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

custom_components/visonic/client.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from jinja2 import Environment, FileSystemLoader
1111
from .pyconst import AlEnum, AlTransport, PanelConfig, AlConfiguration, AlPanelMode, AlPanelCommand, AlPanelStatus, AlTroubleType, AlAlarmType, AlSensorCondition, AlCommandStatus, AlX10Command, AlCondition, AlSensorDevice, AlLogPanelEvent, AlSensorType, AlSwitchDevice
1212
from .pyvisonic import VisonicProtocol
13+
from functools import partial
1314

1415
from enum import IntEnum
1516
from requests import ConnectTimeout, HTTPError
@@ -98,7 +99,7 @@ class PanelCondition(IntEnum):
9899
CHECK_EVENT_LOG_COMMAND = 13
99100
CHECK_X10_COMMAND = 14
100101

101-
CLIENT_VERSION = "0.9.1.0"
102+
CLIENT_VERSION = "0.9.1.1"
102103

103104
MAX_CLIENT_LOG_ENTRIES = 100
104105

@@ -124,10 +125,15 @@ def close(self):
124125
# transport needs to have 2 functions: write(bytearray) and close()
125126
class ClientVisonicProtocol(asyncio.Protocol, VisonicProtocol):
126127

127-
def __init__(self, *args, **kwargs):
128+
def __init__(self, client = None, *args, **kwargs):
128129
_LOGGER.debug("Initialising ClientVisonicProtocol A")
129130
super().__init__(*args, **kwargs)
130131
_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")
131137

132138
def data_received(self, data):
133139
super().vp_data_received(data)
@@ -851,26 +857,52 @@ async def async_create_tcp_visonic_connection(self, address, port, panelConfig :
851857
sock.close()
852858
return None, None
853859

860+
def tellemaboutme(self, thisisme):
861+
self.logstate_debug(f"Here This is me {thisisme}")
862+
self.vp = thisisme
854863

855864
# Create a connection using asyncio through a linux port (usb or rs232)
856865
async def async_create_usb_visonic_connection(self, path, baud="9600", panelConfig : PanelConfig = None, loop=None):
857866
"""Create Visonic manager class, returns rs232 transport coroutine."""
858867
from serial_asyncio import create_serial_connection
859868

869+
self.logstate_debug("Here AA")
860870
loop=loop if loop else asyncio.get_event_loop()
861871

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+
862884
# setup serial connection
863885
path = path
864886
baud = int(baud)
865887
try:
866-
vp = ClientVisonicProtocol(panelConfig=panelConfig, loop=loop)
888+
self.logstate_debug(f"Here CC {path} {baud}")
889+
self.vp = None
867890
# 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))
870893
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}")
874906
return None, None
875907

876908
async def connect_to_alarm(self) -> bool:

0 commit comments

Comments
 (0)