From 30d604c8510bae333088155122621aa28b6af32e Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Sat, 16 Sep 2023 13:46:11 +0200 Subject: [PATCH] Use central logger in Withings (#100406) --- homeassistant/components/withings/__init__.py | 9 ++++--- homeassistant/components/withings/api.py | 8 +++---- homeassistant/components/withings/common.py | 24 +++++++++---------- homeassistant/components/withings/const.py | 3 +++ 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/withings/__init__.py b/homeassistant/components/withings/__init__.py index 589bfe79094..5e733708639 100644 --- a/homeassistant/components/withings/__init__.py +++ b/homeassistant/components/withings/__init__.py @@ -34,13 +34,12 @@ from homeassistant.helpers.typing import ConfigType from . import const from .common import ( - _LOGGER, async_get_data_manager, async_remove_data_manager, get_data_manager_by_webhook_id, json_message_response, ) -from .const import CONF_USE_WEBHOOK, CONFIG +from .const import CONF_USE_WEBHOOK, CONFIG, LOGGER DOMAIN = const.DOMAIN PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR] @@ -92,7 +91,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: conf[CONF_CLIENT_SECRET], ), ) - _LOGGER.warning( + LOGGER.warning( "Configuration of Withings integration OAuth2 credentials in YAML " "is deprecated and will be removed in a future release; Your " "existing OAuth Application Credentials have been imported into " @@ -125,7 +124,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: data_manager = await async_get_data_manager(hass, entry) - _LOGGER.debug("Confirming %s is authenticated to withings", entry.title) + LOGGER.debug("Confirming %s is authenticated to withings", entry.title) await data_manager.poll_data_update_coordinator.async_config_entry_first_refresh() webhook.async_register( @@ -205,7 +204,7 @@ async def async_webhook_handler( data_manager = get_data_manager_by_webhook_id(hass, webhook_id) if not data_manager: - _LOGGER.error( + LOGGER.error( ( "Webhook id %s not handled by data manager. This is a bug and should be" " reported" diff --git a/homeassistant/components/withings/api.py b/homeassistant/components/withings/api.py index fff9767ebda..3a81fb298ea 100644 --- a/homeassistant/components/withings/api.py +++ b/homeassistant/components/withings/api.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio from collections.abc import Iterable -import logging from typing import Any import arrow @@ -26,9 +25,8 @@ from homeassistant.helpers.config_entry_oauth2_flow import ( OAuth2Session, ) -from .const import LOG_NAMESPACE +from .const import LOGGER -_LOGGER = logging.getLogger(LOG_NAMESPACE) _RETRY_COEFFICIENT = 0.5 @@ -73,11 +71,11 @@ class ConfigEntryWithingsApi(AbstractWithingsApi): """ exception = None for attempt in range(1, attempts + 1): - _LOGGER.debug("Attempt %s of %s", attempt, attempts) + LOGGER.debug("Attempt %s of %s", attempt, attempts) try: return await func() except Exception as exception1: # pylint: disable=broad-except - _LOGGER.debug( + LOGGER.debug( "Failed attempt %s of %s (%s)", attempt, attempts, exception1 ) # Make each backoff pause a little bit longer diff --git a/homeassistant/components/withings/common.py b/homeassistant/components/withings/common.py index 446fb4b58e5..5f0090ad9a6 100644 --- a/homeassistant/components/withings/common.py +++ b/homeassistant/components/withings/common.py @@ -8,7 +8,6 @@ import datetime from datetime import timedelta from enum import IntEnum, StrEnum from http import HTTPStatus -import logging import re from typing import Any @@ -35,9 +34,8 @@ from homeassistant.util import dt as dt_util from . import const from .api import ConfigEntryWithingsApi -from .const import Measurement +from .const import LOGGER, Measurement -_LOGGER = logging.getLogger(const.LOG_NAMESPACE) NOT_AUTHENTICATED_ERROR = re.compile( f"^{HTTPStatus.UNAUTHORIZED},.*", re.IGNORECASE, @@ -181,7 +179,7 @@ class DataManager: self.subscription_update_coordinator = DataUpdateCoordinator( hass, - _LOGGER, + LOGGER, name="subscription_update_coordinator", update_interval=timedelta(minutes=120), update_method=self.async_subscribe_webhook, @@ -190,7 +188,7 @@ class DataManager: dict[MeasureType, Any] | None ]( hass, - _LOGGER, + LOGGER, name="poll_data_update_coordinator", update_interval=timedelta(minutes=120) if self._webhook_config.enabled @@ -232,14 +230,14 @@ class DataManager: async def async_subscribe_webhook(self) -> None: """Subscribe the webhook to withings data updates.""" - _LOGGER.debug("Configuring withings webhook") + LOGGER.debug("Configuring withings webhook") # On first startup, perform a fresh re-subscribe. Withings stops pushing data # if the webhook fails enough times but they don't remove the old subscription # config. This ensures the subscription is setup correctly and they start # pushing again. if self._subscribe_webhook_run_count == 0: - _LOGGER.debug("Refreshing withings webhook configs") + LOGGER.debug("Refreshing withings webhook configs") await self.async_unsubscribe_webhook() self._subscribe_webhook_run_count += 1 @@ -262,7 +260,7 @@ class DataManager: # Subscribe to each one. for appli in to_add_applis: - _LOGGER.debug( + LOGGER.debug( "Subscribing %s for %s in %s seconds", self._webhook_config.url, appli, @@ -280,7 +278,7 @@ class DataManager: # Revoke subscriptions. for profile in response.profiles: - _LOGGER.debug( + LOGGER.debug( "Unsubscribing %s for %s in %s seconds", profile.callbackurl, profile.appli, @@ -310,7 +308,7 @@ class DataManager: async def async_get_measures(self) -> dict[Measurement, Any]: """Get the measures data.""" - _LOGGER.debug("Updating withings measures") + LOGGER.debug("Updating withings measures") now = dt_util.utcnow() startdate = now - datetime.timedelta(days=7) @@ -338,7 +336,7 @@ class DataManager: async def async_get_sleep_summary(self) -> dict[Measurement, Any]: """Get the sleep summary data.""" - _LOGGER.debug("Updating withing sleep summary") + LOGGER.debug("Updating withing sleep summary") now = dt_util.now() yesterday = now - datetime.timedelta(days=1) yesterday_noon = dt_util.start_of_local_day(yesterday) + datetime.timedelta( @@ -419,7 +417,7 @@ class DataManager: async def async_webhook_data_updated(self, data_category: NotifyAppli) -> None: """Handle scenario when data is updated from a webook.""" - _LOGGER.debug("Withings webhook triggered") + LOGGER.debug("Withings webhook triggered") if data_category in { NotifyAppli.WEIGHT, NotifyAppli.CIRCULATORY, @@ -442,7 +440,7 @@ async def async_get_data_manager( config_entry_data = hass.data[const.DOMAIN][config_entry.entry_id] if const.DATA_MANAGER not in config_entry_data: - _LOGGER.debug( + LOGGER.debug( "Creating withings data manager for profile: %s", config_entry.title ) config_entry_data[const.DATA_MANAGER] = DataManager( diff --git a/homeassistant/components/withings/const.py b/homeassistant/components/withings/const.py index 926d29abe5c..545c7bfcb26 100644 --- a/homeassistant/components/withings/const.py +++ b/homeassistant/components/withings/const.py @@ -1,5 +1,6 @@ """Constants used by the Withings component.""" from enum import StrEnum +import logging DEFAULT_TITLE = "Withings" CONF_PROFILES = "profiles" @@ -13,6 +14,8 @@ LOG_NAMESPACE = "homeassistant.components.withings" PROFILE = "profile" PUSH_HANDLER = "push_handler" +LOGGER = logging.getLogger(__package__) + class Measurement(StrEnum): """Measurement supported by the withings integration."""