From 8bf58e1df5f190c53a6d923dfce6cc748c97be66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Fri, 19 Oct 2018 09:29:48 +0200 Subject: [PATCH] Revert "De-syncing binary_sensor.ping (#17056)" (#17606) This reverts commit 11d5671ee0a4dbf98c5e9f58a2dec85bdb35f4d0. --- .../components/binary_sensor/ping.py | 42 ++++++++----------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/binary_sensor/ping.py b/homeassistant/components/binary_sensor/ping.py index f12957e6129..4c597dd63e1 100644 --- a/homeassistant/components/binary_sensor/ping.py +++ b/homeassistant/components/binary_sensor/ping.py @@ -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 https://home-assistant.io/components/binary_sensor.ping/ """ -import asyncio -from datetime import timedelta import logging -import re import subprocess +import re import sys +from datetime import timedelta 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 +from homeassistant.components.binary_sensor import ( + BinarySensorDevice, PLATFORM_SCHEMA) +from homeassistant.const import CONF_NAME, CONF_HOST _LOGGER = logging.getLogger(__name__) @@ -49,14 +48,13 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ }) -async def async_setup_platform( - hass, config, async_add_entities, discovery_info=None): +def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Ping Binary sensor.""" name = config.get(CONF_NAME) host = config.get(CONF_HOST) 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): @@ -93,9 +91,9 @@ class PingBinarySensor(BinarySensorDevice): ATTR_ROUND_TRIP_TIME_MIN: self.ping.data['min'], } - async def async_update(self): + def update(self): """Get the latest data.""" - await self.ping.update() + self.ping.update() class PingData: @@ -116,13 +114,12 @@ class PingData: 'ping', '-n', '-q', '-c', str(self._count), '-W1', self._ip_address] - async def ping(self): + def ping(self): """Send ICMP echo request and return details if success.""" - pinger = await asyncio.create_subprocess_shell( - ' '.join(self._ping_cmd), stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + pinger = subprocess.Popen( + self._ping_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: - out = await pinger.communicate() + out = pinger.communicate() _LOGGER.debug("Output is %s", str(out)) if sys.platform == 'win32': match = WIN32_PING_MATCHER.search(str(out).split('\n')[-1]) @@ -131,8 +128,7 @@ class PingData: 'min': rtt_min, 'avg': rtt_avg, 'max': rtt_max, - 'mdev': '', - } + 'mdev': ''} if 'max/' not in str(out): match = PING_MATCHER_BUSYBOX.search(str(out).split('\n')[-1]) rtt_min, rtt_avg, rtt_max = match.groups() @@ -140,20 +136,18 @@ class PingData: 'min': rtt_min, 'avg': rtt_avg, 'max': rtt_max, - 'mdev': '', - } + 'mdev': ''} match = PING_MATCHER.search(str(out).split('\n')[-1]) rtt_min, rtt_avg, rtt_max, rtt_mdev = match.groups() return { 'min': rtt_min, 'avg': rtt_avg, 'max': rtt_max, - 'mdev': rtt_mdev, - } + 'mdev': rtt_mdev} except (subprocess.CalledProcessError, AttributeError): return False - async def update(self): + def update(self): """Retrieve the latest details from the host.""" - self.data = await self.ping() + self.data = self.ping() self.available = bool(self.data)