Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -7,21 +7,25 @@ from datetime import timedelta
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.device_tracker import (
|
||||
PLATFORM_SCHEMA)
|
||||
from homeassistant.components.device_tracker import PLATFORM_SCHEMA
|
||||
from homeassistant.components.device_tracker.const import (
|
||||
CONF_SCAN_INTERVAL, SCAN_INTERVAL, SOURCE_TYPE_ROUTER)
|
||||
CONF_SCAN_INTERVAL,
|
||||
SCAN_INTERVAL,
|
||||
SOURCE_TYPE_ROUTER,
|
||||
)
|
||||
from homeassistant import util
|
||||
from homeassistant import const
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_PING_COUNT = 'count'
|
||||
CONF_PING_COUNT = "count"
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(const.CONF_HOSTS): {cv.string: cv.string},
|
||||
vol.Optional(CONF_PING_COUNT, default=1): cv.positive_int,
|
||||
})
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(const.CONF_HOSTS): {cv.string: cv.string},
|
||||
vol.Optional(CONF_PING_COUNT, default=1): cv.positive_int,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class Host:
|
||||
@@ -33,17 +37,16 @@ class Host:
|
||||
self.ip_address = ip_address
|
||||
self.dev_id = dev_id
|
||||
self._count = config[CONF_PING_COUNT]
|
||||
if sys.platform == 'win32':
|
||||
self._ping_cmd = ['ping', '-n', '1', '-w', '1000', self.ip_address]
|
||||
if sys.platform == "win32":
|
||||
self._ping_cmd = ["ping", "-n", "1", "-w", "1000", self.ip_address]
|
||||
else:
|
||||
self._ping_cmd = ['ping', '-n', '-q', '-c1', '-W1',
|
||||
self.ip_address]
|
||||
self._ping_cmd = ["ping", "-n", "-q", "-c1", "-W1", self.ip_address]
|
||||
|
||||
def ping(self):
|
||||
"""Send an ICMP echo request and return True if success."""
|
||||
pinger = subprocess.Popen(self._ping_cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL)
|
||||
pinger = subprocess.Popen(
|
||||
self._ping_cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
|
||||
)
|
||||
try:
|
||||
pinger.communicate()
|
||||
return pinger.returncode == 0
|
||||
@@ -64,14 +67,19 @@ class Host:
|
||||
|
||||
def setup_scanner(hass, config, see, discovery_info=None):
|
||||
"""Set up the Host objects and return the update function."""
|
||||
hosts = [Host(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]))
|
||||
hosts = [
|
||||
Host(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."""
|
||||
@@ -80,7 +88,8 @@ def setup_scanner(hass, config, see, discovery_info=None):
|
||||
host.update(see)
|
||||
finally:
|
||||
hass.helpers.event.track_point_in_utc_time(
|
||||
update_interval, util.dt.utcnow() + interval)
|
||||
update_interval, util.dt.utcnow() + interval
|
||||
)
|
||||
|
||||
update_interval(None)
|
||||
return True
|
||||
Reference in new issue
Block a user