mirror of
https://github.com/esphome/esphome.git
synced 2025-10-15 15:07:52 +02:00
Fix log retrieval with FQDN when mDNS is disabled (#11202)
This commit is contained in:
parent
3df4dbd3a6
commit
9fb254fdc2
@ -268,8 +268,10 @@ def has_ip_address() -> bool:
|
||||
|
||||
|
||||
def has_resolvable_address() -> bool:
|
||||
"""Check if CORE.address is resolvable (via mDNS or is an IP address)."""
|
||||
return has_mdns() or has_ip_address()
|
||||
"""Check if CORE.address is resolvable (via mDNS, DNS, or is an IP address)."""
|
||||
# Any address (IP, mDNS hostname, or regular DNS hostname) is resolvable
|
||||
# The resolve_ip_address() function in helpers.py handles all types via AsyncResolver
|
||||
return CORE.address is not None
|
||||
|
||||
|
||||
def mqtt_get_ip(config: ConfigType, username: str, password: str, client_id: str):
|
||||
@ -578,11 +580,12 @@ def show_logs(config: ConfigType, args: ArgsProtocol, devices: list[str]) -> int
|
||||
if has_api():
|
||||
addresses_to_use: list[str] | None = None
|
||||
|
||||
if port_type == "NETWORK" and (has_mdns() or is_ip_address(port)):
|
||||
if port_type == "NETWORK":
|
||||
# Network addresses (IPs, mDNS names, or regular DNS hostnames) can be used
|
||||
# The resolve_ip_address() function in helpers.py handles all types
|
||||
addresses_to_use = devices
|
||||
elif port_type in ("NETWORK", "MQTT", "MQTTIP") and has_mqtt_ip_lookup():
|
||||
# Only use MQTT IP lookup if the first condition didn't match
|
||||
# (for MQTT/MQTTIP types, or for NETWORK when mdns/ip check fails)
|
||||
elif port_type in ("MQTT", "MQTTIP") and has_mqtt_ip_lookup():
|
||||
# Use MQTT IP lookup for MQTT/MQTTIP types
|
||||
addresses_to_use = mqtt_get_ip(
|
||||
config, args.username, args.password, args.client_id
|
||||
)
|
||||
|
@ -1203,6 +1203,31 @@ def test_show_logs_api(
|
||||
)
|
||||
|
||||
|
||||
@patch("esphome.components.api.client.run_logs")
|
||||
def test_show_logs_api_with_fqdn_mdns_disabled(
|
||||
mock_run_logs: Mock,
|
||||
) -> None:
|
||||
"""Test show_logs with API using FQDN when mDNS is disabled."""
|
||||
setup_core(
|
||||
config={
|
||||
"logger": {},
|
||||
CONF_API: {},
|
||||
CONF_MDNS: {CONF_DISABLED: True},
|
||||
},
|
||||
platform=PLATFORM_ESP32,
|
||||
)
|
||||
mock_run_logs.return_value = 0
|
||||
|
||||
args = MockArgs()
|
||||
devices = ["device.example.com"]
|
||||
|
||||
result = show_logs(CORE.config, args, devices)
|
||||
|
||||
assert result == 0
|
||||
# Should use the FQDN directly, not try MQTT lookup
|
||||
mock_run_logs.assert_called_once_with(CORE.config, ["device.example.com"])
|
||||
|
||||
|
||||
@patch("esphome.components.api.client.run_logs")
|
||||
def test_show_logs_api_with_mqtt_fallback(
|
||||
mock_run_logs: Mock,
|
||||
@ -1222,7 +1247,7 @@ def test_show_logs_api_with_mqtt_fallback(
|
||||
mock_mqtt_get_ip.return_value = ["192.168.1.200"]
|
||||
|
||||
args = MockArgs(username="user", password="pass", client_id="client")
|
||||
devices = ["device.local"]
|
||||
devices = ["MQTTIP"]
|
||||
|
||||
result = show_logs(CORE.config, args, devices)
|
||||
|
||||
@ -1487,27 +1512,31 @@ def test_mqtt_get_ip() -> None:
|
||||
def test_has_resolvable_address() -> None:
|
||||
"""Test has_resolvable_address function."""
|
||||
|
||||
# Test with mDNS enabled and hostname address
|
||||
# Test with mDNS enabled and .local hostname address
|
||||
setup_core(config={}, address="esphome-device.local")
|
||||
assert has_resolvable_address() is True
|
||||
|
||||
# Test with mDNS disabled and hostname address
|
||||
# Test with mDNS disabled and .local hostname address (still resolvable via DNS)
|
||||
setup_core(
|
||||
config={CONF_MDNS: {CONF_DISABLED: True}}, address="esphome-device.local"
|
||||
)
|
||||
assert has_resolvable_address() is False
|
||||
assert has_resolvable_address() is True
|
||||
|
||||
# Test with IP address (mDNS doesn't matter)
|
||||
# Test with mDNS disabled and regular DNS hostname (resolvable)
|
||||
setup_core(config={CONF_MDNS: {CONF_DISABLED: True}}, address="device.example.com")
|
||||
assert has_resolvable_address() is True
|
||||
|
||||
# Test with IP address (always resolvable, mDNS doesn't matter)
|
||||
setup_core(config={}, address="192.168.1.100")
|
||||
assert has_resolvable_address() is True
|
||||
|
||||
# Test with IP address and mDNS disabled
|
||||
# Test with IP address and mDNS disabled (still resolvable)
|
||||
setup_core(config={CONF_MDNS: {CONF_DISABLED: True}}, address="192.168.1.100")
|
||||
assert has_resolvable_address() is True
|
||||
|
||||
# Test with no address but mDNS enabled (can still resolve mDNS names)
|
||||
# Test with no address
|
||||
setup_core(config={}, address=None)
|
||||
assert has_resolvable_address() is True
|
||||
assert has_resolvable_address() is False
|
||||
|
||||
# Test with no address and mDNS disabled
|
||||
setup_core(config={CONF_MDNS: {CONF_DISABLED: True}}, address=None)
|
||||
|
Loading…
x
Reference in New Issue
Block a user