Add more debug logging to Ping integration (#119318)

This commit is contained in:
Jan-Philipp Benecke 2024-06-10 20:09:39 +02:00 committed by GitHub
parent 42b984ee4f
commit 508564ece2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,6 +9,7 @@ from typing import TYPE_CHECKING, Any
from icmplib import NameLookupError, async_ping from icmplib import NameLookupError, async_ping
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import UpdateFailed
from .const import ICMP_TIMEOUT, PING_TIMEOUT from .const import ICMP_TIMEOUT, PING_TIMEOUT
@ -58,9 +59,16 @@ class PingDataICMPLib(PingData):
timeout=ICMP_TIMEOUT, timeout=ICMP_TIMEOUT,
privileged=self._privileged, privileged=self._privileged,
) )
except NameLookupError: except NameLookupError as err:
self.is_alive = False self.is_alive = False
return raise UpdateFailed(f"Error resolving host: {self.ip_address}") from err
_LOGGER.debug(
"async_ping returned: reachable=%s sent=%i received=%s",
data.is_alive,
data.packets_sent,
data.packets_received,
)
self.is_alive = data.is_alive self.is_alive = data.is_alive
if not self.is_alive: if not self.is_alive:
@ -94,6 +102,10 @@ class PingDataSubProcess(PingData):
async def async_ping(self) -> dict[str, Any] | None: async def async_ping(self) -> dict[str, Any] | None:
"""Send ICMP echo request and return details if success.""" """Send ICMP echo request and return details if success."""
_LOGGER.debug(
"Pinging %s with: `%s`", self.ip_address, " ".join(self._ping_cmd)
)
pinger = await asyncio.create_subprocess_exec( pinger = await asyncio.create_subprocess_exec(
*self._ping_cmd, *self._ping_cmd,
stdin=None, stdin=None,
@ -140,20 +152,17 @@ class PingDataSubProcess(PingData):
if TYPE_CHECKING: if TYPE_CHECKING:
assert match is not None assert match is not None
rtt_min, rtt_avg, rtt_max, rtt_mdev = match.groups() rtt_min, rtt_avg, rtt_max, rtt_mdev = match.groups()
except TimeoutError: except TimeoutError as err:
_LOGGER.exception(
"Timed out running command: `%s`, after: %ss",
self._ping_cmd,
self._count + PING_TIMEOUT,
)
if pinger: if pinger:
with suppress(TypeError): with suppress(TypeError):
await pinger.kill() # type: ignore[func-returns-value] await pinger.kill() # type: ignore[func-returns-value]
del pinger del pinger
return None raise UpdateFailed(
except AttributeError: f"Timed out running command: `{self._ping_cmd}`, after: {self._count + PING_TIMEOUT}s"
return None ) from err
except AttributeError as err:
raise UpdateFailed from err
return {"min": rtt_min, "avg": rtt_avg, "max": rtt_max, "mdev": rtt_mdev} return {"min": rtt_min, "avg": rtt_avg, "max": rtt_max, "mdev": rtt_mdev}
async def async_update(self) -> None: async def async_update(self) -> None: