mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +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 time
|
||||
from typing import TYPE_CHECKING, Any, Literal, TypedDict, TypeVar, cast
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import attr
|
||||
from yarl import URL
|
||||
@ -212,22 +211,22 @@ def _validate_device_info(
|
||||
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:
|
||||
"""Validate and convert configuration_url."""
|
||||
if value is None:
|
||||
return None
|
||||
if (
|
||||
isinstance(value, URL)
|
||||
and (value.scheme not in CONFIGURATION_URL_SCHEMES or not value.host)
|
||||
) or (
|
||||
(parsed_url := urlparse(str(value)))
|
||||
and (
|
||||
parsed_url.scheme not in CONFIGURATION_URL_SCHEMES
|
||||
or not parsed_url.hostname
|
||||
)
|
||||
):
|
||||
|
||||
url_as_str = str(value)
|
||||
url = value if type(value) is URL else _cached_parse_url(url_as_str)
|
||||
|
||||
if url.scheme not in CONFIGURATION_URL_SCHEMES or not url.host:
|
||||
raise ValueError(f"invalid configuration_url '{value}'")
|
||||
return str(value)
|
||||
|
||||
return url_as_str
|
||||
|
||||
|
||||
@attr.s(frozen=True)
|
||||
|
@ -125,6 +125,7 @@ def setup_mock_onvif_camera(
|
||||
def setup_mock_device(mock_device, capabilities=None):
|
||||
"""Prepare mock ONVIFDevice."""
|
||||
mock_device.async_setup = AsyncMock(return_value=True)
|
||||
mock_device.port = 80
|
||||
mock_device.available = True
|
||||
mock_device.name = NAME
|
||||
mock_device.info = DeviceInfo(
|
||||
|
Loading…
x
Reference in New Issue
Block a user