From 19964e914af274d6245636be69e3d384167ab44f Mon Sep 17 00:00:00 2001 From: theolind Date: Sat, 7 Mar 2015 14:54:08 +0100 Subject: [PATCH 1/7] Added a system monitoring platform to the sensor component --- config/configuration.yaml.example | 12 +++ .../components/sensor/systemmonitor.py | 89 +++++++++++++++++++ requirements.txt | 3 + 3 files changed, 104 insertions(+) create mode 100644 homeassistant/components/sensor/systemmonitor.py diff --git a/config/configuration.yaml.example b/config/configuration.yaml.example index 355a24e94d4..1e42c836a33 100644 --- a/config/configuration.yaml.example +++ b/config/configuration.yaml.example @@ -102,3 +102,15 @@ automation 2: execute_service: notify.notify service_data: {"message":"It's 4, time for beer!"} + +sensor: + platform: systemmonitor + devices: + - {type: 'disk_use_percent', arg: '/'} + - {type: 'disk_use_percent', arg: '/home'} + - {type: 'disk_use', arg: '/home'} + - {type: 'disk_free', arg: '/'} + - {type: 'memory_use_percent'} + - {type: 'memory_use'} + - {type: 'memory_free'} + - {type: 'processor_use'} \ No newline at end of file diff --git a/homeassistant/components/sensor/systemmonitor.py b/homeassistant/components/sensor/systemmonitor.py new file mode 100644 index 00000000000..f53c4c09a33 --- /dev/null +++ b/homeassistant/components/sensor/systemmonitor.py @@ -0,0 +1,89 @@ +""" +homeassistant.components.sensor.systemmonitor +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Shows system monitor values such as: disk, memory and processor use + +""" + +from homeassistant.helpers.device import Device +from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, ATTR_FRIENDLY_NAME) +import psutil +import logging + +sensor_types = { + 'disk_use_percent': ['Disk Use', '%'], + 'disk_use': ['Disk Use', 'GiB'], + 'disk_free': ['Disk Free', 'GiB'], + 'memory_use_percent': ['RAM Use', '%'], + 'memory_use': ['RAM Use', 'MiB'], + 'memory_free': ['RAM Free', 'MiB'], + 'processor_use': ['CPU Use', '%'], +} + +_LOGGER = logging.getLogger(__name__) + +# pylint: disable=unused-argument +def setup_platform(hass, config, add_devices, discovery_info=None): + """ Sets up the sensors """ + + devices = [] + for device in config['devices']: + if 'arg' not in device: + device['arg'] = '' + if device['type'] not in sensor_types: + _LOGGER.error('Sensor type: "%s" does not exist', device['type']) + else: + devices.append(SystemMonitorSensor(device['type'], device['arg'])) + + add_devices(devices) + + +class SystemMonitorSensor(Device): + """ A system monitor sensor """ + + def __init__(self, type, argument=''): + self._name = sensor_types[type][0] + ' ' + argument + self.argument = argument + self.type = type + self._state = None + self.unit_of_measurement = sensor_types[type][1] + self.update() + + @property + def name(self): + return self._name + + @property + def state(self): + """ Returns the state of the device. """ + return self._state + + @property + def state_attributes(self): + """ Returns the state attributes. """ + return { + ATTR_FRIENDLY_NAME: self.name, + ATTR_UNIT_OF_MEASUREMENT: self.unit_of_measurement, + } + + def update(self): + 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) + elif self.type == 'processor_use': + self._state = round(psutil.cpu_percent(interval=None)) diff --git a/requirements.txt b/requirements.txt index 0d727f10b85..156181da508 100644 --- a/requirements.txt +++ b/requirements.txt @@ -35,3 +35,6 @@ pydispatcher>=2.0.5 # pyyaml pyyaml + +# psutil +psutil From 97258747c789602b346a7dce2c07238f2b85a3cf Mon Sep 17 00:00:00 2001 From: theolind Date: Sat, 7 Mar 2015 15:22:03 +0100 Subject: [PATCH 2/7] Renamed device to resource --- config/configuration.yaml.example | 2 +- homeassistant/components/sensor/systemmonitor.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/config/configuration.yaml.example b/config/configuration.yaml.example index 1e42c836a33..fde4040863f 100644 --- a/config/configuration.yaml.example +++ b/config/configuration.yaml.example @@ -105,7 +105,7 @@ automation 2: sensor: platform: systemmonitor - devices: + resources: - {type: 'disk_use_percent', arg: '/'} - {type: 'disk_use_percent', arg: '/home'} - {type: 'disk_use', arg: '/home'} diff --git a/homeassistant/components/sensor/systemmonitor.py b/homeassistant/components/sensor/systemmonitor.py index f53c4c09a33..631610b09da 100644 --- a/homeassistant/components/sensor/systemmonitor.py +++ b/homeassistant/components/sensor/systemmonitor.py @@ -29,13 +29,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the sensors """ devices = [] - for device in config['devices']: - if 'arg' not in device: - device['arg'] = '' - if device['type'] not in sensor_types: - _LOGGER.error('Sensor type: "%s" does not exist', device['type']) + for resurce in config['resources']: + if 'arg' not in resurce: + resurce['arg'] = '' + if resurce['type'] not in sensor_types: + _LOGGER.error('Sensor type: "%s" does not exist', resurce['type']) else: - devices.append(SystemMonitorSensor(device['type'], device['arg'])) + devices.append(SystemMonitorSensor(resurce['type'], resurce['arg'])) add_devices(devices) From 3173249dc320438a98f72872b25212993d0264a0 Mon Sep 17 00:00:00 2001 From: theolind Date: Sat, 7 Mar 2015 15:24:35 +0100 Subject: [PATCH 3/7] fixed typo --- homeassistant/components/sensor/systemmonitor.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/sensor/systemmonitor.py b/homeassistant/components/sensor/systemmonitor.py index 631610b09da..9fe67e206f2 100644 --- a/homeassistant/components/sensor/systemmonitor.py +++ b/homeassistant/components/sensor/systemmonitor.py @@ -29,13 +29,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the sensors """ devices = [] - for resurce in config['resources']: - if 'arg' not in resurce: - resurce['arg'] = '' - if resurce['type'] not in sensor_types: - _LOGGER.error('Sensor type: "%s" does not exist', resurce['type']) + for resource in config['resources']: + if 'arg' not in resource: + resource['arg'] = '' + if resource['type'] not in sensor_types: + _LOGGER.error('Sensor type: "%s" does not exist', resource['type']) else: - devices.append(SystemMonitorSensor(resurce['type'], resurce['arg'])) + devices.append(SystemMonitorSensor(resource['type'], resource['arg'])) add_devices(devices) From dd9217357660dbcd4a2d7dfe5053c5541520e605 Mon Sep 17 00:00:00 2001 From: theolind Date: Sat, 7 Mar 2015 15:27:31 +0100 Subject: [PATCH 4/7] Made example config more YAMLy --- config/configuration.yaml.example | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/config/configuration.yaml.example b/config/configuration.yaml.example index fde4040863f..619b9f56f87 100644 --- a/config/configuration.yaml.example +++ b/config/configuration.yaml.example @@ -106,11 +106,16 @@ automation 2: sensor: platform: systemmonitor resources: - - {type: 'disk_use_percent', arg: '/'} - - {type: 'disk_use_percent', arg: '/home'} - - {type: 'disk_use', arg: '/home'} - - {type: 'disk_free', arg: '/'} - - {type: 'memory_use_percent'} - - {type: 'memory_use'} - - {type: 'memory_free'} - - {type: 'processor_use'} \ No newline at end of file + - type: 'disk_use_percent' + arg: '/' + - type: 'disk_use_percent' + arg: '/home' + - type: 'disk_use' + arg: '/home' + - type: 'disk_free' + arg: '/' + - type: 'memory_use_percent' + - type: 'memory_use' + - type: 'memory_free' + - type: 'processor_use' + - type: 'unknown sensor type' \ No newline at end of file From d7a4242c74b6fbbe8fbf5a7b45a0c266c0470715 Mon Sep 17 00:00:00 2001 From: theolind Date: Sat, 7 Mar 2015 15:31:09 +0100 Subject: [PATCH 5/7] Specified the component that uses psutil (sensor.systemmonitor) --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 156181da508..695512c9fea 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,5 +36,5 @@ pydispatcher>=2.0.5 # pyyaml pyyaml -# psutil +# sensor.systemmonitor psutil From 828c78cb1f5404e536a3d05955f82b0b184b8cd8 Mon Sep 17 00:00:00 2001 From: theolind Date: Sat, 7 Mar 2015 15:49:58 +0100 Subject: [PATCH 6/7] fixed style errors --- homeassistant/components/sensor/systemmonitor.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/sensor/systemmonitor.py b/homeassistant/components/sensor/systemmonitor.py index 9fe67e206f2..fb7614c761e 100644 --- a/homeassistant/components/sensor/systemmonitor.py +++ b/homeassistant/components/sensor/systemmonitor.py @@ -12,7 +12,7 @@ from homeassistant.const import ( import psutil import logging -sensor_types = { +SENSOR_TYPES = { 'disk_use_percent': ['Disk Use', '%'], 'disk_use': ['Disk Use', 'GiB'], 'disk_free': ['Disk Free', 'GiB'], @@ -24,31 +24,32 @@ sensor_types = { _LOGGER = logging.getLogger(__name__) + # pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the sensors """ - devices = [] + dev = [] for resource in config['resources']: if 'arg' not in resource: resource['arg'] = '' - if resource['type'] not in sensor_types: + if resource['type'] not in SENSOR_TYPES: _LOGGER.error('Sensor type: "%s" does not exist', resource['type']) else: - devices.append(SystemMonitorSensor(resource['type'], resource['arg'])) + dev.append(SystemMonitorSensor(resource['type'], resource['arg'])) - add_devices(devices) + add_devices(dev) class SystemMonitorSensor(Device): """ A system monitor sensor """ def __init__(self, type, argument=''): - self._name = sensor_types[type][0] + ' ' + argument + self._name = SENSOR_TYPES[type][0] + ' ' + argument self.argument = argument self.type = type self._state = None - self.unit_of_measurement = sensor_types[type][1] + self.unit_of_measurement = SENSOR_TYPES[type][1] self.update() @property From 1215b5e994ca325932f5d3bfc246701c2125f666 Mon Sep 17 00:00:00 2001 From: theolind Date: Sat, 7 Mar 2015 15:55:32 +0100 Subject: [PATCH 7/7] Fixed type issue --- homeassistant/components/sensor/systemmonitor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/systemmonitor.py b/homeassistant/components/sensor/systemmonitor.py index fb7614c761e..ab7c2decbc7 100644 --- a/homeassistant/components/sensor/systemmonitor.py +++ b/homeassistant/components/sensor/systemmonitor.py @@ -44,12 +44,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class SystemMonitorSensor(Device): """ A system monitor sensor """ - def __init__(self, type, argument=''): - self._name = SENSOR_TYPES[type][0] + ' ' + argument + def __init__(self, sensor_type, argument=''): + self._name = SENSOR_TYPES[sensor_type][0] + ' ' + argument self.argument = argument - self.type = type + self.type = sensor_type self._state = None - self.unit_of_measurement = SENSOR_TYPES[type][1] + self.unit_of_measurement = SENSOR_TYPES[sensor_type][1] self.update() @property