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

View file

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

View file

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

View file

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

View file

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

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from collections.abc import Iterable from collections.abc import Iterable
import dataclasses import dataclasses
import logging
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from async_upnp_client.client import UpnpDevice 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: async def test_ssdp_flow_success(hass: HomeAssistant) -> None:
"""Test that SSDP discovery with an available device works.""" """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( result = await hass.config_entries.flow.async_init(
DLNA_DOMAIN, DLNA_DOMAIN,
context={"source": config_entries.SOURCE_SSDP}, context={"source": config_entries.SOURCE_SSDP},

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from collections.abc import Iterable from collections.abc import Iterable
import dataclasses import dataclasses
import logging
from typing import Final from typing import Final
from unittest.mock import Mock, patch 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: async def test_ssdp_flow_success(hass: HomeAssistant) -> None:
"""Test that SSDP discovery with an available device works.""" """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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_SSDP}, context={"source": config_entries.SOURCE_SSDP},

View file

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