diff --git a/homeassistant/components/http.py b/homeassistant/components/http.py index 99f745adcb2..03d18170b3b 100644 --- a/homeassistant/components/http.py +++ b/homeassistant/components/http.py @@ -30,8 +30,6 @@ from homeassistant.const import ( HTTP_NOT_FOUND, HTTP_OK, HTTP_UNAUTHORIZED, HTTP_UNPROCESSABLE_ENTITY, SERVER_PORT, __version__) -REQUIREMENTS = ["zeroconf==0.17.5"] - DOMAIN = "http" CONF_API_PASSWORD = "api_password" @@ -106,15 +104,14 @@ class HomeAssistantHTTPServer(ThreadingMixIn, HTTPServer): self.development = development self.paths = [] self.sessions = SessionStore() - self.use_ssl = ssl_certificate is not None + self.protocol = 'https' if ssl_certificate is not None else 'http' + self.base_url = "{}://{}:{}".format(self.protocol, + util.get_local_ip(), + self.server_address[1]) # We will lazy init this one if needed self.event_forwarder = None - from zeroconf import Zeroconf - - self.zeroconf = Zeroconf() - if development: _LOGGER.info("running http in development mode") @@ -129,34 +126,10 @@ class HomeAssistantHTTPServer(ThreadingMixIn, HTTPServer): def stop_http(event): """Stop the HTTP server.""" self.shutdown() - self.zeroconf.unregister_all_services() self.hass.bus.listen_once(ha.EVENT_HOMEASSISTANT_STOP, stop_http) - protocol = 'https' if self.use_ssl else 'http' - - base_url = "{}://{}:{}".format(protocol, util.get_local_ip(), - self.server_address[1]) - - zeroconf_type = "_home-assistant._tcp.local." - zeroconf_name = "{}.{}".format(self.hass.config.location_name, - zeroconf_type) - - has_device_tracker = ("device_tracker" in self.hass.config.components) - - params = {"version": __version__, "base_url": base_url, - "device_tracker_component": has_device_tracker, - "needs_password": (self.api_password != "")} - - from zeroconf import ServiceInfo - - info = ServiceInfo(zeroconf_type, zeroconf_name, - socket.inet_aton(util.get_local_ip()), - self.server_address[1], 0, 0, params) - - self.zeroconf.register_service(info) - - _LOGGER.info("Starting web interface at %s", base_url) + _LOGGER.info("Starting web interface at %s", self.base_url) # 31-1-2015: Refactored frontend/api components out of this component # To prevent stuff from breaking, load the two extracted components diff --git a/homeassistant/components/zeroconf.py b/homeassistant/components/zeroconf.py new file mode 100644 index 00000000000..a9e773fae6d --- /dev/null +++ b/homeassistant/components/zeroconf.py @@ -0,0 +1,50 @@ +""" +This module exposes Home Assistant via Zeroconf, also sometimes known as +Bonjour, Rendezvous, Avahi or Multicast DNS (mDNS). + +For more details about Zeroconf, please refer to the documentation at +https://home-assistant.io/components/zeroconf/ +""" +import logging +import socket + +from homeassistant.const import ( + EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, __version__) + +import homeassistant.util as util + +REQUIREMENTS = ["zeroconf==0.17.5"] + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = "zeroconf" + +ZEROCONF_TYPE = "_home-assistant._tcp.local." + +DEPENDENCIES = ["http", "api"] + +def setup(hass, config): + + from zeroconf import Zeroconf, ServiceInfo + + zeroconf = Zeroconf() + + zeroconf_name = "{}.{}".format(hass.config.location_name, + ZEROCONF_TYPE) + + params = {"version": __version__, "base_url": hass.http.base_url, + "has_password": (hass.http.api_password != "")} + + info = ServiceInfo(ZEROCONF_TYPE, zeroconf_name, + socket.inet_aton(util.get_local_ip()), + hass.http.server_address[1], 0, 0, params) + + zeroconf.register_service(info) + + def stop_zeroconf(event): + """Stop Zeroconf.""" + zeroconf.unregister_all_services() + + hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_zeroconf) + + return True diff --git a/requirements_all.txt b/requirements_all.txt index 56829f67f97..720c49a47c8 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -314,5 +314,5 @@ xbee-helper==0.0.6 # homeassistant.components.sensor.yr xmltodict -# homeassistant.components.http +# homeassistant.components.zeroconf zeroconf==0.17.5