An uninitialised probe reports -128 (0x80 read as a signed 8-bit integer) while the refrigeration starts up. The cloud API relays it verbatim and the sensor reported it as a real measurement, polluting history graphs and long-term statistics. Readings outside -20..50 C are now treated as firmware sentinels and the sensor reports no value, so the state is 'unknown' and the recorder excludes it. A range check is used rather than matching -128 exactly so sibling sentinels are caught too. Both temperature fields are validated; previously the details.temperature fallback was used unconditionally. The sensor deliberately reports 'unknown' rather than going unavailable: the coordinator poll succeeded and only the reading is missing, which matches how every other sensor here signals absent data. Logic lives in a new HA-free temperature.py so it can be unit-tested without the Home Assistant harness, following the keg_detection.py precedent. Refs: Falkvinge/hassio-integration-perfectdraft-pro#4 Co-authored-by: Cursor <cursoragent@cursor.com>
128 lines
3.7 KiB
Python
128 lines
3.7 KiB
Python
"""Unit tests for the pure temperature extraction logic.
|
|
|
|
Runs with the stdlib only (no Home Assistant): the logic under test lives
|
|
in ``temperature.py`` which has no Home Assistant imports.
|
|
"""
|
|
import os
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(
|
|
0,
|
|
os.path.join(os.path.dirname(__file__), "..", "custom_components", "perfectdraft"),
|
|
)
|
|
|
|
from temperature import ( # noqa: E402
|
|
TEMP_MAX_PLAUSIBLE,
|
|
TEMP_MIN_PLAUSIBLE,
|
|
beer_temperature,
|
|
)
|
|
|
|
# The sentinel an uninitialised probe reports: 0x80 as a signed 8-bit integer.
|
|
SENTINEL = -128
|
|
|
|
|
|
class TestBeerTemperature(unittest.TestCase):
|
|
def test_normal_reading_from_displayed_field(self):
|
|
self.assertEqual(
|
|
beer_temperature(
|
|
{"displayedBeerTemperatureInCelsius": 3, "temperature": 3}
|
|
),
|
|
3.0,
|
|
)
|
|
|
|
# --- The reported bug: -128 before the probe initialises ---
|
|
def test_sentinel_with_no_usable_fallback(self):
|
|
self.assertIsNone(
|
|
beer_temperature({"displayedBeerTemperatureInCelsius": SENTINEL})
|
|
)
|
|
|
|
def test_sentinel_falls_through_to_plausible_fallback(self):
|
|
self.assertEqual(
|
|
beer_temperature(
|
|
{"displayedBeerTemperatureInCelsius": SENTINEL, "temperature": 4}
|
|
),
|
|
4.0,
|
|
)
|
|
|
|
def test_sentinel_in_both_fields(self):
|
|
self.assertIsNone(
|
|
beer_temperature(
|
|
{
|
|
"displayedBeerTemperatureInCelsius": SENTINEL,
|
|
"temperature": SENTINEL,
|
|
}
|
|
)
|
|
)
|
|
|
|
def test_sentinel_in_fallback_field_only(self):
|
|
self.assertIsNone(beer_temperature({"temperature": SENTINEL}))
|
|
|
|
# --- Pre-existing behaviour that must not regress ---
|
|
def test_zero_on_displayed_field_falls_through(self):
|
|
self.assertEqual(
|
|
beer_temperature(
|
|
{"displayedBeerTemperatureInCelsius": 0, "temperature": 5}
|
|
),
|
|
5.0,
|
|
)
|
|
|
|
def test_fallback_used_when_displayed_absent(self):
|
|
self.assertEqual(beer_temperature({"temperature": 6}), 6.0)
|
|
|
|
def test_no_fields_present(self):
|
|
self.assertIsNone(beer_temperature({}))
|
|
|
|
def test_both_fields_null(self):
|
|
self.assertIsNone(
|
|
beer_temperature(
|
|
{"displayedBeerTemperatureInCelsius": None, "temperature": None}
|
|
)
|
|
)
|
|
|
|
# --- Defensive coercion ---
|
|
def test_non_numeric_value_does_not_raise(self):
|
|
self.assertIsNone(
|
|
beer_temperature({"displayedBeerTemperatureInCelsius": "n/a"})
|
|
)
|
|
|
|
def test_numeric_string_is_accepted(self):
|
|
self.assertEqual(
|
|
beer_temperature({"displayedBeerTemperatureInCelsius": "3.5"}), 3.5
|
|
)
|
|
|
|
# --- Range boundaries are inclusive ---
|
|
def test_lower_bound_accepted(self):
|
|
self.assertEqual(
|
|
beer_temperature(
|
|
{"displayedBeerTemperatureInCelsius": TEMP_MIN_PLAUSIBLE}
|
|
),
|
|
TEMP_MIN_PLAUSIBLE,
|
|
)
|
|
|
|
def test_upper_bound_accepted(self):
|
|
self.assertEqual(
|
|
beer_temperature(
|
|
{"displayedBeerTemperatureInCelsius": TEMP_MAX_PLAUSIBLE}
|
|
),
|
|
TEMP_MAX_PLAUSIBLE,
|
|
)
|
|
|
|
def test_just_below_lower_bound_rejected(self):
|
|
self.assertIsNone(
|
|
beer_temperature(
|
|
{"displayedBeerTemperatureInCelsius": TEMP_MIN_PLAUSIBLE - 0.1}
|
|
)
|
|
)
|
|
|
|
def test_just_above_upper_bound_rejected(self):
|
|
self.assertIsNone(
|
|
beer_temperature(
|
|
{"displayedBeerTemperatureInCelsius": TEMP_MAX_PLAUSIBLE + 0.1}
|
|
)
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|