From b2d5de6a798a28537410f0ced6e9c55aab558c7b Mon Sep 17 00:00:00 2001 From: Rohan Kapoor Date: Wed, 4 Dec 2019 22:49:26 -0800 Subject: [PATCH] Switch iperf3 to generate a new client every time it runs a test (#29495) * Switch iperf3 to generate a new client every time it runs a test * Add myself to CODEOWNERS * Fix imperative mood --- CODEOWNERS | 1 + homeassistant/components/iperf3/__init__.py | 38 ++++++++++--------- homeassistant/components/iperf3/manifest.json | 4 +- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index 66332531bd8323..8078aadf6419ac 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -162,6 +162,7 @@ homeassistant/components/input_text/* @home-assistant/core homeassistant/components/integration/* @dgomes homeassistant/components/intent/* @home-assistant/core homeassistant/components/ios/* @robbiet480 +homeassistant/components/iperf3/* @rohankapoorcom homeassistant/components/ipma/* @dgomes homeassistant/components/iqvia/* @bachya homeassistant/components/irish_rail_transport/* @ttroy50 diff --git a/homeassistant/components/iperf3/__init__.py b/homeassistant/components/iperf3/__init__.py index 753ea60efa4fe3..9272a725bb7f03 100644 --- a/homeassistant/components/iperf3/__init__.py +++ b/homeassistant/components/iperf3/__init__.py @@ -85,17 +85,7 @@ async def async_setup(hass, config): conf = config[DOMAIN] for host in conf[CONF_HOSTS]: - host_name = host[CONF_HOST] - - client = iperf3.Client() - client.duration = host[CONF_DURATION] - client.server_hostname = host_name - client.port = host[CONF_PORT] - client.num_streams = host[CONF_PARALLEL] - client.protocol = host[CONF_PROTOCOL] - client.verbose = False - - data = hass.data[DOMAIN][host_name] = Iperf3Data(hass, client) + data = hass.data[DOMAIN][host[CONF_HOST]] = Iperf3Data(hass, host) if not conf[CONF_MANUAL]: async_track_time_interval(hass, data.update, conf[CONF_SCAN_INTERVAL]) @@ -123,26 +113,37 @@ def update(call): class Iperf3Data: """Get the latest data from iperf3.""" - def __init__(self, hass, client): + def __init__(self, hass, host): """Initialize the data object.""" self._hass = hass - self._client = client + self._host = host self.data = {ATTR_DOWNLOAD: None, ATTR_UPLOAD: None, ATTR_VERSION: None} + def create_client(self): + """Create a new iperf3 client to use for measurement.""" + client = iperf3.Client() + client.duration = self._host[CONF_DURATION] + client.server_hostname = self._host[CONF_HOST] + client.port = self._host[CONF_PORT] + client.num_streams = self._host[CONF_PARALLEL] + client.protocol = self._host[CONF_PROTOCOL] + client.verbose = False + return client + @property def protocol(self): """Return the protocol used for this connection.""" - return self._client.protocol + return self._host[CONF_PROTOCOL] @property def host(self): """Return the host connected to.""" - return self._client.server_hostname + return self._host[CONF_HOST] @property def port(self): """Return the port on the host connected to.""" - return self._client.port + return self._host[CONF_PORT] def update(self, now=None): """Get the latest data from iperf3.""" @@ -165,9 +166,10 @@ def update(self, now=None): def _run_test(self, test_type): """Run and return the iperf3 data.""" - self._client.reverse = test_type == ATTR_DOWNLOAD + client = self.create_client() + client.reverse = test_type == ATTR_DOWNLOAD try: - result = self._client.run() + result = client.run() except (AttributeError, OSError, ValueError) as error: _LOGGER.error("Iperf3 error: %s", error) return None diff --git a/homeassistant/components/iperf3/manifest.json b/homeassistant/components/iperf3/manifest.json index c3b1e27c77acc9..6b7cadfd5ded8c 100644 --- a/homeassistant/components/iperf3/manifest.json +++ b/homeassistant/components/iperf3/manifest.json @@ -6,5 +6,7 @@ "iperf3==0.1.11" ], "dependencies": [], - "codeowners": [] + "codeowners": [ + "@rohankapoorcom" + ] }