1- """
2- Use of this source code is governed by the MIT license found in the LICENSE file.
3-
4- Base for serial or socket connections
5- """
1+ """Base for serial or socket connections."""
62import logging
73import queue
84import threading
1511
1612
1713class StickConnection :
18- """ Generic Plugwise stick connection"""
14+ """Generic Plugwise stick connection. """
1915
2016 def __init__ (self , port , stick = None ):
17+ """Initialize StickConnection."""
2118 self .port = port
2219 self .stick = stick
2320 self .run_reader_thread = False
2421 self .run_writer_thread = False
2522 self ._is_connected = False
2623 self ._writer = None
2724
25+ self ._reader_thread = None
26+ self ._write_queue = None
27+ self ._writer_thread = None
28+
2829 ################################################
2930 ### Open connection ###
3031 ################################################
3132
3233 def connect (self ):
33- """Open the connection"""
34+ """Open the connection. """
3435 if not self ._is_connected :
3536 self ._open_connection ()
3637
3738 def _open_connection (self ):
38- """Placeholder to initialize the connection"""
39+ """Placeholder to initialize the connection. """
3940 raise NotImplementedError
4041
4142 ################################################
4243 ### Reader ###
4344 ################################################
4445
4546 def _reader_start (self , name ):
46- """Start the reader thread to receive data"""
47- self ._reader_thread = threading .Thread (None , self ._reader_daemon , name , (), {})
47+ """Start the reader thread to receive data. """
48+ self ._reader_thread = threading .Thread (None , self ._reader_deamon , name , (), {})
4849 self .run_reader_thread = True
4950 self ._reader_thread .start ()
5051
51- def _reader_daemon (self ):
52- """Thread to collect available data from connection"""
52+ def _reader_deamon (self ):
53+ """Thread to collect available data from connection. """
5354 while self .run_reader_thread :
5455 data = self ._read_data ()
5556 if data :
@@ -58,15 +59,15 @@ def _reader_daemon(self):
5859 _LOGGER .debug ("Reader daemon stopped" )
5960
6061 def _read_data (self ):
61- """Placeholder to receive message from the connection"""
62+ """Placeholder to receive message from the connection. """
6263 raise NotImplementedError
6364
6465 ################################################
6566 ### Writer ###
6667 ################################################
6768
6869 def _writer_start (self , name : str ):
69- """Start the writer thread to send data"""
70+ """Start the writer thread to send data. """
7071 self ._write_queue = queue .Queue ()
7172 self ._writer_thread = threading .Thread (None , self ._writer_daemon , name , (), {})
7273 self ._writer_thread .daemon = True
@@ -105,23 +106,23 @@ def send(self, message: PlugwiseMessage, callback=None):
105106 ################################################
106107
107108 def is_connected (self ):
108- """Return connection state"""
109+ """Return connection state. """
109110 return self ._is_connected
110111
111112 def read_thread_alive (self ):
112- """Return state of write thread"""
113+ """Return state of write thread. """
113114 return self ._reader_thread .isAlive () if self .run_reader_thread else False
114115
115116 def write_thread_alive (self ):
116- """Return state of write thread"""
117+ """Return state of write thread. """
117118 return self ._writer_thread .isAlive () if self .run_writer_thread else False
118119
119120 ################################################
120121 ### Close connection ###
121122 ################################################
122123
123124 def disconnect (self ):
124- """Close the connection"""
125+ """Close the connection. """
125126 if self ._is_connected :
126127 self ._is_connected = False
127128 self .run_writer_thread = False
@@ -133,5 +134,5 @@ def disconnect(self):
133134 self ._close_connection ()
134135
135136 def _close_connection (self ):
136- """Placeholder to close the port"""
137+ """Placeholder to close the port. """
137138 raise NotImplementedError
0 commit comments