dashboard: Fix online status when api is disabled (#5791)

This commit is contained in:
J. Nick Koston 2023-11-21 01:15:32 +01:00 committed by GitHub
parent 1a9f66e630
commit 47d42afda3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View file

@ -973,6 +973,7 @@ class MDNSStatusThread(threading.Thread):
self.host_name_to_filename: dict[str, str] = {} self.host_name_to_filename: dict[str, str] = {}
# This is a set of host names to track (i.e no_mdns = false) # This is a set of host names to track (i.e no_mdns = false)
self.host_name_with_mdns_enabled: set[set] = set() self.host_name_with_mdns_enabled: set[set] = set()
self.zc: EsphomeZeroconf | None = None
self._refresh_hosts() self._refresh_hosts()
def _refresh_hosts(self): def _refresh_hosts(self):
@ -996,7 +997,14 @@ class MDNSStatusThread(threading.Thread):
# If we just adopted/imported this host, we likely # If we just adopted/imported this host, we likely
# already have a state for it, so we should make sure # already have a state for it, so we should make sure
# to set it so the dashboard shows it as online # to set it so the dashboard shows it as online
if name in host_mdns_state: if self.zc and (
entry.loaded_integrations and "api" not in entry.loaded_integrations
):
# No api available so we have to poll since
# the device won't respond to a request to ._esphomelib._tcp.local.
PING_RESULT[filename] = bool(self.zc.resolve_host(entry.name))
elif name in host_mdns_state:
# We already have a state for this host
PING_RESULT[filename] = host_mdns_state[name] PING_RESULT[filename] = host_mdns_state[name]
# Make sure the mapping is up to date # Make sure the mapping is up to date
@ -1007,7 +1015,8 @@ class MDNSStatusThread(threading.Thread):
def run(self): def run(self):
global IMPORT_RESULT global IMPORT_RESULT
zc = EsphomeZeroconf() self.zc = EsphomeZeroconf()
zc = self.zc
host_mdns_state = self.host_mdns_state host_mdns_state = self.host_mdns_state
host_name_to_filename = self.host_name_to_filename host_name_to_filename = self.host_name_to_filename
host_name_with_mdns_enabled = self.host_name_with_mdns_enabled host_name_with_mdns_enabled = self.host_name_with_mdns_enabled

View file

@ -150,7 +150,11 @@ class EsphomeZeroconf(Zeroconf):
def resolve_host(self, host: str, timeout: float = 3.0) -> str | None: def resolve_host(self, host: str, timeout: float = 3.0) -> str | None:
"""Resolve a host name to an IP address.""" """Resolve a host name to an IP address."""
name = host.partition(".")[0] name = host.partition(".")[0]
info = HostResolver(ESPHOME_SERVICE_TYPE, f"{name}.{ESPHOME_SERVICE_TYPE}") info = HostResolver(
ESPHOME_SERVICE_TYPE,
f"{name}.{ESPHOME_SERVICE_TYPE}",
server=f"{name}.local.",
)
if ( if (
info.load_from_cache(self) info.load_from_cache(self)
or (timeout and info.request(self, timeout * 1000)) or (timeout and info.request(self, timeout * 1000))