Add zeroconf/homekit/ssdp discovery support for custom components (#38466)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
1 parent
1ebc420c75
commit
d0d0403664
5 files changed
+193
-37
No files matched your search
@@ -25,10 +25,10 @@ from homeassistant.const import (
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
__version__,
|
||||
)
|
||||
from homeassistant.generated.zeroconf import HOMEKIT, ZEROCONF
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.network import NoURLAvailableError, get_url
|
||||
from homeassistant.helpers.singleton import singleton
|
||||
from homeassistant.loader import async_get_homekit, async_get_zeroconf
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -197,8 +197,14 @@ def setup(hass, config):
|
||||
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, zeroconf_hass_start)
|
||||
|
||||
zeroconf_types = {}
|
||||
homekit_models = {}
|
||||
|
||||
def service_update(zeroconf, service_type, name, state_change):
|
||||
"""Service state changed."""
|
||||
nonlocal zeroconf_types
|
||||
nonlocal homekit_models
|
||||
|
||||
if state_change != ServiceStateChange.Added:
|
||||
return
|
||||
|
||||
@@ -219,7 +225,7 @@ def setup(hass, config):
|
||||
|
||||
# If we can handle it as a HomeKit discovery, we do that here.
|
||||
if service_type == HOMEKIT_TYPE:
|
||||
discovery_was_forwarded = handle_homekit(hass, info)
|
||||
discovery_was_forwarded = handle_homekit(hass, homekit_models, info)
|
||||
# Continue on here as homekit_controller
|
||||
# still needs to get updates on devices
|
||||
# so it can see when the 'c#' field is updated.
|
||||
@@ -241,20 +247,25 @@ def setup(hass, config):
|
||||
# likely bad homekit data
|
||||
return
|
||||
|
||||
for domain in ZEROCONF[service_type]:
|
||||
for domain in zeroconf_types[service_type]:
|
||||
hass.add_job(
|
||||
hass.config_entries.flow.async_init(
|
||||
domain, context={"source": DOMAIN}, data=info
|
||||
)
|
||||
)
|
||||
|
||||
types = list(ZEROCONF)
|
||||
|
||||
if HOMEKIT_TYPE not in ZEROCONF:
|
||||
types.append(HOMEKIT_TYPE)
|
||||
|
||||
def zeroconf_hass_started(_event):
|
||||
async def zeroconf_hass_started(_event):
|
||||
"""Start the service browser."""
|
||||
nonlocal zeroconf_types
|
||||
nonlocal homekit_models
|
||||
|
||||
zeroconf_types = await async_get_zeroconf(hass)
|
||||
homekit_models = await async_get_homekit(hass)
|
||||
|
||||
types = list(zeroconf_types)
|
||||
|
||||
if HOMEKIT_TYPE not in zeroconf_types:
|
||||
types.append(HOMEKIT_TYPE)
|
||||
|
||||
_LOGGER.debug("Starting Zeroconf browser")
|
||||
HaServiceBrowser(zeroconf, types, handlers=[service_update])
|
||||
@@ -264,7 +275,7 @@ def setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
def handle_homekit(hass, info) -> bool:
|
||||
def handle_homekit(hass, homekit_models, info) -> bool:
|
||||
"""Handle a HomeKit discovery.
|
||||
|
||||
Return if discovery was forwarded.
|
||||
@@ -280,7 +291,7 @@ def handle_homekit(hass, info) -> bool:
|
||||
if model is None:
|
||||
return False
|
||||
|
||||
for test_model in HOMEKIT:
|
||||
for test_model in homekit_models:
|
||||
if (
|
||||
model != test_model
|
||||
and not model.startswith(f"{test_model} ")
|
||||
@@ -290,7 +301,7 @@ def handle_homekit(hass, info) -> bool:
|
||||
|
||||
hass.add_job(
|
||||
hass.config_entries.flow.async_init(
|
||||
HOMEKIT[test_model], context={"source": "homekit"}, data=info
|
||||
homekit_models[test_model], context={"source": "homekit"}, data=info
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
Reference in new issue
Block a user