Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Unit test for KNX DPT 5.010 value.""" | ||
import unittest | ||
|
||
from xknx.exceptions import ConversionError | ||
from xknx.knx import DPTValue1Ucount | ||
|
||
|
||
class TestDPTValue1Ucount(unittest.TestCase): | ||
"""Test class for KNX scaling value.""" | ||
|
||
# pylint: disable=too-many-public-methods,invalid-name | ||
|
||
def test_value_50(self): | ||
"""Test parsing and streaming of DPTValue1Ucount 50.""" | ||
self.assertEqual(DPTValue1Ucount().to_knx(50), (0x32,)) | ||
self.assertEqual(DPTValue1Ucount().from_knx((0x32,)), 50) | ||
|
||
def test_value_max(self): | ||
"""Test parsing and streaming of DPTValue1Ucount 63.""" | ||
self.assertEqual(DPTValue1Ucount().to_knx(63), (0x3F,)) | ||
self.assertEqual(DPTValue1Ucount().from_knx((0x3F,)), 63) | ||
|
||
def test_value_min(self): | ||
"""Test parsing and streaming of DPTValue1Ucount 0.""" | ||
self.assertEqual(DPTValue1Ucount().to_knx(0), (0x00,)) | ||
self.assertEqual(DPTValue1Ucount().from_knx((0x00,)), 0) | ||
|
||
def test_to_knx_min_exceeded(self): | ||
"""Test parsing of DPTValue1Ucount with wrong value (underflow).""" | ||
with self.assertRaises(ConversionError): | ||
DPTValue1Ucount().to_knx(DPTValue1Ucount.value_min - 1) | ||
|
||
def test_to_knx_max_exceeded(self): | ||
"""Test parsing of DPTValue1Ucount with wrong value (overflow).""" | ||
with self.assertRaises(ConversionError): | ||
DPTValue1Ucount().to_knx(DPTValue1Ucount.value_max + 1) | ||
|
||
def test_to_knx_wrong_parameter(self): | ||
"""Test parsing of DPTValue1Ucount with wrong value (string).""" | ||
with self.assertRaises(ConversionError): | ||
DPTValue1Ucount().to_knx("fnord") | ||
|
||
def test_from_knx_wrong_parameter(self): | ||
"""Test parsing of DPTValue1Ucount with wrong value (3 byte array).""" | ||
with self.assertRaises(ConversionError): | ||
DPTValue1Ucount().from_knx((0x01, 0x02, 0x03)) | ||
|
||
def test_from_knx_wrong_value(self): | ||
"""Test parsing of DPTValue1Ucount with value which exceeds limits.""" | ||
with self.assertRaises(ConversionError): | ||
DPTValue1Ucount().from_knx((0x256,)) | ||
|
||
def test_from_knx_wrong_parameter2(self): | ||
"""Test parsing of DPTValue1Ucount with wrong value (array containing string).""" | ||
with self.assertRaises(ConversionError): | ||
DPTValue1Ucount().from_knx(("0x23")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" | ||
Module for managing a DTP 5010 remote value. | ||
DPT 5.010. | ||
""" | ||
from xknx.knx import DPTArray, DPTValue1Ucount | ||
|
||
from .remote_value import RemoteValue | ||
|
||
|
||
class RemoteValueDptValue1Ucount(RemoteValue): | ||
"""Abstraction for remote value of KNX DPT 5.010.""" | ||
|
||
def __init__(self, | ||
xknx, | ||
group_address=None, | ||
device_name=None, | ||
after_update_cb=None): | ||
"""Initialize remote value of KNX DPT 5.010.""" | ||
# pylint: disable=too-many-arguments | ||
super(RemoteValueDptValue1Ucount, self).__init__( | ||
xknx, group_address, None, | ||
device_name=device_name, after_update_cb=after_update_cb) | ||
|
||
def payload_valid(self, payload): | ||
"""Test if telegram payload may be parsed.""" | ||
return (isinstance(payload, DPTArray) | ||
and len(payload.value) == 1) | ||
|
||
def to_knx(self, value): | ||
"""Convert value to payload.""" | ||
return DPTArray(DPTValue1Ucount.to_knx(value)) | ||
|
||
def from_knx(self, payload): | ||
"""Convert current payload to value.""" | ||
return DPTValue1Ucount.from_knx(payload.value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Implementation of Basic KNX DPT_1_Ucount Values.""" | ||
from xknx.exceptions import ConversionError | ||
|
||
from .dpt import DPTBase | ||
|
||
|
||
class DPTValue1Ucount(DPTBase): | ||
""" | ||
Abstraction for KNX 1 Octet. | ||
DPT 5.010 | ||
""" | ||
|
||
value_min = 0 | ||
value_max = 255 | ||
unit = "" | ||
resolution = 1 | ||
|
||
@classmethod | ||
def from_knx(cls, raw): | ||
"""Parse/deserialize from KNX/IP raw data.""" | ||
cls.test_bytesarray(raw, 1) | ||
|
||
value = raw[0] | ||
|
||
if not cls._test_boundaries(value): | ||
raise ConversionError("Cant parse DPTValue1Ucount", value=value, raw=raw) | ||
|
||
return value | ||
|
||
@classmethod | ||
def to_knx(cls, value): | ||
"""Serialize to KNX/IP raw data.""" | ||
if not isinstance(value, (int)): | ||
raise ConversionError("Cant serialize DPTValue1Ucount", value=value) | ||
if not cls._test_boundaries(value): | ||
raise ConversionError("Cant serialize DPTValue1Ucount", value=value) | ||
return (value,) | ||
|
||
@classmethod | ||
def _test_boundaries(cls, value): | ||
"""Test if value is within defined range for this object.""" | ||
return value >= cls.value_min and \ | ||
value <= cls.value_max |