Skip to content
Snippets Groups Projects
Commit 14d1494c authored by Matt N's avatar Matt N Committed by Fabian Affolter
Browse files

systemmonitor: Support monitoring removable network interfaces (#4462)

parent f1d11e77
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,7 @@ import voluptuous as vol ...@@ -10,7 +10,7 @@ import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import ( from homeassistant.const import (
CONF_RESOURCES, STATE_OFF, STATE_ON, CONF_TYPE) CONF_RESOURCES, STATE_OFF, STATE_ON, STATE_UNKNOWN, CONF_TYPE)
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
...@@ -49,6 +49,18 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ ...@@ -49,6 +49,18 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
})]) })])
}) })
IO_COUNTER = {
'network_out': 0,
'network_in': 1,
'packets_out': 2,
'packets_in': 3,
}
IF_ADDRS = {
'ipv4_address': 0,
'ipv6_address': 1,
}
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
...@@ -126,20 +138,25 @@ class SystemMonitorSensor(Entity): ...@@ -126,20 +138,25 @@ class SystemMonitorSensor(Entity):
self._state = STATE_ON self._state = STATE_ON
else: else:
self._state = STATE_OFF self._state = STATE_OFF
elif self.type == 'network_out': elif self.type == 'network_out' or self.type == 'network_in':
self._state = round(psutil.net_io_counters(pernic=True) counters = psutil.net_io_counters(pernic=True)
[self.argument][0] / 1024**2, 1) if self.argument in counters:
elif self.type == 'network_in': counter = counters[self.argument][IO_COUNTER[self.type]]
self._state = round(psutil.net_io_counters(pernic=True) self._state = round(counter / 1024**2, 1)
[self.argument][1] / 1024**2, 1) else:
elif self.type == 'packets_out': self._state = STATE_UNKNOWN
self._state = psutil.net_io_counters(pernic=True)[self.argument][2] elif self.type == 'packets_out' or self.type == 'packets_in':
elif self.type == 'packets_in': counters = psutil.net_io_counters(pernic=True)
self._state = psutil.net_io_counters(pernic=True)[self.argument][3] if self.argument in counters:
elif self.type == 'ipv4_address': self._state = counters[self.argument][IO_COUNTER[self.type]]
self._state = psutil.net_if_addrs()[self.argument][0][1] else:
elif self.type == 'ipv6_address': self._state = STATE_UNKNOWN
self._state = psutil.net_if_addrs()[self.argument][1][1] elif self.type == 'ipv4_address' or self.type == 'ipv6_address':
addresses = psutil.net_if_addrs()
if self.argument in addresses:
self._state = addresses[self.argument][IF_ADDRS[self.type]][1]
else:
self._state = STATE_UNKNOWN
elif self.type == 'last_boot': elif self.type == 'last_boot':
self._state = dt_util.as_local( self._state = dt_util.as_local(
dt_util.utc_from_timestamp(psutil.boot_time()) dt_util.utc_from_timestamp(psutil.boot_time())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment