Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .bandit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bandit]
skips: B106
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source = plugwise
omit=
*/venv/*
setup.py
plugwise/connections/*
plugwise/message.py
plugwise/messages/*
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ env.DEFAULT_PYTHON }}
- name: Pre-black
uses: psf/black@stable
- name: Restore base Python ${{ env.DEFAULT_PYTHON }} virtual environment
id: cache-venv
uses: actions/cache@v2
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.8.2 - Code quality improvements
- Switch Smile to defusedxml from lxml (improving security)
- Lint and flake recommendations fixed
- Project CI changes

## 0.8.1 - Standardize logging for stick

## 0.8.0 - Merged Smile/USB module
Expand Down
2 changes: 1 addition & 1 deletion plugwise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Plugwise module."""

__version__ = "0.8.2a0"
__version__ = "0.8.2a1"

from plugwise.smile import Smile
from plugwise.stick import stick
1 change: 1 addition & 0 deletions plugwise/connections/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Plugwise connections."""
39 changes: 20 additions & 19 deletions plugwise/connections/connection.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
"""
Use of this source code is governed by the MIT license found in the LICENSE file.

Base for serial or socket connections
"""
"""Base for serial or socket connections."""
import logging
import queue
import threading
Expand All @@ -15,41 +11,46 @@


class StickConnection:
""" Generic Plugwise stick connection"""
"""Generic Plugwise stick connection."""

def __init__(self, port, stick=None):
"""Initialize StickConnection."""
self.port = port
self.stick = stick
self.run_reader_thread = False
self.run_writer_thread = False
self._is_connected = False
self._writer = None

self._reader_thread = None
self._write_queue = None
self._writer_thread = None

Comment thread
brefra marked this conversation as resolved.
Resolved
################################################
### Open connection ###
################################################

def connect(self):
"""Open the connection"""
"""Open the connection."""
if not self._is_connected:
self._open_connection()

def _open_connection(self):
"""Placeholder to initialize the connection"""
"""Placeholder to initialize the connection."""
raise NotImplementedError

################################################
### Reader ###
################################################

def _reader_start(self, name):
"""Start the reader thread to receive data"""
self._reader_thread = threading.Thread(None, self._reader_daemon, name, (), {})
"""Start the reader thread to receive data."""
self._reader_thread = threading.Thread(None, self._reader_deamon, name, (), {})
self.run_reader_thread = True
self._reader_thread.start()

def _reader_daemon(self):
"""Thread to collect available data from connection"""
def _reader_deamon(self):
"""Thread to collect available data from connection."""
while self.run_reader_thread:
data = self._read_data()
if data:
Expand All @@ -58,15 +59,15 @@ def _reader_daemon(self):
_LOGGER.debug("Reader daemon stopped")

def _read_data(self):
"""Placeholder to receive message from the connection"""
"""Placeholder to receive message from the connection."""
raise NotImplementedError

################################################
### Writer ###
################################################

def _writer_start(self, name: str):
"""Start the writer thread to send data"""
"""Start the writer thread to send data."""
self._write_queue = queue.Queue()
self._writer_thread = threading.Thread(None, self._writer_daemon, name, (), {})
self._writer_thread.daemon = True
Expand Down Expand Up @@ -105,23 +106,23 @@ def send(self, message: PlugwiseMessage, callback=None):
################################################

def is_connected(self):
"""Return connection state"""
"""Return connection state."""
return self._is_connected

def read_thread_alive(self):
"""Return state of write thread"""
"""Return state of write thread."""
return self._reader_thread.isAlive() if self.run_reader_thread else False

def write_thread_alive(self):
"""Return state of write thread"""
"""Return state of write thread."""
return self._writer_thread.isAlive() if self.run_writer_thread else False

################################################
### Close connection ###
################################################

def disconnect(self):
"""Close the connection"""
"""Close the connection."""
if self._is_connected:
self._is_connected = False
self.run_writer_thread = False
Expand All @@ -133,5 +134,5 @@ def disconnect(self):
self._close_connection()

def _close_connection(self):
"""Placeholder to close the port"""
"""Placeholder to close the port."""
raise NotImplementedError
16 changes: 6 additions & 10 deletions plugwise/connections/serial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
"""
Use of this source code is governed by the MIT license found in the LICENSE file.

Serial connection
"""
"""Serial connection."""
import logging

import serial
Expand All @@ -11,13 +7,11 @@
from plugwise.constants import BAUD_RATE, BYTE_SIZE, STOPBITS
from plugwise.exceptions import PortError

# from plugwise.message import PlugwiseMessage

_LOGGER = logging.getLogger(__name__)


class PlugwiseUSBConnection(StickConnection):
"""simple wrapper around serial module"""
"""Simple wrapper around serial module."""

def __init__(self, port, stick=None):
super().__init__(port, stick)
Expand All @@ -26,8 +20,10 @@ def __init__(self, port, stick=None):
self._stopbits = STOPBITS
self._parity = serial.PARITY_NONE

self._serial = None

Comment thread
brefra marked this conversation as resolved.
Resolved
def _open_connection(self):
"""Open serial port"""
"""Open serial port."""
_LOGGER.debug("Open serial port %s", self.port)
try:
self._serial = serial.Serial(
Expand Down Expand Up @@ -83,7 +79,7 @@ def _read_data(self):
return None

def _write_data(self, data):
"""Write data to serial port"""
"""Write data to serial port."""
try:
self._serial.write(data)
except serial.serialutil.SerialException as err:
Expand Down
18 changes: 6 additions & 12 deletions plugwise/connections/socket.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
"""
Use of this source code is governed by the MIT license found in the LICENSE file.

Socket connection
"""
"""Socket connection."""
import logging
import socket

from plugwise.connections.connection import StickConnection
from plugwise.constants import SLEEP_TIME
from plugwise.exceptions import PortError
from plugwise.message import PlugwiseMessage

_LOGGER = logging.getLogger(__name__)


class SocketConnection(StickConnection):
"""
Wrapper for Socket connection configuration
"""
"""Wrapper for Socket connection configuration."""

def __init__(self, port, stick=None):
super().__init__(port, stick)
Expand All @@ -27,8 +19,10 @@ def __init__(self, port, stick=None):
self._socket_port = int(port_split[1])
self._socket_address = (self._socket_host, self._socket_port)

self._socket = None

Comment thread
brefra marked this conversation as resolved.
Resolved
def _open_connection(self):
"""Open socket"""
"""Open socket."""
_LOGGER.debug(
"Open socket to host '%s' at port %s",
self._socket_host,
Expand Down Expand Up @@ -87,7 +81,7 @@ def _read_data(self):
return None

def _write_data(self, data):
"""Write data to socket"""
"""Write data to socket."""
try:
self._socket.send(data)
except Exception as err:
Expand Down
2 changes: 1 addition & 1 deletion plugwise/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Copyright (C) 2011 Sven Petai <hadara@bsd.ee>, use of this source code is governed by the MIT license found in the LICENSE file."""
"""Plugwise Exceptions."""


class PlugwiseException(Exception):
Expand Down
23 changes: 6 additions & 17 deletions plugwise/message.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
"""
Use of this source code is governed by the MIT license found in the LICENSE file.

Base for Plugwise messages
"""
"""Base for Plugwise messages."""

from plugwise.constants import MESSAGE_FOOTER, MESSAGE_HEADER, UTF8_DECODE
from plugwise.util import crc_fun


class ParserError(Exception):
"""
Error when invalid message is received
"""

pass
class PlugwiseMessage:
"""Plugwise message base."""

# TODO: none of the self objects are predefined

class PlugwiseMessage:
def serialize(self):
"""
return message in a serialized format that can be sent out on wire

return: bytes
"""
"""Return message in a serialized format that can be sent out on wire."""
args = b"".join(a.serialize() for a in self.args)
msg = self.ID
if self.mac != "":
Expand All @@ -32,4 +20,5 @@ def serialize(self):
return MESSAGE_HEADER + msg + checksum + MESSAGE_FOOTER

def calculate_checksum(self, s):
"""Calculate crc checksum."""
return bytes("%04X" % crc_fun(s), UTF8_DECODE)
2 changes: 1 addition & 1 deletion plugwise/messages/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Use of this source code is governed by the MIT license found in the LICENSE file.
"""Plugwise messaging."""
Loading