From c7a49e0820a5f1c7b4eae1e49c7a219fda4267c4 Mon Sep 17 00:00:00 2001 From: roblandry Date: Sun, 7 Apr 2019 13:07:05 -0400 Subject: [PATCH] Fix glances docker container errors (#22846) * Fix unavailable container errors * Update to dev * Use const --- homeassistant/components/glances/sensor.py | 41 ++++++++++++++-------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/glances/sensor.py b/homeassistant/components/glances/sensor.py index e59b9144b4c..db8f0397887 100644 --- a/homeassistant/components/glances/sensor.py +++ b/homeassistant/components/glances/sensor.py @@ -7,7 +7,7 @@ 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, TEMP_CELSIUS) + 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 @@ -183,21 +183,34 @@ class GlancesSensor(Entity): self._state = sensor['value'] elif self.type == 'docker_active': count = 0 - for container in value['docker']['containers']: - if container['Status'] == 'running' or \ - 'Up' in container['Status']: - count += 1 - self._state = count + try: + 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': - use = 0.0 - for container in value['docker']['containers']: - use += container['cpu']['total'] - self._state = round(use, 1) + 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'] + self._state = round(cpu_use, 1) + except KeyError: + self._state = STATE_UNAVAILABLE elif self.type == 'docker_memory_use': - use = 0.0 - for container in value['docker']['containers']: - use += container['memory']['usage'] - self._state = round(use / 1024**2, 1) + 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) + except KeyError: + self._state = STATE_UNAVAILABLE class GlancesData: