Change zeroconf to use local ip not base_url (#5151)

Rather than rely on base_url being unconfigured and therefore be
set as the host ip in the api component, get the local ip directly.

Use server port from the http component instead of the api component
where it will be None if base_url is set.

Change the exception catch meant to check for ipv4 vs ipv6 to wrap the
address encoding only instead of the zeroconf service info declaration.
This commit is contained in:
Mike Hennessy 2017-01-03 15:39:42 -05:00 committed by Paulus Schoutsen
parent 9eed03108f
commit 254eb4e88a

View file

@ -9,6 +9,7 @@ import socket
import voluptuous as vol
from homeassistant import util
from homeassistant.const import (EVENT_HOMEASSISTANT_STOP, __version__)
_LOGGER = logging.getLogger(__name__)
@ -40,16 +41,15 @@ def setup(hass, config):
'requires_api_password': requires_api_password,
}
host_ip = util.get_local_ip()
try:
info = ServiceInfo(ZEROCONF_TYPE, zeroconf_name,
socket.inet_pton(
socket.AF_INET, hass.config.api.host),
hass.config.api.port, 0, 0, params)
host_ip_pton = socket.inet_pton(socket.AF_INET, host_ip)
except socket.error:
info = ServiceInfo(ZEROCONF_TYPE, zeroconf_name,
socket.inet_pton(
socket.AF_INET6, hass.config.api.host),
hass.config.api.port, 0, 0, params)
host_ip_pton = socket.inet_pton(socket.AF_INET6, host_ip)
info = ServiceInfo(ZEROCONF_TYPE, zeroconf_name, host_ip_pton,
hass.http.server_port, 0, 0, params)
zeroconf.register_service(info)