This commit is contained in:
Paulus Schoutsen 2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View file

@ -6,8 +6,17 @@ import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_HOST, CONF_NAME, CONF_PORT, CONF_USERNAME, CONF_PASSWORD, CONF_SSL,
CONF_VERIFY_SSL, CONF_RESOURCES, STATE_UNAVAILABLE, TEMP_CELSIUS)
CONF_HOST,
CONF_NAME,
CONF_PORT,
CONF_USERNAME,
CONF_PASSWORD,
CONF_SSL,
CONF_VERIFY_SSL,
CONF_RESOURCES,
STATE_UNAVAILABLE,
TEMP_CELSIUS,
)
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
@ -16,53 +25,55 @@ from homeassistant.util import Throttle
_LOGGER = logging.getLogger(__name__)
CONF_VERSION = 'version'
CONF_VERSION = "version"
DEFAULT_HOST = 'localhost'
DEFAULT_NAME = 'Glances'
DEFAULT_PORT = '61208'
DEFAULT_HOST = "localhost"
DEFAULT_NAME = "Glances"
DEFAULT_PORT = "61208"
DEFAULT_VERSION = 2
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
SENSOR_TYPES = {
'disk_use_percent': ['Disk used percent', '%', 'mdi:harddisk'],
'disk_use': ['Disk used', 'GiB', 'mdi:harddisk'],
'disk_free': ['Disk free', 'GiB', 'mdi:harddisk'],
'memory_use_percent': ['RAM used percent', '%', 'mdi:memory'],
'memory_use': ['RAM used', 'MiB', 'mdi:memory'],
'memory_free': ['RAM free', 'MiB', 'mdi:memory'],
'swap_use_percent': ['Swap used percent', '%', 'mdi:memory'],
'swap_use': ['Swap used', 'GiB', 'mdi:memory'],
'swap_free': ['Swap free', 'GiB', 'mdi:memory'],
'processor_load': ['CPU load', '15 min', 'mdi:memory'],
'process_running': ['Running', 'Count', 'mdi:memory'],
'process_total': ['Total', 'Count', 'mdi:memory'],
'process_thread': ['Thread', 'Count', 'mdi:memory'],
'process_sleeping': ['Sleeping', 'Count', 'mdi:memory'],
'cpu_use_percent': ['CPU used', '%', 'mdi:memory'],
'cpu_temp': ['CPU Temp', TEMP_CELSIUS, 'mdi:thermometer'],
'docker_active': ['Containers active', '', 'mdi:docker'],
'docker_cpu_use': ['Containers CPU used', '%', 'mdi:docker'],
'docker_memory_use': ['Containers RAM used', 'MiB', 'mdi:docker'],
"disk_use_percent": ["Disk used percent", "%", "mdi:harddisk"],
"disk_use": ["Disk used", "GiB", "mdi:harddisk"],
"disk_free": ["Disk free", "GiB", "mdi:harddisk"],
"memory_use_percent": ["RAM used percent", "%", "mdi:memory"],
"memory_use": ["RAM used", "MiB", "mdi:memory"],
"memory_free": ["RAM free", "MiB", "mdi:memory"],
"swap_use_percent": ["Swap used percent", "%", "mdi:memory"],
"swap_use": ["Swap used", "GiB", "mdi:memory"],
"swap_free": ["Swap free", "GiB", "mdi:memory"],
"processor_load": ["CPU load", "15 min", "mdi:memory"],
"process_running": ["Running", "Count", "mdi:memory"],
"process_total": ["Total", "Count", "mdi:memory"],
"process_thread": ["Thread", "Count", "mdi:memory"],
"process_sleeping": ["Sleeping", "Count", "mdi:memory"],
"cpu_use_percent": ["CPU used", "%", "mdi:memory"],
"cpu_temp": ["CPU Temp", TEMP_CELSIUS, "mdi:thermometer"],
"docker_active": ["Containers active", "", "mdi:docker"],
"docker_cpu_use": ["Containers CPU used", "%", "mdi:docker"],
"docker_memory_use": ["Containers RAM used", "MiB", "mdi:docker"],
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
vol.Optional(CONF_SSL, default=False): cv.boolean,
vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
vol.Optional(CONF_RESOURCES, default=['disk_use']):
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
vol.Optional(CONF_VERSION, default=DEFAULT_VERSION): vol.In([2, 3]),
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
vol.Optional(CONF_SSL, default=False): cv.boolean,
vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
vol.Optional(CONF_RESOURCES, default=["disk_use"]): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)]
),
vol.Optional(CONF_VERSION, default=DEFAULT_VERSION): vol.In([2, 3]),
}
)
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Glances sensors."""
from glances_api import Glances
@ -78,8 +89,17 @@ async def async_setup_platform(
session = async_get_clientsession(hass, verify_ssl)
glances = GlancesData(
Glances(hass.loop, session, host=host, port=port, version=version,
username=username, password=password, ssl=ssl))
Glances(
hass.loop,
session,
host=host,
port=port,
version=version,
username=username,
password=password,
ssl=ssl,
)
)
await glances.async_update()
@ -107,7 +127,7 @@ class GlancesSensor(Entity):
@property
def name(self):
"""Return the name of the sensor."""
return '{} {}'.format(self._name, SENSOR_TYPES[self.type][0])
return "{} {}".format(self._name, SENSOR_TYPES[self.type][0])
@property
def icon(self):
@ -135,80 +155,94 @@ class GlancesSensor(Entity):
value = self.glances.api.data
if value is not None:
if self.type == 'disk_use_percent':
self._state = value['fs'][0]['percent']
elif self.type == 'disk_use':
self._state = round(value['fs'][0]['used'] / 1024**3, 1)
elif self.type == 'disk_free':
if self.type == "disk_use_percent":
self._state = value["fs"][0]["percent"]
elif self.type == "disk_use":
self._state = round(value["fs"][0]["used"] / 1024 ** 3, 1)
elif self.type == "disk_free":
try:
self._state = round(value['fs'][0]['free'] / 1024**3, 1)
self._state = round(value["fs"][0]["free"] / 1024 ** 3, 1)
except KeyError:
self._state = round((value['fs'][0]['size'] -
value['fs'][0]['used']) / 1024**3, 1)
elif self.type == 'memory_use_percent':
self._state = value['mem']['percent']
elif self.type == 'memory_use':
self._state = round(value['mem']['used'] / 1024**2, 1)
elif self.type == 'memory_free':
self._state = round(value['mem']['free'] / 1024**2, 1)
elif self.type == 'swap_use_percent':
self._state = value['memswap']['percent']
elif self.type == 'swap_use':
self._state = round(value['memswap']['used'] / 1024**3, 1)
elif self.type == 'swap_free':
self._state = round(value['memswap']['free'] / 1024**3, 1)
elif self.type == 'processor_load':
self._state = round(
(value["fs"][0]["size"] - value["fs"][0]["used"]) / 1024 ** 3, 1
)
elif self.type == "memory_use_percent":
self._state = value["mem"]["percent"]
elif self.type == "memory_use":
self._state = round(value["mem"]["used"] / 1024 ** 2, 1)
elif self.type == "memory_free":
self._state = round(value["mem"]["free"] / 1024 ** 2, 1)
elif self.type == "swap_use_percent":
self._state = value["memswap"]["percent"]
elif self.type == "swap_use":
self._state = round(value["memswap"]["used"] / 1024 ** 3, 1)
elif self.type == "swap_free":
self._state = round(value["memswap"]["free"] / 1024 ** 3, 1)
elif self.type == "processor_load":
# Windows systems don't provide load details
try:
self._state = value['load']['min15']
self._state = value["load"]["min15"]
except KeyError:
self._state = value['cpu']['total']
elif self.type == 'process_running':
self._state = value['processcount']['running']
elif self.type == 'process_total':
self._state = value['processcount']['total']
elif self.type == 'process_thread':
self._state = value['processcount']['thread']
elif self.type == 'process_sleeping':
self._state = value['processcount']['sleeping']
elif self.type == 'cpu_use_percent':
self._state = value['quicklook']['cpu']
elif self.type == 'cpu_temp':
for sensor in value['sensors']:
if sensor['label'] in ['CPU', "CPU Temperature",
"Package id 0", "Physical id 0",
"cpu_thermal 1", "cpu-thermal 1",
"exynos-therm 1", "soc_thermal 1",
"soc-thermal 1", "aml_thermal"]:
self._state = sensor['value']
elif self.type == 'docker_active':
self._state = value["cpu"]["total"]
elif self.type == "process_running":
self._state = value["processcount"]["running"]
elif self.type == "process_total":
self._state = value["processcount"]["total"]
elif self.type == "process_thread":
self._state = value["processcount"]["thread"]
elif self.type == "process_sleeping":
self._state = value["processcount"]["sleeping"]
elif self.type == "cpu_use_percent":
self._state = value["quicklook"]["cpu"]
elif self.type == "cpu_temp":
for sensor in value["sensors"]:
if sensor["label"] in [
"CPU",
"CPU Temperature",
"Package id 0",
"Physical id 0",
"cpu_thermal 1",
"cpu-thermal 1",
"exynos-therm 1",
"soc_thermal 1",
"soc-thermal 1",
"aml_thermal",
]:
self._state = sensor["value"]
elif self.type == "docker_active":
count = 0
try:
for container in value['docker']['containers']:
if container['Status'] == 'running' or \
'Up' in container['Status']:
for container in value["docker"]["containers"]:
if (
container["Status"] == "running"
or "Up" in container["Status"]
):
count += 1
self._state = count
except KeyError:
self._state = count
elif self.type == 'docker_cpu_use':
elif self.type == "docker_cpu_use":
cpu_use = 0.0
try:
for container in value['docker']['containers']:
if container['Status'] == 'running' or \
'Up' in container['Status']:
cpu_use += container['cpu']['total']
for container in value["docker"]["containers"]:
if (
container["Status"] == "running"
or "Up" in container["Status"]
):
cpu_use += container["cpu"]["total"]
self._state = round(cpu_use, 1)
except KeyError:
self._state = STATE_UNAVAILABLE
elif self.type == 'docker_memory_use':
elif self.type == "docker_memory_use":
mem_use = 0.0
try:
for container in value['docker']['containers']:
if container['Status'] == 'running' or \
'Up' in container['Status']:
mem_use += container['memory']['usage']
self._state = round(mem_use / 1024**2, 1)
for container in value["docker"]["containers"]:
if (
container["Status"] == "running"
or "Up" in container["Status"]
):
mem_use += container["memory"]["usage"]
self._state = round(mem_use / 1024 ** 2, 1)
except KeyError:
self._state = STATE_UNAVAILABLE