Skip to content

Commit

Permalink
Merge 74db24d into 4379417
Browse files Browse the repository at this point in the history
  • Loading branch information
farmio committed Jan 6, 2021
2 parents 4379417 + 74db24d commit 502f73a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 67 deletions.
31 changes: 12 additions & 19 deletions test/dpt_tests/dpt_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Unit test for KNX binary/integer objects."""
import unittest

from xknx.dpt import DPTArray, DPTBase, DPTBinary, DPTComparator
from xknx.dpt import DPTArray, DPTBase, DPTBinary
from xknx.exceptions import ConversionError


Expand All @@ -13,6 +13,8 @@ class TestDPT(unittest.TestCase):
def test_compare_binary(self):
"""Test comparison of DPTBinary objects."""
self.assertEqual(DPTBinary(0), DPTBinary(0))
self.assertEqual(DPTBinary(0), DPTBinary(False))
self.assertEqual(DPTBinary(1), DPTBinary(True))
self.assertEqual(DPTBinary(2), DPTBinary(2))
self.assertNotEqual(DPTBinary(1), DPTBinary(4))
self.assertNotEqual(DPTBinary(2), DPTBinary(0))
Expand All @@ -30,20 +32,22 @@ def test_compare_array(self):
self.assertNotEqual(DPTArray((1, 2, 3)), DPTArray([1, 2, 4]))

def test_compare_none(self):
"""Test comparison of empty DPTArray objects with None."""
self.assertEqual(DPTArray(()), None)
self.assertEqual(None, DPTArray(()))
self.assertEqual(DPTBinary(0), None)
self.assertEqual(None, DPTBinary(0))
"""Test comparison DPTArray objects with None."""
self.assertNotEqual(DPTArray(()), None)
self.assertNotEqual(None, DPTArray(()))
self.assertNotEqual(DPTBinary(0), None)
self.assertNotEqual(None, DPTBinary(0))
self.assertNotEqual(DPTArray((1, 2, 3)), None)
self.assertNotEqual(None, DPTArray((1, 2, 3)))
self.assertNotEqual(DPTBinary(1), None)
self.assertNotEqual(None, DPTBinary(1))

def test_compare_array_binary(self):
"""Test comparison of empty DPTArray objects with DPTBinary objects."""
self.assertEqual(DPTArray(()), DPTBinary(0))
self.assertEqual(DPTBinary(0), DPTArray(()))
self.assertNotEqual(DPTArray(()), DPTBinary(0))
self.assertNotEqual(DPTBinary(0), DPTArray(()))
self.assertNotEqual(DPTBinary(0), DPTArray(0))
self.assertNotEqual(DPTBinary(1), DPTArray(1))
self.assertNotEqual(DPTArray((1, 2, 3)), DPTBinary(2))
self.assertNotEqual(DPTBinary(2), DPTArray((1, 2, 3)))
self.assertNotEqual(DPTArray((2,)), DPTBinary(2))
Expand All @@ -68,17 +72,6 @@ def test_dpt_array_init_with_string(self):
with self.assertRaises(TypeError):
DPTArray("bla")

def test_dpt_comparator_none_with_none(self):
"""Test comperator for DPTBinary and DPTBinary - missing cases."""
self.assertTrue(DPTComparator.compare(None, None))
self.assertTrue(DPTComparator.compare(None, DPTBinary(0)))
self.assertTrue(DPTComparator.compare(None, DPTArray(())))

def test_dpt_comparator_none_with_wrong_parameter(self):
"""Test comperator for DPTBinary and DPTBinary - wrong parameter."""
with self.assertRaises(TypeError):
DPTComparator.compare("bla", DPTBinary(0))


class TestDPTBase(unittest.TestCase):
"""Test class for transcoder base object."""
Expand Down
4 changes: 2 additions & 2 deletions test/knxip_tests/cemi_frame_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest.mock import MagicMock

from pytest import fixture, raises
from xknx.dpt import DPTBinary, DPTComparator
from xknx.dpt import DPTBinary
from xknx.exceptions import ConversionError, CouldNotParseKNXIP, UnsupportedCEMIMessage
from xknx.knxip.cemi_frame import CEMIFrame
from xknx.knxip.knxip_enum import CEMIFlags, CEMIMessageCode
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_invalid_payload(frame):
frame.code = CEMIMessageCode.L_DATA_IND
frame.flags = 0
frame.mpdu_len = 1
frame.payload = DPTComparator()
frame.payload = DPTBinary(1)
frame.src_addr = IndividualAddress(0)
frame.dst_addr = IndividualAddress(0)

Expand Down
3 changes: 1 addition & 2 deletions xknx/dpt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Derived KNX Values like Scaling, Temperature
"""
# flake8: noqa
from .dpt import DPTArray, DPTBase, DPTBinary, DPTComparator
from .dpt import DPTArray, DPTBase, DPTBinary
from .dpt_1byte_signed import DPTPercentV8, DPTSignedRelativeValue, DPTValue1Count
from .dpt_1byte_uint import (
DPTDecimalFactor,
Expand Down Expand Up @@ -208,7 +208,6 @@
"DPTChargeDensityVolume",
"DPTColorTemperature",
"DPTCommonTemperature",
"DPTComparator",
"DPTCompressibility",
"DPTConductance",
"DPTControllerStatus",
Expand Down
53 changes: 9 additions & 44 deletions xknx/dpt/dpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class DPTBinary:
APCI_BITMASK = 0x3F
APCI_MAX_VALUE = APCI_BITMASK

def __init__(self, value) -> None:
def __init__(self, value: int) -> None:
"""Initialize DPTBinary class."""
if not isinstance(value, int):
raise TypeError()
Expand All @@ -137,9 +137,11 @@ def __init__(self, value) -> None:

self.value = value

def __eq__(self, other):
def __eq__(self, other: object) -> bool:
"""Equal operator."""
return DPTComparator.compare(self, other)
if isinstance(other, DPTBinary):
return self.value == other.value
return False

def __str__(self) -> str:
"""Return object as readable string."""
Expand All @@ -163,49 +165,12 @@ def __init__(self, value: Union[int, list, bytes, tuple]) -> None:
else:
raise TypeError()

def __eq__(self, other):
def __eq__(self, other: object) -> bool:
"""Equal operator."""
return DPTComparator.compare(self, other)
if isinstance(other, DPTArray):
return self.value == other.value
return False

def __str__(self) -> str:
"""Return object as readable string."""
return '<DPTArray value="[{}]" />'.format(",".join(hex(b) for b in self.value))


class DPTComparator:
"""Helper class to compare different types of DPT objects."""

# pylint: disable=too-few-public-methods

@staticmethod
def compare(a, b):
"""Test if 'a' and 'b' are the same."""
# pylint: disable=invalid-name,too-many-return-statements,len-as-condition
if a is None and b is None:
return True

if a is None:
if isinstance(b, DPTBinary):
return b.value == 0
if isinstance(b, DPTArray):
return len(b.value) == 0

if b is None:
if isinstance(a, DPTBinary):
return a.value == 0
if isinstance(a, DPTArray):
return len(a.value) == 0

if isinstance(a, DPTArray) and isinstance(b, DPTArray):
return a.value == b.value

if isinstance(a, DPTBinary) and isinstance(b, DPTBinary):
return a.value == b.value

if isinstance(a, DPTBinary) and isinstance(b, DPTArray):
return a.value == 0 and len(b.value) == 0

if isinstance(a, DPTArray) and isinstance(b, DPTBinary):
return len(a.value) == 0 and b.value == 0

raise TypeError()

0 comments on commit 502f73a

Please sign in to comment.