Home Assistant Unofficial Reference 2024.12.1
usage.py
Go to the documentation of this file.
1 """Zeroconf usage utility to warn about multiple instances."""
2 
3 from typing import Any
4 
5 import zeroconf
6 
7 from homeassistant.helpers.frame import ReportBehavior, report_usage
8 
9 from .models import HaZeroconf
10 
11 
12 def install_multiple_zeroconf_catcher(hass_zc: HaZeroconf) -> None:
13  """Wrap the Zeroconf class to return the shared instance.
14 
15  Only if if multiple instances are detected.
16  """
17 
18  def new_zeroconf_new(self: zeroconf.Zeroconf, *k: Any, **kw: Any) -> HaZeroconf:
20  (
21  "attempted to create another Zeroconf instance. Please use the shared"
22  " Zeroconf via await"
23  " homeassistant.components.zeroconf.async_get_instance(hass)"
24  ),
25  exclude_integrations={"zeroconf"},
26  core_behavior=ReportBehavior.LOG,
27  )
28  return hass_zc
29 
30  def new_zeroconf_init(self: zeroconf.Zeroconf, *k: Any, **kw: Any) -> None:
31  return
32 
33  zeroconf.Zeroconf.__new__ = new_zeroconf_new # type: ignore[assignment]
34  zeroconf.Zeroconf.__init__ = new_zeroconf_init # type: ignore[method-assign]
None install_multiple_zeroconf_catcher(HaZeroconf hass_zc)
Definition: usage.py:12
None report_usage(str what, *str|None breaks_in_ha_version=None, ReportBehavior core_behavior=ReportBehavior.ERROR, ReportBehavior core_integration_behavior=ReportBehavior.LOG, ReportBehavior custom_integration_behavior=ReportBehavior.LOG, set[str]|None exclude_integrations=None, str|None integration_domain=None, int level=logging.WARNING)
Definition: frame.py:195