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.core import HomeAssistant
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 (
UpdateEntity,
@ -92,18 +92,18 @@ class MikrotikRouterOSUpdate(MikrotikEntity, UpdateEntity):
async def async_release_notes(self) -> str:
"""Return the release notes."""
try:
async with async_create_clientsession(self.hass) as session:
async with session.get(
f"https://cdn.mikrotik.com/routeros/{self._data['latest-version']}/CHANGELOG"
) as response:
if response.status == 200:
text = await response.text()
return text.replace("*) ", "- ")
else:
_LOGGER.warning(
"Failed to fetch release notes due to a network error."
)
return "Failed to fetch release notes due to a network error."
session = async_get_clientsession(self.hass)
async with session.get(
f"https://cdn.mikrotik.com/routeros/{self._data['latest-version']}/CHANGELOG"
) as response:
if response.status == 200:
text = await response.text()
return text.replace("*) ", "- ")
else:
_LOGGER.warning(
"Failed to fetch release notes due to a network error."
)
return "Failed to fetch release notes due to a network error."
except Exception as e:
_LOGGER.warning("Failed to download release notes (%s)", e)