Use voluptuous for system monitoring sensors (#2813)
* Use voluptuous for system monitoring sensors * Extent platform, ordering, and consts * Add resource/resources
This commit is contained in:
parent
def9bbf827
commit
4f1712c933
4 changed files with 57 additions and 32 deletions
|
@ -6,27 +6,38 @@ https://home-assistant.io/components/sensor.cpuspeed/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
|
from homeassistant.const import CONF_NAME
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
REQUIREMENTS = ['py-cpuinfo==0.2.3']
|
REQUIREMENTS = ['py-cpuinfo==0.2.3']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
DEFAULT_NAME = 'CPU speed'
|
||||||
|
|
||||||
DEFAULT_NAME = "CPU speed"
|
|
||||||
ATTR_VENDOR = 'Vendor ID'
|
ATTR_VENDOR = 'Vendor ID'
|
||||||
ATTR_BRAND = 'Brand'
|
ATTR_BRAND = 'Brand'
|
||||||
ATTR_HZ = 'GHz Advertised'
|
ATTR_HZ = 'GHz Advertised'
|
||||||
ICON = 'mdi:pulse'
|
ICON = 'mdi:pulse'
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
|
})
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-variable
|
# pylint: disable=unused-variable
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the CPU speed sensor."""
|
"""Setup the CPU speed sensor."""
|
||||||
add_devices([CpuSpeedSensor(config.get('name', DEFAULT_NAME))])
|
name = config.get(CONF_NAME)
|
||||||
|
|
||||||
|
add_devices([CpuSpeedSensor(name)])
|
||||||
|
|
||||||
|
|
||||||
class CpuSpeedSensor(Entity):
|
class CpuSpeedSensor(Entity):
|
||||||
"""Representation a CPU sensor."""
|
"""Representation of a CPU sensor."""
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
|
|
|
@ -8,17 +8,20 @@ import logging
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import STATE_UNKNOWN
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
|
from homeassistant.const import (
|
||||||
|
CONF_HOST, CONF_PORT, STATE_UNKNOWN, CONF_NAME, CONF_RESOURCES)
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
DEFAULT_NAME = 'Glances'
|
||||||
|
DEFAULT_HOST = 'localhost'
|
||||||
|
_RESOURCE = 'api/2/all'
|
||||||
|
DEFAULT_PORT = '61208'
|
||||||
|
|
||||||
_RESOURCE = '/api/2/all'
|
|
||||||
CONF_HOST = 'host'
|
|
||||||
CONF_PORT = '61208'
|
|
||||||
CONF_RESOURCES = 'resources'
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES = {
|
||||||
'disk_use_percent': ['Disk Use', '%'],
|
'disk_use_percent': ['Disk Use', '%'],
|
||||||
'disk_use': ['Disk Use', 'GiB'],
|
'disk_use': ['Disk Use', 'GiB'],
|
||||||
|
@ -36,7 +39,16 @@ SENSOR_TYPES = {
|
||||||
'process_sleeping': ['Sleeping', None]
|
'process_sleeping': ['Sleeping', None]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Required(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
||||||
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||||
|
vol.Optional(CONF_RESOURCES, default=['disk_use']):
|
||||||
|
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
||||||
|
})
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Return cached results if last scan was less then this time ago.
|
# Return cached results if last scan was less then this time ago.
|
||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
||||||
|
|
||||||
|
@ -44,25 +56,17 @@ MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
||||||
# pylint: disable=unused-variable
|
# pylint: disable=unused-variable
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the Glances sensor."""
|
"""Setup the Glances sensor."""
|
||||||
|
name = config.get(CONF_NAME)
|
||||||
host = config.get(CONF_HOST)
|
host = config.get(CONF_HOST)
|
||||||
port = config.get('port', CONF_PORT)
|
port = config.get(CONF_PORT)
|
||||||
url = 'http://{}:{}{}'.format(host, port, _RESOURCE)
|
url = 'http://{}:{}/{}'.format(host, port, _RESOURCE)
|
||||||
var_conf = config.get(CONF_RESOURCES)
|
var_conf = config.get(CONF_RESOURCES)
|
||||||
|
|
||||||
if None in (host, var_conf):
|
|
||||||
_LOGGER.error('Not all required config keys present: %s',
|
|
||||||
', '.join((CONF_HOST, CONF_RESOURCES)))
|
|
||||||
return False
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.get(url, timeout=10)
|
response = requests.get(url, timeout=10)
|
||||||
if not response.ok:
|
if not response.ok:
|
||||||
_LOGGER.error('Response status is "%s"', response.status_code)
|
_LOGGER.error('Response status is "%s"', response.status_code)
|
||||||
return False
|
return False
|
||||||
except requests.exceptions.MissingSchema:
|
|
||||||
_LOGGER.error("Missing resource or schema in configuration. "
|
|
||||||
"Please check the details in the configuration file")
|
|
||||||
return False
|
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
_LOGGER.error("No route to resource/endpoint: %s", url)
|
_LOGGER.error("No route to resource/endpoint: %s", url)
|
||||||
return False
|
return False
|
||||||
|
@ -71,10 +75,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
|
|
||||||
dev = []
|
dev = []
|
||||||
for resource in var_conf:
|
for resource in var_conf:
|
||||||
if resource not in SENSOR_TYPES:
|
dev.append(GlancesSensor(rest, name, resource))
|
||||||
_LOGGER.error('Sensor type: "%s" does not exist', resource)
|
|
||||||
else:
|
|
||||||
dev.append(GlancesSensor(rest, config.get('name'), resource))
|
|
||||||
|
|
||||||
add_devices(dev)
|
add_devices(dev)
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,17 @@ https://home-assistant.io/components/sensor.systemmonitor/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
from homeassistant.const import STATE_OFF, STATE_ON
|
|
||||||
|
from homeassistant.const import (CONF_RESOURCES, STATE_OFF, STATE_ON)
|
||||||
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
REQUIREMENTS = ['psutil==4.3.0']
|
REQUIREMENTS = ['psutil==4.3.0']
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES = {
|
||||||
'disk_use_percent': ['Disk Use', '%', 'mdi:harddisk'],
|
'disk_use_percent': ['Disk Use', '%', 'mdi:harddisk'],
|
||||||
'disk_use': ['Disk Use', 'GiB', 'mdi:harddisk'],
|
'disk_use': ['Disk Use', 'GiB', 'mdi:harddisk'],
|
||||||
|
@ -33,6 +39,14 @@ SENSOR_TYPES = {
|
||||||
'since_last_boot': ['Since Last Boot', '', 'mdi:clock']
|
'since_last_boot': ['Since Last Boot', '', 'mdi:clock']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Optional(CONF_RESOURCES, default=['disk_use']):
|
||||||
|
vol.All(cv.ensure_list, [vol.Schema({
|
||||||
|
vol.Required('type'): vol.In(SENSOR_TYPES),
|
||||||
|
vol.Optional('arg'): cv.string,
|
||||||
|
})])
|
||||||
|
})
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,12 +54,9 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the System sensors."""
|
"""Setup the System sensors."""
|
||||||
dev = []
|
dev = []
|
||||||
for resource in config['resources']:
|
for resource in config[CONF_RESOURCES]:
|
||||||
if 'arg' not in resource:
|
if 'arg' not in resource:
|
||||||
resource['arg'] = ''
|
resource['arg'] = ''
|
||||||
if resource['type'] not in SENSOR_TYPES:
|
|
||||||
_LOGGER.error('Sensor type: "%s" does not exist', resource['type'])
|
|
||||||
else:
|
|
||||||
dev.append(SystemMonitorSensor(resource['type'], resource['arg']))
|
dev.append(SystemMonitorSensor(resource['type'], resource['arg']))
|
||||||
|
|
||||||
add_devices(dev)
|
add_devices(dev)
|
||||||
|
|
|
@ -50,6 +50,8 @@ CONF_PAYLOAD = 'payload'
|
||||||
CONF_PENDING_TIME = 'pending_time'
|
CONF_PENDING_TIME = 'pending_time'
|
||||||
CONF_PLATFORM = 'platform'
|
CONF_PLATFORM = 'platform'
|
||||||
CONF_PORT = 'port'
|
CONF_PORT = 'port'
|
||||||
|
CONF_RESOURCE = 'resource'
|
||||||
|
CONF_RESOURCES = 'resources'
|
||||||
CONF_SCAN_INTERVAL = 'scan_interval'
|
CONF_SCAN_INTERVAL = 'scan_interval'
|
||||||
CONF_STATE = 'state'
|
CONF_STATE = 'state'
|
||||||
CONF_TEMPERATURE_UNIT = 'temperature_unit'
|
CONF_TEMPERATURE_UNIT = 'temperature_unit'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue