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 __future__ import annotations
from contextlib import contextmanager, nullcontext
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import Any from typing import Any
from qnapstats import QNAPStats from qnapstats import QNAPStats
import urllib3
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -28,6 +30,17 @@ UPDATE_INTERVAL = timedelta(minutes=1)
_LOGGER = logging.getLogger(__name__) _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]]]): class QnapCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]):
"""Custom coordinator for the qnap integration.""" """Custom coordinator for the qnap integration."""
@ -42,17 +55,24 @@ class QnapCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]):
) )
protocol = "https" if config_entry.data[CONF_SSL] else "http" protocol = "https" if config_entry.data[CONF_SSL] else "http"
self._verify_ssl = config_entry.data.get(CONF_VERIFY_SSL)
self._api = QNAPStats( self._api = QNAPStats(
f"{protocol}://{config_entry.data.get(CONF_HOST)}", f"{protocol}://{config_entry.data.get(CONF_HOST)}",
config_entry.data.get(CONF_PORT), config_entry.data.get(CONF_PORT),
config_entry.data.get(CONF_USERNAME), config_entry.data.get(CONF_USERNAME),
config_entry.data.get(CONF_PASSWORD), 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), timeout=config_entry.data.get(CONF_TIMEOUT),
) )
def _sync_update(self) -> dict[str, dict[str, Any]]: def _sync_update(self) -> dict[str, dict[str, Any]]:
"""Get the latest data from the Qnap API.""" """Get the latest data from the Qnap API."""
with (
suppress_insecure_request_warning()
if not self._verify_ssl
else nullcontext()
):
return { return {
"system_stats": self._api.get_system_stats(), "system_stats": self._api.get_system_stats(),
"system_health": self._api.get_system_health(), "system_health": self._api.get_system_health(),