core/homeassistant/components/supervisord/sensor.py

79 lines
2.2 KiB
Python
Raw Normal View History

"""Sensor for Supervisord process status."""
2016-05-13 05:16:58 +00:00
import logging
import xmlrpc.client
2016-09-02 04:34:07 +00:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_URL
2016-05-13 05:16:58 +00:00
from homeassistant.helpers.entity import Entity
2016-09-02 04:34:07 +00:00
import homeassistant.helpers.config_validation as cv
2016-05-13 05:16:58 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
ATTR_DESCRIPTION = "description"
ATTR_GROUP = "group"
2019-07-31 19:25:30 +00:00
DEFAULT_URL = "http://localhost:9001/RPC2"
2016-09-02 04:34:07 +00:00
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Optional(CONF_URL, default=DEFAULT_URL): cv.url}
)
2016-09-02 04:34:07 +00:00
2016-05-13 05:16:58 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Supervisord platform."""
2016-09-02 04:34:07 +00:00
url = config.get(CONF_URL)
2016-05-13 05:16:58 +00:00
try:
2016-09-02 04:34:07 +00:00
supervisor_server = xmlrpc.client.ServerProxy(url)
processes = supervisor_server.supervisor.getAllProcessInfo()
2016-05-13 05:16:58 +00:00
except ConnectionRefusedError:
_LOGGER.error("Could not connect to Supervisord")
2016-09-02 04:34:07 +00:00
return False
add_entities(
2019-07-31 19:25:30 +00:00
[SupervisorProcessSensor(info, supervisor_server) for info in processes], True
)
2016-05-13 05:16:58 +00:00
class SupervisorProcessSensor(Entity):
2016-09-02 04:34:07 +00:00
"""Representation of a supervisor-monitored process."""
2016-05-13 05:16:58 +00:00
def __init__(self, info, server):
"""Initialize the sensor."""
self._info = info
self._server = server
self._available = True
2016-05-13 05:16:58 +00:00
@property
def name(self):
"""Return the name of the sensor."""
2019-07-31 19:25:30 +00:00
return self._info.get("name")
2016-05-13 05:16:58 +00:00
@property
def state(self):
"""Return the state of the sensor."""
2019-07-31 19:25:30 +00:00
return self._info.get("statename")
2016-05-13 05:16:58 +00:00
@property
def available(self):
"""Could the device be accessed during the last update call."""
return self._available
2016-05-13 05:16:58 +00:00
@property
def device_state_attributes(self):
"""Return the state attributes."""
return {
2019-07-31 19:25:30 +00:00
ATTR_DESCRIPTION: self._info.get("description"),
ATTR_GROUP: self._info.get("group"),
2016-05-13 05:16:58 +00:00
}
def update(self):
"""Update device state."""
try:
2019-07-31 19:25:30 +00:00
self._info = self._server.supervisor.getProcessInfo(self._info.get("name"))
self._available = True
except ConnectionRefusedError:
_LOGGER.warning("Supervisord not available")
self._available = False