refactor(mikrotik_router): use existing session for fetching release notes

Optimized the process of fetching release notes in the Mikrotik Router integration by utilizing the existing HTTP client session from Home Assistant's aiohttp_client. This change avoids creating a new session for each request, leading to more efficient resource usage and potentially reducing the likelihood of encountering issues related to session management.

By leveraging `async_get_clientsession`, the update component now aligns better with Home Assistant's recommended practices for external HTTP requests, ensuring consistency and reliability in how network calls are made within integrations.
This commit is contained in:
Sergey Krashevich 2024-04-12 16:13:54 +03:00
parent ccc097c438
commit 2aa2191334
No known key found for this signature in database
GPG key ID: 625171324E7D3856

View file

@ -9,7 +9,7 @@ from typing import Any
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.components.update import ( from homeassistant.components.update import (
UpdateEntity, UpdateEntity,
@ -92,7 +92,7 @@ class MikrotikRouterOSUpdate(MikrotikEntity, UpdateEntity):
async def async_release_notes(self) -> str: async def async_release_notes(self) -> str:
"""Return the release notes.""" """Return the release notes."""
try: try:
async with async_create_clientsession(self.hass) as session: session = async_get_clientsession(self.hass)
async with session.get( async with session.get(
f"https://cdn.mikrotik.com/routeros/{self._data['latest-version']}/CHANGELOG" f"https://cdn.mikrotik.com/routeros/{self._data['latest-version']}/CHANGELOG"
) as response: ) as response: