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 moodpull/29839/head
parent
dad11f8208
commit
b2d5de6a79
homeassistant/components/iperf3
|
@ -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
|
||||
|
|
|
@ -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 @@ async def async_setup(hass, config):
|
|||
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 @@ class Iperf3Data:
|
|||
|
||||
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
|
||||
|
|
|
@ -6,5 +6,7 @@
|
|||
"iperf3==0.1.11"
|
||||
],
|
||||
"dependencies": [],
|
||||
"codeowners": []
|
||||
"codeowners": [
|
||||
"@rohankapoorcom"
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue