Fix spamming log message in QNAP (#141752)

This commit is contained in:
Franck Nijhof 2025-03-29 21:57:43 +01:00 committed by GitHub
parent 83f4f4cc96
commit 4398af51c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,11 +2,13 @@
from __future__ import annotations
from contextlib import contextmanager, nullcontext
from datetime import timedelta
import logging
from typing import Any
from qnapstats import QNAPStats
import urllib3
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -28,6 +30,17 @@ UPDATE_INTERVAL = timedelta(minutes=1)
_LOGGER = logging.getLogger(__name__)
@contextmanager
def suppress_insecure_request_warning():
"""Context manager to suppress InsecureRequestWarning.
Was added in here to solve the following issue, not being solved upstream.
https://github.com/colinodell/python-qnapstats/issues/96
"""
with urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning):
yield
class QnapCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]):
"""Custom coordinator for the qnap integration."""
@ -42,24 +55,31 @@ class QnapCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]):
)
protocol = "https" if config_entry.data[CONF_SSL] else "http"
self._verify_ssl = config_entry.data.get(CONF_VERIFY_SSL)
self._api = QNAPStats(
f"{protocol}://{config_entry.data.get(CONF_HOST)}",
config_entry.data.get(CONF_PORT),
config_entry.data.get(CONF_USERNAME),
config_entry.data.get(CONF_PASSWORD),
verify_ssl=config_entry.data.get(CONF_VERIFY_SSL),
verify_ssl=self._verify_ssl,
timeout=config_entry.data.get(CONF_TIMEOUT),
)
def _sync_update(self) -> dict[str, dict[str, Any]]:
"""Get the latest data from the Qnap API."""
return {
"system_stats": self._api.get_system_stats(),
"system_health": self._api.get_system_health(),
"smart_drive_health": self._api.get_smart_disk_health(),
"volumes": self._api.get_volumes(),
"bandwidth": self._api.get_bandwidth(),
}
with (
suppress_insecure_request_warning()
if not self._verify_ssl
else nullcontext()
):
return {
"system_stats": self._api.get_system_stats(),
"system_health": self._api.get_system_health(),
"smart_drive_health": self._api.get_smart_disk_health(),
"volumes": self._api.get_volumes(),
"bandwidth": self._api.get_bandwidth(),
}
async def _async_update_data(self) -> dict[str, dict[str, Any]]:
"""Get the latest data from the Qnap API."""