Skip to content

Commit

Permalink
Allow read of CoilData with missing mappings (#172)
Browse files Browse the repository at this point in the history
Fallback to `UNKNOWN (int)`.

Writes should fail.
  • Loading branch information
yozik04 authored Jul 27, 2024
2 parents b5594c4 + c6818d3 commit a879e7b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
5 changes: 4 additions & 1 deletion nibe/coil.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ def __repr__(self) -> str:
@staticmethod
def from_mapping(coil: Coil, value: int) -> "CoilData":
"""Create CoilData from raw value using mappings."""
return CoilData(coil, coil.get_mapping_for(value))
try:
return CoilData(coil, coil.get_mapping_for(value))
except NoMappingException:
return CoilData(coil, f"UNKNOWN ({value})")

@staticmethod
def from_raw_value(coil: Coil, value: Union[int, None]) -> "CoilData":
Expand Down
5 changes: 3 additions & 2 deletions nibe/connection/nibegw.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,10 @@ def _on_coil_value(self, coil_address: int, value: Union[float, int, str]) -> No
value = int(value)

if coil.has_mappings and isinstance(value, int):
value = coil.get_mapping_for(value)
coil_data = CoilData.from_mapping(coil, value)
else:
coil_data = CoilData(coil, value)

coil_data = CoilData(coil, value)
logger.info(coil_data)
self._on_coil_read_success(coil_data)
except NibeException as e:
Expand Down
11 changes: 7 additions & 4 deletions tests/connection/test_nibegw.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from nibe.coil import CoilData
from nibe.connection.nibegw import ConnectionStatus, NibeGW
from nibe.exceptions import ReadException, ReadTimeoutException, WriteException
from nibe.exceptions import NoMappingException, ReadTimeoutException, WriteException
from nibe.heatpump import HeatPump, Model, ProductInfo


Expand Down Expand Up @@ -95,9 +95,12 @@ async def test_read_coil_decode_failed(
_enqueue_datagram(nibegw, "5c00206a064ea8f51200004d")

start = time.time()
with pytest.raises(ReadException) as excinfo:
await nibegw.read_coil(coil, timeout=0.1)
assert "Decode failed" in str(excinfo.value)
data = await nibegw.read_coil(coil, timeout=0.1)
assert data.value == "UNKNOWN (245)"

with pytest.raises(NoMappingException):
data.validate()

assert 1 == transport.sendto.call_count
duration = time.time() - start
assert duration <= 0.1
Expand Down
15 changes: 12 additions & 3 deletions tests/test_coil.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

from nibe.coil import Coil, CoilData
from nibe.connection.nibegw import CoilDataEncoderNibeGw
from nibe.exceptions import DecodeException, EncodeException, ValidationError
from nibe.exceptions import (
DecodeException,
EncodeException,
NoMappingException,
ValidationError,
)
from nibe.parsers import swapwords


Expand Down Expand Up @@ -447,8 +452,12 @@ def test_unsigned_u8_mapping_decode(
def test_unsigned_u8_mapping_decode_exception(
encoder_word_swap_false: CoilDataEncoderNibeGw, coil_unsigned_u8_mapping: Coil
):
with pytest.raises(DecodeException):
encoder_word_swap_false.decode(coil_unsigned_u8_mapping, b"\x00")
coil_data = encoder_word_swap_false.decode(coil_unsigned_u8_mapping, b"\x00")

assert coil_data.value == "UNKNOWN (0)"

with pytest.raises(NoMappingException):
coil_data.validate()


@pytest.mark.parametrize(
Expand Down

0 comments on commit a879e7b

Please sign in to comment.