File: remote_value_sensor_test.py

package info (click to toggle)
python-xknx 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: python: 39,710; javascript: 8,556; makefile: 27; sh: 12
file content (77 lines) | stat: -rw-r--r-- 3,152 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Unit test for RemoteValueSensor objects."""

import pytest

from xknx import XKNX
from xknx.dpt import DPTArray, DPTBase, DPTBinary, DPTValue1Ucount
from xknx.exceptions import ConversionError, CouldNotParseTelegram
from xknx.remote_value import RemoteValueNumeric, RemoteValueSensor


class TestRemoteValueSensor:
    """Test class for RemoteValueSensor objects."""

    def test_value_type(self) -> None:
        """Test initializing a value_type."""
        xknx = XKNX()
        assert RemoteValueSensor(xknx=xknx, value_type="pulse")
        assert RemoteValueSensor(xknx=xknx, value_type=9)
        assert RemoteValueSensor(xknx=xknx, value_type="9.021")
        assert RemoteValueSensor(xknx=xknx, value_type="string")

    def test_wrong_value_type(self) -> None:
        """Test initializing with wrong value_type."""
        xknx = XKNX()
        with pytest.raises(ConversionError):
            RemoteValueSensor(xknx=xknx, value_type="wrong_value_type")
        with pytest.raises(ConversionError):
            RemoteValueSensor(xknx=xknx, value_type="binary")
        with pytest.raises(ConversionError):
            RemoteValueSensor(xknx=xknx, value_type=1)
        with pytest.raises(ConversionError):
            RemoteValueSensor(xknx=xknx, value_type=2)
        with pytest.raises(ConversionError):
            RemoteValueSensor(xknx=xknx, value_type=None)
        with pytest.raises(ConversionError):
            RemoteValueSensor(xknx=xknx)

    def test_payload_length_defined(self) -> None:
        """Test if all members of DPTMAP implement payload_length."""
        for dpt_class in DPTBase.__recursive_subclasses__():
            assert isinstance(dpt_class.payload_length, int)

    def test_payload_invalid(self) -> None:
        """Test invalid payloads."""
        xknx = XKNX()
        remote_value = RemoteValueSensor(xknx=xknx, value_type="pulse")

        assert remote_value.dpt_class == DPTValue1Ucount
        with pytest.raises(CouldNotParseTelegram):
            remote_value.from_knx(None)
        with pytest.raises(CouldNotParseTelegram):
            remote_value.from_knx(DPTArray((1, 2, 3, 4)))
        with pytest.raises(CouldNotParseTelegram):
            remote_value.from_knx(DPTBinary(1))


class TestRemoteValueNumeric:
    """Test class for RemoteValueNumeric objects."""

    def test_value_type(self) -> None:
        """Test initializing a value_type."""
        xknx = XKNX()
        assert RemoteValueNumeric(xknx=xknx, value_type="pulse")
        assert RemoteValueNumeric(xknx=xknx, value_type=9)
        assert RemoteValueNumeric(xknx=xknx, value_type="9.021")

    def test_wrong_value_type(self) -> None:
        """Test initializing with wrong value_type."""
        xknx = XKNX()
        with pytest.raises(ConversionError):
            RemoteValueNumeric(xknx=xknx, value_type="string")
        with pytest.raises(ConversionError):
            RemoteValueNumeric(xknx=xknx, value_type=16)
        with pytest.raises(ConversionError):
            RemoteValueNumeric(xknx=xknx, value_type="binary")
        with pytest.raises(ConversionError):
            RemoteValueNumeric(xknx=xknx)