Add debug logging to tomorrowio and mask API key (#78915)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Raman Gupta 2022-09-22 04:09:37 -04:00 committed by GitHub
parent da4ceea647
commit a286600b03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 6 deletions

View File

@ -3,7 +3,6 @@ from __future__ import annotations
import asyncio import asyncio
from datetime import timedelta from datetime import timedelta
import logging
from math import ceil from math import ceil
from typing import Any from typing import Any
@ -40,6 +39,7 @@ from .const import (
CONF_TIMESTEP, CONF_TIMESTEP,
DOMAIN, DOMAIN,
INTEGRATION_NAME, INTEGRATION_NAME,
LOGGER,
TMRW_ATTR_CARBON_MONOXIDE, TMRW_ATTR_CARBON_MONOXIDE,
TMRW_ATTR_CHINA_AQI, TMRW_ATTR_CHINA_AQI,
TMRW_ATTR_CHINA_HEALTH_CONCERN, TMRW_ATTR_CHINA_HEALTH_CONCERN,
@ -78,8 +78,6 @@ from .const import (
TMRW_ATTR_WIND_SPEED, TMRW_ATTR_WIND_SPEED,
) )
_LOGGER = logging.getLogger(__name__)
PLATFORMS = [SENSOR_DOMAIN, WEATHER_DOMAIN] PLATFORMS = [SENSOR_DOMAIN, WEATHER_DOMAIN]
@ -110,6 +108,18 @@ def async_set_update_interval(
(24 * 60 * len(entries) * api.num_api_requests) (24 * 60 * len(entries) * api.num_api_requests)
/ (api.max_requests_per_day * 0.9) / (api.max_requests_per_day * 0.9)
) )
LOGGER.debug(
(
"Number of config entries: %s\n"
"Number of API Requests per call: %s\n"
"Max requests per day: %s\n"
"Update interval: %s minutes"
),
len(entries),
api.num_api_requests,
api.max_requests_per_day,
minutes,
)
return timedelta(minutes=minutes) return timedelta(minutes=minutes)
@ -126,7 +136,7 @@ def async_migrate_entry_from_climacell(
data = entry.data.copy() data = entry.data.copy()
old_config_entry_id = data.pop("old_config_entry_id") old_config_entry_id = data.pop("old_config_entry_id")
hass.config_entries.async_update_entry(entry, data=data) hass.config_entries.async_update_entry(entry, data=data)
_LOGGER.debug( LOGGER.debug(
( (
"Setting up imported climacell entry %s for the first time as " "Setting up imported climacell entry %s for the first time as "
"tomorrowio entry %s" "tomorrowio entry %s"
@ -152,7 +162,7 @@ def async_migrate_entry_from_climacell(
new_device_id=device.id, new_device_id=device.id,
) )
assert entity_entry assert entity_entry
_LOGGER.debug( LOGGER.debug(
"Migrated %s from %s to %s", "Migrated %s from %s to %s",
entity_entry.entity_id, entity_entry.entity_id,
old_platform, old_platform,
@ -238,7 +248,7 @@ class TomorrowioDataUpdateCoordinator(DataUpdateCoordinator):
self.entry_id_to_location_dict: dict[str, str] = {} self.entry_id_to_location_dict: dict[str, str] = {}
self._coordinator_ready: asyncio.Event | None = None self._coordinator_ready: asyncio.Event | None = None
super().__init__(hass, _LOGGER, name=f"{DOMAIN}_{self._api.api_key}") super().__init__(hass, LOGGER, name=f"{DOMAIN}_{self._api.api_key_masked}")
def add_entry_to_location_dict(self, entry: ConfigEntry) -> None: def add_entry_to_location_dict(self, entry: ConfigEntry) -> None:
"""Add an entry to the location dict.""" """Add an entry to the location dict."""
@ -253,9 +263,17 @@ class TomorrowioDataUpdateCoordinator(DataUpdateCoordinator):
# may start setup before we finish setting the initial data and we don't want # may start setup before we finish setting the initial data and we don't want
# to do multiple refreshes on startup. # to do multiple refreshes on startup.
if self._coordinator_ready is None: if self._coordinator_ready is None:
LOGGER.debug(
"Setting up coordinator for API key %s, loading data for all entries",
self._api.api_key_masked,
)
self._coordinator_ready = asyncio.Event() self._coordinator_ready = asyncio.Event()
for entry_ in async_get_entries_by_api_key(self.hass, self._api.api_key): for entry_ in async_get_entries_by_api_key(self.hass, self._api.api_key):
self.add_entry_to_location_dict(entry_) self.add_entry_to_location_dict(entry_)
LOGGER.debug(
"Loaded %s entries, initiating first refresh",
len(self.entry_id_to_location_dict),
)
await self.async_config_entry_first_refresh() await self.async_config_entry_first_refresh()
self._coordinator_ready.set() self._coordinator_ready.set()
else: else:
@ -265,6 +283,13 @@ class TomorrowioDataUpdateCoordinator(DataUpdateCoordinator):
# don't need to schedule a refresh # don't need to schedule a refresh
if entry.entry_id in self.entry_id_to_location_dict: if entry.entry_id in self.entry_id_to_location_dict:
return return
LOGGER.debug(
(
"Adding new entry to existing coordinator for API key %s, doing a "
"partial refresh"
),
self._api.api_key_masked,
)
# We need a refresh, but it's going to be a partial refresh so we can # We need a refresh, but it's going to be a partial refresh so we can
# minimize repeat API calls # minimize repeat API calls
self.add_entry_to_location_dict(entry) self.add_entry_to_location_dict(entry)
@ -294,6 +319,10 @@ class TomorrowioDataUpdateCoordinator(DataUpdateCoordinator):
): ):
data = self.data data = self.data
LOGGER.debug(
"Fetching data for %s entries",
len(set(self.entry_id_to_location_dict) - set(data)),
)
for entry_id, location in self.entry_id_to_location_dict.items(): for entry_id, location in self.entry_id_to_location_dict.items():
if entry_id in data: if entry_id in data:
continue continue

View File

@ -1,6 +1,8 @@
"""Constants for the Tomorrow.io integration.""" """Constants for the Tomorrow.io integration."""
from __future__ import annotations from __future__ import annotations
import logging
from pytomorrowio.const import DAILY, HOURLY, NOWCAST, WeatherCode from pytomorrowio.const import DAILY, HOURLY, NOWCAST, WeatherCode
from homeassistant.components.weather import ( from homeassistant.components.weather import (
@ -18,6 +20,8 @@ from homeassistant.components.weather import (
ATTR_CONDITION_WINDY, ATTR_CONDITION_WINDY,
) )
LOGGER = logging.getLogger(__package__)
CONF_TIMESTEP = "timestep" CONF_TIMESTEP = "timestep"
FORECAST_TYPES = [DAILY, HOURLY, NOWCAST] FORECAST_TYPES = [DAILY, HOURLY, NOWCAST]