Skip to content

Commit

Permalink
Merge pull request #970 from liudger/add-public-initialize
Browse files Browse the repository at this point in the history
fix: refactor BSBLAN initialization
  • Loading branch information
liudger authored Oct 8, 2024
2 parents 9bd5b38 + 0733d36 commit 4c96d60
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/bsblan/bsblan.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,28 @@ class BSBLAN:
_max_temp: float | None = None
_temperature_range_initialized: bool = False
_api_data: APIConfig | None = None

def __post_init__(self) -> None:
"""Initialize the session if not provided."""
if self.session is None:
self.session = aiohttp.ClientSession()
self._close_session = True
_initialized: bool = False

async def __aenter__(self) -> Self:
"""Enter the context manager."""
if self.session is None:
self.session = aiohttp.ClientSession()
self._close_session = True
await self._initialize()
await self.initialize()
return self

async def __aexit__(self, *args: object) -> None:
"""Exit the context manager."""
if self._close_session and self.session:
await self.session.close()

async def _initialize(self) -> None:
async def initialize(self) -> None:
"""Initialize the BSBLAN client."""
await self._fetch_firmware_version()
await self._initialize_temperature_range()
await self._initialize_api_data()
if not self._initialized:
await self._fetch_firmware_version()
await self._initialize_temperature_range()
await self._initialize_api_data()
self._initialized = True

async def _fetch_firmware_version(self) -> None:
"""Fetch the firmware version if not already available."""
Expand Down

0 comments on commit 4c96d60

Please sign in to comment.