Skip to content

Commit 3941c3f

Browse files
Copilotliudger
andcommitted
Fix pre-commit failures by addressing pylint and ruff issues
Co-authored-by: liudger <4112111+liudger@users.noreply.github.com>
1 parent 2508b14 commit 3941c3f

7 files changed

Lines changed: 96 additions & 86 deletions

tests/test_api_initialization.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for API data initialization error handling."""
2+
# pylint: disable=protected-access
23

3-
from unittest.mock import AsyncMock, MagicMock, patch
4+
from unittest.mock import patch
45

56
import pytest
67

@@ -15,7 +16,7 @@ async def test_initialize_api_data_no_api_version() -> None:
1516
"""Test initializing API data with no API version set."""
1617
config = BSBLANConfig(host="example.com")
1718
bsblan = BSBLAN(config)
18-
19+
1920
# API version is None by default
2021
with pytest.raises(BSBLANError, match=API_VERSION_ERROR_MSG):
2122
await bsblan._initialize_api_data()
@@ -26,22 +27,23 @@ async def test_initialize_api_data_unexpected_none() -> None:
2627
"""Test edge case where API data is still None after initialization."""
2728
config = BSBLANConfig(host="example.com")
2829
bsblan = BSBLAN(config)
29-
30+
3031
# Set API version but simulate data being None after initialization
3132
bsblan._api_version = "v3"
32-
33+
3334
# Mock API_VERSIONS to return None for this specific test
34-
with patch("bsblan.bsblan.API_VERSIONS", {"v3": None}):
35-
with pytest.raises(BSBLANError, match=API_DATA_NOT_INITIALIZED_ERROR_MSG):
36-
await bsblan._initialize_api_data()
35+
with patch("bsblan.bsblan.API_VERSIONS", {"v3": None}), pytest.raises(
36+
BSBLANError, match=API_DATA_NOT_INITIALIZED_ERROR_MSG
37+
):
38+
await bsblan._initialize_api_data()
3739

3840

3941
@pytest.mark.asyncio
4042
async def test_request_no_session() -> None:
4143
"""Test request method with no session initialized."""
4244
config = BSBLANConfig(host="example.com")
4345
bsblan = BSBLAN(config)
44-
46+
4547
# Session is None by default
4648
with pytest.raises(BSBLANError, match="Session not initialized"):
47-
await bsblan._request()
49+
await bsblan._request()

tests/test_context_manager.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
"""Tests for BSBLAN context manager features."""
2+
# pylint: disable=protected-access
23

3-
import asyncio
44
from unittest.mock import AsyncMock, patch
55

6-
import aiohttp
76
import pytest
87

98
from bsblan import BSBLAN
109
from bsblan.bsblan import BSBLANConfig
11-
from bsblan.exceptions import BSBLANError
1210

1311

1412
@pytest.mark.asyncio
1513
async def test_context_manager_session_creation() -> None:
1614
"""Test that context manager creates and closes a session."""
1715
config = BSBLANConfig(host="example.com")
18-
16+
1917
# Mock the initialize method to avoid actual API calls
2018
with patch.object(BSBLAN, "initialize", AsyncMock()) as mock_init:
2119
async with BSBLAN(config) as bsblan:
2220
# Check that session was created
2321
assert bsblan.session is not None
2422
assert bsblan._close_session is True
25-
23+
2624
# Check that initialize was called
2725
mock_init.assert_called_once()
28-
26+
2927
# After context exit, session.close should have been called
3028
# This is implicit since we're testing the context manager behavior
3129

@@ -34,21 +32,21 @@ async def test_context_manager_session_creation() -> None:
3432
async def test_context_manager_with_existing_session() -> None:
3533
"""Test that context manager uses an existing session if provided."""
3634
config = BSBLANConfig(host="example.com")
37-
35+
3836
# Create a mock session
3937
session = AsyncMock()
4038
session.closed = False
41-
39+
4240
# Mock initialize to avoid actual API calls
4341
with patch.object(BSBLAN, "initialize", AsyncMock()) as mock_init:
4442
async with BSBLAN(config, session=session) as bsblan:
45-
# Check that our session was used
43+
# Check that our session was used
4644
assert bsblan.session is session
4745
assert bsblan._close_session is False
48-
46+
4947
# Check that initialize was called
5048
mock_init.assert_called_once()
51-
49+
5250
# After context exit, session.close should not have been called
5351
session.close.assert_not_called()
5452

@@ -57,18 +55,18 @@ async def test_context_manager_with_existing_session() -> None:
5755
async def test_aexit_exception_handling() -> None:
5856
"""Test that BSBLAN doesn't swallow exceptions during session close."""
5957
config = BSBLANConfig(host="example.com")
60-
58+
6159
# Create a mock session with a close method that raises an exception
6260
mock_session = AsyncMock()
6361
mock_session.close = AsyncMock(side_effect=Exception("Test exception"))
64-
62+
6563
# Patch initialize to avoid actual API calls
6664
with patch.object(BSBLAN, "initialize", AsyncMock()):
6765
# Create BSBLAN instance with our mock session
6866
bsblan = BSBLAN(config, session=mock_session)
6967
bsblan._close_session = True # Force close on exit
70-
68+
7169
# The exception from session.close should be propagated
7270
with pytest.raises(Exception, match="Test exception"):
7371
async with bsblan:
74-
pass # Just enter and exit the context
72+
pass # Just enter and exit the context

tests/test_entity_info.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_entity_info_invalid_time_conversion() -> None:
1818
desc="",
1919
data_type=DataType.TIME,
2020
)
21-
21+
2222
# The value should remain as string since conversion failed
2323
assert entity.value == "24:61"
2424

@@ -33,7 +33,7 @@ def test_entity_info_invalid_weekday_conversion() -> None:
3333
desc="",
3434
data_type=DataType.WEEKDAY,
3535
)
36-
36+
3737
# The value should remain as string since conversion failed
3838
assert entity.value == "not-a-number"
3939

@@ -49,7 +49,7 @@ def test_entity_info_general_conversion_error(caplog: pytest.LogCaptureFixture)
4949
desc="",
5050
data_type=DataType.PLAIN_NUMBER,
5151
)
52-
52+
5353
# The original value should be preserved
5454
assert isinstance(entity.value, object)
5555
assert "Failed to convert value" in caplog.text
@@ -65,7 +65,7 @@ def test_entity_info_valid_time_conversion() -> None:
6565
desc="",
6666
data_type=DataType.TIME,
6767
)
68-
68+
6969
# The value should be converted to a time object
7070
assert isinstance(entity.value, time)
7171
assert entity.value.hour == 14
@@ -82,10 +82,10 @@ def test_entity_info_enum_description() -> None:
8282
desc="Enum Description",
8383
data_type=DataType.ENUM,
8484
)
85-
85+
8686
# The enum_description should return the desc field
8787
assert enum_entity.enum_description == "Enum Description"
88-
88+
8989
# Create EntityInfo with non-ENUM data type
9090
non_enum_entity = EntityInfo(
9191
name="Test Value",
@@ -94,6 +94,6 @@ def test_entity_info_enum_description() -> None:
9494
desc="Not an Enum",
9595
data_type=DataType.PLAIN_NUMBER,
9696
)
97-
97+
9898
# The enum_description should return None
99-
assert non_enum_entity.enum_description is None
99+
assert non_enum_entity.enum_description is None

tests/test_reset_validation.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Tests for APIValidator reset_validation method."""
22

3-
import pytest
4-
53
from bsblan.utility import APIValidator
64

75

@@ -12,18 +10,18 @@ def test_reset_validation_specific_section() -> None:
1210
"heating": {"param1": "value1"},
1311
"sensor": {"param2": "value2"},
1412
}
15-
13+
1614
# Initialize APIValidator with some validated sections
1715
validator = APIValidator(api_config)
1816
validator.validated_sections.update({"heating", "sensor"})
19-
17+
2018
# Verify initial state
2119
assert validator.is_section_validated("heating") is True
2220
assert validator.is_section_validated("sensor") is True
23-
21+
2422
# Reset validation for just the heating section
2523
validator.reset_validation("heating")
26-
24+
2725
# Verify only heating was reset
2826
assert validator.is_section_validated("heating") is False
2927
assert validator.is_section_validated("sensor") is True
@@ -36,18 +34,18 @@ def test_reset_validation_nonexistent_section() -> None:
3634
"heating": {"param1": "value1"},
3735
"sensor": {"param2": "value2"},
3836
}
39-
37+
4038
# Initialize APIValidator with some validated sections
4139
validator = APIValidator(api_config)
4240
validator.validated_sections.add("heating")
43-
41+
4442
# Verify initial state
4543
assert validator.is_section_validated("heating") is True
4644
assert validator.is_section_validated("nonexistent") is False
47-
45+
4846
# Reset validation for a nonexistent section (should not error)
4947
validator.reset_validation("nonexistent")
50-
48+
5149
# Verify state remains unchanged
5250
assert validator.is_section_validated("heating") is True
53-
assert validator.is_section_validated("nonexistent") is False
51+
assert validator.is_section_validated("nonexistent") is False

tests/test_temperature_unit.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Tests for temperature unit handling in BSBLAN."""
2+
# pylint: disable=protected-access
23

34
from unittest.mock import AsyncMock, patch
45

@@ -14,10 +15,10 @@ async def test_temperature_unit_getter() -> None:
1415
"""Test the get_temperature_unit property."""
1516
config = BSBLANConfig(host="example.com")
1617
bsblan = BSBLAN(config)
17-
18+
1819
# Test default unit
1920
assert bsblan.get_temperature_unit == "°C"
20-
21+
2122
# Test with custom unit set
2223
bsblan._temperature_unit = "°F"
2324
assert bsblan.get_temperature_unit == "°F"
@@ -28,16 +29,16 @@ async def test_initialize_temperature_range_celsius() -> None:
2829
"""Test initialization of temperature range with Celsius unit."""
2930
config = BSBLANConfig(host="example.com")
3031
bsblan = BSBLAN(config)
31-
32+
3233
# Create mock static values with Celsius unit
3334
min_temp = EntityInfo(name="Min Temp", value="10", unit="°C", desc="", data_type=0)
3435
max_temp = EntityInfo(name="Max Temp", value="30", unit="°C", desc="", data_type=0)
3536
static_values = StaticState(min_temp=min_temp, max_temp=max_temp)
36-
37+
3738
# Mock static_values method to return our test data
3839
with patch.object(bsblan, "static_values", AsyncMock(return_value=static_values)):
3940
await bsblan._initialize_temperature_range()
40-
41+
4142
# Verify temperature range was set correctly
4243
assert bsblan._min_temp == 10.0
4344
assert bsblan._max_temp == 30.0
@@ -50,16 +51,16 @@ async def test_initialize_temperature_range_fahrenheit() -> None:
5051
"""Test initialization of temperature range with Fahrenheit unit."""
5152
config = BSBLANConfig(host="example.com")
5253
bsblan = BSBLAN(config)
53-
54+
5455
# Create mock static values with Fahrenheit unit
5556
min_temp = EntityInfo(name="Min Temp", value="50", unit="°F", desc="", data_type=0)
5657
max_temp = EntityInfo(name="Max Temp", value="86", unit="°F", desc="", data_type=0)
5758
static_values = StaticState(min_temp=min_temp, max_temp=max_temp)
58-
59+
5960
# Mock static_values method to return our test data
6061
with patch.object(bsblan, "static_values", AsyncMock(return_value=static_values)):
6162
await bsblan._initialize_temperature_range()
62-
63+
6364
# Verify temperature range was set correctly
6465
assert bsblan._min_temp == 50.0
6566
assert bsblan._max_temp == 86.0
@@ -69,21 +70,28 @@ async def test_initialize_temperature_range_fahrenheit() -> None:
6970

7071
@pytest.mark.asyncio
7172
async def test_initialize_temperature_range_alternate_celsius_format() -> None:
72-
"""Test initialization of temperature range with alternate Celsius format (&deg;C)."""
73+
"""Test initialization of temperature range with alternate Celsius format.
74+
75+
Tests with HTML entity format (&deg;C) instead of unicode character.
76+
"""
7377
config = BSBLANConfig(host="example.com")
7478
bsblan = BSBLAN(config)
75-
79+
7680
# Create mock static values with HTML degree symbol
77-
min_temp = EntityInfo(name="Min Temp", value="10", unit="&deg;C", desc="", data_type=0)
78-
max_temp = EntityInfo(name="Max Temp", value="30", unit="&deg;C", desc="", data_type=0)
81+
min_temp = EntityInfo(
82+
name="Min Temp", value="10", unit="&deg;C", desc="", data_type=0
83+
)
84+
max_temp = EntityInfo(
85+
name="Max Temp", value="30", unit="&deg;C", desc="", data_type=0
86+
)
7987
static_values = StaticState(min_temp=min_temp, max_temp=max_temp)
80-
88+
8189
# Mock static_values method to return our test data
8290
with patch.object(bsblan, "static_values", AsyncMock(return_value=static_values)):
8391
await bsblan._initialize_temperature_range()
84-
92+
8593
# Verify temperature range was set correctly
8694
assert bsblan._min_temp == 10.0
8795
assert bsblan._max_temp == 30.0
8896
assert bsblan._temperature_range_initialized is True
89-
assert bsblan._temperature_unit == "°C"
97+
assert bsblan._temperature_unit == "°C"

0 commit comments

Comments
 (0)