Revert "De-syncing binary_sensor.ping (#17056)" (#17606)

This reverts commit 11d5671ee0a4dbf98c5e9f58a2dec85bdb35f4d0.
This commit is contained in:
Thomas Lovén 2018-10-19 09:29:48 +02:00 committed by Paulus Schoutsen
parent 90183bd682
commit 8bf58e1df5

View File

@ -4,19 +4,18 @@ Tracks the latency of a host by sending ICMP echo requests (ping).
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/binary_sensor.ping/ https://home-assistant.io/components/binary_sensor.ping/
""" """
import asyncio
from datetime import timedelta
import logging import logging
import re
import subprocess import subprocess
import re
import sys import sys
from datetime import timedelta
import voluptuous as vol import voluptuous as vol
from homeassistant.components.binary_sensor import (
PLATFORM_SCHEMA, BinarySensorDevice)
from homeassistant.const import CONF_HOST, CONF_NAME
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.components.binary_sensor import (
BinarySensorDevice, PLATFORM_SCHEMA)
from homeassistant.const import CONF_NAME, CONF_HOST
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -49,14 +48,13 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
}) })
async def async_setup_platform( def setup_platform(hass, config, add_entities, discovery_info=None):
hass, config, async_add_entities, discovery_info=None):
"""Set up the Ping Binary sensor.""" """Set up the Ping Binary sensor."""
name = config.get(CONF_NAME) name = config.get(CONF_NAME)
host = config.get(CONF_HOST) host = config.get(CONF_HOST)
count = config.get(CONF_PING_COUNT) count = config.get(CONF_PING_COUNT)
async_add_entities([PingBinarySensor(name, PingData(host, count))], True) add_entities([PingBinarySensor(name, PingData(host, count))], True)
class PingBinarySensor(BinarySensorDevice): class PingBinarySensor(BinarySensorDevice):
@ -93,9 +91,9 @@ class PingBinarySensor(BinarySensorDevice):
ATTR_ROUND_TRIP_TIME_MIN: self.ping.data['min'], ATTR_ROUND_TRIP_TIME_MIN: self.ping.data['min'],
} }
async def async_update(self): def update(self):
"""Get the latest data.""" """Get the latest data."""
await self.ping.update() self.ping.update()
class PingData: class PingData:
@ -116,13 +114,12 @@ class PingData:
'ping', '-n', '-q', '-c', str(self._count), '-W1', 'ping', '-n', '-q', '-c', str(self._count), '-W1',
self._ip_address] self._ip_address]
async def ping(self): def ping(self):
"""Send ICMP echo request and return details if success.""" """Send ICMP echo request and return details if success."""
pinger = await asyncio.create_subprocess_shell( pinger = subprocess.Popen(
' '.join(self._ping_cmd), stdout=subprocess.PIPE, self._ping_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stderr=subprocess.PIPE)
try: try:
out = await pinger.communicate() out = pinger.communicate()
_LOGGER.debug("Output is %s", str(out)) _LOGGER.debug("Output is %s", str(out))
if sys.platform == 'win32': if sys.platform == 'win32':
match = WIN32_PING_MATCHER.search(str(out).split('\n')[-1]) match = WIN32_PING_MATCHER.search(str(out).split('\n')[-1])
@ -131,8 +128,7 @@ class PingData:
'min': rtt_min, 'min': rtt_min,
'avg': rtt_avg, 'avg': rtt_avg,
'max': rtt_max, 'max': rtt_max,
'mdev': '', 'mdev': ''}
}
if 'max/' not in str(out): if 'max/' not in str(out):
match = PING_MATCHER_BUSYBOX.search(str(out).split('\n')[-1]) match = PING_MATCHER_BUSYBOX.search(str(out).split('\n')[-1])
rtt_min, rtt_avg, rtt_max = match.groups() rtt_min, rtt_avg, rtt_max = match.groups()
@ -140,20 +136,18 @@ class PingData:
'min': rtt_min, 'min': rtt_min,
'avg': rtt_avg, 'avg': rtt_avg,
'max': rtt_max, 'max': rtt_max,
'mdev': '', 'mdev': ''}
}
match = PING_MATCHER.search(str(out).split('\n')[-1]) match = PING_MATCHER.search(str(out).split('\n')[-1])
rtt_min, rtt_avg, rtt_max, rtt_mdev = match.groups() rtt_min, rtt_avg, rtt_max, rtt_mdev = match.groups()
return { return {
'min': rtt_min, 'min': rtt_min,
'avg': rtt_avg, 'avg': rtt_avg,
'max': rtt_max, 'max': rtt_max,
'mdev': rtt_mdev, 'mdev': rtt_mdev}
}
except (subprocess.CalledProcessError, AttributeError): except (subprocess.CalledProcessError, AttributeError):
return False return False
async def update(self): def update(self):
"""Retrieve the latest details from the host.""" """Retrieve the latest details from the host."""
self.data = await self.ping() self.data = self.ping()
self.available = bool(self.data) self.available = bool(self.data)