mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Use central logger in Withings (#100406)
This commit is contained in:
parent
16cc87bf45
commit
30d604c851
@ -34,13 +34,12 @@ from homeassistant.helpers.typing import ConfigType
|
|||||||
|
|
||||||
from . import const
|
from . import const
|
||||||
from .common import (
|
from .common import (
|
||||||
_LOGGER,
|
|
||||||
async_get_data_manager,
|
async_get_data_manager,
|
||||||
async_remove_data_manager,
|
async_remove_data_manager,
|
||||||
get_data_manager_by_webhook_id,
|
get_data_manager_by_webhook_id,
|
||||||
json_message_response,
|
json_message_response,
|
||||||
)
|
)
|
||||||
from .const import CONF_USE_WEBHOOK, CONFIG
|
from .const import CONF_USE_WEBHOOK, CONFIG, LOGGER
|
||||||
|
|
||||||
DOMAIN = const.DOMAIN
|
DOMAIN = const.DOMAIN
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||||
@ -92,7 +91,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
conf[CONF_CLIENT_SECRET],
|
conf[CONF_CLIENT_SECRET],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
_LOGGER.warning(
|
LOGGER.warning(
|
||||||
"Configuration of Withings integration OAuth2 credentials in YAML "
|
"Configuration of Withings integration OAuth2 credentials in YAML "
|
||||||
"is deprecated and will be removed in a future release; Your "
|
"is deprecated and will be removed in a future release; Your "
|
||||||
"existing OAuth Application Credentials have been imported into "
|
"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)
|
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()
|
await data_manager.poll_data_update_coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
webhook.async_register(
|
webhook.async_register(
|
||||||
@ -205,7 +204,7 @@ async def async_webhook_handler(
|
|||||||
|
|
||||||
data_manager = get_data_manager_by_webhook_id(hass, webhook_id)
|
data_manager = get_data_manager_by_webhook_id(hass, webhook_id)
|
||||||
if not data_manager:
|
if not data_manager:
|
||||||
_LOGGER.error(
|
LOGGER.error(
|
||||||
(
|
(
|
||||||
"Webhook id %s not handled by data manager. This is a bug and should be"
|
"Webhook id %s not handled by data manager. This is a bug and should be"
|
||||||
" reported"
|
" reported"
|
||||||
|
@ -3,7 +3,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
import logging
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import arrow
|
import arrow
|
||||||
@ -26,9 +25,8 @@ from homeassistant.helpers.config_entry_oauth2_flow import (
|
|||||||
OAuth2Session,
|
OAuth2Session,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .const import LOG_NAMESPACE
|
from .const import LOGGER
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(LOG_NAMESPACE)
|
|
||||||
_RETRY_COEFFICIENT = 0.5
|
_RETRY_COEFFICIENT = 0.5
|
||||||
|
|
||||||
|
|
||||||
@ -73,11 +71,11 @@ class ConfigEntryWithingsApi(AbstractWithingsApi):
|
|||||||
"""
|
"""
|
||||||
exception = None
|
exception = None
|
||||||
for attempt in range(1, attempts + 1):
|
for attempt in range(1, attempts + 1):
|
||||||
_LOGGER.debug("Attempt %s of %s", attempt, attempts)
|
LOGGER.debug("Attempt %s of %s", attempt, attempts)
|
||||||
try:
|
try:
|
||||||
return await func()
|
return await func()
|
||||||
except Exception as exception1: # pylint: disable=broad-except
|
except Exception as exception1: # pylint: disable=broad-except
|
||||||
_LOGGER.debug(
|
LOGGER.debug(
|
||||||
"Failed attempt %s of %s (%s)", attempt, attempts, exception1
|
"Failed attempt %s of %s (%s)", attempt, attempts, exception1
|
||||||
)
|
)
|
||||||
# Make each backoff pause a little bit longer
|
# Make each backoff pause a little bit longer
|
||||||
|
@ -8,7 +8,6 @@ import datetime
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from enum import IntEnum, StrEnum
|
from enum import IntEnum, StrEnum
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
import logging
|
|
||||||
import re
|
import re
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -35,9 +34,8 @@ from homeassistant.util import dt as dt_util
|
|||||||
|
|
||||||
from . import const
|
from . import const
|
||||||
from .api import ConfigEntryWithingsApi
|
from .api import ConfigEntryWithingsApi
|
||||||
from .const import Measurement
|
from .const import LOGGER, Measurement
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(const.LOG_NAMESPACE)
|
|
||||||
NOT_AUTHENTICATED_ERROR = re.compile(
|
NOT_AUTHENTICATED_ERROR = re.compile(
|
||||||
f"^{HTTPStatus.UNAUTHORIZED},.*",
|
f"^{HTTPStatus.UNAUTHORIZED},.*",
|
||||||
re.IGNORECASE,
|
re.IGNORECASE,
|
||||||
@ -181,7 +179,7 @@ class DataManager:
|
|||||||
|
|
||||||
self.subscription_update_coordinator = DataUpdateCoordinator(
|
self.subscription_update_coordinator = DataUpdateCoordinator(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
LOGGER,
|
||||||
name="subscription_update_coordinator",
|
name="subscription_update_coordinator",
|
||||||
update_interval=timedelta(minutes=120),
|
update_interval=timedelta(minutes=120),
|
||||||
update_method=self.async_subscribe_webhook,
|
update_method=self.async_subscribe_webhook,
|
||||||
@ -190,7 +188,7 @@ class DataManager:
|
|||||||
dict[MeasureType, Any] | None
|
dict[MeasureType, Any] | None
|
||||||
](
|
](
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
LOGGER,
|
||||||
name="poll_data_update_coordinator",
|
name="poll_data_update_coordinator",
|
||||||
update_interval=timedelta(minutes=120)
|
update_interval=timedelta(minutes=120)
|
||||||
if self._webhook_config.enabled
|
if self._webhook_config.enabled
|
||||||
@ -232,14 +230,14 @@ class DataManager:
|
|||||||
|
|
||||||
async def async_subscribe_webhook(self) -> None:
|
async def async_subscribe_webhook(self) -> None:
|
||||||
"""Subscribe the webhook to withings data updates."""
|
"""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
|
# 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
|
# 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
|
# config. This ensures the subscription is setup correctly and they start
|
||||||
# pushing again.
|
# pushing again.
|
||||||
if self._subscribe_webhook_run_count == 0:
|
if self._subscribe_webhook_run_count == 0:
|
||||||
_LOGGER.debug("Refreshing withings webhook configs")
|
LOGGER.debug("Refreshing withings webhook configs")
|
||||||
await self.async_unsubscribe_webhook()
|
await self.async_unsubscribe_webhook()
|
||||||
self._subscribe_webhook_run_count += 1
|
self._subscribe_webhook_run_count += 1
|
||||||
|
|
||||||
@ -262,7 +260,7 @@ class DataManager:
|
|||||||
|
|
||||||
# Subscribe to each one.
|
# Subscribe to each one.
|
||||||
for appli in to_add_applis:
|
for appli in to_add_applis:
|
||||||
_LOGGER.debug(
|
LOGGER.debug(
|
||||||
"Subscribing %s for %s in %s seconds",
|
"Subscribing %s for %s in %s seconds",
|
||||||
self._webhook_config.url,
|
self._webhook_config.url,
|
||||||
appli,
|
appli,
|
||||||
@ -280,7 +278,7 @@ class DataManager:
|
|||||||
|
|
||||||
# Revoke subscriptions.
|
# Revoke subscriptions.
|
||||||
for profile in response.profiles:
|
for profile in response.profiles:
|
||||||
_LOGGER.debug(
|
LOGGER.debug(
|
||||||
"Unsubscribing %s for %s in %s seconds",
|
"Unsubscribing %s for %s in %s seconds",
|
||||||
profile.callbackurl,
|
profile.callbackurl,
|
||||||
profile.appli,
|
profile.appli,
|
||||||
@ -310,7 +308,7 @@ class DataManager:
|
|||||||
|
|
||||||
async def async_get_measures(self) -> dict[Measurement, Any]:
|
async def async_get_measures(self) -> dict[Measurement, Any]:
|
||||||
"""Get the measures data."""
|
"""Get the measures data."""
|
||||||
_LOGGER.debug("Updating withings measures")
|
LOGGER.debug("Updating withings measures")
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
startdate = now - datetime.timedelta(days=7)
|
startdate = now - datetime.timedelta(days=7)
|
||||||
|
|
||||||
@ -338,7 +336,7 @@ class DataManager:
|
|||||||
|
|
||||||
async def async_get_sleep_summary(self) -> dict[Measurement, Any]:
|
async def async_get_sleep_summary(self) -> dict[Measurement, Any]:
|
||||||
"""Get the sleep summary data."""
|
"""Get the sleep summary data."""
|
||||||
_LOGGER.debug("Updating withing sleep summary")
|
LOGGER.debug("Updating withing sleep summary")
|
||||||
now = dt_util.now()
|
now = dt_util.now()
|
||||||
yesterday = now - datetime.timedelta(days=1)
|
yesterday = now - datetime.timedelta(days=1)
|
||||||
yesterday_noon = dt_util.start_of_local_day(yesterday) + datetime.timedelta(
|
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:
|
async def async_webhook_data_updated(self, data_category: NotifyAppli) -> None:
|
||||||
"""Handle scenario when data is updated from a webook."""
|
"""Handle scenario when data is updated from a webook."""
|
||||||
_LOGGER.debug("Withings webhook triggered")
|
LOGGER.debug("Withings webhook triggered")
|
||||||
if data_category in {
|
if data_category in {
|
||||||
NotifyAppli.WEIGHT,
|
NotifyAppli.WEIGHT,
|
||||||
NotifyAppli.CIRCULATORY,
|
NotifyAppli.CIRCULATORY,
|
||||||
@ -442,7 +440,7 @@ async def async_get_data_manager(
|
|||||||
config_entry_data = hass.data[const.DOMAIN][config_entry.entry_id]
|
config_entry_data = hass.data[const.DOMAIN][config_entry.entry_id]
|
||||||
|
|
||||||
if const.DATA_MANAGER not in config_entry_data:
|
if const.DATA_MANAGER not in config_entry_data:
|
||||||
_LOGGER.debug(
|
LOGGER.debug(
|
||||||
"Creating withings data manager for profile: %s", config_entry.title
|
"Creating withings data manager for profile: %s", config_entry.title
|
||||||
)
|
)
|
||||||
config_entry_data[const.DATA_MANAGER] = DataManager(
|
config_entry_data[const.DATA_MANAGER] = DataManager(
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""Constants used by the Withings component."""
|
"""Constants used by the Withings component."""
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
|
import logging
|
||||||
|
|
||||||
DEFAULT_TITLE = "Withings"
|
DEFAULT_TITLE = "Withings"
|
||||||
CONF_PROFILES = "profiles"
|
CONF_PROFILES = "profiles"
|
||||||
@ -13,6 +14,8 @@ LOG_NAMESPACE = "homeassistant.components.withings"
|
|||||||
PROFILE = "profile"
|
PROFILE = "profile"
|
||||||
PUSH_HANDLER = "push_handler"
|
PUSH_HANDLER = "push_handler"
|
||||||
|
|
||||||
|
LOGGER = logging.getLogger(__package__)
|
||||||
|
|
||||||
|
|
||||||
class Measurement(StrEnum):
|
class Measurement(StrEnum):
|
||||||
"""Measurement supported by the withings integration."""
|
"""Measurement supported by the withings integration."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user