Skip to content

Commit a474c4f

Browse files
committed
Fix TOU status showing inverted values
TOU status was always appearing as OFF regardless of actual state. Root cause: tou_status_to_python converter was using incorrect encoding: - Old (incorrect): 0 = OFF, 1 = ON - Correct: 1 = OFF, 2 = ON (standard OnOffFlag encoding) When device sent 2 (ON), converter returned False. When device sent 1 (OFF), it incorrectly returned True. Changes: - Updated tou_status_to_python() to use OnOffFlag encoding (1=OFF, 2=ON) - Fixed all TOU status converter tests to match correct encoding - Added documentation noting this uses standard OnOffFlag encoding All 378 tests pass.
1 parent 999d1c2 commit a474c4f

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

src/nwp500/converters.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def device_bool_from_python(value: bool) -> int:
6565
def tou_status_to_python(value: Any) -> bool:
6666
"""Convert Time of Use status to Python bool.
6767
68-
Device representation: 0 = Off/False, 1 = On/True
68+
Device representation: 1 = Off/False, 2 = On/True
69+
(Uses standard OnOffFlag encoding)
6970
7071
Args:
7172
value: Device TOU status value.
@@ -74,12 +75,12 @@ def tou_status_to_python(value: Any) -> bool:
7475
Python boolean.
7576
7677
Example:
77-
>>> tou_status_to_python(1)
78+
>>> tou_status_to_python(2)
7879
True
79-
>>> tou_status_to_python(0)
80+
>>> tou_status_to_python(1)
8081
False
8182
"""
82-
return bool(value == 1)
83+
return bool(value == 2)
8384

8485

8586
def tou_override_to_python(value: Any) -> bool:

tests/test_model_converters.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,48 +93,49 @@ def test_off_value_variations(self, off_value):
9393
class TestTouStatusConverter:
9494
"""Test tou_status_to_python converter.
9595
96-
TOU (Time of Use) status encoding converts device state to boolean.
97-
Device: 1 = Enabled (True), anything else = Disabled (False)
98-
NOTE: String values are NOT converted to int before comparison.
96+
TOU (Time of Use) status encoding uses standard OnOffFlag:
97+
Device: 1 = OFF/False, 2 = ON/True
9998
"""
10099

101100
def test_tou_disabled(self):
102-
"""TOU disabled state: 0 = False."""
103-
result = tou_status_to_python(0)
101+
"""TOU disabled state: 1 = False."""
102+
result = tou_status_to_python(1)
104103
assert isinstance(result, bool)
105104
assert result is False
106105

107106
def test_tou_enabled(self):
108-
"""TOU enabled state: 1 = True."""
109-
result = tou_status_to_python(1)
107+
"""TOU enabled state: 2 = True."""
108+
result = tou_status_to_python(2)
110109
assert isinstance(result, bool)
111110
assert result is True
112111

113112
def test_string_disabled(self):
114-
"""String '0' = TOU disabled."""
115-
assert tou_status_to_python("0") is False
113+
"""String '1' is not equal to int 2, so returns False."""
114+
assert tou_status_to_python("1") is False
116115

117116
def test_string_enabled(self):
118-
"""String '1' is not equal to int 1, so returns False."""
119-
# tou_status_to_python uses: bool(value == 1)
120-
# String "1" != int 1, so result is False
121-
assert tou_status_to_python("1") is False
117+
"""String '2' is not equal to int 2, so returns False."""
118+
# tou_status_to_python uses: bool(value == 2)
119+
# String "2" != int 2, so result is False
120+
assert tou_status_to_python("2") is False
122121

123122
def test_invalid_value(self):
124-
"""Value other than 1 is treated as False."""
125-
assert tou_status_to_python(2) is False
123+
"""Value other than 2 is treated as False."""
124+
assert tou_status_to_python(0) is False
126125
assert tou_status_to_python(3) is False
127126
assert tou_status_to_python(-1) is False
128127

129-
@pytest.mark.parametrize("enabled_value", [1, 1.0])
128+
@pytest.mark.parametrize("enabled_value", [2, 2.0])
130129
def test_enabled_variations(self, enabled_value):
131-
"""Test numeric variations of enabled (value == 1)."""
132-
# Only numeric 1 and float 1.0 equal int 1
130+
"""Test numeric variations of enabled (value == 2)."""
131+
# Only numeric 2 and float 2.0 equal int 2
133132
assert tou_status_to_python(enabled_value) is True
134133

135-
@pytest.mark.parametrize("disabled_value", [0, "0", 0.0, 2, 3, -1, "1"])
134+
@pytest.mark.parametrize(
135+
"disabled_value", [0, "0", 0.0, 1, 3, -1, "1", "2"]
136+
)
136137
def test_disabled_variations(self, disabled_value):
137-
"""Test various representations of disabled (value != 1)."""
138+
"""Test various representations of disabled (value != 2)."""
138139
assert tou_status_to_python(disabled_value) is False
139140

140141

0 commit comments

Comments
 (0)