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
This commit is contained in:
Rohan Kapoor 2019-12-04 22:49:26 -08:00 committed by Paulus Schoutsen
parent dad11f8208
commit b2d5de6a79
3 changed files with 24 additions and 19 deletions

View File

@ -162,6 +162,7 @@ homeassistant/components/input_text/* @home-assistant/core
homeassistant/components/integration/* @dgomes homeassistant/components/integration/* @dgomes
homeassistant/components/intent/* @home-assistant/core homeassistant/components/intent/* @home-assistant/core
homeassistant/components/ios/* @robbiet480 homeassistant/components/ios/* @robbiet480
homeassistant/components/iperf3/* @rohankapoorcom
homeassistant/components/ipma/* @dgomes homeassistant/components/ipma/* @dgomes
homeassistant/components/iqvia/* @bachya homeassistant/components/iqvia/* @bachya
homeassistant/components/irish_rail_transport/* @ttroy50 homeassistant/components/irish_rail_transport/* @ttroy50

View File

@ -85,17 +85,7 @@ async def async_setup(hass, config):
conf = config[DOMAIN] conf = config[DOMAIN]
for host in conf[CONF_HOSTS]: for host in conf[CONF_HOSTS]:
host_name = host[CONF_HOST] data = hass.data[DOMAIN][host[CONF_HOST]] = Iperf3Data(hass, 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)
if not conf[CONF_MANUAL]: if not conf[CONF_MANUAL]:
async_track_time_interval(hass, data.update, conf[CONF_SCAN_INTERVAL]) async_track_time_interval(hass, data.update, conf[CONF_SCAN_INTERVAL])
@ -123,26 +113,37 @@ async def async_setup(hass, config):
class Iperf3Data: class Iperf3Data:
"""Get the latest data from iperf3.""" """Get the latest data from iperf3."""
def __init__(self, hass, client): def __init__(self, hass, host):
"""Initialize the data object.""" """Initialize the data object."""
self._hass = hass self._hass = hass
self._client = client self._host = host
self.data = {ATTR_DOWNLOAD: None, ATTR_UPLOAD: None, ATTR_VERSION: None} 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 @property
def protocol(self): def protocol(self):
"""Return the protocol used for this connection.""" """Return the protocol used for this connection."""
return self._client.protocol return self._host[CONF_PROTOCOL]
@property @property
def host(self): def host(self):
"""Return the host connected to.""" """Return the host connected to."""
return self._client.server_hostname return self._host[CONF_HOST]
@property @property
def port(self): def port(self):
"""Return the port on the host connected to.""" """Return the port on the host connected to."""
return self._client.port return self._host[CONF_PORT]
def update(self, now=None): def update(self, now=None):
"""Get the latest data from iperf3.""" """Get the latest data from iperf3."""
@ -165,9 +166,10 @@ class Iperf3Data:
def _run_test(self, test_type): def _run_test(self, test_type):
"""Run and return the iperf3 data.""" """Run and return the iperf3 data."""
self._client.reverse = test_type == ATTR_DOWNLOAD client = self.create_client()
client.reverse = test_type == ATTR_DOWNLOAD
try: try:
result = self._client.run() result = client.run()
except (AttributeError, OSError, ValueError) as error: except (AttributeError, OSError, ValueError) as error:
_LOGGER.error("Iperf3 error: %s", error) _LOGGER.error("Iperf3 error: %s", error)
return None return None

View File

@ -6,5 +6,7 @@
"iperf3==0.1.11" "iperf3==0.1.11"
], ],
"dependencies": [], "dependencies": [],
"codeowners": [] "codeowners": [
"@rohankapoorcom"
]
} }