1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-30 14:43:51 +00:00

Add local mDNS responder for .local (#386)

* Add local mDNS responder

* Fix

* Use mDNS in dashboard status

* Lint

* Lint

* Fix test

* Remove hostname

* Fix use enum

* Lint
This commit is contained in:
Otto Winter
2019-02-10 16:57:34 +01:00
committed by GitHub
parent fcf5da66c0
commit d9cf91210e
13 changed files with 837 additions and 63 deletions

View File

@@ -2,11 +2,9 @@
from __future__ import print_function
import codecs
import collections
import hmac
import json
import logging
import multiprocessing
import os
import subprocess
import threading
@@ -25,7 +23,7 @@ import tornado.websocket
from esphomeyaml import const
from esphomeyaml.__main__ import get_serial_ports
from esphomeyaml.helpers import mkdir_p, run_system_command
from esphomeyaml.helpers import mkdir_p
from esphomeyaml.py_compat import IS_PY2
from esphomeyaml.storage_json import EsphomeyamlStorageJSON, StorageJSON, \
esphomeyaml_storage_path, ext_storage_path
@@ -34,6 +32,8 @@ from esphomeyaml.util import shlex_quote
# pylint: disable=unused-import, wrong-import-order
from typing import Optional # noqa
from esphomeyaml.zeroconf import Zeroconf, DashboardStatus
_LOGGER = logging.getLogger(__name__)
CONFIG_DIR = ''
PASSWORD_DIGEST = ''
@@ -315,55 +315,25 @@ class MainRequestHandler(BaseHandler):
get_static_file_url=get_static_file_url)
def _ping_func(filename, address):
if os.name == 'nt':
command = ['ping', '-n', '1', address]
else:
command = ['ping', '-c', '1', address]
rc, _, _ = run_system_command(*command)
return filename, rc == 0
class PingThread(threading.Thread):
def run(self):
pool = multiprocessing.Pool(processes=8)
zc = Zeroconf()
def on_update(dat):
for key, b in dat.items():
PING_RESULT[key] = b
stat = DashboardStatus(zc, on_update)
stat.start()
while not STOP_EVENT.is_set():
# Only do pings if somebody has the dashboard open
entries = _list_dashboard_entries()
stat.request_query({entry.filename: entry.name + '.local.' for entry in entries})
PING_REQUEST.wait()
PING_REQUEST.clear()
def callback(ret):
PING_RESULT[ret[0]] = ret[1]
entries = _list_dashboard_entries()
queue = collections.deque()
for entry in entries:
if entry.address is None:
PING_RESULT[entry.filename] = None
continue
result = pool.apply_async(_ping_func, (entry.filename, entry.address),
callback=callback)
queue.append(result)
while queue:
item = queue[0]
if item.ready():
queue.popleft()
continue
try:
item.get(0.1)
except OSError:
# ping not installed
pass
except multiprocessing.TimeoutError:
pass
if STOP_EVENT.is_set():
pool.terminate()
return
stat.stop()
stat.join()
zc.close()
class PingRequestHandler(BaseHandler):