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 asyncio
import logging import logging
import os import os
from typing import TYPE_CHECKING, Any
from collections import defaultdict from collections import defaultdict
from typing import TYPE_CHECKING, Any
from esphome import const, util from esphome import const, util
from esphome.storage_json import StorageJSON, ext_storage_path from esphome.storage_json import StorageJSON, ext_storage_path

View File

@ -24,8 +24,6 @@ class MDNSStatus:
self.aiozc: AsyncEsphomeZeroconf | None = None self.aiozc: AsyncEsphomeZeroconf | None = None
# This is the current mdns state for each host (True, False, None) # This is the current mdns state for each host (True, False, None)
self.host_mdns_state: dict[str, bool | 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() self._loop = asyncio.get_running_loop()
def get_path_to_host_name(self, path: str) -> str | None: def get_path_to_host_name(self, path: str) -> str | None:
@ -41,24 +39,15 @@ class MDNSStatus:
async def async_refresh_hosts(self): async def async_refresh_hosts(self):
"""Refresh the hosts to track.""" """Refresh the hosts to track."""
dashboard = DASHBOARD 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 host_mdns_state = self.host_mdns_state
entries = dashboard.entries entries = dashboard.entries
for entry in entries.async_all():
for entry in current_entries:
name = entry.name
# If no_mdns is set, remove it from the set
if entry.no_mdns: if entry.no_mdns:
host_name_with_mdns_enabled.discard(name)
continue continue
# We are tracking this host
host_name_with_mdns_enabled.add(name)
# 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 (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)) entries.async_set_state(entry, bool_to_entry_state(online))
async def async_run(self) -> None: async def async_run(self) -> None:
@ -67,16 +56,14 @@ class MDNSStatus:
aiozc = AsyncEsphomeZeroconf() aiozc = AsyncEsphomeZeroconf()
self.aiozc = aiozc self.aiozc = aiozc
host_mdns_state = self.host_mdns_state 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: def on_update(dat: dict[str, bool | None]) -> None:
"""Update the entry state.""" """Update the entry state."""
for name, result in dat.items(): for name, result in dat.items():
host_mdns_state[name] = result host_mdns_state[name] = result
if name not in host_name_with_mdns_enabled:
continue
if matching_entries := entries.get_by_name(name): if matching_entries := entries.get_by_name(name):
for entry in matching_entries: for entry in matching_entries:
if not entry.no_mdns:
entries.async_set_state(entry, bool_to_entry_state(result)) entries.async_set_state(entry, bool_to_entry_state(result))
stat = DashboardStatus(on_update) stat = DashboardStatus(on_update)
@ -89,10 +76,11 @@ class MDNSStatus:
[stat.browser_callback, imports.browser_callback], [stat.browser_callback, imports.browser_callback],
) )
ping_request = dashboard.ping_request
while not dashboard.stop_event.is_set(): while not dashboard.stop_event.is_set():
await self.async_refresh_hosts() await self.async_refresh_hosts()
await dashboard.ping_request.wait() await ping_request.wait()
dashboard.ping_request.clear() ping_request.clear()
await browser.async_cancel() await browser.async_cancel()
await aiozc.async_close() await aiozc.async_close()

View File

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