2019-05-21 22:36:26 +00:00
|
|
|
"""Test Zeroconf component setup process."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2019-05-29 21:20:06 +00:00
|
|
|
from zeroconf import ServiceInfo, ServiceStateChange
|
2019-05-21 22:36:26 +00:00
|
|
|
|
2019-05-29 18:19:50 +00:00
|
|
|
from homeassistant.generated import zeroconf as zc_gen
|
2019-05-21 22:36:26 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
from homeassistant.components import zeroconf
|
|
|
|
|
|
|
|
|
|
|
|
def service_update_mock(zeroconf, service, handlers):
|
|
|
|
"""Call service update handler."""
|
|
|
|
handlers[0](
|
2019-05-29 21:20:06 +00:00
|
|
|
zeroconf, service, '{}.{}'.format('name', service),
|
2019-05-21 22:36:26 +00:00
|
|
|
ServiceStateChange.Added)
|
|
|
|
|
|
|
|
|
2019-05-29 21:20:06 +00:00
|
|
|
def get_service_info_mock(service_type, name):
|
2019-05-21 22:36:26 +00:00
|
|
|
"""Return service info for get_service_info."""
|
|
|
|
return ServiceInfo(
|
|
|
|
service_type, name, address=b'\n\x00\x00\x14', port=80, weight=0,
|
|
|
|
priority=0, server='name.local.',
|
|
|
|
properties={b'macaddress': b'ABCDEF012345'})
|
|
|
|
|
|
|
|
|
|
|
|
async def test_setup(hass):
|
|
|
|
"""Test configured options for a device are loaded via config entry."""
|
|
|
|
with patch.object(hass.config_entries, 'flow') as mock_config_flow, \
|
|
|
|
patch.object(zeroconf, 'ServiceBrowser') as MockServiceBrowser, \
|
|
|
|
patch.object(zeroconf.Zeroconf, 'get_service_info') as \
|
|
|
|
mock_get_service_info:
|
|
|
|
|
|
|
|
MockServiceBrowser.side_effect = service_update_mock
|
|
|
|
mock_get_service_info.side_effect = get_service_info_mock
|
|
|
|
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, zeroconf.DOMAIN, {zeroconf.DOMAIN: {}})
|
|
|
|
|
2019-05-29 21:20:06 +00:00
|
|
|
assert len(MockServiceBrowser.mock_calls) == len(zc_gen.ZEROCONF)
|
|
|
|
assert len(mock_config_flow.mock_calls) == len(zc_gen.ZEROCONF) * 2
|