mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Cache parsing the url for the device registry (#113910)
* Cache parsing the url for the device registry There are lots of hub integrations that use the same url for every sub-device which results in a lot of url parsing at startup. The logic can be simplified quite a bit here by only using yarl for URLs * fix onvif
This commit is contained in:
parent
267fe3dc34
commit
e9c1753f3a
@ -9,7 +9,6 @@ from functools import lru_cache, partial
|
|||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
from typing import TYPE_CHECKING, Any, Literal, TypedDict, TypeVar, cast
|
from typing import TYPE_CHECKING, Any, Literal, TypedDict, TypeVar, cast
|
||||||
from urllib.parse import urlparse
|
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
from yarl import URL
|
from yarl import URL
|
||||||
@ -212,22 +211,22 @@ def _validate_device_info(
|
|||||||
return device_info_type
|
return device_info_type
|
||||||
|
|
||||||
|
|
||||||
|
_cached_parse_url = lru_cache(maxsize=512)(URL)
|
||||||
|
"""Parse a URL and cache the result."""
|
||||||
|
|
||||||
|
|
||||||
def _validate_configuration_url(value: Any) -> str | None:
|
def _validate_configuration_url(value: Any) -> str | None:
|
||||||
"""Validate and convert configuration_url."""
|
"""Validate and convert configuration_url."""
|
||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
if (
|
|
||||||
isinstance(value, URL)
|
url_as_str = str(value)
|
||||||
and (value.scheme not in CONFIGURATION_URL_SCHEMES or not value.host)
|
url = value if type(value) is URL else _cached_parse_url(url_as_str)
|
||||||
) or (
|
|
||||||
(parsed_url := urlparse(str(value)))
|
if url.scheme not in CONFIGURATION_URL_SCHEMES or not url.host:
|
||||||
and (
|
|
||||||
parsed_url.scheme not in CONFIGURATION_URL_SCHEMES
|
|
||||||
or not parsed_url.hostname
|
|
||||||
)
|
|
||||||
):
|
|
||||||
raise ValueError(f"invalid configuration_url '{value}'")
|
raise ValueError(f"invalid configuration_url '{value}'")
|
||||||
return str(value)
|
|
||||||
|
return url_as_str
|
||||||
|
|
||||||
|
|
||||||
@attr.s(frozen=True)
|
@attr.s(frozen=True)
|
||||||
|
@ -125,6 +125,7 @@ def setup_mock_onvif_camera(
|
|||||||
def setup_mock_device(mock_device, capabilities=None):
|
def setup_mock_device(mock_device, capabilities=None):
|
||||||
"""Prepare mock ONVIFDevice."""
|
"""Prepare mock ONVIFDevice."""
|
||||||
mock_device.async_setup = AsyncMock(return_value=True)
|
mock_device.async_setup = AsyncMock(return_value=True)
|
||||||
|
mock_device.port = 80
|
||||||
mock_device.available = True
|
mock_device.available = True
|
||||||
mock_device.name = NAME
|
mock_device.name = NAME
|
||||||
mock_device.info = DeviceInfo(
|
mock_device.info = DeviceInfo(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user