mirror of
https://github.com/home-assistant/core
synced 2024-09-06 10:29:55 +02:00
Catch ConnectionRefusedError (#8844)
* Do not call update() in constructor * Catch ConnectionRefusedError
This commit is contained in:
parent
569d9764ab
commit
5059d4c54b
@ -16,6 +16,9 @@ import homeassistant.helpers.config_validation as cv
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_DESCRIPTION = 'description'
|
||||
ATTR_GROUP = 'group'
|
||||
|
||||
DEFAULT_URL = 'http://localhost:9001/RPC2'
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
@ -29,15 +32,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
url = config.get(CONF_URL)
|
||||
try:
|
||||
supervisor_server = xmlrpc.client.ServerProxy(url)
|
||||
processes = supervisor_server.supervisor.getAllProcessInfo()
|
||||
except ConnectionRefusedError:
|
||||
_LOGGER.error("Could not connect to Supervisord")
|
||||
return False
|
||||
|
||||
processes = supervisor_server.supervisor.getAllProcessInfo()
|
||||
|
||||
add_devices(
|
||||
[SupervisorProcessSensor(info, supervisor_server)
|
||||
for info in processes])
|
||||
for info in processes], True)
|
||||
|
||||
|
||||
class SupervisorProcessSensor(Entity):
|
||||
@ -47,7 +49,7 @@ class SupervisorProcessSensor(Entity):
|
||||
"""Initialize the sensor."""
|
||||
self._info = info
|
||||
self._server = server
|
||||
self.update()
|
||||
self._available = True
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@ -59,15 +61,25 @@ class SupervisorProcessSensor(Entity):
|
||||
"""Return the state of the sensor."""
|
||||
return self._info.get('statename')
|
||||
|
||||
def update(self):
|
||||
"""Update device state."""
|
||||
self._info = self._server.supervisor.getProcessInfo(
|
||||
self._info.get('name'))
|
||||
@property
|
||||
def available(self):
|
||||
"""Could the device be accessed during the last update call."""
|
||||
return self._available
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return {
|
||||
'group': self._info.get('group'),
|
||||
'description': self._info.get('description')
|
||||
ATTR_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
|
||||
|
Loading…
Reference in New Issue
Block a user