Update icmplib privilege detection function to be async in ping integration (#103925)

* Make icmplib privilege detection function async

* I should also commit the tests..
pull/103875/head^2
Jan-Philipp Benecke 2023-11-13 19:37:58 +01:00 committed by GitHub
parent 1610dd94f9
commit f0a455e5c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 6 deletions

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
import logging import logging
from icmplib import SocketPermissionError, ping as icmp_ping from icmplib import SocketPermissionError, async_ping
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
@ -30,19 +30,19 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
await async_setup_reload_service(hass, DOMAIN, PLATFORMS) await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
hass.data[DOMAIN] = PingDomainData( hass.data[DOMAIN] = PingDomainData(
privileged=await hass.async_add_executor_job(_can_use_icmp_lib_with_privilege), privileged=await _can_use_icmp_lib_with_privilege(),
) )
return True return True
def _can_use_icmp_lib_with_privilege() -> None | bool: async def _can_use_icmp_lib_with_privilege() -> None | bool:
"""Verify we can create a raw socket.""" """Verify we can create a raw socket."""
try: try:
icmp_ping("127.0.0.1", count=0, timeout=0, privileged=True) await async_ping("127.0.0.1", count=0, timeout=0, privileged=True)
except SocketPermissionError: except SocketPermissionError:
try: try:
icmp_ping("127.0.0.1", count=0, timeout=0, privileged=False) await async_ping("127.0.0.1", count=0, timeout=0, privileged=False)
except SocketPermissionError: except SocketPermissionError:
_LOGGER.debug( _LOGGER.debug(
"Cannot use icmplib because privileges are insufficient to create the" "Cannot use icmplib because privileges are insufficient to create the"

View File

@ -14,7 +14,7 @@ from tests.common import get_fixture_path
@pytest.fixture @pytest.fixture
def mock_ping() -> None: def mock_ping() -> None:
"""Mock icmplib.ping.""" """Mock icmplib.ping."""
with patch("homeassistant.components.ping.icmp_ping"): with patch("homeassistant.components.ping.async_ping"):
yield yield