Skip to content

Commit

Permalink
Added UDP and parallel streams support to Iperf3 (home-assistant#14629)
Browse files Browse the repository at this point in the history
  • Loading branch information
tchellomello authored and fabaff committed May 26, 2018
1 parent edfc54b commit 28d6910
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions homeassistant/components/sensor/iperf3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from homeassistant.components.sensor import DOMAIN, PLATFORM_SCHEMA
from homeassistant.const import (
ATTR_ATTRIBUTION, ATTR_ENTITY_ID, CONF_MONITORED_CONDITIONS,
CONF_HOST, CONF_PORT)
CONF_HOST, CONF_PORT, CONF_PROTOCOL)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity

Expand All @@ -27,13 +27,16 @@

CONF_ATTRIBUTION = 'Data retrieved using Iperf3'
CONF_DURATION = 'duration'
CONF_PARALLEL = 'parallel'

DEFAULT_DURATION = 10
DEFAULT_PORT = 5201
DEFAULT_PARALLEL = 1
DEFAULT_PROTOCOL = 'tcp'

IPERF3_DATA = 'iperf3'

SCAN_INTERVAL = timedelta(minutes=30)
SCAN_INTERVAL = timedelta(minutes=60)

SERVICE_NAME = 'iperf3_update'

Expand All @@ -50,6 +53,9 @@
vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_DURATION, default=DEFAULT_DURATION): vol.Range(5, 10),
vol.Optional(CONF_PARALLEL, default=DEFAULT_PARALLEL): vol.Range(1, 20),
vol.Optional(CONF_PROTOCOL, default=DEFAULT_PROTOCOL):
vol.In(['tcp', 'udp']),
})


Expand All @@ -70,6 +76,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
Iperf3Sensor(config[CONF_HOST],
config[CONF_PORT],
config[CONF_DURATION],
config[CONF_PARALLEL],
config[CONF_PROTOCOL],
sensor))

hass.data[IPERF3_DATA]['sensors'].extend(dev)
Expand Down Expand Up @@ -98,10 +106,12 @@ def _service_handler(service):
class Iperf3Sensor(Entity):
"""A Iperf3 sensor implementation."""

def __init__(self, server, port, duration, sensor_type):
def __init__(self, server, port, duration, streams,
protocol, sensor_type):
"""Initialize the sensor."""
self._attrs = {
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
ATTR_PROTOCOL: protocol,
}
self._name = \
"{} {}".format(SENSOR_TYPES[sensor_type][0], server)
Expand All @@ -111,6 +121,8 @@ def __init__(self, server, port, duration, sensor_type):
self._port = port
self._server = server
self._duration = duration
self._num_streams = streams
self._protocol = protocol
self.result = None

@property
Expand All @@ -133,7 +145,6 @@ def device_state_attributes(self):
"""Return the state attributes."""
if self.result is not None:
self._attrs[ATTR_ATTRIBUTION] = CONF_ATTRIBUTION
self._attrs[ATTR_PROTOCOL] = self.result.protocol
self._attrs[ATTR_REMOTE_HOST] = self.result.remote_host
self._attrs[ATTR_REMOTE_PORT] = self.result.remote_port
self._attrs[ATTR_VERSION] = self.result.version
Expand All @@ -147,14 +158,16 @@ def update(self):
client.server_hostname = self._server
client.port = self._port
client.verbose = False
client.num_streams = self._num_streams
client.protocol = self._protocol

# when testing download bandwith, reverse must be True
if self._sensor_type == 'download':
client.reverse = True

try:
self.result = client.run()
except (OSError, AttributeError) as error:
except (AttributeError, OSError, ValueError) as error:
self.result = None
_LOGGER.error("Iperf3 sensor error: %s", error)
return
Expand All @@ -166,7 +179,11 @@ def update(self):
self.result = None
return

if self._sensor_type == 'download':
# UDP only have 1 way attribute
if self._protocol == 'udp':
self._state = round(self.result.Mbps, 2)

elif self._sensor_type == 'download':
self._state = round(self.result.received_Mbps, 2)

elif self._sensor_type == 'upload':
Expand Down

0 comments on commit 28d6910

Please sign in to comment.