Use the shared zeroconf instance when attempting to create another Zeroconf instance (#38744)
This commit is contained in:
1 parent
34cb12d3c9
commit
444df4a7d2
8 files changed
+187
-17
No files matched your search
@@ -30,6 +30,8 @@ from homeassistant.helpers.network import NoURLAvailableError, get_url
|
||||
from homeassistant.helpers.singleton import singleton
|
||||
from homeassistant.loader import async_get_homekit, async_get_zeroconf
|
||||
|
||||
from .usage import install_multiple_zeroconf_catcher
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = "zeroconf"
|
||||
@@ -135,6 +137,8 @@ def setup(hass, config):
|
||||
ipv6=zc_config.get(CONF_IPV6, DEFAULT_IPV6),
|
||||
)
|
||||
|
||||
install_multiple_zeroconf_catcher(zeroconf)
|
||||
|
||||
# Get instance UUID
|
||||
uuid = asyncio.run_coroutine_threadsafe(
|
||||
hass.helpers.instance_id.async_get(), hass.loop
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
"""Zeroconf usage utility to warn about multiple instances."""
|
||||
|
||||
import logging
|
||||
|
||||
import zeroconf
|
||||
|
||||
from homeassistant.helpers.frame import (
|
||||
MissingIntegrationFrame,
|
||||
get_integration_frame,
|
||||
report_integration,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def install_multiple_zeroconf_catcher(hass_zc) -> None:
|
||||
"""Wrap the Zeroconf class to return the shared instance if multiple instances are detected."""
|
||||
|
||||
def new_zeroconf_new(self, *k, **kw):
|
||||
_report(
|
||||
"attempted to create another Zeroconf instance. Please use the shared Zeroconf via await homeassistant.components.zeroconf.async_get_instance(hass)",
|
||||
)
|
||||
return hass_zc
|
||||
|
||||
def new_zeroconf_init(self, *k, **kw):
|
||||
return
|
||||
|
||||
zeroconf.Zeroconf.__new__ = new_zeroconf_new
|
||||
zeroconf.Zeroconf.__init__ = new_zeroconf_init
|
||||
|
||||
|
||||
def _report(what: str) -> None:
|
||||
"""Report incorrect usage.
|
||||
|
||||
Async friendly.
|
||||
"""
|
||||
integration_frame = None
|
||||
|
||||
try:
|
||||
integration_frame = get_integration_frame(exclude_integrations={"zeroconf"})
|
||||
except MissingIntegrationFrame:
|
||||
pass
|
||||
|
||||
if not integration_frame:
|
||||
_LOGGER.warning(
|
||||
"Detected code that %s. Please report this issue.", what, stack_info=True
|
||||
)
|
||||
return
|
||||
|
||||
report_integration(what, integration_frame)
|
||||
Reference in new issue
Block a user