Skip to content

Commit

Permalink
Logging improvements. Timeouts or write failures raise Read/Write exc…
Browse files Browse the repository at this point in the history
…eptions.
  • Loading branch information
yozik04 committed Apr 25, 2022
1 parent a904e28 commit 2c6fd38
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
4 changes: 2 additions & 2 deletions nibe/coil.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ def value(self, value: Union[int, float, str]):
value = value.upper()
assert (
value in self.reverse_mappings
), f"Provided value {value} is not in {self.reverse_mappings.keys()}"
), f"Provided value '{value}' is not in {self.reverse_mappings.keys()} for {self.name}"

self._value = value
return

assert isinstance(value, (int, float)), f"Provided value {value} is invalid type (int and float are supported)"
assert isinstance(value, (int, float)), f"Provided value '{value}' is invalid type (int and float are supported) for {self.name}"

self.check_value_bounds(value)

Expand Down
39 changes: 29 additions & 10 deletions nibe/connection/nibegw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from binascii import hexlify

from nibe.coil import Coil
from nibe.exceptions import DecodeException, NibeException
from nibe.exceptions import DecodeException, NibeException, CoilWriteException, CoilReadException
from nibe.heatpump import HeatPump
from nibe.parsers import ReadRequest, Response, WriteRequest

Expand Down Expand Up @@ -82,15 +82,21 @@ async def read_coil(self, coil: Coil, timeout: int = 1) -> Coil:

self._read_future = asyncio.get_event_loop().create_future()

logger.debug(f"Sending {hexlify(data)} (read request) to {self._remote_ip}:{self._remote_write_port}")
self._transport.sendto(data, (self._remote_ip, self._remote_read_port))
await asyncio.wait_for(self._read_future, timeout)
logger.debug(f"Waiting for read response for {coil.name}")

self._read_future = None
try:
await asyncio.wait_for(self._read_future, timeout)
except asyncio.TimeoutError:
raise CoilReadException(f"Timeout waiting for read response for {coil.name}")
finally:
self._read_future = None

return coil

async def write_coil(self, coil: Coil, timeout: int = 1) -> bool:
assert coil.is_writable, f"Coil {coil.name} is not writable"
async def write_coil(self, coil: Coil, timeout: int = 1) -> Coil:
assert coil.is_writable, f"{coil.name} is not writable"
assert coil.value is not None
async with self._send_lock:
data = WriteRequest.build(
Expand All @@ -103,21 +109,34 @@ async def write_coil(self, coil: Coil, timeout: int = 1) -> bool:

self._write_future = asyncio.get_event_loop().create_future()

logger.debug(f"Sending {hexlify(data)} (write request) to {self._remote_ip}:{self._remote_write_port}")
self._transport.sendto(data, (self._remote_ip, self._remote_write_port))
await asyncio.wait_for(self._write_future, timeout)
logger.debug(f"Waiting for write feedback for {coil.name}")

result = self._write_future.result()
self._write_future = None
try:
await asyncio.wait_for(self._write_future, timeout)

return result
result = self._write_future.result()
logger.debug(f"Write feedback received: {result}")

if not result:
raise CoilWriteException(f"Heatpump denied writing {coil.name}")
else:
logger.debug(f"Write confirmed for {coil.name}")
except asyncio.TimeoutError:
raise CoilWriteException(f"Timeout waiting for write feedback for {coil.name}")
finally:
self._write_future = None

return coil

def error_received(self, exc):
logger.error(exc)

def _on_raw_coil_value(self, coil_address: int, raw_value: bytes):
coil = self._heatpump.get_coil_by_address(coil_address)
if not coil:
logger.warning(f"Unable to decode: coil {coil_address} not found")
logger.warning(f"Unable to decode: {coil_address} not found")
return

try:
Expand Down
4 changes: 4 additions & 0 deletions nibe/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ class EncodeException(NibeException):

class CoilWriteException(NibeException):
pass


class CoilReadException(NibeException):
pass

0 comments on commit 2c6fd38

Please sign in to comment.