Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
b55effa
Test with the actual updated code
bouwew Dec 24, 2020
1af06b0
Move connection.py to __init__.py
brefra Dec 19, 2020
4247254
Move message.py to __init__.py
brefra Dec 19, 2020
1298406
Move node.py to __init__.py
brefra Dec 19, 2020
dda9192
Refactor parser.py
brefra Dec 19, 2020
5b44947
Reorganize acknowledge id's
brefra Dec 19, 2020
d4aed4c
Extract msg controller out of stick.py
brefra Dec 19, 2020
5f792e0
Refactor node discovery
brefra Dec 19, 2020
4812e40
Reorganize discovery code
brefra Dec 19, 2020
e2a6d87
Rewrite update_loop stick.py
brefra Dec 19, 2020
c0b2ccc
Readd node join and unjoin requests
brefra Dec 19, 2020
abbb35b
Fix refactor bugs
brefra Dec 19, 2020
cbc7121
Reformat docstring
brefra Dec 19, 2020
888e0eb
Fix last hour power consumption and production
brefra Dec 19, 2020
9f3eef3
Guard scan sensitivity setting
brefra Dec 19, 2020
0c8f862
Fix typos in docstrings and comments
brefra Dec 19, 2020
83e731e
Apply isort
brefra Dec 19, 2020
4649ff2
Isort controller.py
brefra Dec 19, 2020
832aa40
Isort parser.py
brefra Dec 19, 2020
49ae959
Fix watchdog to check receiver and sender theads
brefra Dec 19, 2020
a281a37
Fix logging receiving messages
brefra Dec 19, 2020
6bb0db8
Remove unused last_log_collected
brefra Dec 19, 2020
7a1c8f3
Fix isort controller.py
brefra Dec 19, 2020
d786b77
Set message timestamp at deserialize
brefra Dec 20, 2020
02ee78f
Only request info msg when node is available
brefra Dec 20, 2020
109e248
Validate received messages
brefra Dec 20, 2020
81e70ff
Code and comment cleanup
brefra Dec 20, 2020
b45b6ef
Make imports relative
brefra Dec 20, 2020
4aa126e
Remove message length in parser
brefra Dec 20, 2020
ec7ef8f
Change log level for drop single ping request
brefra Dec 20, 2020
ab65746
Correct formatring of raise message in response.py
brefra Dec 23, 2020
6e6468f
Refactor message passing
brefra Dec 23, 2020
f154fb9
Correct message passing sense
brefra Dec 23, 2020
d7aabf5
Correct message passing
brefra Dec 23, 2020
c301297
Reformat imports
brefra Dec 23, 2020
745f043
Update .coveragerc
brefra Dec 23, 2020
12d7e40
Fix messages for sed devices
brefra Dec 23, 2020
864c141
Fix processing node join request messages
brefra Dec 23, 2020
91f0947
Use proper slicing in responses.py
brefra Dec 24, 2020
5adde96
Do not discover yet while accept joining request
brefra Dec 24, 2020
46c9226
Disable negative Int deserialize for known positive values
brefra Nov 29, 2020
68a7339
Mark UnixTimestamp as positive Int
brefra Dec 1, 2020
c994d8a
Improve DateTime conversion
brefra Dec 1, 2020
58691aa
Specify exception
brefra Dec 8, 2020
2fb04df
Fix retry drop at discovery phase
brefra Dec 24, 2020
e9a1f55
Update CHANGELOG.md
brefra Dec 24, 2020
fef48fb
Fix availability check
brefra Dec 26, 2020
7813bbf
Fix restarting receive_timeout_loop
brefra Dec 27, 2020
bc978c5
Fix node availabillity
brefra Dec 27, 2020
f1b6732
Fixed typo function call
brefra Dec 29, 2020
bc4adf7
Fix ping parameters sed.py
brefra Dec 29, 2020
7bcd368
Do timeout handling at seq reset
brefra Dec 29, 2020
14b4f32
Bump to 0.8.6
brefra Dec 29, 2020
1f7238f
Start sending deamon first
brefra Dec 29, 2020
6658825
Update docstrings file heders
brefra Dec 30, 2020
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
3 changes: 1 addition & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ omit=
*/venv/*
setup.py
plugwise/connections/*
plugwise/message.py
plugwise/controller.py
plugwise/messages/*
plugwise/node.py
plugwise/nodes/*
plugwise/parser.py
plugwise/stick.py
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changelog

## 0.8.6 - Code quality improvements for stick

- Bug-fix: Power history was not reported (0 value) during last week of the month
- Improvement: Validate message checksums
- Improvement: Do a single ping request to validate if node is on-line
- Improvement: Guard Scan sensitivity setting to medium
- Improvement: Move general module code of messages, nodes, connection to the __init__.py files.
- Improvement: Do proper timeout handling while sequence counter resets (once every 65532 messages)
- Improvement: Better code separation. All logic is in their designated files:
1. Connection (connection/*.py)
2. Data parsing (parser.py)
3. Data encoding/decoding of message (messages/*.py)
4. Message handling - Initialization & transportation (controller.py)
5. Message processing - Do the required stuff (stick.py & nodes/*.py)
- Improvement: Resolves all flake8 comments

## 0.8.5 - Fix sensor scaling
- Fix for via HA Core issue #44349
- Fix other value scaling bugs
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.5"
__version__ = "0.8.6"

from plugwise.smile import Smile
from plugwise.stick import stick
124 changes: 123 additions & 1 deletion plugwise/connections/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,123 @@
"""Plugwise connections."""
"""Base class for serial or socket connections to USB-Stick."""
import logging
import queue
import threading
import time

from ..constants import SLEEP_TIME
from ..messages.requests import NodeRequest

_LOGGER = logging.getLogger(__name__)


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

def __init__(self, port, parser):
"""Initialize StickConnection."""
self.port = port
self.parser = parser
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

################################################
### Open connection ###
################################################

def connect(self) -> bool:
"""Open the connection."""
if not self._is_connected:
self._open_connection()
return self._is_connected

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

def _reader_start(self, 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_deamon(self):
"""Thread to collect available data from connection."""
while self.run_reader_thread:
data = self._read_data()
if data:
self.parser(data)
time.sleep(0.01)
_LOGGER.debug("Reader daemon stopped")

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

def _writer_start(self, name: str):
"""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
self.run_writer_thread = True
self._writer_thread.start()

def _writer_daemon(self):
"""Thread to write data from queue to existing connection."""
while self.run_writer_thread:
try:
(message, callback) = self._write_queue.get(block=True, timeout=1)
except queue.Empty:
time.sleep(SLEEP_TIME)
else:
_LOGGER.debug(
"Sending %s to plugwise stick (%s)",
message.__class__.__name__,
message.serialize(),
)
self._write_data(message.serialize())
time.sleep(SLEEP_TIME)
if callback:
callback()
_LOGGER.debug("Writer daemon stopped")

def send(self, message: NodeRequest, callback=None):
"""Add message to write queue."""
self._write_queue.put_nowait((message, callback))

################################################
### Connection state ###
################################################

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

def read_thread_alive(self):
"""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 self._writer_thread.isAlive() if self.run_writer_thread else False

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

def disconnect(self):
"""Close the connection."""
if self._is_connected:
self._is_connected = False
self.run_writer_thread = False
self.run_reader_thread = False
max_wait = 5 * SLEEP_TIME
while self._writer_thread.isAlive():
time.sleep(SLEEP_TIME)
max_wait -= SLEEP_TIME
self._close_connection()
138 changes: 0 additions & 138 deletions plugwise/connections/connection.py

This file was deleted.

10 changes: 5 additions & 5 deletions plugwise/connections/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@

import serial

from plugwise.connections.connection import StickConnection
from plugwise.constants import BAUD_RATE, BYTE_SIZE, STOPBITS
from plugwise.exceptions import PortError
from ..connections import StickConnection
from ..constants import BAUD_RATE, BYTE_SIZE, STOPBITS
from ..exceptions import PortError

_LOGGER = logging.getLogger(__name__)


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

def __init__(self, port, stick=None):
super().__init__(port, stick)
def __init__(self, port, parser):
super().__init__(port, parser)
self._baud = BAUD_RATE
self._byte_size = BYTE_SIZE
self._stopbits = STOPBITS
Expand Down
8 changes: 4 additions & 4 deletions plugwise/connections/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import logging
import socket

from plugwise.connections.connection import StickConnection
from plugwise.exceptions import PortError
from ..connections import StickConnection
from ..exceptions import PortError

_LOGGER = logging.getLogger(__name__)


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

def __init__(self, port, stick=None):
super().__init__(port, stick)
def __init__(self, port, parser):
super().__init__(port, parser)
# get the address from a <host>:<port> format
port_split = self.port.split(":")
self._socket_host = port_split[0]
Expand Down
Loading