Guard expensive debug formatting with calls with isEnabledFor (#97073)

This commit is contained in:
J. Nick Koston 2023-07-23 03:54:25 -05:00 committed by GitHub
parent 61532475f9
commit d4cdb0453f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 9 deletions

View file

@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio
from collections.abc import Mapping
import logging
from pprint import pformat
from typing import Any, cast
from urllib.parse import urlparse
@ -106,7 +107,8 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
except (asyncio.TimeoutError, ResponseError):
self.bridges = []
LOGGER.debug("Discovered deCONZ gateways %s", pformat(self.bridges))
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug("Discovered deCONZ gateways %s", pformat(self.bridges))
if self.bridges:
hosts = []
@ -215,7 +217,8 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
"""Handle a discovered deCONZ bridge."""
LOGGER.debug("deCONZ SSDP discovery %s", pformat(discovery_info))
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug("deCONZ SSDP discovery %s", pformat(discovery_info))
self.bridge_id = normalize_bridge_id(discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL])
parsed_url = urlparse(discovery_info.ssdp_location)
@ -248,7 +251,8 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
This flow is triggered by the discovery component.
"""
LOGGER.debug("deCONZ HASSIO discovery %s", pformat(discovery_info.config))
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug("deCONZ HASSIO discovery %s", pformat(discovery_info.config))
self.bridge_id = normalize_bridge_id(discovery_info.config[CONF_SERIAL])
await self.async_set_unique_id(self.bridge_id)

View file

@ -126,7 +126,8 @@ class DlnaDmrFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
"""Handle a flow initialized by SSDP discovery."""
LOGGER.debug("async_step_ssdp: discovery_info %s", pformat(discovery_info))
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug("async_step_ssdp: discovery_info %s", pformat(discovery_info))
await self._async_set_info_from_discovery(discovery_info)

View file

@ -67,7 +67,8 @@ class DlnaDmsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
"""Handle a flow initialized by SSDP discovery."""
LOGGER.debug("async_step_ssdp: discovery_info %s", pformat(discovery_info))
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug("async_step_ssdp: discovery_info %s", pformat(discovery_info))
await self._async_parse_discovery(discovery_info)

View file

@ -2,6 +2,7 @@
from __future__ import annotations
from collections.abc import Mapping
import logging
from pprint import pformat
from typing import Any
from urllib.parse import urlparse
@ -218,7 +219,8 @@ class OnvifFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
if not configured:
self.devices.append(device)
LOGGER.debug("Discovered ONVIF devices %s", pformat(self.devices))
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug("Discovered ONVIF devices %s", pformat(self.devices))
if self.devices:
devices = {CONF_MANUAL_INPUT: CONF_MANUAL_INPUT}
@ -274,9 +276,10 @@ class OnvifFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
self, configure_unique_id: bool = True
) -> tuple[dict[str, str], dict[str, str]]:
"""Fetch ONVIF device profiles."""
LOGGER.debug(
"Fetching profiles from ONVIF device %s", pformat(self.onvif_config)
)
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug(
"Fetching profiles from ONVIF device %s", pformat(self.onvif_config)
)
device = get_device(
self.hass,

View file

@ -1,5 +1,6 @@
"""Tests for deCONZ config flow."""
import asyncio
import logging
from unittest.mock import patch
import pydeconz
@ -42,6 +43,7 @@ async def test_flow_discovered_bridges(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test that config flow works for discovered bridges."""
logging.getLogger("homeassistant.components.deconz").setLevel(logging.DEBUG)
aioclient_mock.get(
pydeconz.utils.URL_DISCOVER,
json=[
@ -142,6 +144,7 @@ async def test_flow_manual_configuration(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test that config flow works with manual configuration after no discovered bridges."""
logging.getLogger("homeassistant.components.deconz").setLevel(logging.DEBUG)
aioclient_mock.get(
pydeconz.utils.URL_DISCOVER,
json=[],

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from collections.abc import Iterable
import dataclasses
import logging
from unittest.mock import Mock, patch
from async_upnp_client.client import UpnpDevice
@ -286,6 +287,9 @@ async def test_user_flow_wrong_st(hass: HomeAssistant, domain_data_mock: Mock) -
async def test_ssdp_flow_success(hass: HomeAssistant) -> None:
"""Test that SSDP discovery with an available device works."""
logging.getLogger("homeassistant.components.dlna_dmr.config_flow").setLevel(
logging.DEBUG
)
result = await hass.config_entries.flow.async_init(
DLNA_DOMAIN,
context={"source": config_entries.SOURCE_SSDP},

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from collections.abc import Iterable
import dataclasses
import logging
from typing import Final
from unittest.mock import Mock, patch
@ -125,6 +126,9 @@ async def test_user_flow_no_devices(
async def test_ssdp_flow_success(hass: HomeAssistant) -> None:
"""Test that SSDP discovery with an available device works."""
logging.getLogger("homeassistant.components.dlna_dms.config_flow").setLevel(
logging.DEBUG
)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_SSDP},

View file

@ -1,4 +1,5 @@
"""Test ONVIF config flow."""
import logging
from unittest.mock import MagicMock, patch
import pytest
@ -103,6 +104,7 @@ def setup_mock_discovery(
async def test_flow_discovered_devices(hass: HomeAssistant) -> None:
"""Test that config flow works for discovered devices."""
logging.getLogger("homeassistant.components.onvif").setLevel(logging.DEBUG)
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -172,6 +174,7 @@ async def test_flow_discovered_devices_ignore_configured_manual_input(
hass: HomeAssistant,
) -> None:
"""Test that config flow discovery ignores configured devices."""
logging.getLogger("homeassistant.components.onvif").setLevel(logging.DEBUG)
await setup_onvif_integration(hass)
result = await hass.config_entries.flow.async_init(
@ -241,6 +244,7 @@ async def test_flow_discovered_no_device(hass: HomeAssistant) -> None:
async def test_flow_discovery_ignore_existing_and_abort(hass: HomeAssistant) -> None:
"""Test that config flow discovery ignores setup devices."""
logging.getLogger("homeassistant.components.onvif").setLevel(logging.DEBUG)
await setup_onvif_integration(hass)
await setup_onvif_integration(
hass,
@ -298,6 +302,7 @@ async def test_flow_discovery_ignore_existing_and_abort(hass: HomeAssistant) ->
async def test_flow_manual_entry(hass: HomeAssistant) -> None:
"""Test that config flow works for discovered devices."""
logging.getLogger("homeassistant.components.onvif").setLevel(logging.DEBUG)
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, context={"source": config_entries.SOURCE_USER}
)