2019-03-17 03:44:05 +00:00
|
|
|
"""The ping component."""
|
2021-03-31 13:06:49 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from icmplib import SocketPermissionError, ping as icmp_ping
|
2020-08-28 17:40:30 +00:00
|
|
|
|
2020-09-09 20:19:14 +00:00
|
|
|
from homeassistant.core import callback
|
2021-03-31 13:06:49 +00:00
|
|
|
from homeassistant.helpers.reload import async_setup_reload_service
|
|
|
|
|
|
|
|
from .const import DEFAULT_START_ID, DOMAIN, MAX_PING_ID, PING_ID, PING_PRIVS, PLATFORMS
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2020-09-09 20:19:14 +00:00
|
|
|
|
|
|
|
|
2021-03-31 13:06:49 +00:00
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the template integration."""
|
|
|
|
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
|
|
|
hass.data[DOMAIN] = {
|
|
|
|
PING_PRIVS: await hass.async_add_executor_job(_can_use_icmp_lib_with_privilege),
|
|
|
|
PING_ID: DEFAULT_START_ID,
|
|
|
|
}
|
|
|
|
return True
|
2020-09-09 20:19:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2021-04-09 23:56:15 +00:00
|
|
|
def async_get_next_ping_id(hass, count=1):
|
2020-09-09 20:19:14 +00:00
|
|
|
"""Find the next id to use in the outbound ping.
|
|
|
|
|
2021-04-09 23:56:15 +00:00
|
|
|
When using multiping, we increment the id
|
|
|
|
by the number of ids that multiping
|
|
|
|
will use.
|
|
|
|
|
2020-09-09 20:19:14 +00:00
|
|
|
Must be called in async
|
|
|
|
"""
|
2021-04-09 23:56:15 +00:00
|
|
|
allocated_id = hass.data[DOMAIN][PING_ID] + 1
|
|
|
|
if allocated_id > MAX_PING_ID:
|
|
|
|
allocated_id -= MAX_PING_ID - DEFAULT_START_ID
|
|
|
|
hass.data[DOMAIN][PING_ID] += count
|
|
|
|
if hass.data[DOMAIN][PING_ID] > MAX_PING_ID:
|
|
|
|
hass.data[DOMAIN][PING_ID] -= MAX_PING_ID - DEFAULT_START_ID
|
|
|
|
return allocated_id
|
2021-03-31 13:06:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
else:
|
|
|
|
_LOGGER.debug("Using icmplib in privileged=False mode")
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
_LOGGER.debug("Using icmplib in privileged=True mode")
|
|
|
|
return True
|