1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-15 15:18:16 +00:00
This commit is contained in:
J. Nick Koston 2023-11-17 21:26:53 -06:00
parent a7275f2ef8
commit d2b85e8b9a
No known key found for this signature in database
3 changed files with 11 additions and 21 deletions

View File

@ -3,8 +3,9 @@ from __future__ import annotations
import asyncio
import logging
import os
from typing import TYPE_CHECKING, Any
from collections import defaultdict
from typing import TYPE_CHECKING, Any
from esphome import const, util
from esphome.storage_json import StorageJSON, ext_storage_path

View File

@ -24,8 +24,6 @@ class MDNSStatus:
self.aiozc: AsyncEsphomeZeroconf | None = None
# This is the current mdns state for each host (True, False, None)
self.host_mdns_state: dict[str, bool | None] = {}
# This is a set of host names to track (i.e no_mdns = false)
self.host_name_with_mdns_enabled: set[set] = set()
self._loop = asyncio.get_running_loop()
def get_path_to_host_name(self, path: str) -> str | None:
@ -41,24 +39,15 @@ class MDNSStatus:
async def async_refresh_hosts(self):
"""Refresh the hosts to track."""
dashboard = DASHBOARD
current_entries = dashboard.entries.async_all()
host_name_with_mdns_enabled = self.host_name_with_mdns_enabled
host_mdns_state = self.host_mdns_state
entries = dashboard.entries
for entry in current_entries:
name = entry.name
# If no_mdns is set, remove it from the set
for entry in entries.async_all():
if entry.no_mdns:
host_name_with_mdns_enabled.discard(name)
continue
# We are tracking this host
host_name_with_mdns_enabled.add(name)
# If we just adopted/imported this host, we likely
# already have a state for it, so we should make sure
# to set it so the dashboard shows it as online
if (online := host_mdns_state.get(name, SENTINEL)) != SENTINEL:
if (online := host_mdns_state.get(entry.name, SENTINEL)) != SENTINEL:
entries.async_set_state(entry, bool_to_entry_state(online))
async def async_run(self) -> None:
@ -67,17 +56,15 @@ class MDNSStatus:
aiozc = AsyncEsphomeZeroconf()
self.aiozc = aiozc
host_mdns_state = self.host_mdns_state
host_name_with_mdns_enabled = self.host_name_with_mdns_enabled
def on_update(dat: dict[str, bool | None]) -> None:
"""Update the entry state."""
for name, result in dat.items():
host_mdns_state[name] = result
if name not in host_name_with_mdns_enabled:
continue
if matching_entries := entries.get_by_name(name):
for entry in matching_entries:
entries.async_set_state(entry, bool_to_entry_state(result))
if not entry.no_mdns:
entries.async_set_state(entry, bool_to_entry_state(result))
stat = DashboardStatus(on_update)
imports = DashboardImportDiscovery()
@ -89,10 +76,11 @@ class MDNSStatus:
[stat.browser_callback, imports.browser_callback],
)
ping_request = dashboard.ping_request
while not dashboard.stop_event.is_set():
await self.async_refresh_hosts()
await dashboard.ping_request.wait()
dashboard.ping_request.clear()
await ping_request.wait()
ping_request.clear()
await browser.async_cancel()
await aiozc.async_close()

View File

@ -270,13 +270,14 @@ class EsphomePortCommandWebSocket(EsphomeCommandWebSocket):
) -> list[str]:
"""Build the command to run."""
dashboard = DASHBOARD
entries = dashboard.entries
configuration = json_message["configuration"]
config_file = settings.rel_path(configuration)
port = json_message["port"]
if (
port == "OTA"
and (mdns := dashboard.mdns_status)
and (host_name := mdns.get_path_to_host_name(config_file))
and (host_name := entries.get(configuration))
and (address := await mdns.async_resolve_host(host_name))
):
port = address