Provide zeroconf option to bind to only the default interface (#35281)

By default zeroconf binds to every interface.
This commit is contained in:
J. Nick Koston 2020-05-06 09:16:59 -05:00 committed by GitHub
parent 35d8890f4e
commit 3219c380c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View file

@ -5,6 +5,7 @@ import socket
import voluptuous as vol
from zeroconf import (
InterfaceChoice,
NonUniqueNameException,
ServiceBrowser,
ServiceInfo,
@ -20,6 +21,7 @@ from homeassistant.const import (
__version__,
)
from homeassistant.generated.zeroconf import HOMEKIT, ZEROCONF
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
@ -34,12 +36,29 @@ ATTR_PROPERTIES = "properties"
ZEROCONF_TYPE = "_home-assistant._tcp.local."
HOMEKIT_TYPE = "_hap._tcp.local."
CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA)
CONF_DEFAULT_INTERFACE = "default_interface"
DEFAULT_DEFAULT_INTERFACE = False
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Optional(
CONF_DEFAULT_INTERFACE, default=DEFAULT_DEFAULT_INTERFACE
): cv.boolean
}
)
},
extra=vol.ALLOW_EXTRA,
)
def setup(hass, config):
"""Set up Zeroconf and make Home Assistant discoverable."""
zeroconf = Zeroconf()
if config.get(CONF_DEFAULT_INTERFACE):
zeroconf = Zeroconf(interfaces=InterfaceChoice.Default)
else:
zeroconf = Zeroconf()
zeroconf_name = f"{hass.config.location_name}.{ZEROCONF_TYPE}"
params = {

View file

@ -1,8 +1,9 @@
"""Test Zeroconf component setup process."""
import pytest
from zeroconf import ServiceInfo, ServiceStateChange
from zeroconf import InterfaceChoice, ServiceInfo, ServiceStateChange
from homeassistant.components import zeroconf
from homeassistant.components.zeroconf import CONF_DEFAULT_INTERFACE
from homeassistant.generated import zeroconf as zc_gen
from homeassistant.setup import async_setup_component
@ -78,6 +79,19 @@ async def test_setup(hass, mock_zeroconf):
assert len(mock_config_flow.mock_calls) == expected_flow_calls
async def test_setup_with_default_interface(hass, mock_zeroconf):
"""Test default interface config."""
with patch.object(hass.config_entries.flow, "async_init"), patch.object(
zeroconf, "ServiceBrowser", side_effect=service_update_mock
):
mock_zeroconf.get_service_info.side_effect = get_service_info_mock
assert await async_setup_component(
hass, zeroconf.DOMAIN, {zeroconf.DOMAIN: {CONF_DEFAULT_INTERFACE: True}}
)
assert mock_zeroconf.called_with(interface_choice=InterfaceChoice.Default)
async def test_homekit_match_partial_space(hass, mock_zeroconf):
"""Test configured options for a device are loaded via config entry."""
with patch.dict(