mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-08-04 09:54:31 +02:00
feat(mikrotik_router): switch to aiohttp for async release notes fetching
This commit replaces the synchronous requests library with aiohttp for asynchronous fetching of Mikrotik RouterOS update release notes. It introduces the use of Home Assistant's built-in `async_create_clientsession` to manage HTTP sessions, enhancing the integration's performance and reliability by leveraging asyncio for network operations. The change aims to improve the responsiveness and efficiency of the Mikrotik Router integration within Home Assistant, particularly in retrieving release notes for updates. By moving to an asynchronous model, the integration can now fetch data without blocking the execution of other tasks, leading to a smoother user experience. Additionally, the error handling has been updated to provide clearer messages in the log when the release notes cannot be fetched, either due to a network error or an exception. This improvement aids in troubleshooting and ensures that users are better informed about the status of their update checks.
This commit is contained in:
parent
8e8ff310c1
commit
ccc097c438
1 changed files with 15 additions and 10 deletions
|
@ -3,12 +3,13 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from logging import getLogger
|
||||
from requests import get as requests_get
|
||||
import aiohttp
|
||||
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.components.update import (
|
||||
UpdateEntity,
|
||||
|
@ -91,18 +92,22 @@ class MikrotikRouterOSUpdate(MikrotikEntity, UpdateEntity):
|
|||
async def async_release_notes(self) -> str:
|
||||
"""Return the release notes."""
|
||||
try:
|
||||
response = await self.coordinator.hass.async_add_executor_job(
|
||||
requests_get,
|
||||
f"https://cdn.mikrotik.com/routeros/{self._data['latest-version']}/CHANGELOG",
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
return response.text.replace(chr(10), "<br />").replace("*) ", "- ")
|
||||
|
||||
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."
|
||||
except Exception as e:
|
||||
_LOGGER.warning("Failed to download release notes (%s)", e)
|
||||
|
||||
return "Failed to download release notes"
|
||||
return "Error fetching release notes."
|
||||
|
||||
@property
|
||||
def release_url(self) -> str:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue