core/homeassistant/components/ping/device_tracker.py

155 lines
4.9 KiB
Python
Raw Normal View History

"""Tracks devices by sending a ICMP echo request (ping)."""
from __future__ import annotations
import asyncio
from datetime import timedelta
import logging
import subprocess
from icmplib import async_multiping
import voluptuous as vol
from homeassistant import util
from homeassistant.components.device_tracker import (
2019-07-31 19:25:30 +00:00
CONF_SCAN_INTERVAL,
PLATFORM_SCHEMA as BASE_PLATFORM_SCHEMA,
2019-07-31 19:25:30 +00:00
SCAN_INTERVAL,
AsyncSeeCallback,
SourceType,
2019-07-31 19:25:30 +00:00
)
from homeassistant.const import CONF_HOSTS
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.async_ import gather_with_concurrency
from homeassistant.util.process import kill_subprocess
from .const import DOMAIN, ICMP_TIMEOUT, PING_ATTEMPTS_COUNT, PING_PRIVS, PING_TIMEOUT
_LOGGER = logging.getLogger(__name__)
PARALLEL_UPDATES = 0
2019-07-31 19:25:30 +00:00
CONF_PING_COUNT = "count"
CONCURRENT_PING_LIMIT = 6
PLATFORM_SCHEMA = BASE_PLATFORM_SCHEMA.extend(
2019-07-31 19:25:30 +00:00
{
vol.Required(CONF_HOSTS): {cv.slug: cv.string},
2019-07-31 19:25:30 +00:00
vol.Optional(CONF_PING_COUNT, default=1): cv.positive_int,
}
)
class HostSubProcess:
"""Host object with ping detection."""
def __init__(self, ip_address, dev_id, hass, config, privileged):
"""Initialize the Host pinger."""
self.hass = hass
self.ip_address = ip_address
self.dev_id = dev_id
self._count = config[CONF_PING_COUNT]
self._ping_cmd = ["ping", "-n", "-q", "-c1", "-W1", ip_address]
def ping(self):
2017-04-11 16:05:27 +00:00
"""Send an ICMP echo request and return True if success."""
2021-04-25 00:39:24 +00:00
with subprocess.Popen(
Avoid subprocess memory copy when c library supports posix_spawn (#87958) * use posix spawn on alpine * Avoid subprocess memory copy when c library supports posix_spawn By default python 3.10 will use the fork() which has to copy all the memory of the parent process (in our case this can be huge since Home Assistant core can use hundreds of megabytes of RAM). By using posix_spawn this is avoided. In python 3.11 vfork will also be available https://github.com/python/cpython/issues/80004#issuecomment-1093810689 https://github.com/python/cpython/pull/11671 but we won't always be able to use it and posix_spawn is considered safer https://bugzilla.kernel.org/show_bug.cgi?id=215813#c14 The subprocess library doesn't know about musl though even though it supports posix_spawn https://git.musl-libc.org/cgit/musl/log/src/process/posix_spawn.c so we have to teach it since it only has checks for glibc https://github.com/python/cpython/blob/1b736838e6ae1b4ef42cdd27c2708face908f92c/Lib/subprocess.py#L745 The constant is documented as being able to be flipped here: https://docs.python.org/3/library/subprocess.html#disabling-use-of-vfork-or-posix-spawn * Avoid subprocess memory copy when c library supports posix_spawn By default python 3.10 will use the fork() which has to copy memory of the parent process (in our case this can be huge since Home Assistant core can use hundreds of megabytes of RAM). By using posix_spawn this is avoided and subprocess creation does not get discernibly slow the larger the Home Assistant python process grows. In python 3.11 vfork will also be available https://github.com/python/cpython/issues/80004#issuecomment-1093810689 https://github.com/python/cpython/pull/11671 but we won't always be able to use it and posix_spawn is considered safer https://bugzilla.kernel.org/show_bug.cgi?id=215813#c14 The subprocess library doesn't know about musl though even though it supports posix_spawn https://git.musl-libc.org/cgit/musl/log/src/process/posix_spawn.c so we have to teach it since it only has checks for glibc https://github.com/python/cpython/blob/1b736838e6ae1b4ef42cdd27c2708face908f92c/Lib/subprocess.py#L745 The constant is documented as being able to be flipped here: https://docs.python.org/3/library/subprocess.html#disabling-use-of-vfork-or-posix-spawn * missed some * adjust more tests * coverage
2023-02-13 14:02:51 +00:00
self._ping_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
close_fds=False, # required for posix_spawn
2021-04-25 00:39:24 +00:00
) as pinger:
try:
pinger.communicate(timeout=1 + PING_TIMEOUT)
return pinger.returncode == 0
except subprocess.TimeoutExpired:
kill_subprocess(pinger)
return False
except subprocess.CalledProcessError:
return False
def update(self) -> bool:
"""Update device state by sending one or more ping messages."""
failed = 0
2017-06-08 05:30:51 +00:00
while failed < self._count: # check more times if host is unreachable
if self.ping():
return True
failed += 1
2017-04-11 16:05:27 +00:00
_LOGGER.debug("No response from %s failed=%d", self.ip_address, failed)
return False
async def async_setup_scanner(
hass: HomeAssistant,
config: ConfigType,
async_see: AsyncSeeCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> bool:
"""Set up the Host objects and return the update function."""
privileged = hass.data[DOMAIN][PING_PRIVS]
ip_to_dev_id = {ip: dev_id for (dev_id, ip) in config[CONF_HOSTS].items()}
2019-07-31 19:25:30 +00:00
interval = config.get(
CONF_SCAN_INTERVAL,
timedelta(seconds=len(ip_to_dev_id) * config[CONF_PING_COUNT]) + SCAN_INTERVAL,
2019-07-31 19:25:30 +00:00
)
_LOGGER.debug(
"Started ping tracker with interval=%s on hosts: %s",
interval,
",".join(ip_to_dev_id.keys()),
2019-07-31 19:25:30 +00:00
)
if privileged is None:
hosts = [
HostSubProcess(ip, dev_id, hass, config, privileged)
for (dev_id, ip) in config[CONF_HOSTS].items()
]
async def async_update(now):
"""Update all the hosts on every interval time."""
results = await gather_with_concurrency(
CONCURRENT_PING_LIMIT,
*(hass.async_add_executor_job(host.update) for host in hosts),
)
await asyncio.gather(
*(
async_see(dev_id=host.dev_id, source_type=SourceType.ROUTER)
for idx, host in enumerate(hosts)
if results[idx]
)
)
else:
async def async_update(now):
"""Update all the hosts on every interval time."""
responses = await async_multiping(
list(ip_to_dev_id),
count=PING_ATTEMPTS_COUNT,
timeout=ICMP_TIMEOUT,
privileged=privileged,
)
_LOGGER.debug("Multiping responses: %s", responses)
await asyncio.gather(
*(
async_see(dev_id=dev_id, source_type=SourceType.ROUTER)
for idx, dev_id in enumerate(ip_to_dev_id.values())
if responses[idx].is_alive
)
)
async def _async_update_interval(now):
try:
await async_update(now)
finally:
if not hass.is_stopping:
async_track_point_in_utc_time(
hass, _async_update_interval, util.dt.utcnow() + interval
)
await _async_update_interval(None)
return True