|
| 1 | +"""Tests for temperature unit handling in BSBLAN.""" |
| 2 | +# pylint: disable=protected-access |
| 3 | + |
| 4 | +from unittest.mock import AsyncMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from bsblan import BSBLAN |
| 9 | +from bsblan.bsblan import BSBLANConfig |
| 10 | +from bsblan.models import EntityInfo, StaticState |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.asyncio |
| 14 | +async def test_temperature_unit_getter() -> None: |
| 15 | + """Test the get_temperature_unit property.""" |
| 16 | + config = BSBLANConfig(host="example.com") |
| 17 | + bsblan = BSBLAN(config) |
| 18 | + |
| 19 | + # Test default unit |
| 20 | + assert bsblan.get_temperature_unit == "°C" |
| 21 | + |
| 22 | + # Test with custom unit set |
| 23 | + bsblan._temperature_unit = "°F" |
| 24 | + assert bsblan.get_temperature_unit == "°F" |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.asyncio |
| 28 | +async def test_initialize_temperature_range_celsius() -> None: |
| 29 | + """Test initialization of temperature range with Celsius unit.""" |
| 30 | + config = BSBLANConfig(host="example.com") |
| 31 | + bsblan = BSBLAN(config) |
| 32 | + |
| 33 | + # Create mock static values with Celsius unit |
| 34 | + min_temp = EntityInfo(name="Min Temp", value="10", unit="°C", desc="", data_type=0) |
| 35 | + max_temp = EntityInfo(name="Max Temp", value="30", unit="°C", desc="", data_type=0) |
| 36 | + static_values = StaticState(min_temp=min_temp, max_temp=max_temp) |
| 37 | + |
| 38 | + # Mock static_values method to return our test data |
| 39 | + with patch.object(bsblan, "static_values", AsyncMock(return_value=static_values)): |
| 40 | + await bsblan._initialize_temperature_range() |
| 41 | + |
| 42 | + # Verify temperature range was set correctly |
| 43 | + assert bsblan._min_temp == 10.0 |
| 44 | + assert bsblan._max_temp == 30.0 |
| 45 | + assert bsblan._temperature_range_initialized is True |
| 46 | + assert bsblan._temperature_unit == "°C" |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.asyncio |
| 50 | +async def test_initialize_temperature_range_fahrenheit() -> None: |
| 51 | + """Test initialization of temperature range with Fahrenheit unit.""" |
| 52 | + config = BSBLANConfig(host="example.com") |
| 53 | + bsblan = BSBLAN(config) |
| 54 | + |
| 55 | + # Create mock static values with Fahrenheit unit |
| 56 | + min_temp = EntityInfo(name="Min Temp", value="50", unit="°F", desc="", data_type=0) |
| 57 | + max_temp = EntityInfo(name="Max Temp", value="86", unit="°F", desc="", data_type=0) |
| 58 | + static_values = StaticState(min_temp=min_temp, max_temp=max_temp) |
| 59 | + |
| 60 | + # Mock static_values method to return our test data |
| 61 | + with patch.object(bsblan, "static_values", AsyncMock(return_value=static_values)): |
| 62 | + await bsblan._initialize_temperature_range() |
| 63 | + |
| 64 | + # Verify temperature range was set correctly |
| 65 | + assert bsblan._min_temp == 50.0 |
| 66 | + assert bsblan._max_temp == 86.0 |
| 67 | + assert bsblan._temperature_range_initialized is True |
| 68 | + assert bsblan._temperature_unit == "°F" |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.asyncio |
| 72 | +async def test_initialize_temperature_range_alternate_celsius_format() -> None: |
| 73 | + """Test initialization of temperature range with alternate Celsius format. |
| 74 | +
|
| 75 | + Tests with HTML entity format (°C) instead of unicode character. |
| 76 | + """ |
| 77 | + config = BSBLANConfig(host="example.com") |
| 78 | + bsblan = BSBLAN(config) |
| 79 | + |
| 80 | + # Create mock static values with HTML degree symbol |
| 81 | + min_temp = EntityInfo( |
| 82 | + name="Min Temp", value="10", unit="°C", desc="", data_type=0 |
| 83 | + ) |
| 84 | + max_temp = EntityInfo( |
| 85 | + name="Max Temp", value="30", unit="°C", desc="", data_type=0 |
| 86 | + ) |
| 87 | + static_values = StaticState(min_temp=min_temp, max_temp=max_temp) |
| 88 | + |
| 89 | + # Mock static_values method to return our test data |
| 90 | + with patch.object(bsblan, "static_values", AsyncMock(return_value=static_values)): |
| 91 | + await bsblan._initialize_temperature_range() |
| 92 | + |
| 93 | + # Verify temperature range was set correctly |
| 94 | + assert bsblan._min_temp == 10.0 |
| 95 | + assert bsblan._max_temp == 30.0 |
| 96 | + assert bsblan._temperature_range_initialized is True |
| 97 | + assert bsblan._temperature_unit == "°C" |
0 commit comments