Zeroconf - replace library (#23835)

* Use aiozeroconf in preparation for new zeroconf discovery

* Update requirements

* Remove sleep

* Make stop zeroconf a coroutine

* Remove unused import

* Fix aiozeroconf dependency in default_config tests
This commit is contained in:
Robert Svensson 2019-05-14 05:58:13 +02:00 committed by Paulus Schoutsen
parent 45085dd97f
commit 0d96095646
4 changed files with 28 additions and 16 deletions

View File

@ -19,11 +19,11 @@ CONFIG_SCHEMA = vol.Schema({
}, extra=vol.ALLOW_EXTRA)
def setup(hass, config):
async def async_setup(hass, config):
"""Set up Zeroconf and make Home Assistant discoverable."""
from zeroconf import Zeroconf, ServiceInfo
from aiozeroconf import Zeroconf, ServiceInfo
zeroconf = Zeroconf()
zeroconf = Zeroconf(hass.loop)
zeroconf_name = '{}.{}'.format(hass.config.location_name, ZEROCONF_TYPE)
@ -38,19 +38,22 @@ def setup(hass, config):
try:
host_ip_pton = socket.inet_pton(socket.AF_INET, host_ip)
info = ServiceInfo(ZEROCONF_TYPE, zeroconf_name, address=host_ip_pton,
port=hass.http.server_port, weight=0, priority=0,
properties=params)
except socket.error:
host_ip_pton = socket.inet_pton(socket.AF_INET6, host_ip)
info = ServiceInfo(ZEROCONF_TYPE, zeroconf_name, address6=host_ip_pton,
port=hass.http.server_port, weight=0, priority=0,
properties=params)
info = ServiceInfo(ZEROCONF_TYPE, zeroconf_name, host_ip_pton,
hass.http.server_port, 0, 0, params)
await zeroconf.register_service(info)
zeroconf.register_service(info)
def stop_zeroconf(event):
async def stop_zeroconf(event):
"""Stop Zeroconf."""
zeroconf.unregister_service(info)
zeroconf.close()
await zeroconf.unregister_service(info)
await zeroconf.close()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_zeroconf)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_zeroconf)
return True

View File

@ -3,7 +3,7 @@
"name": "Zeroconf",
"documentation": "https://www.home-assistant.io/components/zeroconf",
"requirements": [
"zeroconf==0.22.0"
"aiozeroconf==0.1.8"
],
"dependencies": [
"api"

View File

@ -154,6 +154,9 @@ aioswitcher==2019.3.21
# homeassistant.components.unifi
aiounifi==4
# homeassistant.components.zeroconf
aiozeroconf==0.1.8
# homeassistant.components.aladdin_connect
aladdin_connect==0.3
@ -1850,9 +1853,6 @@ youtube_dl==2019.05.11
# homeassistant.components.zengge
zengge==0.2
# homeassistant.components.zeroconf
zeroconf==0.22.0
# homeassistant.components.zha
zha-quirks==0.0.12

View File

@ -5,7 +5,16 @@ from homeassistant.setup import async_setup_component
import pytest
from tests.common import MockDependency
from tests.common import MockDependency, mock_coro
@pytest.fixture(autouse=True)
def aiozeroconf_mock():
"""Mock aiozeroconf."""
with MockDependency('aiozeroconf') as mocked_zeroconf:
mocked_zeroconf.Zeroconf.return_value.register_service \
.return_value = mock_coro(True)
yield
@pytest.fixture(autouse=True)