Exemplo n.º 1
0
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
Exemplo n.º 2
0
    def ping(self):
        """Send an ICMP echo request and return True if success."""
        next_id = run_callback_threadsafe(self.hass.loop,
                                          async_get_next_ping_id,
                                          self.hass).result()

        return icmp_ping(self.ip_address,
                         count=PING_ATTEMPTS_COUNT,
                         id=next_id).is_alive
Exemplo n.º 3
0
def setup_platform(hass, config, add_entities, discovery_info=None) -> None:
    """Set up the Ping Binary sensor."""
    setup_reload_service(hass, DOMAIN, PLATFORMS)

    host = config[CONF_HOST]
    count = config[CONF_PING_COUNT]
    name = config.get(CONF_NAME, f"{DEFAULT_NAME} {host}")

    try:
        # Verify we can create a raw socket, or
        # fallback to using a subprocess
        icmp_ping("127.0.0.1", count=0, timeout=0)
        ping_cls = PingDataICMPLib
    except SocketPermissionError:
        ping_cls = PingDataSubProcess

    ping_data = ping_cls(hass, host, count)

    add_entities([PingBinarySensor(name, ping_data)], True)
Exemplo n.º 4
0
def setup_scanner(hass, config, see, discovery_info=None):
    """Set up the Host objects and return the update function."""

    try:
        # Verify we can create a raw socket, or
        # fallback to using a subprocess
        icmp_ping("127.0.0.1", count=0, timeout=0)
        host_cls = HostICMPLib
    except SocketPermissionError:
        host_cls = HostSubProcess

    hosts = [
        host_cls(ip, dev_id, hass, config)
        for (dev_id, ip) in config[const.CONF_HOSTS].items()
    ]
    interval = config.get(
        CONF_SCAN_INTERVAL,
        timedelta(seconds=len(hosts) * config[CONF_PING_COUNT]) +
        SCAN_INTERVAL,
    )
    _LOGGER.debug(
        "Started ping tracker with interval=%s on hosts: %s",
        interval,
        ",".join([host.ip_address for host in hosts]),
    )

    def update_interval(now):
        """Update all the hosts on every interval time."""
        try:
            for host in hosts:
                host.update(see)
        finally:
            hass.helpers.event.track_point_in_utc_time(
                update_interval,
                util.dt.utcnow() + interval)

    update_interval(None)
    return True