mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 22:07:10 +00:00
Do not call update() in constructor (#8148)
* Do not call update() in constructor * Move handling to update and re-add throttle * Fix indent * Fix interval
This commit is contained in:
parent
5ef274adce
commit
fa6e93f0c7
@ -25,7 +25,7 @@ DEFAULT_HOST = 'localhost'
|
|||||||
DEFAULT_NAME = 'Glances'
|
DEFAULT_NAME = 'Glances'
|
||||||
DEFAULT_PORT = '61208'
|
DEFAULT_PORT = '61208'
|
||||||
|
|
||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES = {
|
||||||
'disk_use_percent': ['Disk Use', '%'],
|
'disk_use_percent': ['Disk Use', '%'],
|
||||||
@ -63,20 +63,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
url = 'http://{}:{}/{}'.format(host, port, _RESOURCE)
|
url = 'http://{}:{}/{}'.format(host, port, _RESOURCE)
|
||||||
var_conf = config.get(CONF_RESOURCES)
|
var_conf = config.get(CONF_RESOURCES)
|
||||||
|
|
||||||
try:
|
|
||||||
response = requests.get(url, timeout=10)
|
|
||||||
if not response.ok:
|
|
||||||
_LOGGER.warning("Response status is '%s'", response.status_code)
|
|
||||||
except requests.exceptions.ConnectionError:
|
|
||||||
_LOGGER.warning("No route to resource/endpoint: %s", url)
|
|
||||||
|
|
||||||
rest = GlancesData(url)
|
rest = GlancesData(url)
|
||||||
|
rest.update()
|
||||||
|
|
||||||
dev = []
|
dev = []
|
||||||
for resource in var_conf:
|
for resource in var_conf:
|
||||||
dev.append(GlancesSensor(rest, name, resource))
|
dev.append(GlancesSensor(rest, name, resource))
|
||||||
|
|
||||||
add_devices(dev)
|
add_devices(dev, True)
|
||||||
|
|
||||||
|
|
||||||
class GlancesSensor(Entity):
|
class GlancesSensor(Entity):
|
||||||
@ -89,7 +83,6 @@ class GlancesSensor(Entity):
|
|||||||
self.type = sensor_type
|
self.type = sensor_type
|
||||||
self._state = STATE_UNKNOWN
|
self._state = STATE_UNKNOWN
|
||||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
||||||
self.update()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -112,54 +105,55 @@ class GlancesSensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the resources."""
|
"""Return the state of the resources."""
|
||||||
value = self.rest.data
|
return self._state
|
||||||
|
|
||||||
if value is not None:
|
|
||||||
if self.type == 'disk_use_percent':
|
|
||||||
return value['fs'][0]['percent']
|
|
||||||
elif self.type == 'disk_use':
|
|
||||||
return round(value['fs'][0]['used'] / 1024**3, 1)
|
|
||||||
elif self.type == 'disk_free':
|
|
||||||
try:
|
|
||||||
return round(value['fs'][0]['free'] / 1024**3, 1)
|
|
||||||
except KeyError:
|
|
||||||
return round((value['fs'][0]['size'] -
|
|
||||||
value['fs'][0]['used']) / 1024**3, 1)
|
|
||||||
elif self.type == 'memory_use_percent':
|
|
||||||
return value['mem']['percent']
|
|
||||||
elif self.type == 'memory_use':
|
|
||||||
return round(value['mem']['used'] / 1024**2, 1)
|
|
||||||
elif self.type == 'memory_free':
|
|
||||||
return round(value['mem']['free'] / 1024**2, 1)
|
|
||||||
elif self.type == 'swap_use_percent':
|
|
||||||
return value['memswap']['percent']
|
|
||||||
elif self.type == 'swap_use':
|
|
||||||
return round(value['memswap']['used'] / 1024**3, 1)
|
|
||||||
elif self.type == 'swap_free':
|
|
||||||
return round(value['memswap']['free'] / 1024**3, 1)
|
|
||||||
elif self.type == 'processor_load':
|
|
||||||
# Windows systems don't provide load details
|
|
||||||
try:
|
|
||||||
return value['load']['min15']
|
|
||||||
except KeyError:
|
|
||||||
return value['cpu']['total']
|
|
||||||
elif self.type == 'process_running':
|
|
||||||
return value['processcount']['running']
|
|
||||||
elif self.type == 'process_total':
|
|
||||||
return value['processcount']['total']
|
|
||||||
elif self.type == 'process_thread':
|
|
||||||
return value['processcount']['thread']
|
|
||||||
elif self.type == 'process_sleeping':
|
|
||||||
return value['processcount']['sleeping']
|
|
||||||
elif self.type == 'cpu_temp':
|
|
||||||
for sensor in value['sensors']:
|
|
||||||
if sensor['label'] == 'CPU':
|
|
||||||
return sensor['value']
|
|
||||||
return None
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from REST API."""
|
"""Get the latest data from REST API."""
|
||||||
self.rest.update()
|
self.rest.update()
|
||||||
|
value = self.rest.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':
|
||||||
|
try:
|
||||||
|
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':
|
||||||
|
# Windows systems don't provide load details
|
||||||
|
try:
|
||||||
|
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_temp':
|
||||||
|
for sensor in value['sensors']:
|
||||||
|
if sensor['label'] == 'CPU':
|
||||||
|
self._state = sensor['value']
|
||||||
|
self._state = None
|
||||||
|
|
||||||
|
|
||||||
class GlancesData(object):
|
class GlancesData(object):
|
||||||
@ -177,5 +171,5 @@ class GlancesData(object):
|
|||||||
response = requests.get(self._resource, timeout=10)
|
response = requests.get(self._resource, timeout=10)
|
||||||
self.data = response.json()
|
self.data = response.json()
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
_LOGGER.debug("Error connecting to glances: %s", self._resource)
|
_LOGGER.error("Connection error: %s", self._resource)
|
||||||
self.data = None
|
self.data = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user