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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
"""Unit test for KNX DPT HVAC Operation modes."""
from typing import Any
import pytest
from xknx.dpt import DPTArray, DPTHVACContrMode, DPTHVACMode, DPTHVACStatus
from xknx.dpt.dpt_20 import HeatCool, HVACControllerMode, HVACOperationMode, HVACStatus
from xknx.exceptions import ConversionError, CouldNotParseTelegram
class TestDPTHVACMode:
"""Test class for KNX DPT HVAC Operation modes."""
def test_mode_to_knx(self) -> None:
"""Test parsing DPTHVACMode to KNX."""
assert DPTHVACMode.to_knx(HVACOperationMode.AUTO) == DPTArray((0x00,))
assert DPTHVACMode.to_knx(HVACOperationMode.COMFORT) == DPTArray((0x01,))
assert DPTHVACMode.to_knx(HVACOperationMode.STANDBY) == DPTArray((0x02,))
assert DPTHVACMode.to_knx(HVACOperationMode.ECONOMY) == DPTArray((0x03,))
assert DPTHVACMode.to_knx(HVACOperationMode.BUILDING_PROTECTION) == DPTArray(
(0x04,)
)
def test_mode_to_knx_by_string(self) -> None:
"""Test parsing DPTHVACMode string values to KNX."""
assert DPTHVACMode.to_knx("auto") == DPTArray((0x00,))
assert DPTHVACMode.to_knx("Comfort") == DPTArray((0x01,))
assert DPTHVACMode.to_knx("standby") == DPTArray((0x02,))
assert DPTHVACMode.to_knx("ECONOMY") == DPTArray((0x03,))
assert DPTHVACMode.to_knx("Building_Protection") == DPTArray((0x04,))
def test_mode_to_knx_wrong_value(self) -> None:
"""Test serializing DPTHVACMode to KNX with wrong value."""
with pytest.raises(ConversionError):
DPTHVACMode.to_knx(5)
def test_mode_from_knx(self) -> None:
"""Test parsing DPTHVACMode from KNX."""
assert DPTHVACMode.from_knx(DPTArray((0x00,))) == HVACOperationMode.AUTO
assert DPTHVACMode.from_knx(DPTArray((0x01,))) == HVACOperationMode.COMFORT
assert DPTHVACMode.from_knx(DPTArray((0x02,))) == HVACOperationMode.STANDBY
assert DPTHVACMode.from_knx(DPTArray((0x03,))) == HVACOperationMode.ECONOMY
assert (
DPTHVACMode.from_knx(DPTArray((0x04,)))
== HVACOperationMode.BUILDING_PROTECTION
)
def test_mode_from_knx_wrong_value(self) -> None:
"""Test parsing of DPTHVACMode with wrong value)."""
with pytest.raises(CouldNotParseTelegram):
DPTHVACMode.from_knx(DPTArray((1, 2)))
with pytest.raises(ConversionError):
DPTHVACMode.from_knx(DPTArray((0x05,)))
class TestDPTHVACControllerMode:
"""Test class for KNX DPT HVAC Controller modes."""
def test_mode_to_knx(self) -> None:
"""Test parsing DPTHVACContrMode to KNX."""
assert DPTHVACContrMode.to_knx(HVACControllerMode.AUTO) == DPTArray((0x00,))
assert DPTHVACContrMode.to_knx(HVACControllerMode.HEAT) == DPTArray((0x01,))
assert DPTHVACContrMode.to_knx(HVACControllerMode.COOL) == DPTArray((0x03,))
assert DPTHVACContrMode.to_knx(HVACControllerMode.OFF) == DPTArray((0x06,))
assert DPTHVACContrMode.to_knx(HVACControllerMode.DEHUMIDIFICATION) == DPTArray(
(0x0E,)
)
def test_mode_to_knx_by_string(self) -> None:
"""Test parsing DPTHVACMode string values to KNX."""
assert DPTHVACContrMode.to_knx("morning_warmup") == DPTArray((0x02,))
assert DPTHVACContrMode.to_knx("NIGHT_PURGE") == DPTArray((0x04,))
assert DPTHVACContrMode.to_knx("precool") == DPTArray((0x05,))
assert DPTHVACContrMode.to_knx("Test") == DPTArray((0x07,))
assert DPTHVACContrMode.to_knx("NODEM") == DPTArray((0x14,))
def test_mode_to_knx_wrong_value(self) -> None:
"""Test serializing DPTHVACMode to KNX with wrong value."""
with pytest.raises(ConversionError):
DPTHVACContrMode.to_knx(18)
with pytest.raises(ConversionError):
DPTHVACContrMode.to_knx(19)
with pytest.raises(ConversionError):
DPTHVACContrMode.to_knx(21)
@pytest.mark.parametrize(
("raw", "value"),
[
(0x00, HVACControllerMode.AUTO),
(0x08, HVACControllerMode.EMERGENCY_HEAT),
(0x09, HVACControllerMode.FAN_ONLY),
(0x0A, HVACControllerMode.FREE_COOL),
(0x0B, HVACControllerMode.ICE),
],
)
def test_mode_from_knx(self, raw: int, value: HVACControllerMode) -> None:
"""Test parsing DPTHVACMode from KNX."""
assert DPTHVACContrMode.from_knx(DPTArray((raw,))) is value
def test_mode_from_knx_wrong_value(self) -> None:
"""Test parsing of DPTHVACMode with wrong value)."""
with pytest.raises(CouldNotParseTelegram):
DPTHVACContrMode.from_knx(DPTArray((1, 2)))
with pytest.raises(ConversionError):
DPTHVACContrMode.from_knx(DPTArray((18,)))
class TestHVACStatus:
"""Test HVACStatus class."""
@pytest.mark.parametrize(
("data", "dict_value"),
[
(
HVACStatus(
mode=HVACOperationMode.COMFORT,
dew_point=False,
heat_cool=HeatCool.HEAT,
inactive=False,
frost_alarm=False,
),
{
"mode": "comfort",
"dew_point": False,
"heat_cool": "heat",
"inactive": False,
"frost_alarm": False,
},
),
(
HVACStatus(
mode=HVACOperationMode.STANDBY,
dew_point=False,
heat_cool=HeatCool.COOL,
inactive=True,
frost_alarm=False,
),
{
"mode": "standby",
"dew_point": False,
"heat_cool": "cool",
"inactive": True,
"frost_alarm": False,
},
),
],
)
def test_dict(self, data: HVACStatus, dict_value: dict[str, Any]) -> None:
"""Test from_dict and as_dict methods."""
assert HVACStatus.from_dict(dict_value) == data
assert data.as_dict() == dict_value
@pytest.mark.parametrize(
"data",
[
{
"mode": 1, # invalid
"dew_point": False,
"heat_cool": "heat",
"inactive": False,
"frost_alarm": False,
},
{
"mode": "comfort",
"dew_point": False,
"heat_cool": "invalid", # invalid
"inactive": False,
"frost_alarm": False,
},
{
"mode": "comfort",
"dew_point": False,
"heat_cool": "nodem", # invalid for HVACStatus
"inactive": False,
"frost_alarm": False,
},
{
"mode": "comfort",
"dew_point": 20, # invalid
"heat_cool": "heat",
"inactive": False,
"frost_alarm": False,
},
],
)
def test_dict_invalid(self, data: dict[str, Any]) -> None:
"""Test from_dict with invalid data."""
with pytest.raises(ValueError):
HVACStatus.from_dict(data)
class TestDPTHVACStatus:
"""Test class for KNX DPTHVACStatus objects."""
@pytest.mark.parametrize(
("value", "raw"),
[
(
HVACStatus(
mode=HVACOperationMode.COMFORT,
dew_point=False,
heat_cool=HeatCool.HEAT,
inactive=False,
frost_alarm=False,
),
(0x21,),
),
(
HVACStatus(
mode=HVACOperationMode.COMFORT,
dew_point=False,
heat_cool=HeatCool.COOL,
inactive=False,
frost_alarm=False,
),
(0x01,),
),
(
HVACStatus(
mode=HVACOperationMode.ECONOMY,
dew_point=True,
heat_cool=HeatCool.COOL,
inactive=False,
frost_alarm=True,
),
(0x94,),
),
],
)
def test_dpt_encoding_decoding(self, value: HVACStatus, raw: tuple[int]) -> None:
"""Test DPTHVACStatus parsing and streaming."""
knx_value = DPTHVACStatus.to_knx(value)
assert knx_value == DPTArray(raw)
assert DPTHVACStatus.from_knx(knx_value) == value
@pytest.mark.parametrize(
("value", "raw"),
[
(
{
"mode": "comfort",
"dew_point": False,
"heat_cool": "heat",
"inactive": False,
"frost_alarm": False,
},
(0x21,),
),
(
{
"mode": "standby",
"dew_point": False,
"heat_cool": "cool",
"inactive": True,
"frost_alarm": False,
},
(0x42,),
),
],
)
def test_dpt_to_knx_from_dict(self, value: dict[str, Any], raw: tuple[int]) -> None:
"""Test DPTHVACStatus parsing from a dict."""
knx_value = DPTHVACStatus.to_knx(value)
assert knx_value == DPTArray(raw)
@pytest.mark.parametrize(
"value",
[
{"mode": "comfort", "dew_point": False, "heat_cool": "heat"},
1,
"cool",
],
)
def test_dpt_wrong_value_to_knx(self, value: Any) -> None:
"""Test DPTHVACStatus parsing with wrong value."""
with pytest.raises(ConversionError):
DPTHVACStatus.to_knx(value)
def test_dpt_wrong_value_from_knx(self) -> None:
"""Test DPTHVACStatus parsing with wrong value."""
with pytest.raises(CouldNotParseTelegram):
DPTHVACStatus.from_knx(DPTArray((0xFF, 0x4E)))
|