From 1ad4779443fa69a14849286e4ed884f6d57d45f9 Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Sat, 2 Mar 2019 10:38:15 +0000 Subject: [PATCH] Add network throughput statistics to systemmonitor sensor (#21575) * add network throughput * lint --- .../components/sensor/systemmonitor.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/homeassistant/components/sensor/systemmonitor.py b/homeassistant/components/sensor/systemmonitor.py index 70fb1f91051..8eccdc7b3b7 100644 --- a/homeassistant/components/sensor/systemmonitor.py +++ b/homeassistant/components/sensor/systemmonitor.py @@ -1,4 +1,5 @@ """Support for monitoring the local system.""" +from datetime import datetime import logging import os import socket @@ -34,6 +35,10 @@ SENSOR_TYPES = { 'network_out': ['Network out', 'MiB', 'mdi:server-network', None], 'packets_in': ['Packets in', ' ', 'mdi:server-network', None], 'packets_out': ['Packets out', ' ', 'mdi:server-network', None], + 'throughput_network_in': ['Network throughput in', 'MB/s', + 'mdi:server-network', None], + 'throughput_network_out': ['Network throughput out', 'MB/s', + 'mdi:server-network', None], 'process': ['Process', ' ', 'mdi:memory', None], 'processor_use': ['Processor use', '%', 'mdi:memory', None], 'swap_free': ['Swap free', 'MiB', 'mdi:harddisk', None], @@ -54,6 +59,8 @@ IO_COUNTER = { 'network_in': 1, 'packets_out': 2, 'packets_in': 3, + 'throughput_network_out': 0, + 'throughput_network_in': 1, } IF_ADDRS_FAMILY = { @@ -84,6 +91,9 @@ class SystemMonitorSensor(Entity): self.type = sensor_type self._state = None self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] + if sensor_type in ['throughput_network_out', 'throughput_network_in']: + self._last_value = None + self._last_update_time = None @property def name(self): @@ -162,6 +172,22 @@ class SystemMonitorSensor(Entity): self._state = counters[self.argument][IO_COUNTER[self.type]] else: self._state = None + elif self.type == 'throughput_network_out' or\ + self.type == 'throughput_network_in': + counters = psutil.net_io_counters(pernic=True) + if self.argument in counters: + counter = counters[self.argument][IO_COUNTER[self.type]] + now = datetime.now() + if self._last_value and self._last_value < counter: + self._state = round( + (counter - self._last_value) / 1000**2 / + (now - self._last_update_time).seconds, 3) + else: + self._state = None + self._last_update_time = now + self._last_value = counter + else: + self._state = None elif self.type == 'ipv4_address' or self.type == 'ipv6_address': addresses = psutil.net_if_addrs() if self.argument in addresses: