Skip to content

Commit

Permalink
Switch iperf3 to generate a new client every time it runs a test (hom…
Browse files Browse the repository at this point in the history
…e-assistant#29495)

* Switch iperf3 to generate a new client every time it runs a test

* Add myself to CODEOWNERS

* Fix imperative mood
  • Loading branch information
rohankapoorcom authored and balloob committed Dec 5, 2019
1 parent dad11f8 commit b2d5de6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 20 additions & 18 deletions homeassistant/components/iperf3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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."""
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/iperf3/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
"iperf3==0.1.11"
],
"dependencies": [],
"codeowners": []
"codeowners": [
"@rohankapoorcom"
]
}

0 comments on commit b2d5de6

Please sign in to comment.