core/homeassistant/components/ping/__init__.py

58 lines
1.6 KiB
Python

"""The ping component."""
from __future__ import annotations
from dataclasses import dataclass
import logging
from icmplib import SocketPermissionError, ping as icmp_ping
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN, PLATFORMS
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = cv.platform_only_config_schema(DOMAIN)
@dataclass(slots=True)
class PingDomainData:
"""Dataclass to store privileged status."""
privileged: bool | None
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the ping integration."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
hass.data[DOMAIN] = PingDomainData(
privileged=await hass.async_add_executor_job(_can_use_icmp_lib_with_privilege),
)
return True
def _can_use_icmp_lib_with_privilege() -> None | bool:
"""Verify we can create a raw socket."""
try:
icmp_ping("127.0.0.1", count=0, timeout=0, privileged=True)
except SocketPermissionError:
try:
icmp_ping("127.0.0.1", count=0, timeout=0, privileged=False)
except SocketPermissionError:
_LOGGER.debug(
"Cannot use icmplib because privileges are insufficient to create the"
" socket"
)
return None
_LOGGER.debug("Using icmplib in privileged=False mode")
return False
_LOGGER.debug("Using icmplib in privileged=True mode")
return True