Skip to content

Commit

Permalink
Merge 989b28c into 4fcf6ea
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Nanko committed Feb 7, 2018
2 parents 4fcf6ea + 989b28c commit b565723
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/dpt_value_1_ucount_test.py
@@ -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"))
1 change: 1 addition & 0 deletions xknx/devices/__init__.py
Expand Up @@ -25,3 +25,4 @@
from .remote_value_scene_number import RemoteValueSceneNumber
from .remote_value_temp import RemoteValueTemp
from .remote_value_scaling import RemoteValueScaling
from. remote_value_dpt_value_1_ucount import RemoteValueDptValue1Ucount
36 changes: 36 additions & 0 deletions xknx/devices/remote_value_dpt_value_1_ucount.py
@@ -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)
7 changes: 7 additions & 0 deletions xknx/devices/sensor.py
Expand Up @@ -9,6 +9,7 @@
from .device import Device
from .remote_value_scaling import RemoteValueScaling
from .remote_value_sensor import RemoteValueSensor
from .remote_value_dpt_value_1_ucount import RemoteValueDptValue1Ucount


class Sensor(Device):
Expand All @@ -31,6 +32,12 @@ def __init__(self,
group_address_state=group_address,
device_name=self.name,
after_update_cb=self.after_update)
elif value_type == "pulse":
self.sensor_value = RemoteValueDptValue1Ucount(
xknx,
group_address=group_address,
device_name=self.name,
after_update_cb=self.after_update)
else:
self.sensor_value = RemoteValueSensor(
xknx,
Expand Down
1 change: 1 addition & 0 deletions xknx/knx/__init__.py
Expand Up @@ -27,3 +27,4 @@
from .dpt_string import DPTString
from .dpt_signed_relative_value import DPTSignedRelativeValue, DPTPercentV8, \
DPTValue1Count
from .dpt_value_1_ucount import DPTValue1Ucount
44 changes: 44 additions & 0 deletions xknx/knx/dpt_value_1_ucount.py
@@ -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

0 comments on commit b565723

Please sign in to comment.