Skip to content

Commit b985ed9

Browse files
committed
Refactor conversion functions to remove suppress statements and improve error handling in EntityInfo
1 parent 22322a4 commit b985ed9

2 files changed

Lines changed: 71 additions & 36 deletions

File tree

src/bsblan/models.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import logging
6-
from contextlib import suppress
76
from dataclasses import dataclass, field
87
from datetime import time
98
from enum import IntEnum
@@ -245,23 +244,17 @@ def _convert_bsblan_value(
245244
if any(unit.endswith(u) for u in TEMPERATURE_UNITS):
246245
result = float(raw)
247246
else:
248-
with suppress(ValueError):
249-
result = float(raw) if "." in raw else int(raw)
247+
result = float(raw) if "." in raw else int(raw)
250248

251249
elif data_type == DataType.ENUM:
252-
with suppress(ValueError):
253-
result = int(raw)
250+
result = int(raw)
254251

255252
elif data_type == DataType.TIME:
256-
try:
257-
hour, minute = map(int, raw.split(":"))
258-
result = time(hour=hour, minute=minute)
259-
except ValueError:
260-
pass
253+
hour, minute = map(int, raw.split(":"))
254+
result = time(hour=hour, minute=minute)
261255

262256
elif data_type == DataType.WEEKDAY:
263-
with suppress(ValueError):
264-
result = int(raw)
257+
result = int(raw)
265258

266259
except (ValueError, TypeError) as e:
267260
logging.getLogger(__name__).warning(

tests/test_entity_info.py

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@
88
from bsblan.models import DataType, EntityInfo
99

1010

11-
def test_entity_info_invalid_time_conversion() -> None:
12-
"""Test EntityInfo with invalid time format."""
13-
# Create EntityInfo with invalid time format
14-
entity = EntityInfo(
15-
name="Invalid Time",
16-
value="24:61", # Invalid time
17-
unit="",
18-
desc="",
19-
data_type=DataType.TIME,
20-
)
11+
def test_entity_info_invalid_time_conversion(
12+
caplog: pytest.LogCaptureFixture,
13+
) -> None:
14+
"""Test EntityInfo with invalid time format logs a warning."""
15+
with caplog.at_level(logging.WARNING):
16+
entity = EntityInfo(
17+
name="Invalid Time",
18+
value="24:61", # Invalid time
19+
unit="",
20+
desc="",
21+
data_type=DataType.TIME,
22+
)
2123

22-
# The value should remain as string since conversion failed
23-
assert entity.value == "24:61"
24+
# The value should remain as string since conversion failed
25+
assert entity.value == "24:61"
26+
assert "Failed to convert value" in caplog.text
2427

2528

2629
def test_entity_info_undefined_value_becomes_none() -> None:
@@ -35,19 +38,58 @@ def test_entity_info_undefined_value_becomes_none() -> None:
3538
assert entity.value is None
3639

3740

38-
def test_entity_info_invalid_weekday_conversion() -> None:
39-
"""Test EntityInfo with invalid weekday format."""
40-
# Create EntityInfo with invalid weekday format
41-
entity = EntityInfo(
42-
name="Invalid Weekday",
43-
value="not-a-number", # Invalid weekday
44-
unit="",
45-
desc="",
46-
data_type=DataType.WEEKDAY,
47-
)
41+
def test_entity_info_invalid_weekday_conversion(
42+
caplog: pytest.LogCaptureFixture,
43+
) -> None:
44+
"""Test EntityInfo with invalid weekday format logs a warning."""
45+
with caplog.at_level(logging.WARNING):
46+
entity = EntityInfo(
47+
name="Invalid Weekday",
48+
value="not-a-number", # Invalid weekday
49+
unit="",
50+
desc="",
51+
data_type=DataType.WEEKDAY,
52+
)
4853

49-
# The value should remain as string since conversion failed
50-
assert entity.value == "not-a-number"
54+
# The value should remain as string since conversion failed
55+
assert entity.value == "not-a-number"
56+
assert "Failed to convert value" in caplog.text
57+
58+
59+
def test_entity_info_invalid_plain_number_conversion(
60+
caplog: pytest.LogCaptureFixture,
61+
) -> None:
62+
"""Test non-temperature PLAIN_NUMBER conversion failure logs a warning."""
63+
with caplog.at_level(logging.WARNING):
64+
entity = EntityInfo(
65+
name="Invalid Number",
66+
value="not-numeric",
67+
unit="%",
68+
desc="",
69+
data_type=DataType.PLAIN_NUMBER,
70+
)
71+
72+
# The value should remain as string since conversion failed
73+
assert entity.value == "not-numeric"
74+
assert "Failed to convert value" in caplog.text
75+
76+
77+
def test_entity_info_invalid_enum_conversion(
78+
caplog: pytest.LogCaptureFixture,
79+
) -> None:
80+
"""Test ENUM conversion failure logs a warning."""
81+
with caplog.at_level(logging.WARNING):
82+
entity = EntityInfo(
83+
name="Invalid Enum",
84+
value="not-an-int",
85+
unit="",
86+
desc="Some description",
87+
data_type=DataType.ENUM,
88+
)
89+
90+
# The value should remain as string since conversion failed
91+
assert entity.value == "not-an-int"
92+
assert "Failed to convert value" in caplog.text
5193

5294

5395
def test_entity_info_general_conversion_error(caplog: pytest.LogCaptureFixture) -> None:

0 commit comments

Comments
 (0)