Catch ConnectionRefusedError (#8844)

* Do not call update() in constructor

* Catch ConnectionRefusedError
This commit is contained in:
Fabian Affolter 2017-08-06 10:08:00 +02:00 committed by GitHub
parent 569d9764ab
commit 5059d4c54b

View File

@ -16,6 +16,9 @@ import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ATTR_DESCRIPTION = 'description'
ATTR_GROUP = 'group'
DEFAULT_URL = 'http://localhost:9001/RPC2' DEFAULT_URL = 'http://localhost:9001/RPC2'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
@ -29,15 +32,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
url = config.get(CONF_URL) url = config.get(CONF_URL)
try: try:
supervisor_server = xmlrpc.client.ServerProxy(url) supervisor_server = xmlrpc.client.ServerProxy(url)
processes = supervisor_server.supervisor.getAllProcessInfo()
except ConnectionRefusedError: except ConnectionRefusedError:
_LOGGER.error("Could not connect to Supervisord") _LOGGER.error("Could not connect to Supervisord")
return False return False
processes = supervisor_server.supervisor.getAllProcessInfo()
add_devices( add_devices(
[SupervisorProcessSensor(info, supervisor_server) [SupervisorProcessSensor(info, supervisor_server)
for info in processes]) for info in processes], True)
class SupervisorProcessSensor(Entity): class SupervisorProcessSensor(Entity):
@ -47,7 +49,7 @@ class SupervisorProcessSensor(Entity):
"""Initialize the sensor.""" """Initialize the sensor."""
self._info = info self._info = info
self._server = server self._server = server
self.update() self._available = True
@property @property
def name(self): def name(self):
@ -59,15 +61,25 @@ class SupervisorProcessSensor(Entity):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._info.get('statename') return self._info.get('statename')
def update(self): @property
"""Update device state.""" def available(self):
self._info = self._server.supervisor.getProcessInfo( """Could the device be accessed during the last update call."""
self._info.get('name')) return self._available
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
return { return {
'group': self._info.get('group'), ATTR_DESCRIPTION: self._info.get('description'),
'description': self._info.get('description') ATTR_GROUP: self._info.get('group'),
} }
def update(self):
"""Update device state."""
try:
self._info = self._server.supervisor.getProcessInfo(
self._info.get('name'))
self._available = True
except ConnectionRefusedError:
_LOGGER.warning("Supervisord not available")
self._available = False