2020-08-12 14:08:33 +00:00
|
|
|
"""Zeroconf usage utility to warn about multiple instances."""
|
|
|
|
|
2021-03-30 16:48:04 +00:00
|
|
|
from typing import Any
|
2020-08-12 14:08:33 +00:00
|
|
|
|
|
|
|
import zeroconf
|
|
|
|
|
2021-11-20 10:53:04 +00:00
|
|
|
from homeassistant.helpers.frame import report
|
2020-08-12 14:08:33 +00:00
|
|
|
|
2021-03-30 16:48:04 +00:00
|
|
|
from .models import HaZeroconf
|
|
|
|
|
2020-08-12 14:08:33 +00:00
|
|
|
|
2021-03-30 16:48:04 +00:00
|
|
|
def install_multiple_zeroconf_catcher(hass_zc: HaZeroconf) -> None:
|
2020-08-12 14:08:33 +00:00
|
|
|
"""Wrap the Zeroconf class to return the shared instance if multiple instances are detected."""
|
|
|
|
|
2021-03-30 16:48:04 +00:00
|
|
|
def new_zeroconf_new(self: zeroconf.Zeroconf, *k: Any, **kw: Any) -> HaZeroconf:
|
2021-11-20 10:53:04 +00:00
|
|
|
report(
|
2020-08-12 14:08:33 +00:00
|
|
|
"attempted to create another Zeroconf instance. Please use the shared Zeroconf via await homeassistant.components.zeroconf.async_get_instance(hass)",
|
2021-11-20 10:53:04 +00:00
|
|
|
exclude_integrations={"zeroconf"},
|
|
|
|
error_if_core=False,
|
2020-08-12 14:08:33 +00:00
|
|
|
)
|
|
|
|
return hass_zc
|
|
|
|
|
2021-03-30 16:48:04 +00:00
|
|
|
def new_zeroconf_init(self: zeroconf.Zeroconf, *k: Any, **kw: Any) -> None:
|
2020-08-12 14:08:33 +00:00
|
|
|
return
|
|
|
|
|
2021-03-30 16:48:04 +00:00
|
|
|
zeroconf.Zeroconf.__new__ = new_zeroconf_new # type: ignore
|
|
|
|
zeroconf.Zeroconf.__init__ = new_zeroconf_init # type: ignore
|