core/homeassistant/components/sensor/systemmonitor.py

140 lines
5.6 KiB
Python
Raw Normal View History

"""
2016-02-23 05:21:49 +00:00
Support for monitoring the local system..
2015-10-13 21:44:27 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/sensor.systemmonitor/
"""
2015-06-17 21:42:11 +00:00
import logging
2015-06-17 21:42:11 +00:00
import homeassistant.util.dt as dt_util
2016-02-19 05:27:50 +00:00
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.helpers.entity import Entity
2016-05-19 00:04:59 +00:00
REQUIREMENTS = ['psutil==4.2.0']
2015-03-07 14:49:58 +00:00
SENSOR_TYPES = {
2015-12-25 20:44:32 +00:00
'disk_use_percent': ['Disk Use', '%', 'mdi:harddisk'],
'disk_use': ['Disk Use', 'GiB', 'mdi:harddisk'],
'disk_free': ['Disk Free', 'GiB', 'mdi:harddisk'],
'memory_use_percent': ['RAM Use', '%', 'mdi:memory'],
'memory_use': ['RAM Use', 'MiB', 'mdi:memory'],
'memory_free': ['RAM Free', 'MiB', 'mdi:memory'],
'processor_use': ['CPU Use', '%', 'mdi:memory'],
'process': ['Process', '', 'mdi:memory'],
'swap_use_percent': ['Swap Use', '%', 'mdi:harddisk'],
'swap_use': ['Swap Use', 'GiB', 'mdi:harddisk'],
'swap_free': ['Swap Free', 'GiB', 'mdi:harddisk'],
'network_out': ['Sent', 'MiB', 'mdi:server-network'],
'network_in': ['Received', 'MiB', 'mdi:server-network'],
2015-12-25 20:44:32 +00:00
'packets_out': ['Packets sent', '', 'mdi:server-network'],
'packets_in': ['Packets received', '', 'mdi:server-network'],
2015-12-25 20:44:32 +00:00
'ipv4_address': ['IPv4 address', '', 'mdi:server-network'],
'ipv6_address': ['IPv6 address', '', 'mdi:server-network'],
'last_boot': ['Last Boot', '', 'mdi:clock'],
'since_last_boot': ['Since Last Boot', '', 'mdi:clock']
}
_LOGGER = logging.getLogger(__name__)
2015-03-07 14:49:58 +00:00
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-23 21:44:05 +00:00
"""Setup the System sensors."""
2015-03-07 14:49:58 +00:00
dev = []
2015-03-07 14:24:35 +00:00
for resource in config['resources']:
if 'arg' not in resource:
resource['arg'] = ''
2015-03-07 14:49:58 +00:00
if resource['type'] not in SENSOR_TYPES:
2015-03-07 14:24:35 +00:00
_LOGGER.error('Sensor type: "%s" does not exist', resource['type'])
else:
2015-03-07 14:49:58 +00:00
dev.append(SystemMonitorSensor(resource['type'], resource['arg']))
2015-03-07 14:49:58 +00:00
add_devices(dev)
class SystemMonitorSensor(Entity):
2016-03-08 15:46:34 +00:00
"""Implementation of a system monitor sensor."""
2015-03-07 14:55:32 +00:00
def __init__(self, sensor_type, argument=''):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2015-03-07 14:55:32 +00:00
self._name = SENSOR_TYPES[sensor_type][0] + ' ' + argument
self.argument = argument
2015-03-07 14:55:32 +00:00
self.type = sensor_type
self._state = None
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self.update()
@property
def name(self):
2016-03-08 15:46:34 +00:00
"""Return the name of the sensor."""
return self._name.rstrip()
2015-12-25 20:19:51 +00:00
@property
def icon(self):
2016-02-23 05:21:49 +00:00
"""Icon to use in the frontend, if any."""
2015-12-25 21:02:10 +00:00
return SENSOR_TYPES[self.type][2]
2015-12-25 20:44:32 +00:00
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the device."""
return self._state
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
2015-06-17 21:58:14 +00:00
# pylint: disable=too-many-branches
def update(self):
2016-02-23 05:21:49 +00:00
"""Get the latest system information."""
2015-09-14 06:29:13 +00:00
import psutil
if self.type == 'disk_use_percent':
self._state = psutil.disk_usage(self.argument).percent
elif self.type == 'disk_use':
self._state = round(psutil.disk_usage(self.argument).used /
1024**3, 1)
elif self.type == 'disk_free':
self._state = round(psutil.disk_usage(self.argument).free /
1024**3, 1)
elif self.type == 'memory_use_percent':
self._state = psutil.virtual_memory().percent
elif self.type == 'memory_use':
self._state = round((psutil.virtual_memory().total -
psutil.virtual_memory().available) /
1024**2, 1)
elif self.type == 'memory_free':
self._state = round(psutil.virtual_memory().available / 1024**2, 1)
2015-06-17 21:42:11 +00:00
elif self.type == 'swap_use_percent':
self._state = psutil.swap_memory().percent
elif self.type == 'swap_use':
self._state = round(psutil.swap_memory().used / 1024**3, 1)
elif self.type == 'swap_free':
self._state = round(psutil.swap_memory().free / 1024**3, 1)
elif self.type == 'processor_use':
self._state = round(psutil.cpu_percent(interval=None))
elif self.type == 'process':
if any(self.argument in l.name() for l in psutil.process_iter()):
self._state = STATE_ON
else:
self._state = STATE_OFF
2015-06-17 21:42:11 +00:00
elif self.type == 'network_out':
self._state = round(psutil.net_io_counters(pernic=True)
[self.argument][0] / 1024**2, 1)
elif self.type == 'network_in':
self._state = round(psutil.net_io_counters(pernic=True)
[self.argument][1] / 1024**2, 1)
elif self.type == 'packets_out':
self._state = psutil.net_io_counters(pernic=True)[self.argument][2]
elif self.type == 'packets_in':
self._state = psutil.net_io_counters(pernic=True)[self.argument][3]
elif self.type == 'ipv4_address':
self._state = psutil.net_if_addrs()[self.argument][0][1]
elif self.type == 'ipv6_address':
self._state = psutil.net_if_addrs()[self.argument][1][1]
elif self.type == 'last_boot':
2016-04-16 07:55:35 +00:00
self._state = dt_util.as_local(
dt_util.utc_from_timestamp(psutil.boot_time())
).date().isoformat()
2015-06-17 21:42:11 +00:00
elif self.type == 'since_last_boot':
self._state = dt_util.utcnow() - dt_util.utc_from_timestamp(
psutil.boot_time())