Add ZeroConf support to http.py
parent
29fb6faa40
commit
86dad0c045
|
@ -7,6 +7,7 @@ https://home-assistant.io/developers/api/
|
||||||
import gzip
|
import gzip
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import socket
|
||||||
import ssl
|
import ssl
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
@ -27,7 +28,9 @@ from homeassistant.const import (
|
||||||
HTTP_HEADER_CONTENT_LENGTH, HTTP_HEADER_CONTENT_TYPE, HTTP_HEADER_EXPIRES,
|
HTTP_HEADER_CONTENT_LENGTH, HTTP_HEADER_CONTENT_TYPE, HTTP_HEADER_EXPIRES,
|
||||||
HTTP_HEADER_HA_AUTH, HTTP_HEADER_VARY, HTTP_METHOD_NOT_ALLOWED,
|
HTTP_HEADER_HA_AUTH, HTTP_HEADER_VARY, HTTP_METHOD_NOT_ALLOWED,
|
||||||
HTTP_NOT_FOUND, HTTP_OK, HTTP_UNAUTHORIZED, HTTP_UNPROCESSABLE_ENTITY,
|
HTTP_NOT_FOUND, HTTP_OK, HTTP_UNAUTHORIZED, HTTP_UNPROCESSABLE_ENTITY,
|
||||||
SERVER_PORT)
|
SERVER_PORT, __version__)
|
||||||
|
|
||||||
|
REQUIREMENTS = ["zeroconf==0.17.5"]
|
||||||
|
|
||||||
DOMAIN = "http"
|
DOMAIN = "http"
|
||||||
|
|
||||||
|
@ -108,6 +111,10 @@ class HomeAssistantHTTPServer(ThreadingMixIn, HTTPServer):
|
||||||
# We will lazy init this one if needed
|
# We will lazy init this one if needed
|
||||||
self.event_forwarder = None
|
self.event_forwarder = None
|
||||||
|
|
||||||
|
from zeroconf import Zeroconf
|
||||||
|
|
||||||
|
self.zeroconf = Zeroconf()
|
||||||
|
|
||||||
if development:
|
if development:
|
||||||
_LOGGER.info("running http in development mode")
|
_LOGGER.info("running http in development mode")
|
||||||
|
|
||||||
|
@ -122,14 +129,35 @@ class HomeAssistantHTTPServer(ThreadingMixIn, HTTPServer):
|
||||||
def stop_http(event):
|
def stop_http(event):
|
||||||
"""Stop the HTTP server."""
|
"""Stop the HTTP server."""
|
||||||
self.shutdown()
|
self.shutdown()
|
||||||
|
self.zeroconf.unregister_all_services()
|
||||||
|
|
||||||
self.hass.bus.listen_once(ha.EVENT_HOMEASSISTANT_STOP, stop_http)
|
self.hass.bus.listen_once(ha.EVENT_HOMEASSISTANT_STOP, stop_http)
|
||||||
|
|
||||||
protocol = 'https' if self.use_ssl else 'http'
|
protocol = 'https' if self.use_ssl else 'http'
|
||||||
|
|
||||||
_LOGGER.info(
|
base_url = "{}://{}:{}".format(protocol, util.get_local_ip(),
|
||||||
"Starting web interface at %s://%s:%d",
|
self.server_address[1])
|
||||||
protocol, self.server_address[0], self.server_address[1])
|
|
||||||
|
zeroconf_type = "_home-assistant._tcp.local."
|
||||||
|
zeroconf_name = "{}.{}".format(self.hass.config.location_name,
|
||||||
|
zeroconf_type)
|
||||||
|
|
||||||
|
ip_address = socket.inet_aton(util.get_local_ip())
|
||||||
|
|
||||||
|
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, ip_address,
|
||||||
|
self.server_address[1], 0, 0, params)
|
||||||
|
|
||||||
|
self.zeroconf.register_service(info)
|
||||||
|
|
||||||
|
_LOGGER.info("Starting web interface at %s", base_url)
|
||||||
|
|
||||||
# 31-1-2015: Refactored frontend/api components out of this component
|
# 31-1-2015: Refactored frontend/api components out of this component
|
||||||
# To prevent stuff from breaking, load the two extracted components
|
# To prevent stuff from breaking, load the two extracted components
|
||||||
|
|
|
@ -313,3 +313,6 @@ xbee-helper==0.0.6
|
||||||
|
|
||||||
# homeassistant.components.sensor.yr
|
# homeassistant.components.sensor.yr
|
||||||
xmltodict
|
xmltodict
|
||||||
|
|
||||||
|
# homeassistant.components.http
|
||||||
|
zeroconf==0.17.5
|
||||||
|
|
Loading…
Reference in New Issue